Hi all, if somebody would please be kind enough to help me delete player objects, after any player disconnects from the game, when hitting the escape key, then this would be greatly appreciated! =)
Here is my code:
+ Code Snippet`Constants --------------------------------
#constant MAXPLAYERS 10
#constant SENDGAP 100
`------------------------------------------
`Types ------------------------------------
type playerstates
keyup as boolean
keydown as boolean
endtype
`------------------------------------------
`Data -------------------------------------
global OURID as integer
global LASTSENT as integer
global PLAYER_ANGLE_Y as float
dim playerdata(MAXPLAYERS) as playerstates
`------------------------------------------
`Ask for choice of mode
print "1. Host"
print "2. Join"
Input "Choice: ", choice
`If no choice then quit
if choice=0 then end
`If choice is 1 then host a multiplayer game
if choice=1
`Ask for username
input "Nickname: ", nick$
`If either variable is empty, set a default
if nick$="" then nick$ = "Host"
`Host the multiplayer game
result = THost("", MAXPLAYERS, nick$)
`If hosting failed then report to user and quit
if not result
print "Failed to host game"
wait key
end
endif
`-----------------------------------------------
`Set our game information for people searching
`for games.
`
`The parameters are as follow:
`gameName$, gameType$, gameMap$, timeLeft
`(timeLeft is specified in milliseconds.)
`
`Custom parameters are planned.
TRegisterGame "Banana Snipers", "Team Deathmatch", "Dessert", 60*10
`-----------------------------------------------
endif
`If choice is 1 then join
if choice=2
`Ask for username
input "Nickname: ", nick$
`If variable is empty, set a default
if nick$="" then nick$ = "Client"
`Search for games on the LAN
TGetGameList
gameAmount = TGetGameAmount()
print "There are currently "+str$(gameAmount)+" games running."
print
`Display games ---------------------------------
`To get game information you use TGetGameData. The
`first parameter is the game number, the second
`parameter is the type of data you want to get.
`
`0 = Game name
`1 = Game type
`2 = Game map
`3 = Time left
`
`This function returns string data always, so if
`you want to use a value returned then you must
`first convert it to an integer using val().
for x=1 to gameAmount
gameName$ = TGetGameData(x, 0)
gameType$ = TGetGameData(x, 1)
gameMap$ = TGetGameData(x, 2)
gameTime$ = TGetGameData(x, 3)
print gameName$+" "+gameType$+" "+gameMap$+" "+gametime$
next x
`-----------------------------------------------
print
`Ask user which game they want to join
input "Enter game number to join: ", gameNum
if gameNum=0 then end
`Join the multiplayer game ---------------------
`This is another TJoin variation that lets you
`use a game number obtained from the games list.
result = TJoin(gameNum, nick$)
`-----------------------------------------------
`If joining failed then report to user and quit
if not result
print "Failed to join game"
wait key
end
endif
endif
`Retrieve our ID in the game and store it
OURID = TGetThisID()
`Set up sync
sync on
sync rate 60
`Make the player objects and hide ones not in use
for x=1 to MAXPLAYERS
make object cube x, 10
color object x, rgb(100+rnd(155), 100+rnd(155), 100+rnd(155))
position object x, 0, 5, 0
if not TPlayerExist(x) then hide object x
next x
`Make a matrix
make matrix 1, 1000, 1000, 10, 10
load image "..\media\sands.bmp", 1
prepare matrix texture 1, 1, 4, 4
do
ms$ = TGetString()
`Output the framerate
text 10, 70, "Screen FPS: "+str$(screen fps())
text 10, 90, "msg string="+ms$
`Show who is online
for x=1 to MAXPLAYERS
if TPlayerExist(x) and TConnected()=1
text 0, x*13+13, TGetPlayername(x)+"("+str$(x)+") is connected."
endif
next x
`Update everything
HandleControls()
HandleCamera()
if TConnected() then HandleNetwork()
HandleMovement()
`Let Tempest update itself
TSync
`Refresh the screen
Sync
loop
function HandleMovement()
for x=1 to MAXPLAYERS
if OURID<>x and object exist(x)
if playerdata(x).keyup then move object x, 1
if playerdata(x).keydown then move object x, -1
endif
next x
endfunction
function HandleControls()
if upkey() then move object OURID, 1
if downkey() then move object OURID, -1
if leftkey() then dec PLAYER_ANGLE_Y
if rightkey() then inc PLAYER_ANGLE_Y
yrotate object OURID, PLAYER_ANGLE_Y
`This shows how the host can boot a player
if spacekey() and TPlayerExist(2)=1
TKillPlayer 2
repeat : until not spacekey()
endif
`This shows how to disconnect
if shiftkey() and TConnected()
TDisconnect
repeat : until not shiftkey()
endif
endfunction
function HandleCamera()
xPos# = cos(270-PLAYER_ANGLE_Y) * 50 + object position x(OURID)
zPos# = sin(270-PLAYER_ANGLE_Y) * 50 + object position z(OURID)
position camera xPos#, 30, zPos#
point camera object position x(OURID), 5, object position z(OURID)
endfunction
function HandleNetwork()
`Player states
newPlayer = TNewPlayer()
if newPlayer>0
show object newPlayer
endif
`Send
if LASTSENT+SENDGAP<timer()
TPutFloat object position x(OURID)
TPutFloat object position z(OURID)
TPutFloat PLAYER_ANGLE_Y
TPutByte upkey()
TPutByte downkey()
TSendAll
LASTSENT = timer()
endif
`Receive
repeat
newMsg = TGetMessage()
if newMsg
from = TGetSender()
x# = TGetFloat()
z# = TGetFloat()
yA# = TGetFloat()
playerdata(from).keyup = TGetByte()
playerdata(from).keydown = TGetByte()
XVAL = x#
ZVAL = z#
if object exist(from)
position object from, x#, 5, z#
yrotate object from, yA#
endif
endif
until not newMsg
endfunction
Thanks all! =)
~M.W~