AndrewofDoom

Side scroller script

Jun 25th, 2012
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.14 KB | None | 0 0
  1. #Global Hooks
  2.  
  3. $Simulation:
  4.  
  5. [
  6. --Main loop that begins once initShmup() is executed.
  7. if booleanInitScroll == true then
  8. if objectPlayer:isValid() then --Player Validity Check
  9.  
  10. --Get Position and Orientation and Speed
  11. vectorCurrentPos = objectPlayer.Position
  12. vectorPlayerVelocity = objectPlayer.Physics.Velocity
  13.  
  14. --Horizontal Position Limit
  15. if objectPlayer.Position["x"] > floatHLimit then
  16. local playPos = ba.createVector((ship_central_camera.Position["x"] + (floatHLimit - 1)), objectPlayer.Position["y"], objectPlayer.Position["z"])
  17. objectPlayer.Position = playPos
  18. elseif objectPlayer.Position["x"] < floatHRLimit then
  19. local playPos = ba.createVector((ship_central_camera.Position["x"] - (floatHRLimit + 1)), objectPlayer.Position["y"], objectPlayer.Position["z"])
  20. objectPlayer.Position = playPos
  21. end
  22.  
  23. --Vertical Position Limit
  24. if objectPlayer.Position["y"] > floatVLimit then
  25. local playPos = ba.createVector(objectPlayer.Position["x"],(ship_central_camera.Position["y"] + (floatVLimit - 1)), objectPlayer.Position["z"])
  26. objectPlayer.Position = playPos
  27. elseif objectPlayer.Position["y"] < floatVRLimit then
  28. local playPos = ba.createVector(objectPlayer.Position["x"],(ship_central_camera.Position["y"] - (floatVRLimit + 1)), objectPlayer.Position["z"])
  29. objectPlayer.Position = playPos
  30. end
  31.  
  32. --Forward Position Limit
  33.  
  34. if objectPlayer.Position["z"] > floatFLimit then
  35. local playPos = ba.createVector(objectPlayer.Position["x"],objectPlayer.Position["y"],(ship_central_camera.Position["z"] + (floatFLimit - 1)))
  36. objectPlayer.Position = playPos
  37. elseif vectorCurrentPos["z"] < floatFRLimit then
  38. local playPos = ba.createVector(objectPlayer.Position["x"],objectPlayer.Position["y"],(ship_central_camera.Position["z"] - (floatFRLimit + 1)))
  39. objectPlayer.Position = playPos
  40. end
  41.  
  42.  
  43. forceOrient = ba.createOrientation(0,0,0)
  44. --Update Player Rotational Velocity, Position
  45. objectPlayer.Physics.Velocity = vectorPlayerVelocity
  46. objectPlayer.Physics.Orientation = forceOrientation
  47.  
  48. --The ideal code that doesn't work...
  49. --local x = gr.getVectorFromCoords(vectorCurrentPos["x"],vectorCurrentPos["y"]) --Use the screen view to determine if the player is in bounds.
  50. --local lx,ly = x:getScreenCoords()
  51. --ba.error(type(x:getScreenCoords()))
  52. --if lx < 0 then
  53. -- x = gr.getVectorFromCoords(0,0)
  54. -- vectorCurrentPos["z"] = x["x"]+10
  55. --elseif lx > gr.getScreenWidth() then
  56. -- x = gr.getVectorFromCoords(gr.getScreenWidth(),0)
  57. -- vectorCurrentPos["z"] = x["x"]-10
  58. --else
  59. --Do nothing
  60. --end
  61. --if ly < 0 or ly == false then
  62. -- x = gr.getVectorFromCoords(0,0)
  63. -- vectorCurrentPos["y"] = x["y"]
  64. --elseif ly > gr.getScreenHeight() then
  65. -- x = gr.getVectorFromCoords(0,gr.getScreenHeight())
  66. -- vectorCurrentPos["y"] = x["y"]
  67. --else
  68. --nothing
  69. --end
  70.  
  71. --AndrewofDoom's mod: addition of scrolling
  72. if booleanInitScroll == true then --double check to see if this still is really true.
  73. ft = ba.getFrametime(false)
  74. intSavedCamPosX = intSavedCamPosX + (floatScrollX*ft)
  75. intSavedCamPosY = intSavedCamPosY + (floatScrollY*ft)
  76. intSavedCamPosZ = intSavedCamPosZ + (floatScrollZ*ft)
  77. vectorCCameraPos = ba.createVector(intSavedCamPosX,intSavedCamPosY,intSavedCamPosZ)
  78. objectCCamera:setPosition(vectorCCameraPos)
  79. floatHLimit = floatHLimit + (floatScrollX*ft)
  80. floatVLimit = floatVLimit + (floatScrollY*ft)
  81. floatFLimit = floatFLimit + (floatScrollZ*ft)
  82. ---Set movement limits again for the rear. The original version makes the assumption that the player's postion will be along the origin and thus symmetrical, which will not be the case if the camera is scrolling.
  83. floatHRLimit = floatHRLimit + (floatScrollX*ft)
  84. floatVRLimit = floatVRLimit + (floatScrollY*ft)
  85. floatFRLimit = floatFRLimit + (floatScrollZ*ft)
  86.  
  87. --all scrolling related to slaved ships.
  88. for i=1,#slaveShips do
  89. if slaveShips[i] ~= nil then
  90. local scrollS = slaveShips[i]
  91. if scrollS ~= nil then --If this ship is null, don't bother playing with it.
  92. local pos = scrollS.Position
  93. if slaveToX[i] == true then
  94. pos["x"] = pos["x"] + (floatScrollX*ft)
  95. end
  96. if slaveToY[i] == true then
  97. pos["y"] = pos["y"] + (floatScrollY*ft)
  98. end
  99. if slaveToZ[i] == true then
  100. pos["z"] = pos["z"] + (floatScrollZ*ft)
  101. end
  102. scrollS.Position = pos
  103. slaveShips[i] = scrollS
  104. end
  105. end
  106. end
  107.  
  108. ---enforce projectile positions relative to scrolling and cull offscreen projectiles.
  109. local numWeps = #mn.Weapons
  110. for i=1,numWeps do
  111. local wep = mn.Weapons[i]
  112. local wPos = wep.Position
  113. if wep.Parent == hv.Player or wep.Target == hv.Player then
  114. --Check for out of bounds
  115. if wPos["y"] > floatVLimit or wPos["y"] < floatVRLimit or wPos["z"] > floatFLimit or wPos["z"] < floatFRLimit then
  116. wep.LifeLeft = 0.0 --Terminate weapon
  117. end
  118. if wep.LifeLeft > 0 then
  119. --if still in bounds, apply scrolling.
  120. wPos["x"] = wPos["x"] + (floatScrollX*ft)
  121. wPos["y"] = wPos["y"] + (floatScrollY*ft)
  122. wPos["z"] = wPos["z"] + (floatScrollZ*ft)
  123. wep.Position = wPos
  124. end
  125. end
  126. if wep.LifeLeft > 0 then --if this projectile is dead, then don't bother.
  127. --force these projectiles from these ships to have their scrolling force to player position and 0'd x component velocities.
  128. --May seem like an ugly O(n^2) algorithm, but this stuff is usually reserved for important enemies. Namely, bosses which are usually one, MAYBE two, in count.
  129. for j=1, #forcedProjShipsToPlayerAxis do
  130. if forcedProjShipsToPlayerAxis[j] ~= nil then --if there is no one here, then don't bother.
  131. if wep.Parent == forcedProjShipsToPlayerAxis[j] then
  132. local wepPos = ba.createVector(objectPlayer.Position["x"],wep.Position["y"],wep.Position["z"])
  133. wep.Position = wepPos
  134. local wepVel = ba.createVector(0,wep.Physics.Velocity["y"],wep.Physics.Velocity["z"])
  135. wep.Physics.Velocity = wepVel
  136. wep.Physics.VelocityDesired = wepVel
  137. wep.Physics.VelocityMax = wepVel
  138. local wepOrient = ba.createOrientation(wep.Orientation["p"],wep.Orientation["b"],0)
  139. wep.Orientation = wepOrient
  140. end
  141. end
  142. end
  143. end
  144. mn.Weapons[i] = wep --Apply weapon change
  145. end
  146.  
  147. end
  148. end --Player Validity Check
  149. end
  150.  
  151. ]
  152.  
  153. #End
  154.  
  155. #Conditional Hooks
  156.  
  157. $State: GS_STATE_GAME_PLAY
  158.  
  159. $On Frame:
  160.  
  161. [
  162. --initializes all needed variables and then sets the camera. an object called Center MUST exist for it to start. Next thing that MUST be done is to slave the player ship first and then a Trip Line Trigger object at the origin.
  163. function initShmup()
  164. ship_central_camera = mn.Ships['Center'] --Look for a ship called Center to...well center the view on.
  165. objectPlayer = hv.Player
  166. if ship_central_camera ~= nil then
  167. --These are the slaves to our scrolling system. Every ship and associated boolean must all be aligned to the same index!
  168. slaveShips = {}
  169. slaveToX = {}
  170. slaveToY = {}
  171. slaveToZ = {}
  172. forcedProjShipsToPlayerAxis = {}
  173.  
  174. ---Create 'blank' orientation
  175. ---these rotations are in RADIANS, not degrees.
  176. orientationBlank = hv.Player.Orientation
  177. orientationBlank["p"] = 0
  178. orientationBlank["b"] = 0
  179. orientationBlank["h"] = -1.57
  180. intSavedCamPosX = (ship_central_camera.Position["x"] + 250)
  181. intSavedCamPosY = ship_central_camera.Position["y"]
  182. intSavedCamPosZ = ship_central_camera.Position["z"]
  183. ---Set movement box limits
  184. floatHLimit = (ship_central_camera.Position["x"] + 30)
  185. floatVLimit = (ship_central_camera.Position["y"] + 88)
  186. floatFLimit = (ship_central_camera.Position["z"] + 155)
  187. --- Set movement limits again for the rear. The original version makes the assumption that the player's postion will be
  188. --- along the origin and thus symmetrical, which will not be the case if the camera is scrolling.
  189. floatHRLimit = (ship_central_camera.Position["x"] - 30)
  190. floatVRLimit = (ship_central_camera.Position["y"] - 88)
  191. floatFRLimit = (ship_central_camera.Position["x"] - 155)
  192. ---Scroll rates for each axis
  193. floatScrollX = 0
  194. floatScrollY = 0
  195. floatScrollZ = 0
  196. vectorCCameraPos = ba.createVector(intSavedCamPosX,intSavedCamPosY,intSavedCamPosZ)
  197. orientationCCamera = orientationBlank
  198.  
  199. --Use the player camera at the start and move it out of first person for a wonderful out-of-body experience.
  200. objectCCamera = gr.Cameras[1]
  201. objectCCamera:setPosition(vectorCCameraPos)
  202. objectCCamera:setOrientation(orientationBlank)
  203. ---Use the new chase camera
  204. gr.setCamera(objectCCamera)
  205. booleanInitScroll = true
  206. else
  207. ba.error("No view was found to center on. Make sure there is a ship called Center to point the camera on.")
  208. end
  209. end
  210.  
  211. --Modifies the scroll speed of the camera on any axes a man desires..
  212. --Value is in meters/frame, but is converted to meters/60 frames by this function for use when it's multiplied by ba.getFrameTime.
  213. function setScrollSpeed(x,y,z)
  214. floatScrollX = (x*60)
  215. floatScrollY = (y*60)
  216. floatScrollZ = (z*60)
  217. end
  218.  
  219. --Sets the current Status of the camera scrolling. initShmup must be called first to avoid funny stuff from happening.
  220. --True turns it on, false to turn it off and rest the camera view to the player ship.
  221. function setScrollingActive(booleanSwitch)
  222. if booleanSwitch == true then
  223. vectorCCameraPos = ba.createVector((objectPlayer.Position["x"]-250),objectPlayer.Position["y"],objectPlayer.Position["z"])
  224. objectCCamera:setPosition(vectorCCameraPos)
  225. gr.setCamera(objectCCamera)
  226.  
  227. --Do NOT assume the player's position hasn't changed, therefore reset the bounds.
  228. floatHLimit = objectPlayer.Position["x"] + 30
  229. floatVLimit = objectPlayer.Position["y"] + 88
  230. floatFLimit = objectPlayer.Position["z"] + 160
  231. floatHRLimit = objectPlayer.Position["x"] - 30
  232. floatVRLimit = objectPlayer.Position["y"] - 90
  233. floatFRLimit = objectPlayer.Position["z"] - 160
  234. --Everything's set. Time to start scrolling again.
  235. booleanInitScroll = true
  236. elseif booleanSwitch == false then
  237. --Disable camera and scrolling, probably for a cutscene or something of that matter.
  238. booleanInitScroll = false
  239. gr.setCamera()
  240. else
  241. --nothing
  242. end
  243. end
  244.  
  245.  
  246. --Will cause the specified ship to be slaved to scrolling system and will move relative to the camera instead for the specified axes
  247. --Note that these ships still do not adhere to the boundary enclosure like the player is.
  248. function sTSC(shipToSlave,booleanSlaveXAxis,booleanSlaveYAxis,booleanSlaveZAxis)
  249. local toAdd = mn.Ships[shipToSlave]
  250. if toAdd ~= nil then --sanity check
  251. slaveShips[(#slaveShips+1)] = toAdd
  252. slaveToX[(#slaveToX+1)] = booleanSlaveXAxis
  253. slaveToY[(#slaveToY+1)] = booleanSlaveYAxis
  254. slaveToZ[(#slaveToZ+1)] = booleanSlaveZAxis
  255. end
  256. end
  257.  
  258. --removes a slaved ship.
  259. function rss(slavedShip)
  260. local toChange = mn.Ships[slavedShip]
  261. local intIndex = 0
  262. for i=1, #slaveShips do
  263. if slaveShips[i] == toChange then
  264. intIndex = i -- found the ship, assign the index and break out of the for loop.
  265. break
  266. end
  267. end
  268. if intIndex >= 1 then --if this number is >= 1 then I have found something.
  269. slaveShips[intIndex] = nil --this may be potentially dangerous, but any good man should always check for null when iterating an array.
  270. end
  271. end
  272.  
  273. --Forces this ships projectiles to be on the same axis on the player's (the x-axis)
  274. --Needs to be done before it starts shooting.
  275. function pgopx(pgopx_ship)
  276. local toAdd = mn.Ships[pgopx_ship]
  277. if toAdd ~= nil then
  278. forcedProjShipsToPlayerAxis[(#forcedProjShipsToPlayerAxis+1)] = toAdd
  279. end
  280. end
  281.  
  282.  
  283. --This crap won't work if the camera isn't the player's
  284. --Guess I gotta use subtitles now. =|
  285. --function drawPlayerHP()
  286. -- local ship = mn.Ships[1]
  287. -- local shipHP = ship.HitpointsLeft
  288. -- if shipHP <= 30 then
  289. -- gr.setColor(255,0,0)
  290. -- else
  291. -- gr.setColor(0,255,0)
  292. -- end
  293. -- gr.drawString(shipHP,200,100)
  294. --end
  295.  
  296. ]
  297.  
  298. $On Death:
  299.  
  300. [
  301. local dyingShip = hv.Self
  302. if dyingShip == hv.Player then --Do not disable the scrolling system if it's not the player who's blowing up!!
  303. booleanInitScroll = false
  304. end
  305.  
  306. ]
  307.  
  308. $On Mission End:
  309. [
  310.  
  311. booleanInitScroll = false --force it off when a mission ends.
  312.  
  313. ]
  314.  
  315. #End
Advertisement
Add Comment
Please, Sign In to add comment