From 4e21f204bfa9f7ad8c949abcb8fe7b13f1325354 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sun, 22 Oct 2023 22:16:06 -0400 Subject: Add state reset. --- game.scm | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/game.scm b/game.scm index 2f18584..8200f3d 100644 --- a/game.scm +++ b/game.scm @@ -281,6 +281,14 @@ (vector-set! scheduler 0 t) (vector-set! scheduler 1 k) to-run)))))))) + (define (scheduler-reset! scheduler) + (match scheduler + (#(ticks num-tasks max-tasks tasks) + (vector-set! scheduler 0 0) + (vector-set! scheduler 1 0) + (do ((i 0 (+ i 1))) + ((= i num-tasks)) + (vector-set! tasks i #f))))) (define *scheduler* (make-scheduler 100)) (define %script-tag (make-prompt-tag "script")) (define (run-script thunk) @@ -333,6 +341,10 @@ (start (bullet-pool-offset (- length 1)))) (bytevector-copy! bullets at bullets start (+ start %bullet-size)) (vector-set! pool 0 (- length 1))))))) + (define (bullet-pool-reset! pool) + (match pool + (#(length capacity bullets) + (vector-set! pool 0 0)))) (define (bullet-pool-update! pool collide) (match pool (#(length capacity bullets) @@ -578,6 +590,15 @@ (let ((offset (enemy-pool-offset i))) (f64-set! enemies (+ offset 40) dx) (f64-set! enemies (+ offset 48) dy))))) + (define (enemy-pool-reset! pool) + (match pool + (#(length capacity enemies scripts) + (do ((i 0 (+ i 1))) + ((= i length)) + (let ((script (vector-ref scripts i))) + (and script (cancel! script)) + (vector-set! scripts i #f))) + (vector-set! pool 0 0)))) (define (enemy-pool-update! pool collide) (match pool (#(length capacity enemies scripts) @@ -798,6 +819,19 @@ (draw-enemy-bullets) (request-animation-frame draw)) + (define (reset!) + (scheduler-reset! *scheduler*) + (set! *scroll* 0.0) + (bullet-pool-reset! player-bullets) + (bullet-pool-reset! enemy-bullets) + (enemy-pool-reset! enemies) + (set-vec2-x! player-position (/ game-width 2.0)) + (set-vec2-y! player-position (- game-height 12.0)) + (set! *player-lives* %default-lives) + (set! *player-invincible?* #f) + (set! *player-visible?* #t) + (set! *player-fire-counter* 0)) + (define (on-key-down event) (let ((code (keyboard-event-code event))) (cond @@ -810,7 +844,9 @@ ((string-=? code "ArrowUp") (set-up! #t)) ((string-=? code "KeyZ") - (set-firing! #t))))) + (set-firing! #t)) + ((string-=? code "KeyR") + (reset!))))) (define (on-key-up event) (let ((code (keyboard-event-code event))) -- cgit v1.2.3