Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Planet.lua 2.5 KiB

il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. Planet = Class {}
  2. function Planet:init(tempX, tempY, tempR, tempM, tempXSpeed, tempYSpeed, tempName, tempC, tempP)
  3. -- Planet Position:
  4. self.x = tempX
  5. self.y = tempY
  6. -- Speed:
  7. self.xSpeed = 0
  8. self.ySpeed = 0
  9. -- Orbital speed:
  10. self.orbitalX = 0
  11. self.orbitalY = 0
  12. -- Speed Change: (throttle 0 - 1; variable) (speed; constant (max speed change))
  13. self.throttle = 0.5
  14. self.speed = 0.05
  15. -- Planet Radius and Mass:
  16. self.r = tempR
  17. self.m = tempM
  18. -- Planet Data:
  19. self.name = tempName
  20. self.colour = tempC
  21. -- Planet Family:
  22. self.children = {}
  23. if (tempP ~= "star") then
  24. self.xSpeed = tempXSpeed
  25. self.ySpeed = tempYSpeed
  26. self.parent = planet[tempP]
  27. table.insert(planet[tempP].children, self)
  28. end
  29. end
  30. -- FUNCTIONS
  31. function Planet:getSpeed()
  32. return math.abs(self.xSpeed) + math.abs(self.ySpeed) + math.abs(self.orbitalY) + math.abs(self.orbitalX)
  33. end
  34. function Planet:updatePosition()
  35. self.x = self.x + self.xSpeed + self.orbitalX
  36. self.y = self.y + self.ySpeed + self.orbitalY
  37. --debug("Updating position of planet " .. self.name .. ": " .. self.x .. " " .. self.y)
  38. end
  39. function Planet:attract(dt) --Planet doing the attracting, divided in two parts:
  40. --Attracting children
  41. for i, child in ipairs(self.children) do
  42. local grav = calc.gPull(self, child)
  43. local dist = calc.distance(self.x, self.y, child.x, child.y)
  44. -- Reworked planetary gravity, now works with multiple layers of parent-children. Also more realistc??
  45. local angle = math.atan((child.y-self.y)/(child.x-self.x))
  46. if self.x < child.x then
  47. angle = angle - 3.14159
  48. end
  49. child.orbitalX = self.xSpeed + self.orbitalX
  50. child.orbitalY = self.ySpeed + self.orbitalY
  51. child.xSpeed = child.xSpeed + grav/child.m*math.cos(angle)*1e9
  52. child.ySpeed = child.ySpeed + grav/child.m*math.sin(angle)*1e9
  53. end
  54. --Attracting the player
  55. -- IF IN SPHERE OF INFLUENCE
  56. local grav = calc.gPull(self, player)
  57. local dist = calc.distance(self.x, self.y, player.x, player.y)
  58. local pull = 20/dist * grav
  59. player.xSpeed = player.xSpeed - (player.x - self.x)*pull
  60. player.ySpeed = player.ySpeed - (player.y - self.y)*pull
  61. end
  62. -- MAIN
  63. function Planet:update(dt)
  64. self:attract(dt)
  65. self:updatePosition()
  66. for i, child in ipairs(self.children) do
  67. child:update(dt)
  68. end
  69. end
  70. function Planet:draw()
  71. local col = self.colour
  72. love.graphics.setColor(calc.c(col[1]), calc.c(col[2]), calc.c(col[3]))
  73. love.graphics.circle("fill", self.x, self.y, self.r)
  74. love.graphics.setColor(0.1,0.1,0.1,0.2)
  75. love.graphics.circle("fill", self.x, self.y, self.r*2)
  76. end