B3D Shooter

Miscellaneous Forums/Blitz Showcase/B3D Shooter

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

AppTitle "3d Shooter"
Graphics3D 640,480,0,1
SeedRnd MilliSecs()
Const Number_of_enemies = 10
Global CenterX = GraphicsWidth()/2
Global CenterY = GraphicsHeight()/2
Global Abort = 0
Global FrameCount = MilliSecs()
Global Camera = CreateCamera()
CameraRange Camera,1,10000
Global Truce = 500 * Number_of_enemies
Global DoFPS
Global Writing$ = "Press Enter to view FPS"
;assemble the player
Global CloneMe = CreatePivot()
Global Player = CreateCylinder()
EntityParent Player,CloneMe
EntityColor Player,0,0,255
Global XLook
Eyes = CreateImage(16,16)
SetBuffer ImageBuffer(Eyes)
Color 200,155,45
Rect 0,0,16,16
Color 64,32,0
Plot 4,8
Plot 12,8
Global FPSCalc = 0
TranslateEntity CloneMe,0,-7,1
TranslateEntity Camera,0,-3,1
ScaleMesh Player,1,3,1
Gun = CreateCylinder()
EntityParent Gun,CloneMe
PositionEntity Gun,0,2,2
ScaleMesh Gun,0.25,2,0.25
RotateEntity Gun,90,0,0
EntityColor gun,64,32,0
Sight = CreateCube()
ScaleMesh Sight,0.0125,0.1,0.1
EntityColor Sight,0,0,0
HeadTexture = CreateTexture(64,64)
SetBuffer TextureBuffer(HeadTexture)
Color 200,155,45
Rect 0,0,64,64
DrawImage Eyes,8,16
Global Grass = CreateTexture(32,32)
Global Light = CreateLight(2)
LightColor Light,10,10,10
EntityParent Light,Camera
Global Head = CreateSphere()
Global Turban = CreateSphere()
RotateEntity Head,0,-90,0
EntityParent Head,CloneMe
EntityParent Turban,CloneMe
EntityColor Turban,230,230,230
ScaleMesh Turban,1.25,0.75,1.25
EntityParent Sight,CloneMe
PositionEntity Sight,0,2.25,3.75
EntityTexture Head,HeadTexture
PositionEntity Head,0,4,0
PositionEntity Turban,0,5,0
SetBuffer TextureBuffer(Grass);Make grass texture

Color 0,255,0
Rect 0,0,16,16
Rect 16,16,16,16
Color 0,250,0
Rect 16,0,16,16
Rect 0,16,16,16
SetBuffer BackBuffer()
EntityType CloneMe,17
EntityRadius CloneMe,3
EntityPickMode CloneMe,1
;done...
;make bullet
Bullet = CreateCylinder()
RotateEntity Bullet,90,0,0
ScaleMesh Bullet,0.125,150,0.125
EntityColor Bullet,255,255,16
Target = CreatePivot()
;RotateEntity Target,90,0,0
Blood = CreateCylinder()
ScaleMesh Blood,5,.001,5
EntityColor Blood,160,0,0
Color 255,128,0
;done...
;Build Enemies
Dim NPC(Number_of_enemies + 1)
Dim NPCBlood(Number_of_enemies)
Dim NPCName$(Number_of_enemies)
Dim NPCPrey(Number_of_enemies + 1)
Dim NPCDead(Number_of_enemies + 1)
Dim NPCInjurePoint(Number_of_enemies + 1)
Dim NPCFiredShot(Number_of_enemies + 1)
Dim NPCDirection(Number_of_enemies)
Dim NPCTime(Number_of_enemies)
Dim NPCGunshot(Number_of_enemies + 1)
Dim NPCInaccuracy#(Number_of_enemies + 1)
Dim NPCFiringDelay(Number_of_enemies + 1)
For loop = 1 To Number_of_enemies
NPCName(Loop) = Rename$()
NPC(Loop) = CopyEntity(CloneMe)
NPCGunshot(Loop) = CopyEntity(Bullet)
NPCInjurePoint(Loop) = CopyEntity(Target)
NPCInaccuracy(Loop) = Rnd(5,15)
NPCBlood(Loop) = CopyEntity(Blood)
HideEntity NPCBlood(Loop)
EntityParent NPCInjurePoint(Loop),NPC(Loop)
PositionEntity NPCInjurePoint(Loop),0,2,1
HideEntity NPCGunshot(Loop)
EntityParent NPCGunshot(Loop),NPC(Loop)
PositionEntity NPCGunshot(Loop),0,2,150
NPCDirection(Loop) = Rnd(-1,1)
NPCTime(Loop) = Rand(1,100)
PositionEntity NPC(Loop),Rand(-100,100),-7,Rand(-100,100)
Next


NPC(Number_of_enemies + 1) = CopyEntity(CloneMe)
NPCGunshot(Loop) = CopyEntity(Bullet)
EntityParent NPCGunshot(Number_of_enemies + 1),NPC(Number_of_enemies + 1)
PositionEntity NPCGunshot(Number_of_enemies + 1),0,2,150
NPCInaccuracy(Number_of_enemies + 1) = Rnd(5,15)
HideEntity NPCGunshot(Number_of_enemies + 1)
NPCInjurePoint(Number_of_enemies + 1) = CopyEntity(Target)
EntityParent NPCInjurePoint(Number_of_enemies + 1),NPC(Number_of_enemies + 1)
PositionEntity NPCInjurePoint(Number_of_enemies + 1),0,2,1




HideEntity Blood
HideEntity CloneMe
HideEntity Bullet
HideEntity Target
HideEntity Turban

EntityParent Camera,NPC(Number_of_enemies + 1)
PositionEntity NPC(Number_of_enemies + 1),Rand(-100,100),-7,Rand(-100,100)


Collisions 17,17,1,2







Global Ground = CreatePlane()
EntityTexture Ground,Grass
TranslateEntity Ground,0,-10,0


Runtime()
End




Function Runtime();Runtime function

Repeat
Timer = MilliSecs()


Bounderies()
UpdateNPCs()
MouseLook()
CheckDead()
If Truce = 0 GunShot()
TurnEntity NPC(Number_of_enemies + 1),0,-Xlook/5,0
MoveEntity NPC(Number_of_enemies + 1),KeyDown(205) - KeyDown(203),0,KeyDown(200) * 2 - KeyDown(208)
UpdateWorld()
RenderWorld()
If KeyDown(28) Text CenterX,10,"FPS:",1,0
If KeyDown(28) Text CenterX,20,FPS(),1,0
Text CenterX,0,Writing,1,0
Flip


If KeyHit(1) Abort = 1


While Not MilliSecs() - Timer > 25
Wend
Until Abort = 1

End Function

Function MouseLook()

Xlook = MouseX() - CenterX
MoveMouse CenterX,CenterY
End Function



Function UpdateNPCs()
For Loop = 1 To Number_of_enemies
If NPCDead(Loop) = 0
NPCTime(Loop) = NPCTime(Loop) - 1
If NPCTime(Loop) < 1
NPCTime(Loop) = Rand(1,100)
NPCDirection(Loop) = Rnd(-1,1)
EndIf
If NPCPrey(Loop) = 0 TurnEntity NPC(Loop),0,NPCDirection(Loop),0

If Truce > 0
Truce = Truce - 1
If Truce = 0 Writing = "Let the killing begin!"
FlushMouse
Else
ChooseTarget()
Attack()
DeadTarget()
EndIf
If NPCPrey(Loop) = 0
MoveEntity NPC(Loop),0,0,1
ElseIf NPCDead(NPCPrey(Loop)) = 0
If EntityDistance(NPC(Loop),NPC(NPCPrey(Loop))) > 250 MoveEntity NPC(Loop),0,0,1
EndIf
EndIf
Next
End Function





;Function









Function Gunshot()


If MouseHit(1) NPCFiredShot(Number_of_enemies + 1) = 1








For Loop = 1 To Number_of_enemies
If NPCDead(Loop) = 0
If NPCPrey(Loop) > 0
If NPCDead(NPCPrey(Loop)) = 0
NPCFiringDelay(Loop) = NPCFiringDelay(Loop) - 1
If NPCFiringDelay(Loop) <= 0
If EntityDistance(NPC(Loop),NPC(NPCPrey(Loop))) < 285
NPCFiredShot(Loop) = 1
NPCFiringDelay(Loop) = Rnd(30,40)
EndIf
EndIf
EndIf
EndIf
EndIf
Next





For Loop = 1 To Number_of_enemies + 1
If NPCDead(Loop) = 0
If NPCFiredShot(Loop) = 1
TurnEntity NPC(Loop),0,Rnd(-NPCInaccuracy(Loop),NPCInaccuracy(Loop)),0
ShowEntity NPCGunshot(Loop)

DeleteEntity% = EntityPick(NPCInjurePoint(Loop),300)
NPCFiredShot(Loop) = 2
ElseIf NPCFiredShot(Loop) = 2
NPCFiredShot(Loop) = 0
HideEntity NPCGunshot(Loop)
EndIf
EndIf

For Loop2 = 1 To Number_of_Enemies + 1

If NPC(Loop2) = DeleteEntity%
If NPCDead(Loop2) = 0
If Loop2 = Number_of_Enemies + 1 GameOver(NPCName(Loop))
PositionEntity NPCBlood(Loop2),EntityX(NPC(Loop2)),-10,EntityZ(NPC(Loop2))
FreeEntity NPC(Loop2)
;NPC(Loop2) = 0
NPCDead(Loop2) = 1
ShowEntity NPCBlood(Loop2)
If Loop = Number_of_Enemies + 1 Kill(NPCName(Loop2))
EndIf
EndIf
Next
Next


;Is An NPC Dead

End Function













Function Rename$()

Return Namegen$() + " " + Namegen$()

End Function
Function Namegen$()
NameLength = Rand(4,8)
Local Letter$[12]
For loop = 1 To namelength
char = Rand(1,26)

Select char
Case 1
Letter[Loop] = "a"
Vowel = Vowel + 1
Case 2
Letter[Loop] = "b"
Case 3
Letter[Loop] = "c"
Case 4
Letter[Loop] = "d"
Case 5
Letter[Loop] = "e"
Vowel = Vowel + 1
Case 6
Letter[Loop] = "f"
Case 7
Letter[Loop] = "g"
Case 8
Letter[Loop] = "h"
Case 9
Letter[Loop] = "i"
Vowel = Vowel + 1
Case 10
Letter[Loop] = "j"
Case 11
Letter[Loop] = "k"
Case 12
Letter[Loop] = "l"
Case 13
Letter[Loop] = "m"
Case 14
Letter[Loop] = "n"
Case 15
Letter[Loop] = "o"
Vowel = Vowel + 1
Case 16
Letter[Loop] = "p"
Case 17
Letter[Loop] = "q"
Case 18
Letter[Loop] = "r"
Case 19
Letter[Loop] = "s"
Case 20
Letter[Loop] = "t"
Case 21
Letter[Loop] = "u"
Vowel = Vowel + 1
Case 22
Letter[Loop] = "v"
Case 23
Letter[Loop] = "w"
Case 24
Letter[Loop] = "x"
Case 25
Letter[Loop] = "y"
Vowel = Vowel + 1
Case 26
Letter[Loop] = "z"
End Select



If Vowel < Ceil(loop/2) Letter[Loop] = Re_vowel$() Vowel = Vowel + 1
If Loop = 1 Letter[Loop] = Upper$(Letter[Loop])
Name$ = name + Letter[Loop]
Next

Return Name
End Function


Function Re_vowel$()
Numb = Rand(1,64)
If Numb > 0 And Numb <= 16 Return "a"
If Numb > 16 And Numb <= 34 Return "e"
If Numb > 34 And Numb <= 46 Return "i"
If Numb > 46 And Numb <= 56 Return "o"
If Numb > 56 And Numb <= 62 Return "u"
If Numb > 62 Return "y"
End Function


Function FPS()

FramesPerSecond# = MilliSecs() - FrameCount
FrameCount = MilliSecs()

Return 1000 / FramesPerSecond
End Function




Function DeadTarget()
For Loop = 1 To Number_of_Enemies
If NPCDead(Loop) = 0
If NPCDead(NPCPrey(Loop)) = 1 NPCPrey(Loop) = 0 
EndIf
Next
End Function



Function ChooseTarget()

For Loop = 1 To Number_Of_Enemies
If NPCDead(Loop) = 0
If NPCPrey(Loop) = 0
For Loop2 = 1 To Number_Of_Enemies + 1
If NPCDead(Loop2) = 0
If Not Loop = Loop2
If JRAILib_Engage(NPC(Loop),80,NPC(Loop2),500) NPCPrey(Loop) = Loop2
EndIf
EndIf
Next
EndIf
EndIf
Next

End Function


Function Attack()
For Loop = 1 To Number_Of_Enemies
If NPCDead(Loop) = 0
If NPCPrey(Loop)> 0 TurnEntity NPC(Loop),0,JRAILib_Orient#(NPC(Loop),NPC(NPCPrey(Loop)),5,1,9),0
EndIf
Next
End Function 



Function JRAILib_Orient#(JRAILib_Source,JRAILib_Recipient,JRAILib_Speed#,JRAILib_ReducedSpeed#,JRAILib_ReducedDistance#)
JRAILib_DeltaPitch# = DeltaYaw#(JRAILib_Source,JRAILib_Recipient)


If Abs(JRAILib_DeltaPitch) <= JRAILib_ReducedSpeed Return JRAILib_Pitch# + JRAILib_DeltaPitch

If Abs(JRAILib_DeltaPitch) > JRAILib_ReducedDistance
JRAILib_PitchChangeAmount# = JRAILib_Speed
Else
JRAILib_PitchChangeAmount# = JRAILib_ReducedSpeed
EndIf
If JRAILib_DeltaPitch# < 0 JRAILib_PitchChangeAmount# = JRAILib_PitchChangeAmount# * -1



Return JRAILib_PitchChangeAmount#
 
End Function
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Function JRAILib_Engage(JRAILib_Hunter,JRAILib_BowlOfVision,JRAILib_Prey,JRAILib_SightRange)
JRAILib_DeltaPitch = DeltaYaw#(JRAILib_Hunter,JRAILib_Prey)
If Abs(JRAILib_DeltaPitch) <= JRAILib_BowlOfVision And EntityDistance(JRAILib_Hunter,JRAILib_Prey) <= JRAILib_SightRange And EntityVisible(JRAILib_Hunter,JRAILib_Prey)
Return 1
Else
Return 0
EndIf
End Function

Function GameOver(Murderer$)
ClsColor 128,0,0
Cls
Text CenterX,0,"You were gunned down by " + Murderer,1,0
Flip
Repeat
Until KeyHit(1)
End
End Function
Function Kill(Victim$)
Writing = "You Killed " + Victim
End Function
Function Bounderies()
For Loop = 1 To Number_of_enemies + 1
If NPCDead(Loop) = 0
If EntityX(NPC(Loop)) > 1000 Or EntityX(NPC(Loop)) < -1000 Or EntityZ(NPC(Loop)) > 1000 Or EntityZ(NPC(Loop)) < -1000
PositionEntity NPC(Loop),Rand(-1000,1000),-7,Rand(-1000,1000)
EndIf
EndIf
Next
End Function
Function Victory()
Color 192,192,255
ClsColor 0,0,255
Cls
Text CenterX,0,"You are victorius",1,0
Flip
Repeat
Until JoyHit(8) Or KeyHit(1)
End
End Function
Function CheckDead()
For Loop = 1 To Number_of_enemies 
If NPCDead(Loop) = 0 continuegame = 1
Next
If continuegame <> 1 Victory()
End Function

Set Bounderies