浏览代码

Added gnet samples.

woollybah 6 年之前
父节点
当前提交
f57f29cd8c
共有 5 个文件被更改,包括 418 次插入0 次删除
  1. 二进制
      samples/mak/bullet1.png
  2. 131 0
      samples/mak/gnetchat.bmx
  3. 287 0
      samples/mak/gnetdemo.bmx
  4. 二进制
      samples/mak/ship.png
  5. 二进制
      samples/mak/sparkle.png

二进制
samples/mak/bullet1.png


+ 131 - 0
samples/mak/gnetchat.bmx

@@ -0,0 +1,131 @@
+
+Strict
+
+?Win32
+Framework BRL.D3D9Max2D
+?MacOS
+Framework BRL.GLMax2D
+?
+Import BRL.GNet
+
+AppTitle="GNet Test2"
+
+Local host:TGNetHost=CreateGNetHost()
+
+Local me:TGNetObject
+Local chat$,info$
+
+Graphics 800,600,0,15
+
+Repeat
+
+	Local c=GetChar()
+	
+	Select c
+	Case 8
+		If chat chat=chat[..chat.length-1]
+	Case 27
+		If Confirm( "Quit?" )
+			CloseGNetHost host
+			End
+		EndIf
+	Case 13
+		If chat.find("/")=0
+			chat=chat[1..]
+			Local cmd$=chat
+			Local arg$
+			Local i=chat.find(" ")
+			If i<>-1
+				cmd=chat[..i]
+				arg=chat[i+1..]
+			EndIf
+			Select cmd
+			Case "create"
+				If me
+					info="Already created"
+				Else
+					me=CreateGNetObject( host )
+					SetGNetString me,0,arg
+					SetGNetString me,1,"Ready"
+				EndIf
+			Case "close"
+				If me
+					CloseGNetObject me
+					me=Null
+				Else
+					info="Not created"
+				EndIf
+			Case "quit","exit"
+				CloseGNetHost host
+				End
+			Case "nick"
+				If arg
+					If me SetGNetString me,0,arg
+					info="Nick changed to "+arg
+				Else
+					info="Expecting arg"
+				EndIf
+			Case "listen"
+				Local port=12345
+				If arg port=Int(arg)
+				If GNetListen( host,port )
+					info="Listening on port "+port
+				Else
+					info="Listen failed"
+				EndIf
+			Case "connect"
+				If arg
+					Local addr$=arg
+					Local port=12345
+					Local i=arg.find(":")
+					If i<>-1
+						addr=arg[..i]
+						port=Int(arg[i+1..])
+					EndIf
+					If GNetConnect( host,addr,port )
+						info="Connected to "+addr+":"+port
+					Else
+						info="Failed to connect to "+addr+":"+port
+					EndIf
+				Else
+					info="Expecting arg"
+				EndIf
+			Default
+				info="Unrecognized command '"+cmd+"'"
+			End Select
+		Else
+			If me SetGNetString me,1,chat
+		EndIf
+		chat=""
+	Default
+		If c>31 And c<127 chat:+Chr(c)
+	End Select
+	
+	GNetSync host
+	
+	Cls
+
+	Local y,h=GraphicsHeight()
+	
+	For Local obj:TGNetObject=EachIn GNetObjects( host,GNET_ALL )
+		If obj.state()=GNET_CLOSED Continue
+		If obj=me
+			SetColor 255,255,255
+		Else
+			SetColor 0,128,255
+		EndIf
+		DrawText GetGNetString( obj,0 )+":"+GetGNetString( obj,1 ),0,y
+		y:+16
+	Next
+	
+	SetColor 255,255,0
+	DrawText info,0,h-32
+	
+	SetColor 0,255,0
+	DrawText ">"+chat,0,h-16
+	DrawRect TextWidth(">"+chat),h-16,8,16
+	DrawText "/create nick    /listen    /connect host    /quit    /nick newnick",0,h-48
+	
+	Flip
+	
+Forever

+ 287 - 0
samples/mak/gnetdemo.bmx

@@ -0,0 +1,287 @@
+
+Strict
+
+?Win32
+Framework BRL.D3D9Max2D
+?MacOS
+Framework BRL.GLMax2D
+?
+Import BRL.GNet
+Import BRL.BASIC
+Import BRL.PNGLoader
+
+Const GAMEPORT=12345
+
+Const SLOT_TYPE=0
+Const SLOT_NAME=1
+Const SLOT_CHAT=2		
+Const SlOT_SCORE=3
+Const SLOT_X=4
+Const SLOT_Y=5
+Const SLOT_VX=6
+Const SLOT_VY=7
+Const SLOT_ROT=8
+Const SLOT_TIMEOUT=9
+Const SLOT_HIT=10
+
+Local GWIDTH=640
+Local GHEIGHT=480
+Local GDEPTH=0
+Local GHERTZ=30
+
+Graphics GWIDTH,GHEIGHT,GDEPTH,GHERTZ
+
+AutoMidHandle True
+Local playerImage:TImage=LoadImage( "ship.png" )
+Local bulletImage:TImage=LoadImage( "bullet1.png" )
+Local warpImage:TImage=LoadImage( "sparkle.png" )
+
+Local host:TGNetHost=CreateGNetHost()
+
+SeedRnd MilliSecs()
+
+Local playerName$="Player"
+Local playerChat$=""
+Local playerX#=Rnd(GWIDTH-64)+32
+Local playerY#=Rnd(GHEIGHT-64)+32
+Local playerVx#=0
+Local playerVy#=0
+Local playerRot#=0
+Local playerScore=0
+Local playerHit#=0
+Local playerShot=0
+
+'create local player
+Local localPlayer:TGNetObject=CreateGNetObject( host )
+
+SetGNetString localPlayer,SLOT_TYPE,"player"
+SetGNetString localPlayer,SLOT_NAME,playerName
+SetGNetString localPlayer,SLOT_CHAT,"Ready"
+SetGNetFloat localPlayer,SLOT_X,playerX
+SetGNetFloat localPlayer,SLOT_Y,playerY
+SetGNetFloat localPlayer,SLOT_ROT,playerRot
+SetGNetFloat localPlayer,SLOT_HIT,playerHit
+SetGNetInt localPlayer,SLOT_SCORE,playerScore
+
+While Not KeyHit( KEY_ESCAPE )
+
+	Local c=GetChar()
+	Select c
+	Case 8
+		If playerChat playerChat=playerChat[..playerChat.length-1]
+	Case 13
+		If playerChat
+			If playerChat[..1]="/"
+				Local cmd$=playerChat[1..]
+				Local i=cmd.Find(" "),arg$
+				If i<>-1
+					arg=cmd[i+1..]
+					cmd=cmd[..i]
+				EndIf
+				Select cmd.ToLower()
+				Case "nick"
+					If arg
+						playerName=arg
+						SetGNetString localPlayer,SLOT_NAME,playerName
+					EndIf
+				Case "listen"
+					If Not GNetListen( host,GAMEPORT ) Notify "Listen failed"
+				Case "connect"
+					If Not arg arg="localhost"
+					If Not GNetConnect( host,arg,GAMEPORT ) Notify "Connect failed"
+				End Select
+			Else
+				SetGNetString localPlayer,SLOT_CHAT,playerChat
+			EndIf
+			playerChat=""
+		EndIf
+	Default
+		If c>31 And c<127 playerChat:+Chr(c)
+	End Select
+	
+	If KeyDown( KEY_LEFT )
+		playerRot:-5
+		If playerRot<-180 playerRot:+360
+		SetGNetFloat localPlayer,SLOT_ROT,playerRot
+	Else If KeyDown( KEY_RIGHT )
+		playerRot:+5
+		If playerRot>=180 playerRot:-360
+		SetGNetFloat localPlayer,SLOT_ROT,playerRot
+	EndIf
+	
+	If KeyDown( KEY_UP )
+		playerVx:+Cos(playerRot)*.15
+		playerVy:+Sin(playerRot)*.15
+	Else
+		playerVx:*.99
+		If Abs(playerVx)<.1 playerVx=0
+		playerVy:*.99
+		If Abs(playerVy)<.1 playerVy=0
+	EndIf
+	
+	If playerVx
+		playerX:+playerVx
+		If playerX<-8 playerX:+GWIDTH+16 Else If playerX>=GWIDTH+8 playerX:-GWIDTH+16
+		SetGNetFloat localPlayer,SLOT_X,playerX
+	EndIf
+	
+	If playerVy
+		playerY:+playerVy
+		If playerY<-8 playerY:+GHEIGHT+16 Else If playerY>=GHEIGHT+8 playerY:-GHEIGHT+16
+		SetGNetFloat localPlayer,SLOT_Y,playerY
+	EndIf
+	
+	If playerShot playerShot:-1
+	
+	If KeyHit( KEY_LALT ) And Not playerShot
+		Local obj:TGnetObject=CreateGNetObject( host )
+		SetGNetString obj,SLOT_TYPE,"bullet"
+		SetGNetFloat obj,SLOT_X,playerX
+		SetGNetFloat obj,SLOT_Y,playerY
+		SetGNetFloat obj,SLOT_VX,playerVx+Cos(playerRot)*10
+		SetGNetFloat obj,SLOT_VY,playerVy+Sin(playerRot)*10
+		SetGNetInt obj,SLOT_TIMEOUT,60
+		playerShot=5
+	EndIf
+	
+	'update bullets
+	For Local obj:TGNetObject=EachIn GNetObjects( host )
+
+		If obj.State()=GNET_CLOSED Continue
+	
+		Local typ$=GetGNetString( obj,SLOT_TYPE )
+		If typ<>"bullet" Continue
+
+		Local x#=GetGNetFloat( obj,SLOT_X )
+		Local y#=GetGNetFloat( obj,SLOT_Y )
+		
+		If GNetObjectRemote( obj )
+			'remote bullet? Check for collision...
+			Local dx#=x-playerX,dy#=y-playerY
+			If dx*dx+dy*dy<256'144
+				Local msg:TGNetObject=CreateGNetMessage( host )
+				If playerHit
+					SetGNetString msg,SLOT_TYPE,"gotme"
+				Else
+					SetGNetString msg,SLOT_TYPE,"hurtme"
+					playerHit=1
+				EndIf
+				SendGNetMessage msg,obj
+			EndIf
+		Else
+			'local bullet? Update...
+			Local t=GetGNetInt( obj,SLOT_TIMEOUT )
+			
+			If Not t
+				CloseGNetObject obj
+				Continue
+			EndIf
+
+			Local vx#=GetGNetFloat( obj,SLOT_VX )
+			Local vy#=GetGNetFloat( obj,SLOT_VY )
+			
+			Local dx#=x-GWIDTH/2
+			Local dy#=y-GHEIGHT/2
+			Local rot#=ATan2(dy,dx)
+			Local accel#=1/(dx*dx+dy*dy)*2000
+			vx:-Cos(rot)*accel
+			vy:-Sin(rot)*accel
+			x:+vx
+			y:+vy
+
+			SetGNetFloat obj,SLOT_X,x
+			SetGNetFloat obj,SLOT_Y,y
+			SetGNetFloat obj,SLOT_VX,vx
+			SetGNetFloat obj,SLOT_VY,vy
+			SetGNetInt obj,SLOT_TIMEOUT,t-1
+		EndIf
+	Next
+	
+	If playerHit
+		playerHit:-.05
+		If playerHit<0 playerHit=0
+		SetGNetFloat localPlayer,SLOT_HIT,playerHit
+	EndIf
+	
+	GNetSync host
+	
+	For Local msg:TGNetObject=EachIn GNetMessages( host )
+		Local typ$=GetGNetString( msg,SLOT_TYPE )
+		Select typ
+		Case "gotme","hurtme"
+			Local obj:TGNetObject=GNetMessageObject(msg)
+			If obj.State()<>GNET_CLOSED
+				If typ="hurtme" 
+					playerScore:+1
+					SetGNetInt localPlayer,SLOT_SCORE,playerScore
+				EndIf
+				CloseGNetObject obj
+			EndIf
+		End Select
+	Next
+	
+	Cls
+	
+	Local ty
+	For Local obj:TGNetObject=EachIn GNetObjects( host )
+
+		If obj.State()=GNET_CLOSED Continue
+
+		Local typ$=GetGNetString( obj,SLOT_TYPE )
+		Local x#=GetGNetFloat( obj,SLOT_X )
+		Local y#=GetGNetFloat( obj,SLOT_Y )
+		Select typ
+		Case "bullet"
+			SetBlend LIGHTBLEND
+			SetColor 255,255,255
+			DrawImage bulletImage,x,y
+			SetBlend MASKBLEND
+		Case "player"
+			Local rot#=GetGNetFloat( obj,SLOT_ROT )
+			Local name$=GetGNetString( obj,SLOT_NAME )
+			Local chat$=GetGNetString( obj,SLOT_CHAT )
+			Local score=GetGNetInt( obj,SLOT_SCORE )
+			Local hit#=GetGNetFloat( obj,SLOT_HIT )
+			SetRotation rot
+			SetColor 255,255,255
+			DrawImage playerImage,x,y
+			If hit 
+				SetAlpha hit
+				SetBlend LIGHTBLEND
+				DrawImage playerImage,x,y
+				SetBlend MASKBLEND
+				SetAlpha 1
+				SetColor 255,255,255
+			EndIf
+			SetRotation 0
+			DrawText name+":"+score,x,y+16
+			If obj=localPlayer SetColor 255,255,255 Else SetColor 0,128,255
+			DrawText name+":"+chat,0,ty
+			ty:+16
+		End Select
+	Next
+	
+	If playerChat
+		SetColor 255,255,0
+		DrawText ">"+playerChat,0,GHEIGHT-16
+		SetColor 0,255,0
+		DrawRect TextWidth(">"+playerChat),GHEIGHT-16,8,16
+	EndIf
+	
+	SetColor 255,255,255
+	Local txt$="MemAllocd:"+GCMemAlloced()
+	DrawText txt,GWIDTH-TextWidth(txt),0
+	
+	SetBlend LIGHTBLEND
+	SetRotation Rnd(360)
+	SetScale Rnd(2,2.125),Rnd(2,2.125)
+	DrawImage warpImage,GWIDTH/2,GHEIGHT/2
+	SetScale 1,1
+	SetRotation 0
+	SetBlend MASKBLEND
+	
+	Flip
+
+Wend
+
+CloseGNetHost host

二进制
samples/mak/ship.png


二进制
samples/mak/sparkle.png