Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

153 rindas
4.3 KiB

  1. ship = Class{}
  2. function ship:init(x, y, image)
  3. self.x = x
  4. self.y = y
  5. self.ox = x
  6. self.oy = y
  7. self.dy = 0
  8. self.dx = 5
  9. self.speed = 1
  10. self.image = love.graphics.newImage(image)
  11. self.oimage = self.image
  12. self.timage = love.graphics.newImage("entities/ship/thrusting01.png")
  13. self.width = self.image:getWidth()
  14. self.height = self.image:getHeight()
  15. self.rotation = 1.5708
  16. self.vector = 1.5708
  17. self.color = {1,1,1,1}
  18. self.path = {}
  19. self.dottimer = 0.5
  20. self.exploded = false
  21. self.fuel = 0
  22. self.destX = x
  23. end
  24. function ship:newPathDot(dotx, doty, color)
  25. return {
  26. x = dotx,
  27. y = doty,
  28. color = color
  29. }
  30. end
  31. function ship:update(dt)
  32. if not shipIsHit then
  33. self.dottimer = self.dottimer - dt
  34. if self.dottimer < 0 then
  35. if (love.keyboard.isDown('w') and self.fuel > 0 and gameState == "levelgeneral") then
  36. self.image = self.timage
  37. table.insert(self.path, self:newPathDot(self.x, self.y, 1))
  38. else
  39. self.image = self.oimage
  40. table.insert(self.path, self:newPathDot(self.x, self.y, 2))
  41. end
  42. self.dottimer = 0.2
  43. end
  44. if love.timer.getFPS() < 20 then
  45. self.path = {}
  46. end
  47. self.x = self.x + self.dx*self.speed/2
  48. self.y = self.y + self.dy*self.speed/2
  49. if self.dx ~= 0 then
  50. self.vector = math.atan( self.dy/ self.dx)
  51. end
  52. --[[if love.keyboard.isDown('s') then
  53. self.speed = math.sqrt(self.dx^2 + self.dy^2)
  54. if self.speed > 0 then
  55. self.speed = self.speed - 0.5
  56. end
  57. self.dx = math.cos(self.vector) * self.speed
  58. self.dy = math.sin(self.vector) * self.speed
  59. end ]]--
  60. if love.keyboard.isDown('w') and self.fuel > 0 and gameState == "levelgeneral" then
  61. self.fuel = self.fuel - 0.5
  62. self.speed = self.speed + 0.05
  63. end
  64. local tag = false
  65. for i in ipairs(planets) do
  66. local distanceToShip = math.sqrt((firstShip.x - planets[i].x)^2 + (firstShip.y - planets[i].y)^2)
  67. if distanceToShip < 200 and not shipIsHit and gameStatus == "play" then
  68. sounds["close"]:play()
  69. tag = true
  70. end
  71. end
  72. for i in ipairs(projectiles) do
  73. local distanceToShip = math.sqrt((firstShip.x - projectiles[i].x)^2 + (firstShip.y - projectiles[i].y)^2)
  74. if distanceToShip < 200 and not shipIsHit and gameStatus == "play" then
  75. sounds["close"]:play()
  76. tag = true
  77. end
  78. end
  79. if not tag or shipIsHit then
  80. sounds["close"]:stop()
  81. end
  82. --[[
  83. if love.keyboard.isDown('left') then
  84. self.dx = self.dx - 0.5
  85. end
  86. if love.keyboard.isDown('right') then
  87. self.dx = self.dx + 0.5
  88. end
  89. if love.keyboard.isDown('up') then
  90. self.dy = self.dy - 0.5
  91. end
  92. if love.keyboard.isDown('down') then
  93. self.dy = self.dy + 0.5
  94. end
  95. ]]--
  96. --print(self.speed)
  97. --[[
  98. if love.keyboard.isDown('right') then
  99. self.rotation = self.rotation + 10
  100. elseif love.keyboard.isDown('left') then
  101. self.rotation = self.rotation - 10
  102. end]]--
  103. --print("rotation:" .. self.rotation)
  104. --print("speed:" .. self.dx .. " " .. self.dy)
  105. if self.dx < 0 then
  106. self.vector = self.vector - 3.14159
  107. end
  108. self.vector = self.vector + 1.5708
  109. end
  110. end
  111. function ship:draw()
  112. -- Draw the `self.canvas` to screen
  113. love.graphics.setColor(unpack(self.color))
  114. --print("DAW" .. camera.x)
  115. for i in ipairs(self.path) do
  116. if i > 1 then
  117. love.graphics.setColor(0.9,0.9,0.9,1)
  118. if (self.path[i].color < 2) then
  119. love.graphics.setColor(0.9,0.4,0.4,1)
  120. end
  121. --print("DOING".. i)
  122. love.graphics.line(self.path[i].x, self.path[i].y, self.path[i-1].x, self.path[i-1].y)
  123. end
  124. end
  125. love.graphics.setColor(1,1,1,1)
  126. love.graphics.draw(self.image, self.x, self.y, self.vector, 1, 1, self.width/2, self.height/2)
  127. end
  128. function ship:reset()
  129. self.x = self.ox
  130. self.y = self.oy
  131. self.dy = 0
  132. self.dx = 5
  133. self.rotation = 1.57
  134. self.canvas = love.graphics.newCanvas(WINDOW_WIDTH, WINDOW_HEIGHT)
  135. self.vector = 1.56
  136. self.speed = 1
  137. self.destX = self.x
  138. self.path = {}
  139. self.dottimer = 0.5
  140. self.exploded = false
  141. end