您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

projectile.lua 1.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. projectile = Class{}
  2. G = 6.67e-5
  3. function projectile:init(x, y, v, angle, timer)
  4. self.x = x
  5. self.y = y
  6. self.dx = 0
  7. self.dy = 0
  8. self.image = love.graphics.newImage("entities/enemy/missile-01.png")
  9. self.w = self.image:getWidth()
  10. self.h = self.image:getHeight()
  11. self.angle = angle
  12. self.v = v
  13. self.timer = timer
  14. self.killed = false
  15. self.dx = math.cos(self.angle) * self.v
  16. self.dy = math.sin(self.angle) * self.v
  17. self.vx = 0
  18. self.vy = 0
  19. end
  20. function projectile:update(dt)
  21. local distanceToShip = math.sqrt((firstShip.x - self.x)^2 + (firstShip.y - self.y)^2)
  22. self.timer = self.timer - dt
  23. if self.timer <= 0 then
  24. self.killed = true
  25. end
  26. self.vx = self.vx + self.dx
  27. self.vy = self.vy + self.dy
  28. self.x = self.x + self.vx
  29. self.y = self.y + self.vy
  30. if distanceToShip < firstShip.width/3 then
  31. shipIsHit = true
  32. sounds["close"]:stop()
  33. sounds["boom"]:play()
  34. end
  35. end
  36. function projectile:draw()
  37. love.graphics.draw(self.image, self.x, self.y, self.angle, 1, 1, self.w/2, self.w/2)
  38. end