January 26, 2021, 05:42:33 AM

Author Topic: [bb] Record, Write, Read, Play, the position and rotation of an entity by RemiD [ 1+ years ago ]  (Read 1408 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Record, Write, Read, Play, the position and rotation of an entity
Author : RemiD
Posted : 1+ years ago

Description : An example on how to have a replay in a video game

Code :
Code: BlitzBasic
  1. ;Record, Write, Read, Play, the position and rotation of an entity
  2. ;F1 to switch from Idle mode to Record mode and from Record mode to Idle mode
  3. ;F2 to switch from Idle mode to Play mode and from Play mode to Idle mode
  4. ;The characters move with their AI when in Idle Mode or in Record Mode, and they move with the Replay data when in Play mode.
  5.  
  6. Graphics3D(800,600,32,2)
  7.  
  8. HidePointer()
  9.  
  10. SeedRnd(MilliSecs())
  11.  
  12. Camera = CreateCamera()
  13. CameraRange(Camera,0.1,1000)
  14. PositionEntity(Camera,0,0,0)
  15.  
  16. GroundMesh = CreateCube()
  17. ScaleEntity(GroundMesh,64.0/2,0.1/2,64.0/2)
  18. PositionEntity(GroundMesh,64.0/2,-0.1/2,64.0/2)
  19. EntityColor(GroundMesh,025,075,025)
  20.  
  21. CharacterXMesh = CreateCube()
  22. ScaleMesh(CharacterXMesh,0.5/2,1.8/2,0.25/2)
  23. PositionMesh(CharacterXMesh,0,1.8/2,0)
  24. HideEntity(CharacterXMesh)
  25.  
  26. Dim CharacterMesh(5)
  27. Global CharactersCount% = 0
  28.  
  29. For CId% = 1 To 5
  30.  CharactersCount% = CharactersCount% + 1
  31.  CId% = CharactersCount%
  32.  CharacterMesh(CId%) = CopyEntity(CharacterXMesh)
  33.  PositionEntity(CharacterMesh(CId%),Rnd(0,64),0,Rnd(0,64))
  34.  RotateEntity(CharacterMesh(CId%),0,Rnd(-180,180),0)
  35.  EntityColor(CharacterMesh(CId%),Rand(000,255),Rand(000,255),Rand(000,255))
  36. Next
  37.  
  38. Const Idle% = 0
  39. Const Record% = 1
  40. Const Play% = 2
  41.  
  42. Global TrackingMode% = Idle%
  43. Global File
  44. Global FileName$ = "Tracking.dat"
  45.  
  46. PositionEntity(Camera,32,32,-16)
  47. RotateEntity(Camera,45,0,0)
  48.  
  49. SunLight = CreateLight(1)
  50. LightColor(SunLight,255,255,255)
  51. PositionEntity(SunLight,32,1024,-1024)
  52. RotateEntity(SunLight,45,0,0)
  53. AmbientLight(125,125,125)
  54.  
  55. Repeat
  56.  
  57.  Cls()
  58.  
  59.  CharactersUpdate()
  60.  
  61.  TrackingModeUpdate()
  62.  
  63.  SetBuffer(BackBuffer())
  64.  RenderWorld()
  65.  
  66.  Text(0,0,"Triangles : "+TrisRendered())
  67.  Text(0,20,"LoopTime% : "+LoopTime%)
  68.  Text(0,40,"TrackingMode% : "+TrackingMode%)
  69.  
  70.  Flip(1)
  71.  
  72.  ;Calculates the time of a loop  
  73.  EndTime% = MilliSecs()    
  74.  LoopTime% = EndTime% - StartTime%  
  75.  StartTime% = MilliSecs()
  76.  
  77. Until(KeyDown(1)=True)
  78.  
  79. Function CharactersUpdate()
  80.  If( TrackingMode% = Idle% Or TrackingMode% = Record%)
  81.   For CId% = 1 To CharactersCount%
  82.    If( EntityX(CharacterMesh(CId%)) > 0 And EntityX(CharacterMesh(CId%)) < 64 And EntityZ(CharacterMesh(CId%)) > 0 And EntityZ(CharacterMesh(CId%)) < 64 )
  83.     MoveEntity(CharacterMesh(CId%),0,0,0.1)
  84.    Else
  85.     RotateEntity(CharacterMesh(CId%),0,Rnd(-180,180),0)
  86.     MoveEntity(CharacterMesh(CId%),0,0,0.1)
  87.    EndIf
  88.   Next
  89.  EndIf
  90. End Function
  91.  
  92. Function TrackingModeUpdate()
  93.  If(KeyHit(59)=True)
  94.   If(TrackingMode% = Idle%)
  95.    TrackingMode% = Record%
  96.    File = WriteFile(FileName$)
  97.   ElseIf(TrackingMode% = Record%)
  98.    TrackingMode% = Idle%
  99.    CloseFile(File)
  100.   EndIf
  101.  EndIf
  102.  If(KeyHit(60)=True)
  103.   If(TrackingMode% = Idle%)
  104.    TrackingMode% = Play%
  105.    File = ReadFile(FileName$)
  106.   ElseIf(TrackingMode% = Play%)
  107.    TrackingMode% = Idle%
  108.    CloseFile(File)
  109.   EndIf
  110.  EndIf
  111.  If(TrackingMode% = Record%)
  112.   TrackingRecordWrite()
  113.  ElseIf(TrackingMode% = Play%)
  114.   TrackingReadPlay()
  115.  EndIf
  116. End Function
  117.  
  118. Function TrackingRecordWrite()
  119.  For CId% = 1 To CharactersCount%
  120.   WriteFloat(File,EntityX(CharacterMesh(CId%)))
  121.   WriteFloat(File,EntityY(CharacterMesh(CId%)))
  122.   WriteFloat(File,EntityZ(CharacterMesh(CId%)))
  123.   WriteFloat(File,EntityPitch(CharacterMesh(CId%)))
  124.   WriteFloat(File,EntityYaw(CharacterMesh(CId%)))
  125.   WriteFloat(File,EntityRoll(CharacterMesh(CId%)))
  126.  Next
  127. End Function
  128.  
  129. Function TrackingReadPlay()
  130.  If(Not Eof(File))
  131.   For CId% = 1 To CharactersCount%
  132.    X# = ReadFloat(File)
  133.    Y# = ReadFloat(File)
  134.    Z# = ReadFloat(File)        
  135.    Pitch# = ReadFloat(File)
  136.    Yaw# = ReadFloat(File)
  137.    Roll# = ReadFloat(File)
  138.    PositionEntity(CharacterMesh(CId%),X#,Y#,Z#)
  139.    RotateEntity(CharacterMesh(CId%),Pitch#,Yaw#,Roll#)
  140.   Next
  141.  Else
  142.   CloseFile(File)
  143.   TrackingMode% = Idle%
  144.  EndIf         
  145. End Function
  146.  
  147. End


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal