Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SeedRnd MilliSecs()
- Include "UDPNetwork_lib.bb"
- Global localID
- Global localName$ = "<Guy Fawkes>"
- Global localPlayerMesh
- Global hide_pointer
- Global itemcollcnt
- Global onground
- Global all_scene_collisions_on = 0
- Global camera_scene_collisions_on = 0
- Global player_scene_collisions_on = 1
- Global player_player_collisions_on = 0
- Global bullet_scene_collisions_on = 1
- Global bullet_player_collisions_on = 1
- Global player_item_collisions_on = 1
- Type player
- Field id
- Field name$
- Field life
- Field mesh
- Field nextX#,nextY#,nextZ#
- Field previousX#,previousY#,previousZ#
- Field nextYaw#
- Field previousYaw#
- Field nextTime#
- Field previousTime#
- Field currentY#
- Field velocityY#
- Field r,g,b
- Field health_factor
- Field max_health_factor
- Field magic_factor
- Field max_magic_factor
- Field bullet_factor
- Field max_bullet_factor
- End Type
- Type item
- Field id
- Field xpos#
- Field ypos#
- Field zpos#
- Field itemcount
- Field r,g,b
- End Type
- Type bullet
- Field mesh
- Field angle#
- Field time
- Field id ; id of player who fired
- End Type
- Global maxhealthpacks = 4
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Camera code from jfk EO-11110, thanks
- Global bounce_cam=1 ; smooth camera motion? (0/1)
- Global bounce_div#=30.0 ; the higher, the more sluggish the camera will follow. Recc: 3.0 to 20.0
- ; some globals used by ChaseCam
- Global old_x#,old_y#,old_z#
- ; desired distance between camera and player mesh
- Global x_dis#=7.0,y_dis#=1,z_dis#=7.0,y_dis_or#=y_dis#
- ;Camera Collision Type
- Global type_camera = 1
- ;Player Collision Type
- Global type_player = 2
- ;Scene Collision Type
- Global type_scene = 3
- ;Bullet Collision Type
- Global type_bullet = 4
- ;Item Collision Type
- Global type_item = 5
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- api_ShowWindow ( SystemProperty$ ( "apphWnd" ), 1 )
- ; network init ----------------------------------------;
- Global localMode = Net_StartInput() ; bring the library "console" to connect
- If localMode = 1 Or localMode = 2 ; 1 means we are client, 2 means we are server
- If localMode = 1 Then AppTitle("Client : " + localName + " (port:" + net_port + ")")
- If localMode = 2 Then AppTitle("Server : " + localName + " (port:" + net_port + ")")
- localId = Net_CreatePlayer(localName) ; this command inits the server or local client
- If localId = 0 ; error
- Net_StopNetwork() ; close the network properly
- RuntimeError("Failed to create player.")
- EndIf
- Else ; error
- Net_StopNetwork()
- RuntimeError("Failed to start game.")
- EndIf
- ;------------------------------------------------------;
- Graphics3D 640,480,32,2
- SetBuffer BackBuffer()
- api_ShowWindow ( SystemProperty$ ( "apphWnd" ), 1 )
- CreateScene()
- Global camera = CreateCamera()
- CameraRange camera, 0.1, 10000.0
- EntityPickMode camera, 2
- EntityType camera, type_camera
- EntityRadius camera, 0.1
- CameraFogMode camera, 1
- CameraFogColor camera, 38, 107, 252
- CameraFogRange camera, 0.0, 250.0
- Global sky = CreateCube()
- EntityFX sky, 1+8
- ScaleEntity sky, 25, 25, 25
- EntityColor sky, 38, 107, 252
- EntityOrder sky, 1000
- FlipMesh sky
- ; create our own local player
- localPlayerMesh = CreatePlayer(localID,localName)
- old_x = EntityX(localPlayerMesh):old_y = EntityY(localPlayerMesh):old_z = EntityZ(localPlayerMesh) ; update camera position
- ; we send our color to the server
- p.player = Object.player(GetPlayer(localID))
- Net_SendMessage(3,p\r + "/" + p\g + "/" + p\b)
- ; variables used to send our positions at a constant delay (saves bandwidth but makes a constant ping)
- Global updatePosition = MilliSecs()
- Global updateTick = 50
- ; chat
- Global messageDisplayNumber = 10
- Global messageOffset
- Dim message$(messageDisplayNumber)
- Global chatMode
- Global chatMessage$ = localName + ": "
- Global chatMillisecs = MilliSecs()
- Global chatCursor
- While Not KeyDown(1)
- If all_scene_collisions_on
- ;; ALL COLLISIONS
- camera_scene_collisions_on = 1
- player_scene_collisions_on = 1
- player_player_collisions_on = 1
- bullet_scene_collisions_on = 1
- bullet_player_collisions_on = 1
- player_item_collisions_on = 1
- Else
- ;; FEW COLLISIONS
- If camera_scene_collisions_on Then Collisions type_camera, type_scene, 2, 2 ;camera to scene
- If player_scene_collisions_on Then Collisions type_player, type_scene, 2, 3 ; player to scene
- If player_player_collisions_on Then Collisions type_player, type_player, 1, 3 ; player to player
- If bullet_scene_collisions_on Then Collisions type_bullet, type_scene, 2, 1 ; bullet to scene
- If bullet_player_collisions_on Then Collisions type_bullet, type_player, 1, 1 ; bullet to player
- If player_item_collisions_on Then Collisions type_player, type_item, 1, 1 ; player to item
- EndIf
- ;; ALL the Collisions on at the SAME TIME
- If all_scene_collisions_on
- Collisions type_camera, type_scene, 2, 3 ; camera to scene
- Collisions type_player, type_scene, 2, 3 ; player to scene
- Collisions type_player, type_player, 1, 3 ; player to player
- Collisions type_bullet, type_scene, 2, 1 ; bullet to scene
- Collisions type_bullet, type_player, 1, 1 ; bullet to player
- Collisions type_player, type_item, 1, 1 ; player to item
- EndIf
- ; update our player
- UpdateLocalPlayer()
- ; see the function, the interpolation variables are set properly here
- UpdateNetwork()
- ; the interpolation is made in this function
- UpdatePlayers()
- ; update the bullets... only the server check for player kill
- UpdateBullets()
- UpdateItems()
- UpdateWorld()
- RenderWorld()
- DrawPlayersNames()
- If KeyHit(28) And chatMode = 0 Then chatMode = 1 : FlushKeys()
- If chatMode = 1 Then UpdateChat()
- DrawMessages()
- Flip
- Wend
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Net_StopNetwork()
- End
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function Jump ( )
- For p.player = Each player
- ; check if player is on the ground
- groundcoll = LinePick(EntityX(p\mesh),EntityY(p\mesh),EntityZ(p\mesh),0,-1.2,0)
- If PickedEntity()
- onground = 1
- ;Detect Ceiling
- ceilingpicked = LinePick ( EntityX ( p\mesh ), EntityY ( p\mesh ), EntityZ ( p\mesh ), 0.0, 300.0, 0.0 )
- ;Stop Ceiling "Floating"
- If ceilingpicked
- p\velocityY = 0.0
- Else
- p\velocityY = 0.5 ; set "inverse" of gravity velocity
- EndIf
- Else
- onground = 0
- EndIf
- Next
- End Function
- Function UpdateChaseCam(camera,player) ; thanks to jfk EO-11110 on bb forums for this function !
- PositionEntity camera,EntityX(player,1),EntityY(player,1),EntityZ(player,1)
- ResetEntity camera
- x#=EntityX(player)+Sin(-EntityYaw(player)+180)*x_dis#
- y#=EntityY(player)+y_dis#
- z#=EntityZ(player)+Cos(-EntityYaw(player)+180)*z_dis#
- obstacle=LinePick(EntityX(player),EntityY(player),EntityZ(player),(x-EntityX(player)),y_dis#,(z-EntityZ(player)),0.1)
- If obstacle=0
- new_x#=x
- new_y#=y
- new_z#=z
- Else
- new_x#=PickedX()
- new_y#=PickedY()
- new_z#=PickedZ()
- EndIf
- If bounce_cam=0
- PositionEntity camera,new_x,new_y,new_z
- Else
- x=old_x+((New_x-old_x)/bounce_div#)
- y=old_y+((New_y-old_y)/bounce_div#)
- z=old_z+((New_z-old_z)/bounce_div#)
- PositionEntity camera,x,y,z
- EndIf
- PointEntity camera,player
- TurnEntity camera,-20,0,0 ; if you want the player mesh more on the bottom of the screen
- old_x#=EntityX(camera)
- old_y#=EntityY(camera)
- old_z#=EntityZ(camera)
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function UpdateNetwork()
- ; update incoming packets (better check ALL incoming messages so use While Wend)
- While Net_CheckMessage() ; will check for a new message and fill Net_MsgType, Net_MsgData$, Net_MsgFrom and Net_MsgTo variables so we can read it
- p.player = Object.player(GetPlayer(Net_MsgFrom)) ; get the player handle
- Select Net_MsgType ; check the type of message
- Case 1 ; chat message
- NewMessage(Net_MsgData)
- Case 2 ; player position and angle, example of packing data into string
- offset1 = Instr(Net_MsgData,"/",1)
- offset2 = Instr(Net_MsgData,"/",offset1+1)
- offset3 = Instr(Net_MsgData,"/",offset2+1)
- p\previousX = p\nextX
- p\previousY = p\nextY
- p\previousZ = p\nextZ
- p\previousYaw = p\nextYaw
- p\nextX = Left(Net_MsgData,offset1-1)
- p\nextY = Mid(Net_MsgData,offset1+1,offset2-offset1-1)
- p\nextZ = Mid(Net_MsgData,offset2+1,offset3-offset2-1)
- p\nextYaw = Right(Net_MsgData,Len(Net_MsgData)-offset3)
- p\previousTime = p\nextTime
- p\nextTime = MilliSecs()
- Case 3 ; update player colors
- offset1 = Instr(Net_MsgData,"/",1)
- offset2 = Instr(Net_MsgData,"/",offset1+1)
- p\r = Left(Net_MsgData,offset1-1)
- p\g = Mid(Net_MsgData,offset1+1,offset2-offset1-1)
- p\b = Right(Net_MsgData,Len(Net_MsgData)-offset2)
- EntityColor p\mesh,p\r,p\g,p\b
- Case 4 ; a player fired
- CreateBullet(Net_MsgFrom)
- Case 5 ; a player lost some life
- p\life = Net_MsgData
- Case 6 ; a player killed another one
- k.player = Object.player(GetPlayer(Net_MsgData)) ; killed player ID in MsgData
- NewMessage(p\name + " killed " + k\name)
- ResetPlayer(k\id)
- Case 100 ; new player connected, OR the server tells us who is already connected so we can create players when joining the game
- If localMode = 2
- For p.player = Each player
- Net_SendMessage(2,p\nextX + "/" + p\nextY + "/" + p\nextZ + "/" + p\nextYaw,p\id,Net_MsgFrom)
- Net_SendMessage(3,p\r + "/" + p\g + "/" + p\b,p\id,Net_MsgFrom)
- Net_SendMessage(5,p\life,p\id,Net_MsgFrom)
- Next
- EndIf
- NewMessage(Net_MsgData + " connected")
- CreatePlayer(Net_MsgFrom,Net_MsgData)
- Case 101 ; a player has quit
- For p.player = Each player
- If Net_MsgFrom = p\id
- NewMessage(p\name + " disconnected")
- FreeEntity p\mesh
- Delete p
- EndIf
- Next
- Case 102 ; host has changed
- For p.player = Each player
- If Net_MsgFrom = p\id
- If p\id = localID
- AppTitle("Server : " + localName + " (port:" + net_port + ")")
- NewMessage("You are the new host")
- localMode = 2
- Else
- NewMessage(p\name + " is the new host")
- EndIf
- EndIf
- Next
- End Select
- Wend
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function UpdateLocalPlayer()
- ; update our player
- p.player = Object.player(GetPlayer(localID)) ; get our player
- ; inputs
- If chatmode = 0
- If KeyDown(200) Or KeyDown(17) Then MoveEntity p\mesh,0,0,0.3
- If KeyDown(208) Or KeyDown(31) Then MoveEntity p\mesh,0,0,-0.3
- If KeyDown(203) Or KeyDown(30) Then TurnEntity p\mesh,0,3,0
- If KeyDown(205) Or KeyDown(32) Then TurnEntity p\mesh,0,-3,0
- EndIf
- ; fire
- If KeyDown(47)
- CreateBullet(localID)
- Net_SendMessage(4,"")
- EndIf
- If hide_pointer <> 0 Then HidePointer ( )
- If hide_pointer = 0 Then ShowPointer ( )
- If Not ( KeyDown ( 29 ) Or KeyDown ( 157 ) )
- ; jump
- If KeyHit(57) And chatmode = 0 Then Jump ( )
- Else
- ; spawn items
- If KeyHit(57) And chatmode = 0
- For x = 1 To maxhealthpacks
- i.item = New item
- i\id = CreateCube()
- i\itemcount = ( maxhealthpacks )
- PositionEntity i\id, Rnd ( -25, 25 ), 1.001, ( Rnd ( -25, 25 ) )
- EntityPickMode i\id, 2
- EntityType i\id, type_item
- EntityRadius i\id, 1
- EntityColor i\id, 0, ( Rnd ( 64, 255 ) ), ( Rnd ( 64, 255 ) )
- EntityAlpha i\id, ( Rnd ( 0.25, 0.65 ) )
- Next
- EndIf
- EndIf
- ; we create a message type "2" for player position
- If MilliSecs()-updatePosition > updateTick
- Net_SendMessage(2,EntityX#(p\mesh, 1) + "/" + EntityY#(p\mesh, 1) + "/" + EntityZ#(p\mesh, 1) + "/" + EntityYaw#(p\mesh, 1))
- updatePosition = MilliSecs()
- EndIf
- If MouseDown(1)
- mxs#=MouseXSpeed()/4.0
- mys#=(MouseYSpeed()/100.0)
- y_dis=y_dis+mys
- If y_dis<-y_dis_or*2 Then y_dis=-y_dis_or*2
- If y_dis>y_dis_or*15 Then y_dis=y_dis_or*15
- MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
- TurnEntity p\mesh,0,-mxs,0
- EndIf
- If ( MouseDown ( 1 ) And MouseDown ( 2 ) )
- MoveEntity p\mesh, 0, 0, 0.3
- EndIf
- UpdateChaseCam(camera,p\mesh)
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function UpdatePlayers()
- For p.player = Each player
- If p\id <> localID ; update other players
- ResetEntity p\mesh ; reset entity is useful here because of collisions
- ;; LINEAR INTERPOLATION ;;;;;;;;;;
- curTime# = (MilliSecs()-p\nextTime) / (p\nextTime-p\previousTime) ; curTime will be ideally 0.0-1.0, if it's > 1.0 then it performs extrapolation
- PositionEntity p\mesh,p\previousX+(p\nextX-p\previousX)*curTime,p\previousY+(p\nextY-p\previousY)*curTime,p\previousZ+(p\nextZ-p\previousZ)*curTime
- ; special fix for yaw as EntityYaw returns a value beetween +180 ans -180, the interpolation can go backward
- If p\previousYaw < 0 Then p\previousYaw = p\previousYaw + 360
- If p\nextYaw < 0 Then p\nextYaw = p\nextYaw + 360
- If Abs(p\nextYaw-p\previousYaw) > 180
- If p\nextYaw > p\previousYaw
- p\previousYaw = p\previousYaw + 360
- Else
- p\previousYaw = p\previousYaw - 360
- EndIf
- EndIf
- RotateEntity p\mesh,0,p\previousYaw+(p\nextYaw-p\previousYaw)*curTime,0
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Else ; update our player, basically it just compute gravity
- p\velocityY = p\velocityY - 0.003 ; fake gravity
- If EntityY(p\mesh) - p\currentY >= 0 ; if player is not falling we limit the gravity
- If p\velocityY < -0.003 Then p\velocityY = -0.003
- EndIf
- p\currentY = EntityY(p\mesh)
- TranslateEntity p\mesh,0,p\velocityY,0
- EndIf
- Next
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function CreatePlayer(id,name$)
- p.player = New player
- p\id = id
- p\name = name
- p\life = 10
- p\mesh = CreateCube()
- EntityType p\mesh,type_player
- EntityRadius p\mesh,1.0,1.0
- ScaleEntity p\mesh, 1, 1, 1
- NameEntity p\mesh,Handle(p)
- nose = CreateSphere(8,p\mesh):EntityColor nose,255,0,0
- PositionEntity nose,0,0,1
- ScaleEntity nose,0.3, 0.3, 0.3
- p\nextX = -20
- p\nextY = 1
- p\nextZ = -20
- p\nextYaw = -45
- p\previousX = p\nextX
- p\previousY = p\nextY
- p\previousZ = p\nextZ
- p\previousYaw = p\nextYaw
- p\r = 0
- p\g = Rand ( 64, 255 )
- p\b = Rand ( 64, 255 )
- EntityColor p\mesh,p\r,p\g,p\b
- PositionEntity p\mesh,p\nextX,p\nextY,p\nextZ
- RotateEntity p\mesh,0,p\nextYaw,0
- ResetEntity p\mesh
- p\currentY = p\nextY
- p\previousTime = MilliSecs()
- p\nextTime = MilliSecs()+1
- Return p\mesh
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function ResetPlayer(id)
- p.player = Object.player(GetPlayer(id))
- p\life = 90
- p\nextX = -20
- p\nextY = 1
- p\nextZ = -20
- p\nextYaw = -45
- p\previousX = p\nextX
- p\previousY = p\nextY
- p\previousZ = p\nextZ
- p\previousYaw = p\nextYaw
- PositionEntity p\mesh,p\nextX,p\nextY,p\nextZ
- RotateEntity p\mesh,0,p\nextYaw,0
- ResetEntity p\mesh
- p\currentY = p\nextY
- p\previousTime = MilliSecs()
- p\nextTime = MilliSecs()+1
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function GetPlayer(id)
- For p.player = Each player
- If p\id = id
- Return Handle(p)
- EndIf
- Next
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function GetItem(id)
- For i.item = Each item
- If i\id = id
- Return Handle(i)
- EndIf
- Next
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function DrawPlayersNames()
- For p.player = Each player
- Color 255,255,255
- CameraProject(camera,EntityX(p\mesh),EntityY(p\mesh),EntityZ(p\mesh))
- If p\id <> localID Then Text ProjectedX(),ProjectedY()-60,p\name,1,1
- Color 255,(255.0/100.0)*p\life,(255.0/100.0)*p\life
- Text ProjectedX(),ProjectedY()-40,p\life,1,1
- Next
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function CreateBullet(id)
- p.player = Object.player(GetPlayer(id))
- b.bullet = New bullet
- b\id = p\id
- b\mesh = CreateSphere(2)
- ScaleEntity b\mesh,0.2,0.2,0.2
- EntityColor b\mesh,255,0,0
- PositionEntity b\mesh,EntityX(p\mesh),EntityY(p\mesh),EntityZ(p\mesh)
- RotateEntity b\mesh,0,EntityYaw(p\mesh),0
- EntityType b\mesh,type_bullet
- EntityRadius b\mesh,0.2
- b\time = MilliSecs()
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function UpdateBullets()
- For b.bullet = Each bullet
- MoveEntity b\mesh,0,0,1
- If CountCollisions(b\mesh)
- If localMode = 2 ; only the server compute bullets collision with player
- If GetEntityType(CollisionEntity(b\mesh,1)) = type_player ; a player is hit
- touchedPlayer.player = Object.player(EntityName(CollisionEntity(b\mesh,1)))
- touchedPlayer\life = touchedPlayer\life - 10
- If touchedPlayer\life > 0 ; touched
- Net_SendMessage(5,touchedPlayer\life,touchedPlayer\id)
- Net_SendMessage(5,touchedPlayer\life,touchedPlayer\id,touchedPlayer\id)
- Else ; killed
- killerPlayer.player = Object.player(GetPlayer(b\id))
- Net_SendMessage(6,touchedPlayer\id,killerPlayer\id)
- Net_SendMessage(6,touchedPlayer\id,killerPlayer\id,killerPlayer\id)
- NewMessage(killerPlayer\name + " killed " + touchedPlayer\name)
- ResetPlayer(touchedPlayer\id)
- EndIf
- EndIf
- EndIf
- FreeEntity b\mesh
- Delete b
- ElseIf MilliSecs()-b\time > 2000
- FreeEntity b\mesh
- Delete b
- EndIf
- Next
- End Function
- Function UpdateItems ( )
- ;For every Player in the Scene
- For p.player = Each player
- ;For every Item in the Scene
- For i.item = Each item
- TurnEntity i\id, 0, -1.0, 0
- ;Is Item Collision on ?
- If player_item_collisions_on
- ;How many times did the Player hit an Item ?
- itemcollcnt = CountCollisions ( i\id )
- ;Count the amount of collisions between an item
- For collcnt = 1 To itemcollcnt
- ;Get Collision between Player & Item
- healthpackcoll = CollisionEntity ( p\mesh, itemcollcnt )
- ;If there was a collision between Player & Item
- If GetEntityType ( healthpackcoll ) = type_item
- ;Get the Touched Item
- ;touchedItem.item = Object.item(GetItem(i\id))
- ; If the Player already has Max Health
- If p\life < 90
- ; Raise the Player's Health by a Factor
- p\life = p\life + 10
- ;Send the Message to the Network
- Net_SendMessage(5,p\life,p\id,Net_MsgFrom)
- ; Delete the touched Object from Game
- FreeEntity i\id
- ; Remove each touched Item from Memory
- Delete i
- ; Otherwise
- Else
- ; Do not let the Player get any more Health
- p\life = 90
- ;Send the Message to the Network
- Net_SendMessage(5,p\life,p\id,Net_MsgFrom)
- ; Delete the touched Object from Game
- FreeEntity i\id
- ; Remove each touched Item from Memory
- Delete i
- EndIf
- EndIf
- Next
- Else
- ; Shoot a pick Line from the player straight to Item
- itempicked = LinePick ( EntityX# ( p\mesh, 1 ), EntityY# ( p\mesh, 1 ), EntityZ# ( p\mesh, 1 ), 0.0, 0.0, 1.0 )
- ; If the Player is within the vicinity of an Item, or the Player "sees" the Item
- If EntityDistance ( p\mesh, i\id ) <= 2.0 Or ( EntityDistance ( p\mesh, i\id ) <= 2.0 And itempicked ) Or ( EntityDistance ( p\mesh, i\id ) <= 2.0 And EntityInView ( i\id, camera ) )
- ; If the Player already has Max Health
- If p\life < 90
- ; Raise the Player's Health by a Factor
- p\life = p\life + 10
- ;Send the Message to the Network
- Net_SendMessage(5,p\life,p\id,Net_MsgFrom)
- ; Delete the touched Object from Game
- FreeEntity i\id
- ; Remove Each touched Item from Memory
- Delete i
- ; Otherwise
- Else
- ; Don't give the Player anymore Health
- p\life = 90
- ;Send the Message to the Network
- Net_SendMessage(5,p\life,p\id,Net_MsgFrom)
- ; Delete the touched Object from Game
- FreeEntity i\id
- ; Remove Each touched Item from Memory
- Delete i
- EndIf
- EndIf
- EndIf
- Next
- PositionEntity sky, EntityX# ( p\mesh, 1 ), EntityY# ( p\mesh, 1 ), EntityZ# ( p\mesh, 1 ), 1
- Next
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function NewMessage(msg$)
- messageOffset = messageOffset + 1
- If messageOffset > messageDisplayNumber
- messageOffset = messageOffset - 1
- For i = 2 To messageDisplayNumber
- message(i-1) = message(i)
- Next
- message(messageDisplayNumber) = msg
- Else
- message(messageOffset) = msg
- EndIf
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function DrawMessages()
- Color 255,255,255
- For i1 = 1 To messageOffset
- Text 0,(i1-1)*15,message(i1)
- Next
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function UpdateChat()
- key = GetKey()
- If key
- If key = 13
- If chatMessage <> localName + ": "
- For i = Len(localName + ": ") To Len(chatMessage)
- If Mid(chatMessage,i,1) <> " "
- Net_SendMessage(1,chatMessage)
- NewMessage(chatMessage)
- Exit
- EndIf
- Next
- EndIf
- chatMessage = localName + ": "
- chatMode = 0
- ElseIf key = 8 And Len(chatMessage) > Len ( localName + ": " )
- chatMessage = Left(chatMessage,Len(chatMessage)-1)
- ElseIf key > 31 And key < 127
- chatMessage = chatMessage + Chr(key)
- EndIf
- EndIf
- Color 255,255,255
- Rect 0,GraphicsHeight()-20,GraphicsWidth(),20,1
- Color 0,0,0
- Rect 0,GraphicsHeight()-20,GraphicsWidth(),20,0
- If MilliSecs()-chatMillisecs > 500
- chatCursor = Not chatCursor
- chatMillisecs = MilliSecs()
- EndIf
- If chatCursor
- Text 5,GraphicsHeight()-17,chatMessage + "|"
- Else
- Text 5,GraphicsHeight()-17,chatMessage
- EndIf
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function CreateScene()
- texture = TextureCreate(128)
- ground = CreatePlane()
- EntityColor ground,150,150,150
- EntityTexture ground,texture
- cube = CreateCube():EntityColor cube,150,200,150
- ScaleEntity cube,5,5,5:PositionEntity cube,0,5,0
- EntityTexture cube,texture
- sphere = CreateSphere(16):EntityColor sphere,200,150,150
- ScaleEntity sphere,3,1,3:PositionEntity sphere,0,9.7,0
- platform = CreateCube():EntityColor platform,150,150,200
- ScaleEntity platform,3,1,10:PositionEntity platform,0,4.13,-13.15
- TurnEntity platform,-30,0,0
- EntityTexture platform,texture
- FreeTexture texture
- EntityType ground,type_scene:EntityPickMode ground,2
- EntityType cube,type_scene:EntityPickMode cube,2
- EntityType sphere,type_scene:EntityPickMode sphere,2
- EntityType platform,type_scene:EntityPickMode platform,2
- light = CreateLight()
- TurnEntity light,30,70,0
- For x = 1 To maxhealthpacks
- i.item = New item
- i\id = CreateCube()
- i\itemcount = ( maxhealthpacks + 1 )
- PositionEntity i\id, Rnd ( -25, 25 ), 1.001, ( Rnd ( -25, 25 ) )
- EntityPickMode i\id, 2
- EntityType i\id, type_item
- EntityRadius i\id, 1
- EntityColor i\id, 0, ( Rnd ( 64, 255 ) ), ( Rnd ( 64, 255 ) )
- EntityAlpha i\id, ( Rnd ( 0.25, 0.65 ) )
- Next
- End Function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- Function TextureCreate(size)
- texture = CreateTexture(size,size,9)
- SetBuffer TextureBuffer(texture)
- Color 255,255,255
- Rect 0,0,size,size,1
- Color 0,0,0
- Rect 0,0,size,size,0
- Line 0,0,size,size
- Line 0,size,size,0
- SetBuffer BackBuffer()
- Return texture
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement