From 150fa89765c71a83c264bed0b55a1851745d3b70 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Wed, 25 Oct 2023 21:45:10 -0400 Subject: Add focus fire. --- game.scm | 104 ++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 66 insertions(+), 38 deletions(-) (limited to 'game.scm') diff --git a/game.scm b/game.scm index 7d8a6a3..cf72933 100644 --- a/game.scm +++ b/game.scm @@ -301,7 +301,7 @@ (define context (get-context canvas "2d")) (define image:background (load-image "images/background.png")) (define image:player (load-image "images/player.png")) - (define image:player-bullet (load-image "images/player-bullet.png")) + (define image:player-bullets (load-image "images/player-bullets.png")) (define image:enemy-bullets (load-image "images/enemy-bullets.png")) (define image:map (load-image "images/map.png")) (define image:enemies (load-image "images/enemies.png")) @@ -454,7 +454,7 @@ (type (s32-ref bullets offset)) (x (f64-ref bullets (+ offset 4))) (y (f64-ref bullets (+ offset 12)))) - (draw-image context image (* type w) (* type h) w h + (draw-image context image (* type w) 0.0 w h (- x (/ w 2.0)) (- y (/ w 2.0)) w h)))))) (define player-bullets (make-bullet-pool 200)) @@ -735,11 +735,13 @@ (define player-position (vec2 (/ game-width 2.0) (- game-height 12.0))) (define player-velocity (vec2 0.0 0.0)) (define player-speed 2.9) - (define player-bullet-speed 12.0) + (define player-focus-speed 1.5) + (define player-bullet-speed 12.3) (define player-width 24.0) (define player-height 24.0) (define *player-fire-counter* 0) (define player-fire-interval 3) + (define player-focus-fire-interval 2) (define player-hitbox-position (vec2 0.0 0.0)) (define player-hitbox-width 2.0) (define player-hitbox-height 2.0) @@ -747,11 +749,11 @@ (define *player-lives* %default-lives) (define *player-visible?* #t) (define *player-invincible?* #f) - ;; left, right, down, up, fire - (define key-state (vector #f #f #f #f #f)) + ;; left, right, down, up, fire, focus + (define key-state (vector #f #f #f #f #f #f)) (define (update-player-velocity!) (match key-state - (#(left? right? down? up? _) + (#(left? right? down? up? _ _) (set-vec2-x! player-velocity (+ (if left? -1.0 0.0) (if right? 1.0 0.0))) @@ -759,7 +761,10 @@ (+ (if down? 1.0 0.0) (if up? -1.0 0.0))) (vec2-normalize! player-velocity) - (vec2-mul-scalar! player-velocity player-speed)))) + (vec2-mul-scalar! player-velocity + (if (focusing?) + player-focus-speed + player-speed))))) (define (set-left! pressed?) (vector-set! key-state 0 pressed?) (update-player-velocity!)) @@ -772,13 +777,21 @@ (define (set-up! pressed?) (vector-set! key-state 3 pressed?) (update-player-velocity!)) + (define (firing?) + (vector-ref key-state 4)) (define (set-firing! pressed?) (let ((was-firing? (firing?))) (vector-set! key-state 4 pressed?) (when (and pressed? (not was-firing?)) (set! *player-fire-counter* 0)))) - (define (firing?) - (vector-ref key-state 4)) + (define (focusing?) + (vector-ref key-state 5)) + (define (set-focusing! pressed?) + (let ((was-focusing? (focusing?))) + (vector-set! key-state 5 pressed?) + (when (and pressed? (not was-focusing?)) + (set! *player-fire-counter* 0) + (update-player-velocity!)))) (define (player-die!) (unless *player-invincible?* ;; (sound-effect-play sound:player-death) @@ -815,17 +828,26 @@ (player-die!)) (when (firing?) (set! *player-fire-counter* - (modulo (+ *player-fire-counter* 1) player-fire-interval)) + (modulo (+ *player-fire-counter* 1) + (if (focusing?) + player-focus-fire-interval + player-fire-interval))) (when (= *player-fire-counter* 0) (sound-effect-play sound:player-shoot 0.2) - (bullet-pool-add! player-bullets 0 - (- (vec2-x player-position) 6.0) - (vec2-y player-position) - 0.0 (- player-bullet-speed)) - (bullet-pool-add! player-bullets 0 - (+ (vec2-x player-position) 8.0) - (vec2-y player-position) - 0.0 (- player-bullet-speed)) + (if (focusing?) + (bullet-pool-add! player-bullets 1 + (vec2-x player-position) + (vec2-y player-position) + 0.0 (- player-bullet-speed)) + (begin + (bullet-pool-add! player-bullets 0 + (- (vec2-x player-position) 6.0) + (vec2-y player-position) + 0.0 (- player-bullet-speed)) + (bullet-pool-add! player-bullets 0 + (+ (vec2-x player-position) 8.0) + (vec2-y player-position) + 0.0 (- player-bullet-speed)))) (set! *player-fire-counter* 0)))) (define (draw-player) (draw-image context image:player @@ -872,7 +894,7 @@ (clear-rect context 0.0 0.0 *canvas-width* *canvas-height*)) (define (draw-player-bullets) - (draw-bullets player-bullets image:player-bullet 8.0 8.0)) + (draw-bullets player-bullets image:player-bullets 8.0 8.0)) (define (draw-enemy-bullets) (draw-bullets enemy-bullets image:enemy-bullets 16.0 16.0)) @@ -969,6 +991,31 @@ ((string-=? code "KeyZ") (set-firing! #t) (prevent-default! event)) + ((string-=? code "ShiftLeft") + (set-focusing! #t) + (prevent-default! event))))) + + (define (on-key-up event) + (let ((code (keyboard-event-code event))) + (cond + ((string-=? code "ArrowLeft") + (set-left! #f) + (prevent-default! event)) + ((string-=? code "ArrowRight") + (set-right! #f) + (prevent-default! event)) + ((string-=? code "ArrowDown") + (set-down! #f) + (prevent-default! event)) + ((string-=? code "ArrowUp") + (set-up! #f) + (prevent-default! event)) + ((string-=? code "KeyZ") + (set-firing! #f) + (prevent-default! event)) + ((string-=? code "ShiftLeft") + (set-focusing! #f) + (prevent-default! event)) (else (match *game-state* ('play @@ -994,25 +1041,6 @@ (prevent-default! event)))) (_ #t)))))) - (define (on-key-up event) - (let ((code (keyboard-event-code event))) - (cond - ((string-=? code "ArrowLeft") - (set-left! #f) - (prevent-default! event)) - ((string-=? code "ArrowRight") - (set-right! #f) - (prevent-default! event)) - ((string-=? code "ArrowDown") - (set-down! #f) - (prevent-default! event)) - ((string-=? code "ArrowUp") - (set-up! #f) - (prevent-default! event)) - ((string-=? code "KeyZ") - (set-firing! #f) - (prevent-default! event))))) - (define (out-of-bounds? x y w h) (let ((padding 32.0)) (not (rect-within? x y w h (- padding) (- padding) -- cgit v1.2.3