Main.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. require "Scripts/Player"
  2. require "Scripts/Alien"
  3. music = Sound("Resources/music/level1.ogg")
  4. music:Play(true)
  5. Services.MaterialManager.premultiplyAlphaOnLoad = true
  6. scene = PhysicsScene2D(1.0, 60)
  7. scene:setGravity(Vector2(0.0, -30.0))
  8. scene:getDefaultCamera():setOrthoSize(0, 14)
  9. level = SceneEntityInstance(scene, "Resources/entities/level.entity")
  10. scene:addChild(level)
  11. playerBody = level:getEntityById("player", true)
  12. player = Player(scene, playerBody)
  13. alienBodies = level:getEntitiesByTag("alien", false)
  14. aliens = {}
  15. for i=1,count(alienBodies) do
  16. local alien = Alien(scene, alienBodies[i], player)
  17. aliens[i] = alien
  18. alien:Respawn()
  19. end
  20. shootTimer = 0.0
  21. platforms = level:getEntitiesByTag("platform", false)
  22. for i=1,count(platforms) do
  23. scene:trackPhysicsChild(platforms[i], PhysicsScene2DEntity.ENTITY_RECT, true, 2.0, 1, 0, false, false)
  24. end
  25. shots = level:getEntitiesByTag("shot", false)
  26. shotDirections = {}
  27. for i=1,count(shots) do
  28. shots[i] = safe_cast(shots[i], SceneSprite)
  29. scene:trackCollisionChild(shots[i], PhysicsScene2DEntity.ENTITY_RECT, -1)
  30. shots[i].backfaceCulled = false
  31. shotDirections[i] = 0.0
  32. end
  33. shotIndex = 1
  34. function onKeyDown(key)
  35. player:onKeyDown(key)
  36. end
  37. function onCollision(t, event)
  38. local physicsEvent = safe_cast(event, PhysicsScene2DEvent)
  39. for i=1,count(shots) do
  40. if physicsEvent.entity2 == shots[i] or physicsEvent.entity1 == shots[i] then
  41. for j=1,count(aliens) do
  42. if aliens[j].body == physicsEvent.entity1 or aliens[j].body == physicsEvent.entity2 then
  43. aliens[j].dead = true
  44. if shots[i]:getPosition().x > aliens[j].body:getPosition().x then
  45. aliens[j].physicsBody:applyImpulse(-230.0, 0.0)
  46. else
  47. aliens[j].physicsBody:applyImpulse(230.0, 0.0)
  48. end
  49. end
  50. end
  51. shots[i]:setPositionX(1000.0)
  52. end
  53. end
  54. end
  55. scene:addEventListener(nil, onCollision, PhysicsScene2DEvent.EVENT_NEW_SHAPE_COLLISION)
  56. function Update(elapsed)
  57. player:Update(elapsed)
  58. shootTimer = shootTimer + elapsed
  59. if Services.Input:getKeyState(KEY_x) then
  60. if shootTimer > 0.15 then
  61. shotDirections[shotIndex] = player:Shoot(shots[shotIndex])
  62. shotIndex = shotIndex +1
  63. if shotIndex > count(shots) then shotIndex = 1 end
  64. shootTimer = 0.0
  65. end
  66. else
  67. shootTimer = 100.0
  68. end
  69. end
  70. function fixedUpdate()
  71. local elapsed = Services.Core:getFixedTimestep()
  72. player:fixedUpdate(elapsed)
  73. for i=1,count(aliens) do
  74. aliens[i]:Update(elapsed)
  75. end
  76. for i=1,count(shots) do
  77. shots[i]:setRoll(shotDirections[i])
  78. shots[i]:Translate(elapsed * 18.0 * cos(shotDirections[i] * 0.0174532925), elapsed * 18.0 * sin(shotDirections[i] * 0.0174532925), 0.0)
  79. end
  80. end