Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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