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.

ship.lua 3.0 KiB

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