gnetdemo.bmx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. Strict
  2. ?Win32
  3. Framework BRL.D3D7Max2D
  4. ?MacOS
  5. Framework BRL.GLMax2D
  6. ?
  7. Import BRL.GNet
  8. Import BRL.BASIC
  9. Import BRL.PNGLoader
  10. Const GAMEPORT=12345
  11. Const SLOT_TYPE=0
  12. Const SLOT_NAME=1
  13. Const SLOT_CHAT=2
  14. Const SlOT_SCORE=3
  15. Const SLOT_X=4
  16. Const SLOT_Y=5
  17. Const SLOT_VX=6
  18. Const SLOT_VY=7
  19. Const SLOT_ROT=8
  20. Const SLOT_TIMEOUT=9
  21. Const SLOT_HIT=10
  22. Local GWIDTH=640
  23. Local GHEIGHT=480
  24. Local GDEPTH=0
  25. Local GHERTZ=30
  26. Graphics GWIDTH,GHEIGHT,GDEPTH,GHERTZ
  27. AutoMidHandle True
  28. Local playerImage:TImage=LoadImage( "ship.png" )
  29. Local bulletImage:TImage=LoadImage( "bullet1.png" )
  30. Local warpImage:TImage=LoadImage( "sparkle.png" )
  31. Local host:TGNetHost=CreateGNetHost()
  32. SeedRnd MilliSecs()
  33. Local playerName$="Player"
  34. Local playerChat$=""
  35. Local playerX#=Rnd(GWIDTH-64)+32
  36. Local playerY#=Rnd(GHEIGHT-64)+32
  37. Local playerVx#=0
  38. Local playerVy#=0
  39. Local playerRot#=0
  40. Local playerScore=0
  41. Local playerHit#=0
  42. Local playerShot=0
  43. 'create local player
  44. Local localPlayer:TGNetObject=CreateGNetObject( host )
  45. SetGNetString localPlayer,SLOT_TYPE,"player"
  46. SetGNetString localPlayer,SLOT_NAME,playerName
  47. SetGNetString localPlayer,SLOT_CHAT,"Ready"
  48. SetGNetFloat localPlayer,SLOT_X,playerX
  49. SetGNetFloat localPlayer,SLOT_Y,playerY
  50. SetGNetFloat localPlayer,SLOT_ROT,playerRot
  51. SetGNetFloat localPlayer,SLOT_HIT,playerHit
  52. SetGNetInt localPlayer,SLOT_SCORE,playerScore
  53. While Not KeyHit( KEY_ESCAPE )
  54. Local c=GetChar()
  55. Select c
  56. Case 8
  57. If playerChat playerChat=playerChat[..playerChat.length-1]
  58. Case 13
  59. If playerChat
  60. If playerChat[..1]="/"
  61. Local cmd$=playerChat[1..]
  62. Local i=cmd.Find(" "),arg$
  63. If i<>-1
  64. arg=cmd[i+1..]
  65. cmd=cmd[..i]
  66. EndIf
  67. Select cmd.ToLower()
  68. Case "nick"
  69. If arg
  70. playerName=arg
  71. SetGNetString localPlayer,SLOT_NAME,playerName
  72. EndIf
  73. Case "listen"
  74. If Not GNetListen( host,GAMEPORT ) Notify "Listen failed"
  75. Case "connect"
  76. If Not arg arg="localhost"
  77. If Not GNetConnect( host,arg,GAMEPORT ) Notify "Connect failed"
  78. End Select
  79. Else
  80. SetGNetString localPlayer,SLOT_CHAT,playerChat
  81. EndIf
  82. playerChat=""
  83. EndIf
  84. Default
  85. If c>31 And c<127 playerChat:+Chr(c)
  86. End Select
  87. If KeyDown( KEY_LEFT )
  88. playerRot:-5
  89. If playerRot<-180 playerRot:+360
  90. SetGNetFloat localPlayer,SLOT_ROT,playerRot
  91. Else If KeyDown( KEY_RIGHT )
  92. playerRot:+5
  93. If playerRot>=180 playerRot:-360
  94. SetGNetFloat localPlayer,SLOT_ROT,playerRot
  95. EndIf
  96. If KeyDown( KEY_UP )
  97. playerVx:+Cos(playerRot)*.15
  98. playerVy:+Sin(playerRot)*.15
  99. Else
  100. playerVx:*.99
  101. If Abs(playerVx)<.1 playerVx=0
  102. playerVy:*.99
  103. If Abs(playerVy)<.1 playerVy=0
  104. EndIf
  105. If playerVx
  106. playerX:+playerVx
  107. If playerX<-8 playerX:+GWIDTH+16 Else If playerX>=GWIDTH+8 playerX:-GWIDTH+16
  108. SetGNetFloat localPlayer,SLOT_X,playerX
  109. EndIf
  110. If playerVy
  111. playerY:+playerVy
  112. If playerY<-8 playerY:+GHEIGHT+16 Else If playerY>=GHEIGHT+8 playerY:-GHEIGHT+16
  113. SetGNetFloat localPlayer,SLOT_Y,playerY
  114. EndIf
  115. If playerShot playerShot:-1
  116. If KeyHit( KEY_LALT ) And Not playerShot
  117. Local obj:TGnetObject=CreateGNetObject( host )
  118. SetGNetString obj,SLOT_TYPE,"bullet"
  119. SetGNetFloat obj,SLOT_X,playerX
  120. SetGNetFloat obj,SLOT_Y,playerY
  121. SetGNetFloat obj,SLOT_VX,playerVx+Cos(playerRot)*10
  122. SetGNetFloat obj,SLOT_VY,playerVy+Sin(playerRot)*10
  123. SetGNetInt obj,SLOT_TIMEOUT,60
  124. playerShot=5
  125. EndIf
  126. 'update bullets
  127. For Local obj:TGNetObject=EachIn GNetObjects( host )
  128. If obj.State()=GNET_CLOSED Continue
  129. Local typ$=GetGNetString( obj,SLOT_TYPE )
  130. If typ<>"bullet" Continue
  131. Local x#=GetGNetFloat( obj,SLOT_X )
  132. Local y#=GetGNetFloat( obj,SLOT_Y )
  133. If GNetObjectRemote( obj )
  134. 'remote bullet? Check for collision...
  135. Local dx#=x-playerX,dy#=y-playerY
  136. If dx*dx+dy*dy<256'144
  137. Local msg:TGNetObject=CreateGNetMessage( host )
  138. If playerHit
  139. SetGNetString msg,SLOT_TYPE,"gotme"
  140. Else
  141. SetGNetString msg,SLOT_TYPE,"hurtme"
  142. playerHit=1
  143. EndIf
  144. SendGNetMessage msg,obj
  145. EndIf
  146. Else
  147. 'local bullet? Update...
  148. Local t=GetGNetInt( obj,SLOT_TIMEOUT )
  149. If Not t
  150. CloseGNetObject obj
  151. Continue
  152. EndIf
  153. Local vx#=GetGNetFloat( obj,SLOT_VX )
  154. Local vy#=GetGNetFloat( obj,SLOT_VY )
  155. Local dx#=x-GWIDTH/2
  156. Local dy#=y-GHEIGHT/2
  157. Local rot#=ATan2(dy,dx)
  158. Local accel#=1/(dx*dx+dy*dy)*2000
  159. vx:-Cos(rot)*accel
  160. vy:-Sin(rot)*accel
  161. x:+vx
  162. y:+vy
  163. SetGNetFloat obj,SLOT_X,x
  164. SetGNetFloat obj,SLOT_Y,y
  165. SetGNetFloat obj,SLOT_VX,vx
  166. SetGNetFloat obj,SLOT_VY,vy
  167. SetGNetInt obj,SLOT_TIMEOUT,t-1
  168. EndIf
  169. Next
  170. If playerHit
  171. playerHit:-.05
  172. If playerHit<0 playerHit=0
  173. SetGNetFloat localPlayer,SLOT_HIT,playerHit
  174. EndIf
  175. GNetSync host
  176. For Local msg:TGNetObject=EachIn GNetMessages( host )
  177. Local typ$=GetGNetString( msg,SLOT_TYPE )
  178. Select typ
  179. Case "gotme","hurtme"
  180. Local obj:TGNetObject=GNetMessageObject(msg)
  181. If obj.State()<>GNET_CLOSED
  182. If typ="hurtme"
  183. playerScore:+1
  184. SetGNetInt localPlayer,SLOT_SCORE,playerScore
  185. EndIf
  186. CloseGNetObject obj
  187. EndIf
  188. End Select
  189. Next
  190. Cls
  191. Local ty
  192. For Local obj:TGNetObject=EachIn GNetObjects( host )
  193. If obj.State()=GNET_CLOSED Continue
  194. Local typ$=GetGNetString( obj,SLOT_TYPE )
  195. Local x#=GetGNetFloat( obj,SLOT_X )
  196. Local y#=GetGNetFloat( obj,SLOT_Y )
  197. Select typ
  198. Case "bullet"
  199. SetBlend LIGHTBLEND
  200. SetColor 255,255,255
  201. DrawImage bulletImage,x,y
  202. SetBlend MASKBLEND
  203. Case "player"
  204. Local rot#=GetGNetFloat( obj,SLOT_ROT )
  205. Local name$=GetGNetString( obj,SLOT_NAME )
  206. Local chat$=GetGNetString( obj,SLOT_CHAT )
  207. Local score=GetGNetInt( obj,SLOT_SCORE )
  208. Local hit#=GetGNetFloat( obj,SLOT_HIT )
  209. SetRotation rot
  210. SetColor 255,255,255
  211. DrawImage playerImage,x,y
  212. If hit
  213. SetAlpha hit
  214. SetBlend LIGHTBLEND
  215. DrawImage playerImage,x,y
  216. SetBlend MASKBLEND
  217. SetAlpha 1
  218. SetColor 255,255,255
  219. EndIf
  220. SetRotation 0
  221. DrawText name+":"+score,x,y+16
  222. If obj=localPlayer SetColor 255,255,255 Else SetColor 0,128,255
  223. DrawText name+":"+chat,0,ty
  224. ty:+16
  225. End Select
  226. Next
  227. If playerChat
  228. SetColor 255,255,0
  229. DrawText ">"+playerChat,0,GHEIGHT-16
  230. SetColor 0,255,0
  231. DrawRect TextWidth(">"+playerChat),GHEIGHT-16,8,16
  232. EndIf
  233. SetColor 255,255,255
  234. Local txt$="MemAllocd:"+GCMemAlloced()
  235. DrawText txt,GWIDTH-TextWidth(txt),0
  236. SetBlend LIGHTBLEND
  237. SetRotation Rnd(360)
  238. SetScale Rnd(2,2.125),Rnd(2,2.125)
  239. DrawImage warpImage,GWIDTH/2,GHEIGHT/2
  240. SetScale 1,1
  241. SetRotation 0
  242. SetBlend MASKBLEND
  243. Flip
  244. Wend
  245. CloseGNetHost host