@@ -3,6 +3,7 @@ package game
33import (
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)
126151func 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}
0 commit comments