gr_eg

solarSystem_love2D

Jun 14th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 KB | None | 0 0
  1.  
  2. -- Génération de la graine
  3. math.randomseed(os.time())
  4.  
  5.  
  6. -- Les constantes de la fenêtre
  7. local WIDTH, HEIGHT = 800, 600
  8. local XC, YC = WIDTH / 2, HEIGHT / 2
  9.  
  10. -- Modifiacation de la taille de la fenêtre
  11. love.window.setMode(WIDTH, HEIGHT)
  12.  
  13.  
  14. -- Le soleil
  15. local sun = {x = XC, y = YC, r = 50, color = {1, 0.81, 0.34, 1}}
  16.  
  17.  
  18. -- Les limites pour l'aléatoire
  19. local rMin, rMax = math.floor(sun.r / 10), math.floor(sun.r / 4) -- Les limites du rayon INFLUENCE BEAUCOUP le nombre de planètes à l'écran
  20. local vMin, vMax = 10, 50  -- vitesse en degré (qui sera convertie en radians lors du tirage)
  21.  
  22.  
  23. -- Le compteur pour éviter une boucle infinie
  24. local securityCounter = 5000
  25.  
  26.  
  27. -- La liste des planètes et le nombre attendu
  28. local listPlanets = {}
  29. local nbPlanets = 8
  30.  
  31.  
  32.  
  33. -- La boucle de création des planètes
  34. while #listPlanets ~= nbPlanets do
  35.  
  36.   -- MAJ du compteur
  37.   securityCounter = securityCounter - 1
  38.   if securityCounter == 0 then
  39.     break
  40.   end
  41.  
  42.  
  43.   -- Création d'une nouvell planète
  44.   local newPlanet = {}
  45.   newPlanet.r = math.random(rMin, rMax)
  46.   newPlanet.x = math.random(newPlanet.r, WIDTH - newPlanet.r)
  47.   newPlanet.y = math.random(newPlanet.r, HEIGHT - newPlanet.r)
  48.  
  49.   -- Ajout de la distane Soleil - planète
  50.   newPlanet.d = ((newPlanet.x - sun.x)^2 + (newPlanet.y - sun.y)^2)^0.5
  51.  
  52.  
  53.   -- En considérant que le soleil se trouve au centre de la fenêtre, cette distance doit être:
  54.                   -- supérieure au rayon du soleil
  55.                   -- inférieure à la plus petite moitié de dimension de la fenêtre            
  56.   local goodDistance = true
  57.   if newPlanet.d <= (newPlanet.r + sun.r) or (newPlanet.d + newPlanet.r) >= math.min(WIDTH / 2, HEIGHT / 2) then
  58.     goodDistance = false
  59.   end
  60.  
  61.   -- Si la distance est correcte ...
  62.   if goodDistance then
  63.  
  64.     -- ... on vérifie que la planète n'entre pas en collision avec celles déjà créées
  65.     local collision = false
  66.     for _, planet in ipairs(listPlanets) do
  67.       local d = math.abs(planet.d - newPlanet.d)
  68.       if d <= (planet.r + newPlanet.r) then
  69.         collision = true
  70.         break
  71.       end
  72.     end
  73.  
  74.     -- S'il n'y a pas de collision, on 'officialise' la planète ...
  75.     if collision == false then
  76.       newPlanet.v = math.rad(math.random(vMin, vMax))
  77.       newPlanet.angle = math.atan2(sun.y - newPlanet.y, sun.x - newPlanet.x)  -- https://love2d.org/wiki/General_math
  78.       newPlanet.color = {math.random(), math.random(), math.random(), 1}
  79.      
  80.       -- ... et on l'ajoute à la liste
  81.       table.insert(listPlanets, newPlanet)
  82.     end
  83.   end
  84. end
  85.  
  86.  
  87.  
  88. function love.update(dt)
  89.   for _, p in ipairs(listPlanets) do
  90.     p.angle = p.angle + (p.v * dt)
  91.     p.x, p.y = p.d * math.cos(p.angle) + sun.x, p.d * math.sin(p.angle) + sun.y
  92.   end
  93. end
  94.  
  95.  
  96.  
  97. function love.draw()
  98.   love.graphics.setColor(1, 1, 1, 1)
  99.   love.graphics.print("Nombre de planètes: " .. #listPlanets, 10, 10)
  100.   love.graphics.print("Compteur sécurité: " .. securityCounter, 10, 30)
  101.  
  102.   -- Affichage du soleil
  103.   love.graphics.setColor(sun.color)
  104.   love.graphics.circle("fill", sun.x, sun.y, sun.r)
  105.  
  106.   -- Affichage des trajectoires & planètes
  107.   for i, p in ipairs(listPlanets) do
  108.  
  109.     -- Trajectoires
  110.     love.graphics.setColor(0.3, 0.3, 0.3, 0.5)
  111.     love.graphics.circle("line", sun.x, sun.y, p.d)
  112.  
  113.     -- Planètes
  114.     love.graphics.setColor(p.color)
  115.     love.graphics.circle("fill", p.x, p.y, p.r)
  116.  
  117.     -- -- Le numéro de création
  118.     -- love.graphics.setColor(1, 1, 1, 1)
  119.     -- love.graphics.print(i, p.x, p.y)
  120.   end
  121. end
Add Comment
Please, Sign In to add comment