diff options
-rw-r--r-- | game.scm | 82 |
1 files changed, 51 insertions, 31 deletions
@@ -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) |