You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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