25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.8 KiB

  1. planet = Class{}
  2. G = 6.67e-1
  3. function planet:init(x, y, mass, radius, img, arg)
  4. self.x = x
  5. self.y = y
  6. self.mass = mass
  7. self.attractionX = 0
  8. self.attractionY = 0
  9. self.r = radius
  10. self.w = img:getWidth()
  11. self.image = img
  12. self.angle = 0
  13. self.color = {1,1,1,1}
  14. if arg == "nodelete" then
  15. self.deletable = false
  16. else
  17. self.deletable = true
  18. end
  19. end
  20. function planet:update(dt)
  21. if not reachedGoal then
  22. local distanceToShip = math.sqrt((firstShip.x - self.x)^2 + (firstShip.y - self.y)^2)
  23. --print("update")
  24. local gravitationalAttraction = G*self.mass/(distanceToShip^2)
  25. --print((firstShip.x - self.x) .. " " .. (firstShip.y - self.y))
  26. self.angle = math.atan( (firstShip.y - self.y)/ (firstShip.x - self.x))
  27. if self.x < firstShip.x then
  28. self.angle = self.angle - 3.14159
  29. end
  30. --print("Angle is:" .. self.angle*57.29)
  31. self.attractionY = math.sin(self.angle) * gravitationalAttraction
  32. self.attractionX = math.cos(self.angle) * gravitationalAttraction
  33. love.window.setTitle(self.attractionX)
  34. firstShip.dx = firstShip.dx + self.attractionX
  35. firstShip.dy = firstShip.dy + self.attractionY
  36. if distanceToShip < self.w/4 then
  37. shipIsHit = true
  38. sounds["close"]:stop()
  39. sounds["boom"]:play()
  40. end
  41. end
  42. end
  43. function planet:draw()
  44. --love.graphics.rectangle("fill", firstShip.x, firstShip.y, 30, 30)
  45. love.graphics.push()
  46. love.graphics.translate(firstShip.x, firstShip.y) -- move relative (0,0) to (x,y)
  47. love.graphics.rotate(self.angle) -- rotate coordinate system around relative (0,0) (absolute (x,y))
  48. --love.graphics.rectangle("fill", -(firstShip.x - self.x)/2, -20/2, (firstShip.x - self.x), 20) VECTOR
  49. love.graphics.pop()
  50. love.graphics.setColor(unpack(self.color))
  51. love.graphics.draw(self.image, self.x, self.y, 1.5708, self.r, self.r, self.w/2, self.w/2)
  52. end