@@ -158,7 +158,27 @@ fn (mut g Game) goto_next_level() {
158
158
g.nlevels++
159
159
}
160
160
161
+ fn (mut g Game) move (k f32 ) {
162
+ if k < 0 {
163
+ if g.paddle_x < = 0 {
164
+ return
165
+ }
166
+ } else if k > 0 {
167
+ if g.paddle_x > = g.width - g.paddle_w {
168
+ return
169
+ }
170
+ }
171
+ g.paddle_x + = k * g.paddle_dx
172
+ }
173
+
161
174
fn (mut g Game) update () {
175
+ if g.ctx.pressed_keys[gg.KeyCode.left] {
176
+ g.move (- 1.0 )
177
+ }
178
+ if g.ctx.pressed_keys[gg.KeyCode.right] {
179
+ g.move (1.0 )
180
+ }
181
+ //
162
182
g.ball_x, g.ball_y = g.ball_x + g.ball_dx, g.ball_y + g.ball_dy
163
183
// Wall collisions
164
184
if g.ball_x < g.ball_r || g.ball_x > g.width - g.ball_r {
@@ -211,31 +231,13 @@ fn (mut g Game) update() {
211
231
}
212
232
}
213
233
214
- fn frame (mut g Game) {
215
- if g.ctx.pressed_keys[gg.KeyCode.left] {
216
- if g.paddle_x > 0 {
217
- g.paddle_x - = g.paddle_dx
218
- }
219
- }
220
- if g.ctx.pressed_keys[gg.KeyCode.right] {
221
- if g.paddle_x < g.width - g.paddle_w {
222
- g.paddle_x + = g.paddle_dx
223
- }
224
- }
225
- g.update ()
226
- g.draw ()
227
- }
228
-
229
- fn key_down (key gg.KeyCode, _ gg.Modifier, mut g Game) {
230
- g.handle_event ()
231
- match key {
232
- .r {
233
- g.game_over ()
234
- }
235
- .escape {
236
- exit (0 )
237
- }
238
- else {}
234
+ fn (mut g Game) touch_event (touch_point gg.TouchPoint) {
235
+ ws := gg.window_size ()
236
+ tx := touch_point.pos_x
237
+ if tx < = f32 (ws.width) * 0.5 {
238
+ g.move (- 1.0 )
239
+ } else {
240
+ g.move (1.0 )
239
241
}
240
242
}
241
243
@@ -244,7 +246,8 @@ fn (mut g Game) handle_event() {
244
246
if g.nevent > 0 {
245
247
return
246
248
}
247
- // the audio has to be started when the wasm canvas has received user interaction, unlike on desktop platforms
249
+ // the audio has to be started when the wasm canvas has received user
250
+ // interaction, unlike on desktop platforms
248
251
audio.setup (buffer_frames: 1024 )
249
252
g.sound.init ()
250
253
g.nevent++
@@ -254,7 +257,7 @@ fn main() {
254
257
mut g := Game.new ()
255
258
mut fpath := asset.get_path ('../assets' , 'fonts/RobotoMono-Regular.ttf' )
256
259
$if ! wasm32_ emscripten {
257
- audio.setup (buffer_frames: 128 )
260
+ audio.setup (buffer_frames: 512 ) // too small values lead to cracking sounds or no sound at all on macos
258
261
g.sound.init ()
259
262
fpath = ''
260
263
}
@@ -263,11 +266,34 @@ fn main() {
263
266
height: g.height
264
267
window_title: 'V Breakout'
265
268
sample_count: 2
266
- frame_fn: frame
267
- keydown_fn: key_down
269
+ frame_fn: fn (mut g Game) {
270
+ g.update ()
271
+ g.draw ()
272
+ }
268
273
click_fn: fn (x f32 , y f32 , btn gg.MouseButton, mut g Game) {
269
274
g.handle_event ()
270
275
}
276
+ event_fn: fn (e & gg.Event, mut g Game) {
277
+ g.handle_event ()
278
+ if e.typ == .touches_began || e.typ == .touches_moved {
279
+ if e.num_touches > 0 {
280
+ touch_point := e.touches[0 ]
281
+ g.touch_event (touch_point)
282
+ }
283
+ }
284
+ }
285
+ keydown_fn: fn (key gg.KeyCode, _ gg.Modifier, mut g Game) {
286
+ g.handle_event ()
287
+ match key {
288
+ .r {
289
+ g.game_over ()
290
+ }
291
+ .escape {
292
+ exit (0 )
293
+ }
294
+ else {}
295
+ }
296
+ }
271
297
user_data: g
272
298
font_path: fpath
273
299
)
0 commit comments