Skip to content

Commit 574ee03

Browse files
committed
Added moons
1 parent f6c23a1 commit 574ee03

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

‎models/galaxy.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require "models.system"
33
Galaxy = {}
44

55
-- x, y are coordinates in the universe?
6-
Galaxy.new = function(x, y, num_systems)
6+
Galaxy.new = function()
77

88
-- variables
99
local self = {}

‎models/moon.lua‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Moon = {}
2+
3+
Moon.new = function()
4+
5+
-- variables
6+
local self = {}
7+
8+
-- positioning
9+
local x = x or 0
10+
local y = y or 0
11+
local angle = angle or 0
12+
local distance = distance or 0
13+
local speed = speed or 0
14+
15+
-- appearance
16+
local radius = radius or 0
17+
local red = red or 0
18+
local green = green or 0
19+
local blue = blue or 0
20+
21+
-- getters
22+
self.getX = function() return x end
23+
self.getY = function() return y end
24+
self.getAngle = function() return angle end
25+
self.getRadius = function() return radius end
26+
self.getDistance = function() return distance end
27+
self.getSpeed = function() return speed end
28+
self.getRed = function() return red end
29+
self.getGreen = function() return green end
30+
self.getBlue = function() return blue end
31+
32+
-- setters
33+
self.setX = function(arg) x = arg end
34+
self.setY = function(arg) y = arg end
35+
self.setAngle = function(arg) angle = arg end
36+
self.setRadius = function(arg) radius = arg end
37+
self.setDistance = function(arg) distance = arg end
38+
self.setSpeed = function(arg) speed = arg end
39+
self.setRed = function(arg) red = arg end
40+
self.setGreen = function(arg) green = arg end
41+
self.setBlue = function(arg) blue = arg end
42+
43+
-- generation methods
44+
45+
-- default moon
46+
self.generate = function ()
47+
-- generate topography
48+
49+
-- colour
50+
red = 255
51+
green = 255
52+
blue = 255
53+
radius = 10
54+
distance = 50
55+
speed = love.math.random(10,40)*0.001
56+
end
57+
58+
return self
59+
end

‎models/planet.lua‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "helper"
2+
require "models.moon"
23

34
Planet = {}
45

@@ -7,6 +8,7 @@ Planet.new = function(x, y, radius, red, green, blue)
78
-- variables
89
local self = {}
910
local composition = {}
11+
local moons = {}
1012

1113
-- positioning
1214
local x = x or 0
@@ -32,6 +34,7 @@ Planet.new = function(x, y, radius, red, green, blue)
3234
self.getGreen = function() return green end
3335
self.getBlue = function() return blue end
3436
self.getComposition = function () return composition end
37+
self.getMoons = function () return moons end
3538

3639
-- setters
3740
self.setX = function(arg) x = arg end
@@ -44,6 +47,7 @@ Planet.new = function(x, y, radius, red, green, blue)
4447
self.setGreen = function(arg) green = arg end
4548
self.setBlue = function(arg) blue = arg end
4649
self.setComposition = function(arg) composition = arg end
50+
self.setMoons = function(arg) moons = arg end
4751

4852
-- composition
4953
self.generate_composition = function()
@@ -104,6 +108,22 @@ Planet.new = function(x, y, radius, red, green, blue)
104108

105109
-- generate composition
106110
self.setComposition(self.generate_composition())
111+
112+
-- generate moons
113+
local num_moons = love.math.random(0,2)
114+
for i=1,num_moons do
115+
local moon = Moon.new()
116+
moon.generate()
117+
-- the position should match the planet
118+
moon.setX(x)
119+
moon.setY(y)
120+
-- the distance should be the radius of the planet + the radius of the moon + a bit
121+
-- but actually randum
122+
moon.setDistance(love.math.random(radius+moon.getRadius()+10,radius+moon.getRadius()+40))
123+
-- the radius should be a percentage of the planet radius
124+
moon.setRadius(0.25*radius)
125+
table.insert(moons, moon)
126+
end
107127
end
108128

109129
-- primary star

‎models/system.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require "models.planet"
33
System = {}
44

55
-- x, y are coordinates in a galaxy
6-
System.new = function(x, y, num_planets)
6+
System.new = function()
77

88
-- variables
99
local self = {}

‎views/system_view.lua‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,20 @@ ViewSystem.new = function()
3232
local planet = value
3333
love.graphics.setColor(planet.getRed(), planet.getGreen(), planet.getBlue(), 255)
3434
love.graphics.circle("fill", planet.getX(), planet.getY(), planet.getRadius(), 100)
35+
36+
-- draw moons
37+
for k,v in pairs(planet.getMoons()) do
38+
local moon = v
39+
love.graphics.setColor(moon.getRed(), moon.getGreen(), moon.getBlue(), 255)
40+
love.graphics.circle("fill", moon.getX(), moon.getY(), moon.getRadius(), 100)
41+
end
3542
end
3643
end
3744
end
3845

3946
self.update = function(dt)
4047
if is_active then
48+
-- update planets
4149
for key, value in pairs(system.getPlanets()) do
4250
local planet = value
4351
planet.setX((love.graphics.getWidth()/2) + math.sin(planet.getAngle()) * planet.getDistance())
@@ -47,6 +55,18 @@ ViewSystem.new = function()
4755
else
4856
planet.setAngle(0)
4957
end
58+
59+
-- update moons
60+
for k,v in pairs(planet.getMoons()) do
61+
local moon = v
62+
moon.setX(planet.getX() + math.sin(moon.getAngle()) * moon.getDistance())
63+
moon.setY(planet.getY() + math.cos(moon.getAngle()) * moon.getDistance())
64+
if moon.getAngle() < 360 then
65+
moon.setAngle(moon.getAngle()+moon.getSpeed())
66+
else
67+
moon.setAngle(0)
68+
end
69+
end
5070
end
5171
end
5272
end

0 commit comments

Comments
 (0)