Skip to content

Commit 47ec243

Browse files
committed
Toggles for adding and removing masses, fix indexing issues
1 parent 22af687 commit 47ec243

1 file changed

Lines changed: 51 additions & 8 deletions

File tree

fw/game/game.go

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,28 @@ func (sim *Simulation) drawTo(sb *util.ScreenBuffer) {
5555

5656
const maxTraceLen = 30
5757

58+
type Toggle struct {
59+
last, state bool
60+
onPress func()
61+
}
62+
63+
func (t *Toggle) Update(current bool) {
64+
// if the value has changed since last frame
65+
if current && current != t.last {
66+
t.state = !t.state
67+
// and it's now pressed
68+
if t.onPress != nil {
69+
t.onPress()
70+
}
71+
}
72+
t.last = current
73+
}
74+
5875
var (
5976
f int
6077
sim = &Simulation{
6178
dt: 0.06,
62-
scale: 75,
79+
scale: 50,
6380
G: 1,
6481
origin: util.V2(64, 64),
6582
masses: []Mass{
@@ -83,7 +100,26 @@ var (
83100
},
84101
},
85102
}
86-
trace []State
103+
trace []State
104+
toggleAdd = &Toggle{
105+
onPress: func() {
106+
newmass := Mass{
107+
mass: 1,
108+
position: util.V2((float64(f%20)-10)*0.1, (float64((f/20)%20)-10)*0.1),
109+
velocity: util.V2[float64](0, 0),
110+
colour: util.White,
111+
}
112+
sim.masses = append(sim.masses, newmass)
113+
},
114+
}
115+
toggleRemove = &Toggle{
116+
onPress: func() {
117+
if len(sim.masses) == 0 {
118+
return
119+
}
120+
sim.masses = sim.masses[:len(sim.masses)-1]
121+
},
122+
}
87123
)
88124

89125
// ran every frame (or, more like this is what makes the frames)
@@ -110,6 +146,9 @@ func Update(en util.Engine) {
110146
}
111147
}
112148

149+
toggleAdd.Update(btns[3].Pressed())
150+
toggleRemove.Update(btns[1].Pressed())
151+
113152
// update simulation
114153
forces := make([]util.Vector2[float64], len(sim.masses))
115154

@@ -150,17 +189,21 @@ func Update(en util.Engine) {
150189

151190
for i := 1; i < len(trace); i++ {
152191
for j := range sim.masses {
153-
drawLine(en.ScreenBuffer(), trace[i-1][j].Mul(sim.scale).Add(util.V2(float64(sim.origin.X), float64(sim.origin.Y))).Int(),
154-
trace[i][j].Mul(sim.scale).Add(util.V2(float64(sim.origin.X), float64(sim.origin.Y))).Int(),
155-
sim.masses[j].colour)
192+
if j >= len(trace[i-1]) || j >= len(trace[i]) {
193+
continue
194+
}
195+
196+
start := trace[i-1][j].Mul(sim.scale).Add(util.V2(float64(sim.origin.X), float64(sim.origin.Y))).Int()
197+
end := trace[i][j].Mul(sim.scale).Add(util.V2(float64(sim.origin.X), float64(sim.origin.Y))).Int()
198+
drawLine(en.ScreenBuffer(), start, end, sim.masses[j].colour)
156199
}
157200
}
158201

159202
sim.drawTo(en.ScreenBuffer())
160203

161-
// for _, e := range Texts {
162-
// e.drawTo(en.ScreenBuffer())
163-
// }
204+
for _, e := range Texts {
205+
e.drawTo(en.ScreenBuffer())
206+
}
164207

165208
en.Render()
166209
f++

0 commit comments

Comments
 (0)