-
Notifications
You must be signed in to change notification settings - Fork 0
/
astros.js
51 lines (40 loc) · 1.17 KB
/
astros.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Astro {
constructor({name, distance, radius, color, satellites, period}) {
this.name = name
this.distance = distance
this.radius = radius
this.color = color
this.period = period
this.angle = 0
this.satellites = {
instances: [],
data: satellites
}
}
spawnSatellites() {
if (this.satellites.data != null) {
this.satellites.data.forEach(s => {
let satellite = new Astro(s)
this.satellites.instances.push(satellite)
satellite.show()
satellite.spawnSatellites()
})
}
}
orbit() {
this.angle += this.period
this.satellites.instances.forEach(s => s.orbit())
}
show() {
push() // Start new drawing state
noStroke()
fill(this.color)
rotate(this.angle)
translate(this.distance, 0)
ellipse(0, 0, this.radius*2, this.radius*2)
if (this.satellites.instances.length > 0) {
this.satellites.instances.forEach(s => s.show())
}
pop() // Restore original drawing space
}
}