Skip to content

Commit 0c8354d

Browse files
committed
Place masses with random velocity, add crosshairs in the center of the screen
1 parent 47ec243 commit 0c8354d

3 files changed

Lines changed: 85 additions & 17 deletions

File tree

fw/game/game.go

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package game
33
import (
44
"fw/util"
55
"math"
6+
"math/rand/v2"
67
"runtime"
78
"strconv"
89
"time"
@@ -103,11 +104,17 @@ var (
103104
trace []State
104105
toggleAdd = &Toggle{
105106
onPress: func() {
107+
colours := []util.Pixel{util.Red, util.Green, util.Blue, util.White, util.Yellow, util.Cyan, util.Magenta, util.Grey2, util.Grey3}
108+
c := colours[len(sim.masses)%len(colours)]
109+
110+
// centre on window crosshairs
111+
centre := util.V2[float64](util.Width/2, util.Height/2).Sub(sim.origin.Float64()).Div(sim.scale)
112+
106113
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,
114+
mass: rand.Float64()*1.5 + 0.5,
115+
position: centre,
116+
velocity: util.V2(rand.Float64()*2-1, rand.Float64()*2-1).Mul(0.3),
117+
colour: c,
111118
}
112119
sim.masses = append(sim.masses, newmass)
113120
},
@@ -122,21 +129,28 @@ var (
122129
}
123130
)
124131

132+
const (
133+
hOffset = 15
134+
vOffset = 18
135+
)
136+
137+
var Texts = [util.ButtonsCount]*Text{
138+
{FontDex, "W", util.V2(2+hOffset-2, 2), util.Red},
139+
{FontDex, "A", util.V2(2, 2+vOffset), util.Red},
140+
{FontDex, "S", util.V2(2+hOffset, 2+vOffset*2), util.Red},
141+
{FontDex, "D", util.V2(2+hOffset*2, 2+vOffset), util.Red},
142+
{FontDex, "I", util.V2(2+util.Width-hOffset*2, 2), util.Red},
143+
{FontDex, "J", util.V2(2+util.Width-hOffset*3, 2+vOffset), util.Red},
144+
{FontDex, "K", util.V2(2+util.Width-hOffset*2, 2+vOffset*2), util.Red},
145+
{FontDex, "L", util.V2(2+util.Width-hOffset, 2+vOffset), util.Red},
146+
}
147+
148+
var Crosshairs = &Crosshair{util.V2(util.Width/2, util.Height/2), util.Grey3, 3}
149+
125150
// ran every frame (or, more like this is what makes the frames)
126151
func Update(en util.Engine) {
127152
btns := en.Buttons()
128153

129-
Texts := [util.ButtonsCount]*Text{
130-
{FontDex, "W", util.V2(2+20-2, 2), util.Red},
131-
{FontDex, "A", util.V2(2, 26), util.Red},
132-
{FontDex, "S", util.V2(2+20, 50), util.Red},
133-
{FontDex, "D", util.V2(2+40, 26), util.Red},
134-
{FontDex, "I", util.V2(109+20+1, 2), util.Red},
135-
{FontDex, "J", util.V2(109, 26), util.Red},
136-
{FontDex, "K", util.V2(109+20, 50), util.Red},
137-
{FontDex, "L", util.V2(109+40, 26), util.Red},
138-
}
139-
140154
// read button states
141155
for i, b := range btns {
142156
if b.Pressed() {
@@ -146,8 +160,29 @@ func Update(en util.Engine) {
146160
}
147161
}
148162

149-
toggleAdd.Update(btns[3].Pressed())
150-
toggleRemove.Update(btns[1].Pressed())
163+
if btns[4].Pressed() {
164+
sim.dt += 0.001
165+
}
166+
167+
if btns[6].Pressed() {
168+
sim.dt -= 0.001
169+
}
170+
171+
if btns[2].Pressed() {
172+
sim.origin = sim.origin.Add(util.V2(0, -2))
173+
}
174+
if btns[0].Pressed() {
175+
sim.origin = sim.origin.Add(util.V2(0, 2))
176+
}
177+
if btns[1].Pressed() {
178+
sim.origin = sim.origin.Add(util.V2(2, 0))
179+
}
180+
if btns[3].Pressed() {
181+
sim.origin = sim.origin.Add(util.V2(-2, 0))
182+
}
183+
184+
toggleAdd.Update(btns[7].Pressed())
185+
toggleRemove.Update(btns[5].Pressed())
151186

152187
// update simulation
153188
forces := make([]util.Vector2[float64], len(sim.masses))
@@ -205,6 +240,8 @@ func Update(en util.Engine) {
205240
e.drawTo(en.ScreenBuffer())
206241
}
207242

243+
Crosshairs.drawTo(en.ScreenBuffer())
244+
208245
en.Render()
209246
f++
210247
}

fw/game/tools.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ func abs(x int) int {
7777
return x
7878
}
7979

80+
func drawVLine(buf *util.ScreenBuffer, x, y1, y2 int, colour util.Pixel) {
81+
for y := y1; y <= y2; y++ {
82+
buf.Set(x, y, colour)
83+
}
84+
}
85+
86+
func drawHLine(buf *util.ScreenBuffer, y, x1, x2 int, colour util.Pixel) {
87+
for x := x1; x <= x2; x++ {
88+
buf.Set(x, y, colour)
89+
}
90+
}
91+
8092
func drawLine(buf *util.ScreenBuffer, p1, p2 util.Vector2[int], colour util.Pixel) {
8193
dx := abs(p2.X - p1.X)
8294
dy := -abs(p2.Y - p1.Y)
@@ -468,3 +480,13 @@ type Mass struct {
468480
colour util.Pixel
469481
}
470482

483+
type Crosshair struct {
484+
pos util.Vector2[int]
485+
colour util.Pixel
486+
size int
487+
}
488+
489+
func (c *Crosshair) drawTo(buf *util.ScreenBuffer) {
490+
drawHLine(buf, c.pos.Y, c.pos.X-c.size, c.pos.X+c.size, c.colour)
491+
drawVLine(buf, c.pos.X, c.pos.Y-c.size, c.pos.Y+c.size, c.colour)
492+
}

fw/util/util.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ var (
7272

7373
Grey1 = MakePixel(0x20, 0x20, 0x20)
7474
Grey2 = MakePixel(0x40, 0x40, 0x40)
75+
Grey3 = MakePixel(0x80, 0x80, 0x80)
7576
)
7677

7778
type buttonId uint8
@@ -141,6 +142,10 @@ func (v Vector2[T]) Div(scalar float64) Vector2[T] {
141142
return Vector2[T]{X: T(float64(v.X) / scalar), Y: T(float64(v.Y) / scalar)}
142143
}
143144

145+
func (v Vector2[T]) Neg() Vector2[T] {
146+
return Vector2[T]{X: -v.X, Y: -v.Y}
147+
}
148+
144149
func (v Vector2[T]) Len() T {
145150
x2 := v.X * v.X
146151
y2 := v.Y * v.Y
@@ -150,3 +155,7 @@ func (v Vector2[T]) Len() T {
150155
func (v Vector2[float64]) Int() Vector2[int] {
151156
return V2(int(v.X), int(v.Y))
152157
}
158+
159+
func (v Vector2[int]) Float64() Vector2[float64] {
160+
return V2(float64(v.X), float64(v.Y))
161+
}

0 commit comments

Comments
 (0)