diff options
author | David Thompson <dthompson2@worcester.edu> | 2023-10-26 08:17:09 -0400 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2023-10-26 08:17:09 -0400 |
commit | 682838d519749d379b0d3fcce575abbc75787478 (patch) | |
tree | 22cf96abb88c38407bc8af633ae84c63948c2e44 | |
parent | e641e20d037b7a135b7bea19e205660f1426d3cf (diff) |
Enemy animation.
-rw-r--r-- | game.scm | 69 |
1 files changed, 41 insertions, 28 deletions
@@ -463,7 +463,7 @@ (y (f64-ref bullets (+ offset 20))) (w (f64-ref bullets (+ offset 28))) (h (f64-ref bullets (+ offset 36)))) - (draw-image context image (pk tx) 0.0 + (draw-image context image tx 0.0 bullet-tile-width bullet-tile-height (- x (/ bullet-tile-width 2.0)) (- y (/ bullet-tile-height 2.0)) @@ -582,7 +582,7 @@ ;; Enemies (define-type enemy - make-enemy + %make-enemy enemy? (type enemy-type set-enemy-type!) (health enemy-health set-enemy-health!) @@ -591,7 +591,14 @@ (stationary? enemy-stationary? set-enemy-stationary!) (velocity enemy-velocity set-enemy-velocity!) (script enemy-script set-enemy-script!) - (points enemy-points set-enemy-points!)) + (points enemy-points set-enemy-points!) + (spawn-time enemy-spawn-time set-enemy-spawn-time!) + (animation enemy-animation set-enemy-animation!) + (image enemy-image set-enemy-image!)) + (define (make-enemy type health position size stationary? velocity + script points animation image) + (%make-enemy type health position size stationary? velocity script + points (inexact (current-jiffy)) animation image)) (define (enemy-x enemy) (vec2-x (enemy-position enemy))) (define (enemy-y enemy) @@ -606,18 +613,18 @@ (vec2-y (enemy-velocity enemy))) (define (enemy-damage! enemy damage) (match enemy - (#('enemy type health _ _ _ _ _ _) + (#('enemy type health _ _ _ _ _ _ _ _ _) (set-enemy-health! enemy (- health damage))))) (define (enemy-dead? enemy) (<= (enemy-health enemy) 0)) (define (enemy-out-of-bounds? enemy) (match enemy - (#('enemy _ _ position size _ _ _ _) + (#('enemy _ _ position size _ _ _ _ _ _ _) (out-of-bounds? (vec2-x position) (vec2-y position) (vec2-x size) (vec2-y size))))) (define (enemy-within-rect? enemy x y w h) (match enemy - (#('enemy _ _ position size _ _ _ _) + (#('enemy _ _ position size _ _ _ _ _ _ _) (let* ((w* (vec2-x size)) (h* (vec2-y size)) (x* (- (vec2-x position) (/ w* 2.0))) @@ -633,29 +640,34 @@ (script-cancel! script)))) (define (enemy-update! enemy) (match enemy - (#('enemy _ _ position size stationary? velocity _ _) + (#('enemy _ _ position size stationary? velocity _ _ _ _ _) (if stationary? (set-vec2-y! position (+ (vec2-y position) (- *scroll* *last-scroll*))) (begin (set-vec2-x! position (+ (vec2-x position) (vec2-x velocity))) (set-vec2-y! position (+ (vec2-y position) (vec2-y velocity)))))))) - (define (enemy-draw enemy) - (match enemy - (#('enemy type _ position size _ _ _ _) - (let* ((t 0.0) - (x (vec2-x position)) - (y (vec2-y position)) - (hbw (vec2-x size)) - (hbh (vec2-y size)) - (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) - (set-fill-color! context "#ff00ff80") - (fill-rect context - (- x (/ hbw 2.0)) - (- y (/ hbh 2.0)) - hbw hbh))))) + (define (draw-enemy enemy time) + (let ((frame-duration 250.0)) + (match enemy + (#('enemy type _ position size _ _ _ _ spawn-time animation image) + (let* ((tx (vector-ref animation + (modulo (trunc + (remainder (- time spawn-time) + frame-duration)) + (vector-length animation)))) + (x (vec2-x position)) + (y (vec2-y position)) + (hbw (vec2-x size)) + (hbh (vec2-y size)) + (w 64.0) + (h 64.0)) + (draw-image context image tx 0.0 w h + (- x (/ w 2.0)) (- y (/ h 2.0)) w h) + (set-fill-color! context "#ff00ff80") + (fill-rect context + (- x (/ hbw 2.0)) + (- y (/ hbh 2.0)) + hbw hbh)))))) (define-type enemy-pool %make-enemy-pool @@ -709,12 +721,12 @@ (loop i (- k 1))) (else (loop (+ i 1) k)))))))))) - (define (draw-enemies pool) + (define (draw-enemies pool time) (match pool (#('enemy-pool length capacity enemies) (do ((i 0 (+ i 1))) ((= i length)) - (enemy-draw (vector-ref enemies i)))))) + (draw-enemy (vector-ref enemies i) time))))) (define (find-enemy pool x y w h) (match pool (#('enemy-pool length capacity enemies) @@ -744,7 +756,8 @@ (wait 30) (loop (+ theta 0.2))))) (let ((enemy (make-enemy 'foo 20 (vec2 x y) (vec2 16.0 16.0) - #t (vec2 0.0 0.0) script 100))) + #t (vec2 0.0 0.0) script 100 + #(0.0 0.0 0.0 0.0) image:enemies))) (enemy-pool-add! enemies enemy))) ;; Player state: @@ -952,7 +965,7 @@ (draw-background image:background 0.75) (draw-level-foreground level) (draw-player-bullets) - (draw-enemies enemies) + (draw-enemies enemies time) (draw-player) (draw-enemy-bullets) (draw-hud) |