fps.bb 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. ; * FPS sample.
  2. ; * Program code by Simon Harrison ([email protected]).
  3. ; * Gargoyle model by Adam Gore.
  4. ; Graphics values
  5. Const width=640,height=480,depth=16
  6. ; Collision type values
  7. Const type_camera=1,type_blood=2 ; Source
  8. Const type_scenery=3,type_ladders=4 ; Destination
  9. ; Camera position, angle values
  10. Global cam_x#,cam_z#,cam_pitch#,cam_yaw# ; Current
  11. Global dest_cam_x#,dest_cam_z#,dest_cam_pitch#,dest_cam_yaw# ; Destination
  12. ; CameraPick value
  13. Global picked
  14. ; Blood intensity, mouse shake values
  15. Global blood_intensity=10,mouse_shake#
  16. ; Gargoyle speed, yaw values
  17. Global garg_speed#=0.2,garg_yaw#
  18. Global node=2 ; Node gargoyle aims for
  19. Dim node_x(10)
  20. Dim node_y(10)
  21. Dim node_z(10)
  22. Dim node_n(10,3)
  23. Dim node_v(10)
  24. node_x(1)=390 ; x position of first node
  25. node_y(1)=160 ; y position of first node
  26. node_z(1)=50 ; z position of first node
  27. node_n(1,0)=1 ; no of connected nodes
  28. node_n(1,1)=2 ; no of the first connected node
  29. node_v(1)=1 ; visits to first node by gargoyle
  30. node_x(2)=390 ; x position of second node
  31. node_y(2)=160 ; y position of second node
  32. node_z(2)=190 ; z position of second node
  33. node_n(2,0)=2 ; no of connected nodes
  34. node_n(2,1)=3 ; no of the first connected node
  35. node_n(2,2)=1 ; no of the second connected node
  36. node_x(3)=210 ; etc
  37. node_y(3)=120 ; etc
  38. node_z(3)=190 ; etc
  39. node_n(3,0)=2 ; etc
  40. node_n(3,1)=4 ; etc
  41. node_n(3,2)=2 ; etc
  42. node_x(4)=210
  43. node_y(4)=120
  44. node_z(4)=10
  45. node_n(4,0)=2
  46. node_n(4,1)=5
  47. node_n(4,2)=3
  48. node_x(5)=390
  49. node_y(5)=80
  50. node_z(5)=10
  51. node_n(5,0)=2
  52. node_n(5,1)=6
  53. node_n(5,2)=4
  54. node_x(6)=390
  55. node_y(6)=80
  56. node_z(6)=190
  57. node_n(6,0)=2
  58. node_n(6,1)=7
  59. node_n(6,2)=5
  60. node_x(7)=210
  61. node_y(7)=40
  62. node_z(7)=190
  63. node_n(7,0)=3
  64. node_n(7,1)=8
  65. node_n(7,2)=10
  66. node_n(7,3)=6
  67. node_x(8)=50
  68. node_y(8)=40
  69. node_z(8)=190
  70. node_n(8,0)=2
  71. node_n(8,1)=9
  72. node_n(8,2)=7
  73. node_x(9)=50
  74. node_y(9)=40
  75. node_z(9)=110
  76. node_n(9,0)=1
  77. node_n(9,1)=8
  78. node_x(10)=210
  79. node_y(10)=40
  80. node_z(10)=10
  81. node_n(10,0)=1
  82. node_n(10,1)=7
  83. ; Bullet spark type
  84. Type spark
  85. Field entity,alpha#
  86. End Type
  87. ; Blood type (hehe)
  88. Type blood
  89. Field entity,alpha#,y_speed#,stuck
  90. End Type
  91. ; Set display mode
  92. Graphics3D width,height,depth
  93. SetBuffer BackBuffer()
  94. ; Set up camera
  95. Global camera=CreateCamera()
  96. CameraRange camera,1,600
  97. EntityRadius camera,7.5
  98. EntityType camera,type_camera
  99. PositionEntity camera,10,10,10
  100. ; Set up lights
  101. AmbientLight 191,191,191
  102. Global light=CreateLight()
  103. LightColor light,31,31,31
  104. RotateEntity light,45,0,0
  105. ; Load level
  106. Global level=LoadMesh("interior.x")
  107. ScaleEntity level,100,100,100
  108. EntityType level,type_scenery
  109. EntityPickMode level,2
  110. ; Load ladders collision mesh
  111. Global ladders=LoadMesh("ladders.x")
  112. ScaleEntity ladders,100,100,100
  113. EntityType ladders,type_ladders
  114. EntityAlpha ladders,0
  115. ; Load gun target
  116. Global target=LoadSprite("target.bmp",1,camera)
  117. MoveEntity target,0,0,75
  118. EntityOrder target,-2
  119. ; Load bullet spark
  120. Global spark=LoadSprite("bigspark.bmp")
  121. ScaleSprite spark,2,2
  122. EntityOrder spark,-1
  123. HideEntity spark
  124. ; Load gargoyle
  125. Global gargoyle=LoadMD2("gargoyle/gargoyle.md2")
  126. garg_tex=LoadTexture("gargoyle/gargoyle.bmp")
  127. EntityTexture gargoyle,garg_tex
  128. AnimateMD2 gargoyle,1,0.1,32,46
  129. ScaleEntity gargoyle,0.2,0.2,0.2
  130. TurnEntity gargoyle,0,180,0
  131. PositionEntity gargoyle,390,160,50
  132. EntityBox gargoyle,-5,0,-5,10,20,10
  133. EntityPickMode gargoyle,3
  134. ; Create gargoyle-walk pivot
  135. Global target_piv=CreatePivot()
  136. PositionEntity target_piv,node_x(2),node_y(2),node_z(2)
  137. ; Load blood sprite
  138. Global blood=LoadSprite("blood.bmp",3)
  139. EntityRadius blood,1
  140. EntityType blood,type_blood
  141. HideEntity blood
  142. ; Load sounds
  143. Global gunshot=LoadSound("gunshot.wav")
  144. Global squish=LoadSound("squish.wav")
  145. ; Initialise collisions
  146. Collisions type_camera,type_scenery,2,3
  147. Collisions type_blood,type_scenery,2,1
  148. Collisions type_blood,type_camera,1,1
  149. Collisions type_camera,type_ladders,2,2
  150. ; Main loop
  151. ; ---------
  152. Global old_time=MilliSecs()
  153. While Not KeyHit(1)
  154. UpdateGame()
  155. UpdateWorld
  156. RenderWorld
  157. ; FPS
  158. frames=frames+1
  159. If MilliSecs()-render_time=>1000 Then fps=frames : frames=0 : render_time=MilliSecs()
  160. Text 0,0,fps
  161. Flip
  162. Wend
  163. End
  164. ; --------------------
  165. ; Update game function
  166. ; --------------------
  167. Function UpdateGame()
  168. GameInput()
  169. UpdateGun()
  170. GargPath()
  171. SpurtBlood()
  172. End Function
  173. ; -------------------
  174. ; Game input function
  175. ; -------------------
  176. Function GameInput()
  177. ; Shoot
  178. ; -----
  179. If MouseHit(1)=True
  180. PlaySound gunshot
  181. picked=CameraPick(camera,width/2,height/2)
  182. s.spark=New spark
  183. s\entity=CopyEntity(spark)
  184. s\alpha=1
  185. PositionEntity s\entity,PickedX(),PickedY(),PickedZ()
  186. EndIf
  187. ; Mouse look
  188. ; ----------
  189. ; Mouse x and y speed
  190. mxs=MouseXSpeed()
  191. mys=MouseYSpeed()
  192. ; Mouse shake (total mouse movement)
  193. mouse_shake=Abs(((mxs+mys)/2)/1000.0)
  194. ; Destination camera angle x and y values
  195. dest_cam_yaw#=dest_cam_yaw#-mxs
  196. dest_cam_pitch#=dest_cam_pitch#+mys
  197. ; Current camera angle x and y values
  198. cam_yaw=cam_yaw+((dest_cam_yaw-cam_yaw)/5)
  199. cam_pitch=cam_pitch+((dest_cam_pitch-cam_pitch)/5)
  200. RotateEntity camera,cam_pitch#,cam_yaw#,0
  201. ; Rest mouse position to centre of screen
  202. MoveMouse width/2,height/2
  203. ; Camera move
  204. ; -----------
  205. ; Forward/backwards - destination camera move z values
  206. If KeyDown(200)=True Or MouseDown(2)=True Then dest_cam_z=1
  207. If KeyDown(208)=True Then dest_cam_z#=-1
  208. ; Strafe - destination camera move x values
  209. If KeyDown(205)=True Then dest_cam_x=1
  210. If KeyDown(203)=True Then dest_cam_x=-1
  211. ; Current camera move x and z values
  212. cam_z=cam_z+((dest_cam_z-cam_z)/5)
  213. cam_x=cam_x+((dest_cam_x-cam_x)/5)
  214. ; Move camera
  215. MoveEntity camera,cam_x,0,cam_z
  216. dest_cam_x=0 : dest_cam_z=0
  217. ; Climb ladders
  218. If EntityCollided(camera,type_ladders)<>0 Then TranslateEntity camera,0,1,0
  219. ; Gravity
  220. TranslateEntity camera,0,-1,0
  221. End Function
  222. ; ----------
  223. ; Update gun
  224. ; ----------
  225. Function UpdateGun()
  226. ; Update each bullet spark
  227. For s.spark=Each spark
  228. EntityAlpha s\entity,s\alpha
  229. s\alpha=s\alpha-0.02
  230. If s\alpha<=0 Then FreeEntity s\entity : Delete s
  231. Next
  232. ; If gargoyle is picked (shot) then play squish sound and call FreshBlood function
  233. If picked=gargoyle
  234. PlaySound squish
  235. FreshBlood()
  236. picked=0
  237. EndIf
  238. End Function
  239. ; --------------------
  240. ; Fresh blood function
  241. ; --------------------
  242. Function FreshBlood()
  243. ; New blood drops
  244. For i=1 To blood_intensity
  245. b.blood=New blood
  246. b\y_speed=0
  247. b\alpha=2
  248. b\entity=CopyEntity(blood)
  249. PositionEntity b\entity,PickedX(),PickedY(),PickedZ()
  250. ResetEntity b\entity
  251. scale_randx#=Rnd(0.2,2.9)
  252. scale_randy#=scale_randx#+Rnd(-0.1,0.1)
  253. ScaleSprite b\entity,scale_randx#,scale_randy#
  254. RotateEntity b\entity,Rnd(360),Rnd(360),Rnd(360)
  255. Next
  256. End Function
  257. ; --------------------
  258. ; Spurt blood function
  259. ; --------------------
  260. Function SpurtBlood()
  261. ; For each drop of blood...
  262. For b.blood=Each blood
  263. ; If blood drop collides with camera then stick blood drop on camera
  264. If EntityCollided(b\entity,type_camera)<>0 Then EntityParent b\entity,camera : EntityType b\entity,0 : b\alpha=1 : b\stuck=True
  265. ; Update blood drops
  266. If b\alpha>0
  267. EntityAlpha b\entity,b\alpha
  268. If b\stuck<>True Then MoveEntity b\entity,0,0,1
  269. If b\stuck<>True Then TranslateEntity b\entity,0,b\y_speed,0
  270. If b\stuck<>True Then b\alpha=b\alpha-0.01
  271. If b\stuck=True Then b\alpha=b\alpha-mouse_shake
  272. b\y_speed=b\y_speed-0.025
  273. Else
  274. FreeEntity b\entity
  275. Delete b
  276. EndIf
  277. Next
  278. End Function
  279. ; ------------------
  280. ; Garg path function
  281. ; ------------------
  282. Function GargPath()
  283. PointEntity gargoyle,target_piv
  284. dest_garg_yaw=EntityYaw(gargoyle)
  285. If dest_garg_yaw-garg_yaw>180 Then garg_yaw=garg_yaw+360
  286. If dest_garg_yaw-garg_yaw<-180 Then garg_yaw=garg_yaw-360
  287. garg_yaw=garg_yaw+((dest_garg_yaw-garg_yaw)/(5/garg_speed))
  288. MoveEntity gargoyle,0,0,garg_speed
  289. RotateEntity gargoyle,0,garg_yaw,0
  290. garg_x=EntityX(gargoyle)
  291. garg_y=EntityY(gargoyle)
  292. garg_z=EntityZ(gargoyle)
  293. If garg_x=node_x(node)
  294. If garg_y=node_y(node)
  295. If garg_z=node_z(node)
  296. least=1000000
  297. node_v(node)=(MilliSecs()-old_time)/1000
  298. For i=1 To node_n(node,0)
  299. nv=node_v(node_n(node,i))
  300. If nv<least Then least=nv : lnode=i
  301. Next
  302. node=node_n(node,lnode)
  303. PositionEntity target_piv,node_x(node),node_y(node),node_z(node)
  304. EndIf
  305. EndIf
  306. EndIf
  307. End Function