summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2023-10-23 22:32:20 -0400
committerDavid Thompson <dthompson2@worcester.edu>2023-10-23 22:32:20 -0400
commitee937abcfeb7bd65617790936bb6ee25a2b1760a (patch)
treef126c7b97d37653a5e73600c898c7d0ba59d7c57
parent22c14c51992e325604d67855c6517199f6573ae1 (diff)
Use tagged vector for level.
-rw-r--r--game.scm20
1 files changed, 11 insertions, 9 deletions
diff --git a/game.scm b/game.scm
index 74c4da8..a4c646c 100644
--- a/game.scm
+++ b/game.scm
@@ -446,6 +446,11 @@
(define tile-width 16.0)
(define tile-height 16.0)
(define level-width 15)
+ (define-type level
+ %make-level
+ level?
+ (height level-height set-level-height!)
+ (tiles level-tiles set-level-tiles!))
(define (make-level tiles)
(let ((k (length tiles)))
(unless (= (modulo k level-width) 0)
@@ -478,10 +483,7 @@
(x-loop rest (+ x 1))))
tiles))
(+ y 1)))))
- (vector (/ k level-width) bv))))
- (define (level-height level)
- (match level
- (#(height tiles) height)))
+ (%make-level (/ k level-width) bv))))
(define-syntax-rule (define-level name tile ...)
(define name (make-level '(tile ...))))
(define-level level
@@ -542,7 +544,7 @@
(* (+ (* level-width y) x) %tile-size))
(define (point-collides-with-level? level x y)
(match level
- (#(height tiles)
+ (#('level height tiles)
(let ((tx (trunc (/ x tile-width)))
(ty (trunc (/ y tile-height))))
(and (>= tx 0) (< tx level-width)
@@ -550,7 +552,7 @@
(>= (f64-ref tiles (level-offset tx ty)) 0))))))
(define (rect-collides-with-level? level x y w h)
(match level
- (#(height tiles)
+ (#('level height tiles)
(let* ((y (+ y (- (* height tile-height) game-height *scroll*)))
(tx0 (trunc (/ x tile-width)))
(ty0 (trunc (/ y tile-height)))
@@ -566,7 +568,7 @@
(occupied? tx0 ty1))))))
(define (draw-tiles level)
(match level
- (#(height tiles)
+ (#('level height tiles)
(let* ((tw tile-width)
(th tile-height)
(pixel-y-offset (- (* height th) *scroll* game-height))
@@ -584,10 +586,10 @@
(draw-image context image:map
(* t tw) 0.0 tw th
tx (- ty pixel-y-offset) tw th))))))))
- (define max-scroll (- (* (vector-ref level 0) tile-height) game-height))
+ (define max-scroll (- (* (level-height level) tile-height) game-height))
(define (level-update! level)
(match level
- (#(height tiles)
+ (#('level height tiles)
(let ((scroll (min (+ *scroll* *scroll-speed*) max-scroll)))
(set! *scroll* scroll)
(let ((row (max (- (trunc