scare.bb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. Graphics3D 800,600,16
  2. SetBuffer BackBuffer()
  3. AmbientLight 120,120,90
  4. Global up=17,down=31,Right=32,Left=30,esc=1,mx,my
  5. Global timer=CreateTimer(60)
  6. Global init=MilliSecs(),frames#=0,fps=0,font=LoadFont("verdana")
  7. Global grav#=.3,mxspd#=1.4,myspd#=1.4,friction#=.04
  8. Type cam
  9. Field x#,y#,z#,picked
  10. Field ax#,ay#,az#,gun,gx#,gy#,gz#,g#,gzz#,gtime
  11. Field ent,spd#,mmx,mmy,mlook,fall,pent,jump
  12. Field mx#,my#,mz#,jstrength#,crouch,cy#,cy2#,mm,zo#,zs#
  13. Field walk,head#,xspd#,yspd#,zspd#,move,gya#,light,s_step#
  14. End Type
  15. Global cursor=LoadAnimImage("textures/gui/cursors.png",31,31,0,17),c_id,m_id
  16. Global c_gui=LoadImage("textures/gui/chat.png"),maxh#,maxl#
  17. Type h
  18. Field time#,ent,ji,spd#
  19. End Type
  20. Global PLYTYPE=1,WALLTYPE=2
  21. Collisions PLYTYPE,WALLTYPE,2,3
  22. Global level=LoadMesh("models/level/level.x")
  23. ScaleEntity level,35,35,35
  24. EntityType level,WALLTYPE
  25. EntityPickMode level,2,1
  26. c.cam=New cam
  27. ;EntityRadius c\ent,3
  28. c\pent=CreateSphere(10)
  29. c\ent=CreateCamera()
  30. EntityRadius c\ent,10
  31. EntityType c\ent,PLYTYPE
  32. hw=True
  33. HWMultiTex hw
  34. CameraClsColor c\ent,0,0,155
  35. c\gun=LoadMesh("models\pistol\pistol.x",c\ent)
  36. Global gzz=EntityZ(c\gun)
  37. c\gya=-180
  38. RotateEntity c\gun,-180,180,0
  39. PositionEntity c\gun,3,-3.5,4
  40. PositionEntity c\pent,0,2,0
  41. c\light=CreateLight(2,c\ent)
  42. LightRange c\light,50
  43. Dim snd(5)
  44. snd(3)=LoadSound("sounds\jump1.wav")
  45. te=LoadTexture("models\pistol\gun.png")
  46. t2=LoadTexture("models\pistol\g1.png",67)
  47. EntityTexture c\gun,te,0,0
  48. EntityTexture c\gun,t2,0,1
  49. UpdateNormals c\gun
  50. TextureBlend te,1
  51. TextureBlend t2,2
  52. ScaleEntity c\pent,12,10,12
  53. EntityType c\pent,PLYTYPE
  54. EntityAlpha c\pent,0
  55. FlipMesh c\pent
  56. EntityRadius c\pent,10
  57. c\jstrength=2/grav#
  58. Global pcam=c\ent
  59. Global sprite=InitSprite()
  60. Global hit=CopyEntity(sprite)
  61. Global sp=LoadAnimTexture("models/pistol/hit.png",1,1,1,0,6)
  62. EntityFX hit,1
  63. EntityTexture hit,sp
  64. HideEntity hit
  65. ; MAIN LOOP
  66. ;#################################
  67. While Not KeyDown(esc)
  68. ticks=WaitTimer(timer)
  69. For k=1 To ticks
  70. UpdateGame()
  71. UpdateWorld
  72. Next
  73. Cls
  74. RenderWorld
  75. RenderGame()
  76. fps = frames#/(MilliSecs()-init)*1000
  77. Flip : frames#=frames#+1
  78. Wend
  79. FreeFont font
  80. FreeTimer(timer)
  81. EndGraphics
  82. End
  83. Function UpdateGame()
  84. mx=MouseX()
  85. my=MouseY()
  86. mxs=MouseXSpeed()/(myspd)
  87. mys=MouseYSpeed()/(mxspd)
  88. For c.cam= Each cam
  89. ox#=EntityX(c\pent)
  90. oz#=EntityZ(c\pent)
  91. If c\zs<c\zo Then c\zs=c\zs+.1
  92. If c\zs>c\zo Then c\zs=c\zs-.1
  93. If c\zs<=0 Then c\zs=1
  94. If KeyDown(57) Then c\zo=2.5 Else c\zo=1
  95. CameraZoom c\ent,c\zs
  96. c\cy2=(EntityY(c\pent))-c\crouch*17
  97. c\cy= CurveValue(c\cy, c\cy2, 5)
  98. If c\gya<0
  99. RotateEntity c\gun,c\gya,180,c\gya
  100. c\gya=c\gya+2.5
  101. EndIf
  102. If KeyHit(4) Then c\gya=-180
  103. PositionEntity c\ent,EntityX(c\pent),c\cy+20,EntityZ(c\pent)
  104. If KeyDown(157) Or KeyDown(29) Then c\crouch=True Else c\crouch=False
  105. If KeyHit(2) Then c_id=c_id+1
  106. If c_id=>13 Then c_id=0
  107. If KeyHit(3) Then m_id=m_id+1
  108. If m_id=>4 Then m_id=0
  109. ; ========== mlook
  110. If KeyHit(42) Or KeyHit(54) Then c\mmx=GraphicsWidth()/2:c\mmy=GraphicsHeight()/2:c\mlook=c\mlook+1
  111. If c\mlook=>2 Then c\mlook=0
  112. ; ========== camera smooth
  113. If c\mlook=1
  114. mx=GraphicsWidth()/2
  115. my=GraphicsHeight()/2
  116. c\ax = c\ax + mys;+(MouseDown(1)*Rand(-2,2))
  117. c\ay = c\ay - mxs
  118. If c\ax=>71 Then c\ax=70.99999999
  119. If c\ax=<-71 Then c\ax=-70.99999999
  120. MoveMouse c\mmx,c\mmy
  121. If MouseDown(2) And c\fall=False Then c\g=c\jstrength#:PlaySound snd(3)
  122. PositionEntity c\gun,EntityX(c\gun),EntityY(c\gun),c\gzz+3
  123. c\gtime=c\gtime+1
  124. If c\picked>0 And c\gtime=>3
  125. CreateShot(PickedX(),PickedY(),PickedZ(),1,2,True)
  126. CreateShot(PickedX(),PickedY(),PickedZ(),1,2,True)
  127. CreateShot(PickedX(),PickedY(),PickedZ(),1,2,True)
  128. CreateShot(PickedX(),PickedY(),PickedZ(),1,2,True)
  129. CreateShot(PickedX(),PickedY(),PickedZ(),3,0,False)
  130. c\picked=0
  131. c\gzz=Rnd(1)
  132. c\gtime=0
  133. Else
  134. c\gzz=gzz
  135. EndIf
  136. If MouseDown(1)
  137. c\picked=CameraPick(c\ent,mx+Rand(-5,5),my+Rand(-5,5))
  138. EndIf
  139. Else
  140. c\ax = c\ax - (KeyDown(up)-KeyDown(down))
  141. If c\ax=>71 Then c\ax=70.99999999
  142. If c\ax=<-71 Then c\ax=-70.99999999
  143. c\ay = c\ay - (KeyDown(Right)-KeyDown(Left))
  144. EndIf
  145. If KeyHit(2) Then ScaleEntity c\pent,12,30,12
  146. ; ===== Head bob =====
  147. ; [variables]
  148. ; c\type
  149. ; g=gravity up/down
  150. ; walk= which way your walking
  151. ; head= which way your head should Zrotate
  152. ; az= actual direction your head Zrotate
  153. ; x,y,z=smoothed X,Y,Z
  154. ; gi=whether or not he rotates his head
  155. va=KeyDown(up)
  156. vb=KeyDown(down)
  157. vc=KeyDown(Left)
  158. vd=KeyDown(Right)
  159. If va Then c\walk=1:c\head=0:c\mm=1
  160. If vb Then c\walk=-1:c\head=0:c\mm=1
  161. If vc Then c\walk=0:c\head=2.9:c\mm=1
  162. If vd Then c\walk=0:c\head=-2.9:c\mm=1
  163. If va=0 And vb=0 And vc=0 And vd=0 Then c\walk=0:c\head=0:c\mm=0
  164. If c\g>2 Then c\walk=0
  165. c\az = 0
  166. If c\walk<>0
  167. c\az = c\az + ((Sin(MilliSecs())/16)*12)
  168. EndIf
  169. If c\head<>0
  170. c\az = c\az + ((Sin(MilliSecs())/16)*12)
  171. EndIf
  172. c\x = CurveValue(c\x + c\walk, c\ax, 5)
  173. c\y = CurveValue(c\y, c\ay, 5)
  174. gi=0
  175. c\z = CurveValue(c\z, c\head - (c\az), 5)
  176. ;If c\head<>0 Then
  177. RotateEntity c\ent, c\x, c\y, c\z
  178. ; ===== /Head bob
  179. ; ========== /camera smooth
  180. c\fall=True
  181. For x=1 To CountCollisions(c\pent)
  182. If GetEntityType(CollisionEntity(c\pent,x))=WALLTYPE
  183. Select CollisionNY(c\pent,x)
  184. Case 1
  185. c\g=0
  186. c\fall=False
  187. End Select
  188. cye#=EntityY(c\ent)-CollisionY(c\pent,x)
  189. If cye>16 And cye<30
  190. MoveEntity c\pent,0,4,0
  191. EndIf
  192. EndIf
  193. Next
  194. c\move=True
  195. For x=1 To CountCollisions(c\ent)
  196. If GetEntityType(CollisionEntity(c\ent,x))=WALLTYPE
  197. c\move=False
  198. Select CollisionNY(c\ent,x)
  199. Case -1
  200. c\g=-9
  201. c\fall=True
  202. End Select
  203. EndIf
  204. Next
  205. If c\fall=True
  206. c\g=c\g-grav#
  207. c\spd=.5
  208. Else
  209. If Not c\crouch
  210. c\spd=5.4
  211. Else
  212. c\spd=2.0
  213. EndIf
  214. If c\mlook
  215. c\mx=(KeyDown(Right)-KeyDown(Left))*c\spd
  216. c\mz=(KeyDown(up)-KeyDown(down))*c\spd
  217. EndIf
  218. EndIf
  219. ; Z Slide/Movement (up/down)
  220. If c\mz=0
  221. If c\zspd>0
  222. c\zspd=c\zspd - friction#
  223. ElseIf c\zspd<0
  224. c\zspd=c\zspd + friction#
  225. EndIf
  226. ;If c\zspd>-1 And c\zspd<0.01 Then c\zspd=0
  227. EndIf
  228. If c\zspd>c\spd Then c\zspd=c\spd
  229. If c\zspd<-c\spd Then c\zspd=-c\spd
  230. c\zspd=c\zspd + c\mz
  231. zi#=CurveValue(zi,c\zspd,3)
  232. ; X Slide/Movement (right/left)
  233. If c\mx=0
  234. If c\xspd>0
  235. c\xspd=c\xspd - friction#
  236. ElseIf c\xspd<0
  237. c\xspd=c\xspd + friction#
  238. EndIf
  239. If c\xspd>-1 And c\xspd<0.1 Then c\xspd=0
  240. EndIf
  241. If c\xspd>c\spd/2 Then c\xspd=c\spd/2
  242. If c\xspd<-c\spd/2 Then c\xspd=-c\spd/2
  243. c\xspd=c\xspd + c\mx
  244. xi#=CurveValue(xi,c\xspd,3)
  245. RotateEntity c\pent, 0, c\y, 0
  246. RotateEntity c\light, EntityPitch(c\ent), EntityYaw(c\ent), 0
  247. ;If Not c\move Then xi=0:zi=0
  248. MoveEntity c\pent,xi,c\g,zi
  249. Next
  250. For hi.h=Each h
  251. If hi\ji=1 And hi\spd>0 Then RotateEntity hi\ent,Rnd(360),Rnd(360),Rnd(360)
  252. If hi\time>1 Then PointEntity hi\ent,pcam
  253. hi\time=hi\time-.01
  254. EntityAlpha hi\ent,hi\time
  255. EntityTexture hi\ent,sp,Rnd(6)
  256. If hi\time>-1 And hi\time<1
  257. MoveEntity hi\ent,0,0,hi\spd
  258. hi\ji=hi\ji+1
  259. TranslateEntity hi\ent,0,hi\spd*.1,0
  260. EndIf
  261. If hi\time=<-1
  262. FreeEntity hi\ent
  263. Delete hi
  264. EndIf
  265. Next
  266. End Function
  267. Function RenderGame()
  268. Color 255,255,255
  269. c.cam=FindCam(pcam)
  270. Text 0,0,fps
  271. If c\mlook=0
  272. DrawImage c_gui,0,GraphicsHeight()-ImageHeight(c_gui)
  273. DrawImage cursor,mx-16,my-16,m_id+13
  274. Else
  275. DrawImage cursor,mx-16,my-16,c_id
  276. EndIf
  277. End Function
  278. Function Distance(scr,x2#,y2#,z2#)
  279. x1#=EntityX(scr)
  280. y1#=EntityY(scr)
  281. z1#=EntityZ(scr)
  282. Return Sqr((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)
  283. End Function
  284. Function parse$(in$, delim$, position)
  285. out$ = ""
  286. max = Len(in$)
  287. count = 0
  288. If position = 0
  289. For x = 1 To max
  290. char$ = Mid$(in$, x, 1)
  291. If char$ <> delim$
  292. out$ = out$ + char$
  293. Else
  294. Exit
  295. EndIf
  296. Next
  297. Return out$
  298. Else
  299. For x = 1 To max
  300. char$ = Mid$(in$, x, 1)
  301. If count = position
  302. If char$ <> delim$
  303. out$ = out$ + char$
  304. Else
  305. Exit
  306. EndIf
  307. Else If char$ = delim$
  308. count = count + 1
  309. EndIf
  310. Next
  311. Return out$
  312. EndIf
  313. End Function
  314. Function CurveValue#(current#,destination#,curve)
  315. current#=current#+((destination#-current#)/curve)
  316. Return current#
  317. End Function
  318. Function FindCam.cam(ent)
  319. For c.cam= Each cam
  320. If c\ent=ent Then Return c.cam
  321. Next
  322. End Function
  323. Function nxv#(x#,d#,s#)
  324. Return -Sin(d#)*s#+x#
  325. End Function
  326. Function nyv#(y#,d#,s#)
  327. Return Sin(d#)*s#+y#
  328. End Function
  329. Function nzv#(z#,d#,s#)
  330. Return Cos(d#)*s#+z#
  331. End Function
  332. Function InitSprite()
  333. sprite=CreateMesh()
  334. he=CreateBrush(255,255,255)
  335. v=CreateSurface(sprite,he)
  336. FreeBrush he
  337. AddVertex ( v,-3,3,0,1,0) ; top left 0,1;1,0
  338. AddVertex ( v,3,3,0,0,0) ; top right 1,1;1,1
  339. AddVertex ( v,-3,-3,0,1,1) ; bottom left 0,0;,0,0
  340. AddVertex ( v,3,-3,0,0,1) ; bottom right 1,0;0,1
  341. AddTriangle( v,0,1,2)
  342. ;AddTriangle( v,3,2,1)
  343. FlipMesh(sprite)
  344. HideEntity(sprite)
  345. Return sprite
  346. End Function
  347. Function CreateShot(x#,y#,z#,time#,spd#,fx)
  348. hi.h= New h
  349. hi\spd=spd
  350. hi\ent=CopyEntity(hit)
  351. PositionEntity hi\ent,x,y,z
  352. PointEntity hi\ent,pcam
  353. EntityFX hi\ent,fx
  354. EntityAlpha hi\ent,.2
  355. If fx=0
  356. ScaleEntity hi\ent,.1,.1,1
  357. Else
  358. ScaleEntity hi\ent,.2,.2,1
  359. EndIf
  360. hi\time=time
  361. End Function