A borderless fullscreen "screensaver" of a galactic travel network.

main.moon 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import graphics from love
  2. import sin, cos from math
  3. import random from love.math
  4. tau = math.pi * 2
  5. local hw, hh
  6. -- hw, hh = graphics.getWidth! / 2, graphics.getHeight! / 2
  7. class System
  8. new: (opts={}) =>
  9. @radius = opts.radius or 0.0000001
  10. @parent = opts.parent or {x: 0, y: 0}
  11. @offset = opts.offset or random! * tau
  12. update: (time) =>
  13. @x = @parent.x + @radius * sin time / @radius + @offset
  14. @y = @parent.y + @radius * cos time / @radius + @offset
  15. draw: =>
  16. graphics.points(@x, @y)
  17. local galaxy
  18. mkGalaxy = ->
  19. galaxy = {}
  20. for i = 1, 2 * math.min hw, hh
  21. table.insert galaxy, System radius: i, offset: 0.2 + random! * (tau - 0.2)
  22. for i = 1, 2 * math.min hw, hh
  23. table.insert galaxy, System radius: i, offset: 0
  24. love.load = ->
  25. love.window.setFullscreen true, "desktop"
  26. hw, hh = graphics.getWidth! / 2, graphics.getHeight! / 2
  27. mkGalaxy!
  28. time = os.time!
  29. speed = 1
  30. love.update = (dt) ->
  31. time += dt * speed
  32. for system in *galaxy
  33. system\update time
  34. dist2 = (a, b) ->
  35. dx = a.x - b.x
  36. dy = a.y - b.y
  37. return dx * dx + dy * dy
  38. love.draw = ->
  39. graphics.translate hw, hh
  40. graphics.scale 0.5, 0.5
  41. for system in *galaxy
  42. system\draw!
  43. -- currentSystem = galaxy[100]
  44. -- dynamic formula (more range further out)
  45. -- d = math.max 10, 0.25^2 * dist2 currentSystem, {x:0, y:0}
  46. -- for system in *galaxy
  47. -- if system != currentSystem
  48. -- if d >= dist2 currentSystem, system
  49. -- graphics.line currentSystem.x, currentSystem.y, system.x, system.y
  50. -- attempted to apply dynamic formula to everything at once, and failed
  51. -- for i = 1, #galaxy - 1
  52. -- a = galaxy[i]
  53. -- d = math.max 10, 0.25^2 * dist2 a, {x:0, y:0}
  54. -- for j = i + 1, #galaxy
  55. -- b = galaxy[j]
  56. -- d = math.min d, 0.25^2 * dist2 b, {x:0, y:0}
  57. -- if d >= dist2 a, b
  58. -- graphics.line a.x, a.y, b.x, b.y
  59. -- basic set formula
  60. for i = 1, #galaxy - 1
  61. a = galaxy[i]
  62. for j = i + 1, #galaxy
  63. b = galaxy[j]
  64. if 50^2 >= dist2 a, b
  65. graphics.line a.x, a.y, b.x, b.y
  66. love.keypressed = (key) ->
  67. if key == "escape"
  68. love.event.quit!
  69. elseif key == "r"
  70. mkGalaxy!
  71. elseif key == "="
  72. speed *= 2
  73. elseif key == "-"
  74. speed /= 2