All pastes #800900 Raw Edit

Jemand

public text v1 · immutable
#800900 ·published 2007-11-28 20:53 UTC
rendered paste body
Include "sprite candy.bb"
Include "sprite candy gui.bb"
;################################################## Player Type, hier werden die Spieler gespeichert.... :A
Type TPlayers
	Field X#
	Field Y#
	Field Rotation#
	Field Beschleunigung#
	Field Port
	Field IP
	Field SchussKraft#
	Field MaxSchussKraft#
	Field PfeilObjekt
	Field LastAufladen#
	Field PlayerImage
	Field Farbe
End Type
;##################################################
Type TSchuss
	Field X#
	Field Y#
	Field Rotation#
	Field Speed#
	Field LastMove
	Field Farbe
	Field SchussObjekt
	Field SpielerIP
End Type
;################################################## Ja, allgemeiner Grafikkram, Auflösung 800x600, 32 bit, Fenstermodus + Tformfilter 0 für die Drehung des Autos
Graphics3D 800,600,32,2
TFormFilter 0
Global OwnUDPStream = CreateUDPStream()
;##################################################
SeedRnd MilliSecs()
;##################################################
Global HauptKamera  = CreateCamera()
CameraClsMode HauptKamera,0,1
Global Main_HUD = HUD_Create(HauptKamera)
;##################################################
Global Sicherheits_Layer = HUD_CreateLayer(Main_HUD, 0, 5)
Global Sicherheits_Shape = HUD_CreateShape(Sicherheits_Layer, "RING", 90, 10, 0,0,500, 500)
HUD_SetObjectOrigin(Sicherheits_Shape, 0,0)
HUD_SetObjectColor(Sicherheits_Shape, 255,0,0,0.8)
HUD_SetObjectShadow(Sicherheits_Shape, 20, 10.0)
;##################################################
Global Tank_Texture = HUD_LoadImageResource("panzer.bmp", 4,255,0,255)
Global Tank_Layer = HUD_CreateLayer(Main_HUD, Tank_Texture, 2)
;##################################################
Global Backgroundimage = LoadImage("hintergrund.bmp")
;##################################################
Global Main_GUI = GUI_Create(Main_HUD, "gui/sd.png", "gui/font_title.png", "gui/font.png", "gui/icons.png")
Global GUI_InformationWindow = GUI_CreateWindow("Schüsse", 5, 570, 780, 24, 0, "LEFT+5")
Global GUI_InformationSchusskraft = GUI_AddProgBar(GUI_InformationWindow, 10,5,760, 1.0)
HUD_SetObjectAlpha(GUI_InformationWindow, 0.8)
;##################################################
Global GUI_FarbWindow = GUI_CreateWindow("Farbe", 720,0, 150,100, 0,"", "TOP")
Global GUI_FarbPicker = GUI_AddColorPicker(GUI_FarbWindow,0,0,150,100)
HUD_SetObjectAlpha(GUI_FarbWindow, 0.8)
GUI_SetColorPickerCoords(GUI_FarbPicker, 0,0,0)
;##################################################
Global GUI_JoinWindow = GUI_CreateWindow("Netzwerk", 105,51,200, 75, 0)
Global GUI_PortLabel = GUI_AddLabel(GUI_JoinWindow, 5,5,190, 17, "Eigener Port: " + UDPStreamPort(OwnUDPStream), "CENTER", 1)
Global GUI_JoinIP = GUI_AddTextField(GUI_JoinWindow, 5,25,190, 200, "1234567890.","IP")
Global GUI_JoinPort = GUI_AddTextField(GUI_JoinWindow, 5,50,50, 200, "1234567890","PORT")
Global GUI_JoinButton = GUI_AddButton(GUI_JoinWindow, 60,50,100,"Beitreten")
HUD_SetObjectAlpha(GUI_JoinWindow, 0.8)
;##################################################
Global Schuss_Texture = HUD_LoadImageResource("schuss.bmp", 4, 0,0,0)
Global Schuss_Layer = HUD_CreateLayer(Main_HUD, Schuss_Texture, 1)
;##################################################
Global Radar_Texture = HUD_LoadImageResource("radar.bmp", 4, 0,0,0)
Global Radar_Layer = HUD_CreateLayer(Main_HUD, Radar_Texture, 3)
;################################################## Öffnung des Lokalen UDP Streams
Global Players.TPlayers
Global Schuss.TSchuss
;##################################################
Global PanzerSchussSound = LoadSound("sound\tank_01.wav")
;################################################## Selbsterklärend
SetBuffer BackBuffer()
;################################################## Der eigene Spieler wird erstellt, IP = 50 habe ich genommen, damit Blitz erkennt, dass es sich hier um den eigenen Spieler handelt.  50, weil es eine tolle Zahl ist.
Global OwnPlayer.TPlayers = New TPlayers
OwnPlayer\X# = Rand(-100, 100)
OwnPlayer\Y# = Rand(-100, 100)
OwnPlayer\Rotation = Rand(0,350)
OwnPlayer\Port = UDPStreamPort(OwnUDPStream)
OwnPlayer\IP = 50
OwnPlayer\SchussKraft# = 1.0
OwnPlayer\MaxSchussKraft# = 5.0
OwnPlayer\PlayerImage = HUD_CreateImage(Tank_Layer, 0,0,0,0,64,64) 
HUD_SetObjectOrigin(OwnPlayer\PlayerImage, 0,0)
;################################################## Kamerapositionen
Global LastPositionSend, LastIPListSend, LastColorSend
;##################################################
Global CameraX
Global CameraY
;##################################################
Function Angle2D#( x1#,y1#,x2#,y2# )
	Return 180-ATan2(x2-x1,y2-y1)
End Function
;################################################## So, hier kommt die Steuerung, dabei wird zuerst nach dem eigenen Spieler gesucht (der, welcher die IP 50 hat, und dieser ist dann Steuerbar)
Function PlayerControls()
   ;##############################################
	For Players = Each TPlayers 
		Players\X = (Players\X+((Players\Beschleunigung#/2)*Cos(Players\Rotation#)))
		Players\Y = (Players\Y+((Players\Beschleunigung#/2)*Sin(Players\Rotation#)))
	Next	
	;##############################################
	CameraX = OwnPlayer\X
	CameraY = OwnPlayer\Y
   ;##############################################
	GUI_SetGadgetValue(GUI_InformationSchusskraft,OwnPlayer\SchussKraft#/OwnPlayer\MaxSchussKraft#)
	;##############################################
	If KeyDown(200) Then
		If OwnPlayer\Beschleunigung# < -3.0 Then
			OwnPlayer\Beschleunigung# = OwnPlayer\Beschleunigung# + 1
		End If
		OwnPlayer\Beschleunigung# = OwnPlayer\Beschleunigung# + 0.3
	ElseIf KeyDown(208) Then
		If OwnPlayer\Beschleunigung# > 3.0 Then
			OwnPlayer\Beschleunigung# = OwnPlayer\Beschleunigung# - 1
		End If
		OwnPlayer\Beschleunigung# = OwnPlayer\Beschleunigung# - 0.3
	EndIf
   ;##############################################
	If OwnPlayer\Beschleunigung# > 0.0
		OwnPlayer\Beschleunigung# = OwnPlayer\Beschleunigung# - 0.1
	ElseIf OwnPlayer\Beschleunigung# < 0.0
		OwnPlayer\Beschleunigung# = OwnPlayer\Beschleunigung# + 0.1
	End If
	;##############################################
	If MilliSecs() - OwnPlayer\LastAufladen# > 300 Then
		OwnPlayer\LastAufladen# = MilliSecs()
		If OwnPlayer\Beschleunigung < 0.0 Then
			OwnPlayer\SchussKraft# = OwnPlayer\SchussKraft# + (0.2+(OwnPlayer\Beschleunigung#/100))
		Else
			OwnPlayer\SchussKraft# = OwnPlayer\SchussKraft# + (0.2-(OwnPlayer\Beschleunigung#/100))
		End If		
		
	End If
	
	If OwnPlayer\SchussKraft# > OwnPlayer\MaxSchussKraft# Then
		OwnPlayer\SchussKraft# = OwnPlayer\MaxSchussKraft#
	End If
	
	If KeyHit(57) And OwnPlayer\SchussKraft# >= 1.0 And HUD_GetDistance(OwnPlayer\X, OwnPlayer\Y, 0, 0) > 250 Then
		PlaySound(PanzerSchussSound)
		OwnPlayer\SchussKraft# = OwnPlayer\SchussKraft#-1
		Schuss = New TSchuss
		Schuss\X# = OwnPlayer\X#
		Schuss\Y# = OwnPlayer\Y#
		Schuss\Speed# = 20.0
		Schuss\Rotation = OwnPlayer\Rotation
		Schuss\SpielerIP = 50
		Schuss\Farbe = GUI_GetGadgetValue(GUI_FarbPicker)	
		Schuss\SchussObjekt = HUD_CreateImage(Schuss_Layer, Schuss\X#, Schuss\Y#,  0,0,16,16)
		HUD_SetObjectColor(Schuss\SchussObjekt, (GUI_GetGadgetValue(GUI_FarbPicker) And $FF0000)/$10000 , (GUI_GetGadgetValue(GUI_FarbPicker) And $FF00)/$100,GUI_GetGadgetValue(GUI_FarbPicker) And $FF)
		SoundVolume PanzerSchussSound, 1.0
		PlaySound PanzerSchussSound
		For Players = Each TPlayers
			If Players\IP <> 50 Then
				WriteInt OwnUDPStream, 4
				WriteFloat OwnUDPStream, Schuss\X#
				WriteFloat OwnUDPStream, Schuss\Y#
				WriteFloat OwnUDPStream, Schuss\Speed#
				WriteInt OwnUDPStream, Schuss\Rotation
				WriteInt OwnUDPStream, Schuss\Farbe
				SendUDPMsg(OwnUDPStream, Players\IP, Players\Port)
			End If
		Next
	End If
	;##############################################
	RotationsFaktor# = 5+OwnPlayer\Beschleunigung/10
	If KeyDown(203) Then
		OwnPlayer\Rotation# = OwnPlayer\Rotation#-RotationsFaktor#
	ElseIf KeyDown(205) Then
		OwnPlayer\Rotation# = OwnPlayer\Rotation#+RotationsFaktor#
	End If
   ;###############################################
	If OwnPlayer\Rotation# > 360 Then OwnPlayer\Rotation# = 0
	If OwnPlayer\Rotation# < 0  Then OwnPlayer\Rotation# = 360
   ;##############################################
	If OwnPlayer\Beschleunigung# > 0 Then
		OwnPlayer\Beschleunigung# =  OwnPlayer\Beschleunigung# - 0.01
	ElseIf OwnPlayer\Beschleunigung# < 0 Then
		OwnPlayer\Beschleunigung# = OwnPlayer\Beschleunigung# + 0.01
	End If
	If OwnPlayer\Beschleunigung# > 20 Then OwnPlayer\Beschleunigung# = 20
	If OwnPlayer\Beschleunigung# < -20 Then OwnPlayer\Beschleunigung# = -20
;##############################################	
	
;##############################################
End Function
;################################################## Hier werden alle Spieler gezeichnet
Function DrawPlayers()
	For Players = Each TPlayers
		If Players\PlayerImage <> 0 Then
			HUD_SetObjectOrigin(Players\PlayerImage, 0,0)
			HUD_PositionObject(Players\PlayerImage, -(Players\X#-CameraX)+GraphicsWidth()/2, -(Players\Y#-CameraY)+GraphicsHeight()/2)
			HUD_SetObjectRotation(Players\PlayerImage, Players\Rotation#)
			If Players\PfeilObjekt <> 0 Then
				HUD_SetObjectOrigin(Players\PfeilObjekt, 0,0)
				HUD_PositionObject(Players\PfeilObjekt,  -(OwnPlayer\X#-CameraX)+GraphicsWidth()/2, -(OwnPlayer\Y#-CameraY)+GraphicsHeight()/2)
				HUD_SetObjectRotation(Players\PfeilObjekt, Angle2D(-(OwnPlayer\X#-CameraX)+GraphicsWidth()/2, -(OwnPlayer\Y#-CameraY)+GraphicsHeight()/2,-(Players\X#-CameraX)+GraphicsWidth()/2, -(Players\Y#-CameraY)+GraphicsHeight()/2)-270)
			End If
		End If
	Next
	HUD_PositionObject(Sicherheits_Shape, CameraX+400, CameraY+300)
	For Schuss = Each TSchuss
		HUD_PositionObject(Schuss\SchussObjekt, -(Schuss\X#-CameraX)+GraphicsWidth()/2, -(Schuss\Y#-CameraY)+GraphicsHeight()/2)
		If MilliSecs()-Schuss\LastMove > 20 Then
			Schuss\X# = (Schuss\X#+((Schuss\Speed#)*Cos(Schuss\Rotation#)))
			Schuss\Y# = (Schuss\Y#+((Schuss\Speed#)*Sin(Schuss\Rotation#)))
			Schuss\Speed# = Schuss\Speed# - 0.1
		End If
		If HUD_ObjectHitsObject(Schuss\SchussObjekt, OwnPlayer\PlayerImage) And Schuss\SpielerIP <> 50 Then
			OwnPlayer\X# = Rand(-10, 10)
			OwnPlayer\Y# = Rand(-10, 10)
			OwnPlayer\Rotation = Rand(0,360)
		End If
		
		If Schuss\Speed# <= 5.0 Or HUD_GetDistance(Schuss\X#, Schuss\Y#, 0,0) < 250 Then
			HUD_RemoveObject Schuss\SchussObjekt
			Delete Schuss
		End If
	Next
End Function
;################################################## Naja, meine mehr oder weniger tolle Netzwerkfunktion.
Function NetWorkData()
   ;########### Empfangen
	While 1
		RemoteIP = RecvUDPMsg(OwnUDPStream)
		If RemoteIP = 0 Then
			Exit
		Else
			For Players = Each TPlayers
				If Players\IP = RemoteIP Then
					Success = 1 : Exit
				End If
			Next
			If Success = 0 Then
				Players = New TPlayers
				Players\PfeilObjekt = HUD_CreateImage(Radar_Layer, 0,0,0,0,32,32)
				Players\PlayerImage = HUD_CreateImage(Tank_Layer, 0,0,0,0,64,64) 
				HUD_SetObjectOrigin(Players\PlayerImage, 0,0)
			End If
			Success = 0
			Command = ReadInt(OwnUDPStream)
			If Command = 1 Then
				Players\X# = ReadFloat(OwnUDPStream)
				Players\Y# = ReadFloat(OwnUDPStream)
				Players\Rotation# = ReadFloat(OwnUDPStream)
				Players\IP = RemoteIP
				Players\Port = ReadInt(OwnUDPStream)
			ElseIf Command = 2 Then
				While ReadAvail(OwnUDPStream) > 0 
					NewIP = ReadInt(OwnUDPStream)
					NewPort = ReadInt(OwnUDPStream)
					Success = 0
					For Players = Each TPlayers
						If NewIP = Players\IP Then
							Success = 1 : Exit
						End If
					Next
					If Success = 0 Then
						Players = New TPlayers
						Players\PfeilObjekt = HUD_CreateImage(Radar_Layer, 0,0,0,0,32,32)
						Players\PlayerImage =  HUD_CreateImage(Tank_Layer, 0,0,0,0,64,64) 
						Players\IP = NewIP
						Players\Port = NewPort
					End If				
				Wend
			ElseIf Command = 3 Then
				For Players = Each TPlayers
					If Players\IP = RemoteIP Then
						rgb = ReadInt(OwnUDPStream)
						HUD_SetObjectColor(Players\PlayerImage, (rgb And $FF0000)/$10000 , (rgb And $FF00)/$100,rgb And $FF)
						HUD_SetObjectColor(Players\PfeilObjekt, (rgb And $FF0000)/$10000 , (rgb And $FF00)/$100,rgb And $FF)
						Exit
					End If	
				Next
			ElseIf Command = 4 Then
				Schuss = New TSchuss
				Schuss\X#  = ReadFloat(OwnUDPStream)
				Schuss\Y# = ReadFloat(OwnUDPStream)
				Schuss\Speed# = ReadFloat(OwnUDPStream)
				Schuss\Rotation = ReadInt(OwnUDPStream)
				Schuss\Farbe = ReadInt(OwnUDPStream)
				Schuss\SpielerIP = RemoteIP
				Schuss\SchussObjekt = HUD_CreateImage(Schuss_Layer, Schuss\X#, Schuss\Y#,  0,0,16,16)
				rgb = Schuss\Farbe
				bla# = HUD_GetDistance(OwnPlayer\X, OwnPlayer\Y, Schuss\X#, Schuss\Y#)
				merkvolume# = 1.0-(bla#/1000.0)
				If merkvolume# > 0.0 Then
					SoundVolume(PanzerSchussSound, merkvolume#)
					PlaySound PanzerSchussSound
					SoundVolume(PanzerSchussSound, 1.0)
				End If
				HUD_SetObjectColor(Schuss\SchussObjekt, (rgb And $FF0000)/$10000 , (rgb And $FF00)/$100,rgb And $FF)
			End If
		End If	
	Wend
	;############################################## Senden
	If MilliSecs()-LastPositionSend > 30 Then
		For Players = Each TPlayers
			If Players\IP <> 50 Then
				WriteInt OwnUDPStream, 1
				WriteFloat OwnUDPStream, OwnPlayer\X#
				WriteFloat OwnUDPStream, OwnPlayer\Y#
				WriteFloat OwnUDPStream, OwnPlayer\Rotation#
				WriteInt OwnUDPStream, UDPStreamPort(OwnUDPStream)
				SendUDPMsg OwnUDPStream, Players\IP, Players\Port
			End If
			LastPositionSend = MilliSecs()
		Next
	End If	
	;##############################################
	If MilliSecs()-LastIPListSend > 500 Then
		For SendThrough.TPlayers = Each TPlayers
			If SendThrough\IP <> 50 Then
				WriteInt OwnUDPStream, 2
				For Players = Each TPlayers
					If SendThrough\IP <> Players\IP And Players\IP <> 50 Then
						WriteInt OwnUDPStream, Players\IP
						WriteInt OwnUDPStream, Players\Port
					End If
				Next
				SendUDPMsg OwnUDPStream, SendThrough\IP, SendThrough\Port
			End If	
		Next
			LastIPListSend = MilliSecs()
	End If
	;##############################################
	If MilliSecs()-LastColorSend > 300 Then
		LastColorSend = MilliSecs()
		For Players = Each TPlayers
			If Players\IP <> 50 Then
				WriteInt OwnUDPStream, 3
				WriteInt OwnUDPStream, GUI_GetGadgetValue(GUI_FarbPicker)
				SendUDPMsg(OwnUDPStream, Players\IP, Players\Port)
			End If
		Next
	End If
	;##############################################
	If OwnPlayer\Farbe <> GUI_GetGadgetValue(GUI_FarbPicker) Then
		OwnPlayer\Farbe = GUI_GetGadgetValue(GUI_FarbPicker)
		HUD_SetObjectColor(OwnPlayer\PlayerImage, (GUI_GetGadgetValue(GUI_FarbPicker) And $FF0000)/$10000 , (GUI_GetGadgetValue(GUI_FarbPicker) And $FF00)/$100,GUI_GetGadgetValue(GUI_FarbPicker) And $FF)
		For Players = Each TPlayers
			If Players\IP <> 50 Then
				WriteInt OwnUDPStream, 3
				WriteInt OwnUDPStream, GUI_GetGadgetValue(GUI_FarbPicker)
				SendUDPMsg(OwnUDPStream, Players\IP, Players\Port)
			End If
		Next
	End If
End Function
;################################################## Aus Blitzbase geklaut, um eine String IP in eine Integer IP zu verwandeln.
Function INT_IP(IP$)
	a1=Int(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
	a2=Int(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
	a3=Int(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
	a4=Int(IP$)
	Return (a1 Shl 24) + (a2 Shl 16) + (a3 Shl 8 ) +a4
End Function
;################################################## Otherstuff umfasst die anzeige des Eigenen Ports und ein Joinmenü, wenn man leertaste drückt.
Function OtherStuff()
	
End Function
;################################################## 
Function CheckGUI()
	ButtonClicked = GUI_GetClickedGadget()
	Select ButtonClicked
		Case GUI_JoinButton
			If GUI_GetGadgetText(GUI_JoinIP) = "IP" Or GUI_GetGadgetText(GUI_JoinPort) = "PORT" Then
				Return
			End If
			Players = New TPlayers
			Players\PfeilObjekt = HUD_CreateImage(Radar_Layer, 0,0,0,0,32,32)
			Players\IP = INT_IP(GUI_GetGadgetText(GUI_JoinIP))
			Players\Port = GUI_GetGadgetText(GUI_JoinPort)
			Players\PlayerImage = HUD_CreateImage(Tank_Layer, 0,0,0,0,64,64) 
			HUD_SetObjectOrigin(Players\PlayerImage, 0,0)
			Players\X = 1
			Players\Y = 1
			Players\Rotation = 1
			GUI_SetGadgetText(GUI_JoinIP, "IP")
			GUI_SetGadgetText(GUI_JoinPort, "PORT")
	End Select
End Function
;################################################## Die Hauptschleife
Global FPSTimer = CreateTimer(30)
;##################################################
Repeat
	WaitTimer FPSTimer
	TileBlock Backgroundimage, CameraX, CameraY
   ;##############################################
	OtherStuff()
	NetWorkData()
	CheckGUI()
	PlayerControls()
	DrawPlayers()
	HUD_Update() : GUI_Update() : RenderWorld()
	Flip 1
	Cls()
Until KeyHit(1)