From 22c14c51992e325604d67855c6517199f6573ae1 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 23 Oct 2023 22:17:11 -0400 Subject: Use tagged vector for bullet pool. --- game.scm | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/game.scm b/game.scm index 925562e..74c4da8 100644 --- a/game.scm +++ b/game.scm @@ -368,38 +368,42 @@ (abort-to-prompt %script-tag delay)) ;; Bullets: + (define-type bullet-pool + %make-bullet-pool + bullet-pool? + (length bullet-pool-length set-bullet-pool-length!) + (capacity bullet-pool-capacity set-bullet-pool-capacity!) + (bullets bullet-pool-bullets set-bullet-pool-bullets!)) ;; per bullet: type, x, y, dx, dy (define %bullet-size (+ 4 8 8 8 8)) (define (make-bullet-pool capacity) - (let ((pool (make-bytevector (* capacity %bullet-size)))) - (vector 0 capacity pool))) + (let ((bullets (make-bytevector (* capacity %bullet-size)))) + (%make-bullet-pool 0 capacity bullets))) (define (bullet-pool-offset i) (* i %bullet-size)) (define (bullet-pool-add! pool type x y dx dy) (match pool - (#(length capacity bullets) + (#('bullet-pool length capacity bullets) (let ((offset (bullet-pool-offset length))) (s32-set! bullets offset type) (f64-set! bullets (+ offset 4) x) (f64-set! bullets (+ offset 12) y) (f64-set! bullets (+ offset 20) dx) (f64-set! bullets (+ offset 28) dy) - (vector-set! pool 0 (+ length 1)))))) + (set-bullet-pool-length! pool (+ length 1)))))) (define (bullet-pool-remove! pool i) (match pool - (#(length capacity bullets) + (#('bullet-pool length capacity bullets) (when (and (>= i 0) (< i length)) (let ((at (bullet-pool-offset i)) (start (bullet-pool-offset (- length 1)))) (bytevector-copy! bullets at bullets start (+ start %bullet-size)) - (vector-set! pool 0 (- length 1))))))) + (set-bullet-pool-length! pool (- length 1))))))) (define (bullet-pool-reset! pool) - (match pool - (#(length capacity bullets) - (vector-set! pool 0 0)))) + (set-bullet-pool-length! pool 0)) (define (bullet-pool-update! pool collide) (match pool - (#(length capacity bullets) + (#('bullet-pool length capacity bullets) (let loop ((i 0) (k length)) (when (< i k) (let* ((offset (bullet-pool-offset i)) @@ -420,7 +424,7 @@ (loop (+ i 1) k))))))))) (define (draw-bullets pool image w h) (match pool - (#(length capacity bullets) + (#('bullet-pool length capacity bullets) (do ((i 0 (+ i 1))) ((= i length)) (let* ((offset (bullet-pool-offset i)) -- cgit v1.2.3