summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2023-10-22 21:02:03 -0400
committerDavid Thompson <dthompson2@worcester.edu>2023-10-22 21:02:03 -0400
commit89db62cd6c3db6b0df829d881ba606ab90c0170c (patch)
treea5407c7e51306829fc7f9746a623f111ce838535
parentc423a0a68b4c3c4031c042d45db17ab554b1fb4d (diff)
Fix enemy collision, collide player with enemies.
-rw-r--r--game.scm82
1 files changed, 51 insertions, 31 deletions
diff --git a/game.scm b/game.scm
index 6f51d5a..7647e68 100644
--- a/game.scm
+++ b/game.scm
@@ -203,8 +203,8 @@
(define (within? x y rx ry rw rh)
(and (>= x rx)
(>= y ry)
- (<= x (+ rx rw))
- (<= y (+ ry rh))))
+ (< x (+ rx rw))
+ (< y (+ ry rh))))
(define (rect-within? ax ay aw ah bx by bw bh)
(let ((ax* (+ ax aw))
(ay* (+ ay ah)))
@@ -523,20 +523,29 @@
(t (s32-ref enemies offset))
(x (f64-ref enemies (+ offset 8)))
(y (f64-ref enemies (+ offset 16)))
+ (hbw (f64-ref enemies (+ offset 24)))
+ (hbh (f64-ref enemies (+ offset 32)))
(w 64.0)
(h 64.0))
(draw-image context image:enemies (* t w) (* t h) w h
- (- x (/ w 2.0)) (- y (/ h 2.0)) w h))))))
+ (- x (/ w 2.0)) (- y (/ h 2.0)) w h)
+ (set-fill-color! context "#ff00ff")
+ (fill-rect context
+ (- x (/ hbw 2.0))
+ (- y (/ hbh 2.0))
+ hbw hbh))))))
(define (find-enemy pool x y w h)
(match pool
(#(length capacity enemies)
(let loop ((i 0))
(and (< i length)
(let* ((offset (enemy-pool-offset i))
- (x* (f64-ref enemies (+ offset 8)))
- (y* (f64-ref enemies (+ offset 16)))
(w* (f64-ref enemies (+ offset 24)))
- (h* (f64-ref enemies (+ offset 32))))
+ (h* (f64-ref enemies (+ offset 32)))
+ (x* (- (f64-ref enemies (+ offset 8))
+ (/ w* 2.0)))
+ (y* (- (f64-ref enemies (+ offset 16))
+ (/ h* 2.0))))
(if (rect-within? x y w h x* y* w* h*)
i
(loop (+ i 1)))))))))
@@ -560,6 +569,7 @@
(define player-height 24.0)
(define *player-fire-counter* 0)
(define player-fire-interval 3)
+ (define player-hitbox-position (vec2 0.0 0.0))
(define player-hitbox-width 2.0)
(define player-hitbox-height 2.0)
(define %default-lives 3)
@@ -614,6 +624,38 @@
(set! *player-invincible?* #f))))
(define (game-over?)
(= *player-lives* 0))
+ (define (player-update!)
+ (vec2-add! player-position player-velocity)
+ (vec2-clamp! player-position 0.0 0.0 game-width game-height)
+ (set-vec2-x! player-hitbox-position
+ (- (vec2-x player-position)
+ (/ player-hitbox-width 2.0)))
+ (set-vec2-y! player-hitbox-position
+ (- (vec2-y player-position)
+ (/ player-hitbox-height 2.0)))
+ (when (and (not *player-invincible?*)
+ (let ((x (vec2-x player-hitbox-position))
+ (y (vec2-y player-hitbox-position))
+ (w player-hitbox-width)
+ (h player-hitbox-height))
+ (or (rect-collides-with-level? level x y w h)
+ (find-enemy enemies x y w h))))
+ (player-die!)))
+ (define (draw-player)
+ (draw-image context image:player
+ (if *player-visible?* 0.0 player-width) 0.0
+ player-width player-height
+ (- (vec2-x player-position)
+ (/ player-width 2.0))
+ (- (vec2-y player-position)
+ (/ player-height 2.0))
+ player-width player-height)
+ (set-fill-color! context "#ff00ff")
+ (fill-rect context
+ (vec2-x player-hitbox-position)
+ (vec2-y player-hitbox-position)
+ player-hitbox-width
+ player-hitbox-height))
(define (clear-screen)
(clear-rect context 0.0 0.0 canvas-width canvas-height))
@@ -624,16 +666,6 @@
(define (draw-enemy-bullets)
(draw-bullets enemy-bullets image:enemy-bullets 16.0 16.0))
- (define (draw-player)
- (draw-image context image:player
- (if *player-visible?* 0.0 player-width) 0.0
- player-width player-height
- (- (vec2-x player-position)
- (/ player-width 2.0))
- (- (vec2-y player-position)
- (/ player-height 2.0))
- player-width player-height))
-
(define (draw time)
(clear-screen)
(set-transform! context 1.0 0.0 0.0 1.0 0.0 0.0)
@@ -698,10 +730,8 @@
(or (out-of-bounds? x* y* w h)
(rect-collides-with-level? level x* y* w h)
(if (rect-within? x y w h
- (- (vec2-x player-position)
- (/ player-hitbox-width 2.0))
- (- (vec2-y player-position)
- (/ player-hitbox-height 2.0))
+ (vec2-x player-hitbox-position)
+ (vec2-y player-hitbox-position)
player-hitbox-width
player-hitbox-height)
(begin
@@ -716,17 +746,7 @@
(define (update)
(scheduler-tick! *scheduler*)
(update-scroll!)
- (vec2-add! player-position player-velocity)
- (vec2-clamp! player-position 0.0 0.0 game-width game-height)
- (when (and (not *player-invincible?*)
- (rect-collides-with-level? level
- (- (vec2-x player-position)
- (/ player-hitbox-width 2.0))
- (- (vec2-y player-position)
- (/ player-hitbox-height 2.0))
- player-hitbox-width
- player-hitbox-height))
- (player-die!))
+ (player-update!)
(bullet-pool-update! player-bullets player-bullet-collide)
(bullet-pool-update! enemy-bullets enemy-bullet-collide)
(enemy-pool-update! enemies enemy-collide)