James Bond the Dinosaur

Blitz3D Forums/Blitz3D Beginners Area/James Bond the Dinosaur

Really basic example:

Not finished...

Start with setting up 3D graphics
Graphics3D 800, 600
SetBuffer BackBuffer()

Set up your types
Type bullettype
  Field entityhandle
End Type
Type badguytype
  Field entityhandle
  Field state
End Type

Load in your 3d objects
light = CreateLight()
player = CreatePivot()
camera = CreateCamera(player)
level = LoadMesh("level.b3d")
weapon1 = LoadMesh("pistol.b3d", camera)
bulletmesh = LoadMesh("bullet.b3d")
badguymesh = LoadMesh("badguy.b3d")
HideEntity bulletmesh
HideEntity badguymesh
etc...

Create your beginning objects
For iter = 1 to 10
  badguy.badguytype = New badguytype
  badguy\entityhandle = CopyEntity(badguymesh)
  badguy\state = Rnd(1, 2) ;1=Guard 2=Search 3=Evade 4=Fire 5=Dead
  EntityType badguy\entityhandle, 3
  EntityRadius badguy\entityhandle, .7, 2
Next

Arrange your objects
MoveEntity camera, 0, .8, 0
MoveEntity weapon1, .1, -.15, .1
MoveEntity player, 20, 1, -20
For bgiter.badguytype = Each badguytype
  PositionEntity iter\entityhandle, Rnd(100)-50, 1, Rnd(100)-50
Next
etc...

Set up collisions between all of the objects. EntityTypes and radiuses for badguys and bullets are set upon their creation.
EntityType player, 1
EntityRadius player, .7, 2
EntityType level, 2
Collisions 1, 2, 2, 2 ;player to level
Collisions 3, 2, 2, 2 ;badguy to level
Collisions 4, 2, 2, 1 ;bullet to level
Collisions 4, 3, 2, 1 ;bullet to badguy

Then add a loop that updates the screen every frame.
While Not Keyhit(1)
  
  ;insert additional code here

  UpdateWorld

  RenderWorld
  Flip
Wend

Inside of your loop (before UpdateWorld) add lines that handle how to move the player (camera)
If Keydown(17) then MoveEntity player, 0, 0, .1
If Keydown(31) then MoveEntity player, 0, 0, -.1
If Keydown(30) then MoveEntity player, -.1, 0, 0
If Keydown(32) then MoveEntity player, .1, 0, 0

Next add lines that handle how to allow the player (camera) to look around
TurnEntity player, 0, -MouseXSpeed()/5.0, 0
TurnEntity camera, MouseYSpeed()/5.0, 0, 0
If EntityPitch(camera) < -45
  RotateEntity camera, -45, EntityYaw(camera), EntityRoll(camera)
EndIf
If EntityPitch(camera) > 45
  RotateEntity camera, 45, EntityYaw(camera), EntityRoll(camera)
EndIf
MoveMouse GraphicsWidth()/2, GraphicsHeight()/2

Add code to allow the player to shoot
If MouseHit(1)
  bullet.bullettype = New bullettype
  bullet\entityhandle = CopyEntity(bulletmesh)
  PositionEntity bullet\entityhandle, EntityX(weapon1, 1), EntityY(weapon1, 1), EntityZ(weapon1, 1)
  RotateEntity bullet\entityhandle, EntityPitch(weapon1, 1), EntityYaw(weapon1, 1), EntityRoll(weapon1, 1)
  EntityType bullet\entityhandle, 4
  EntityRadius bullet\entityhandle, .01
EndIf

Add code to move bullets every frame
For biter.bullettype = Each bullettype
  MoveEntity biter\entityhandle, 0, 0, 2
  If Abs(EntityX(biter\entityhandle, 1)) > 10000
    FreeEntity biter\entityhandle
    Delete biter
  ElseIf Abs(EntityY(biter\entityhandle, 1)) > 10000
    FreeEntity biter\entityhandle
    Delete biter
  ElseIf Abs(EntityZ(biter\entityhandle, 1)) > 10000
    FreeEntity biter\entityhandle
    Delete biter
  EndIf
Next

Add code between UpdateWorld and RenderWorld to check if any collisions occured between the bullets and anything else. Kill a badguy if the bullet hit one.
For biter.bullettype = Each bullettype
  If CountCollisions(biter\entityhandle) > 0
    badguyhit = EntityCollided(biter\entityhandle, 3)
    For badguysearch.badguytype = Each badguytype
      If badguyhit = badguysearch\entityhandle
        If badguysearch\state <> 5
          badguysearch\state = 5 ;dead
          RotateEntity badguysearch\entityhandle, 0, 0, 90
          TranslateEntity badguysearch\entityhandle, 0, -1, 0
          Exit
        EndIf
      EndIf
    Next    
    FreeEntity biter\entityhandle
    Delete biter
  EndIf
Next