summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson@vistahigherlearning.com>2020-10-06 18:57:23 -0400
committerDavid Thompson <dthompson@vistahigherlearning.com>2020-10-06 18:57:23 -0400
commitc808d99f048daf4284e1ab431166347dc5456706 (patch)
treefd8ffd8d2a3a96ef133d6ed6ea1e5ac2b3699663
parente02d3d74b1aae368084c3c81145568274054a48c (diff)
examples: shmup: Stop polling input devices and only use events.
-rw-r--r--examples/shmup/shmup.scm50
1 files changed, 23 insertions, 27 deletions
diff --git a/examples/shmup/shmup.scm b/examples/shmup/shmup.scm
index b775234..135027b 100644
--- a/examples/shmup/shmup.scm
+++ b/examples/shmup/shmup.scm
@@ -1,5 +1,4 @@
-(use-modules (chickadee)
- (chickadee math)
+(use-modules (chickadee math)
(chickadee math matrix)
(chickadee math rect)
(chickadee math vector)
@@ -602,7 +601,8 @@
(state #:accessor state #:init-form 'play)
(enemies #:accessor enemies #:init-form '())
(cont #:accessor cont #:init-form #f)
- (cont-x #:accessor cont-x #:init-form +inf.0))
+ (cont-x #:accessor cont-x #:init-form +inf.0)
+ (move-state #:getter move-state #:init-thunk make-hash-table))
(define-method (update-hud (shmup <shmup>))
(let ((player (& shmup player)))
@@ -785,22 +785,14 @@
(dy (* (sin theta) speed)))
(add-bullet (& shmup enemy-bullets) type x y dx dy)))
-(define-method (update-player-movement (shmup <shmup>))
- (change-direction (& shmup player)
- (key-pressed? 'left)
- (key-pressed? 'right)
- (key-pressed? 'down)
- (key-pressed? 'up)))
-
-(define-method (update-player-shooting (shmup <shmup>))
- (if (key-pressed? 'z)
- (begin-shooting (& shmup player))
- (end-shooting (& shmup player))))
-
-(define-method (update-player-guarding (shmup <shmup>))
- (if (key-pressed? 'x)
- (begin-guarding (& shmup player))
- (end-guarding (& shmup player))))
+(define-method (update-player-movement (shmup <shmup>) key down?)
+ (let ((state (move-state shmup)))
+ (hashq-set! state key down?)
+ (change-direction (& shmup player)
+ (hashq-ref state 'left)
+ (hashq-ref state 'right)
+ (hashq-ref state 'down)
+ (hashq-ref state 'up))))
(define-method (pause-game (shmup <shmup>))
(set! (state shmup) 'pause)
@@ -808,6 +800,10 @@
(pause (& shmup player-bullets))
(pause (& shmup enemy-bullets))
(pause (& shmup player))
+ (end-shooting (& shmup player))
+ (end-guarding (& shmup player))
+ (hash-clear! (move-state shmup))
+ (change-direction (& shmup player) #f #f #f #f)
(for-each pause (enemies shmup)))
(define-method (resume-game (shmup <shmup>))
@@ -816,17 +812,16 @@
(resume (& shmup player-bullets))
(resume (& shmup enemy-bullets))
(resume (& shmup player))
- (update-player-movement shmup)
- (update-player-shooting shmup)
(for-each resume (enemies shmup)))
(define-method (on-key-press (shmup <shmup>) key scancode modifiers repeat?)
(match (state shmup)
('play
(match key
- ((or 'up 'down 'left 'right) (update-player-movement shmup))
- ('z (update-player-shooting shmup))
- ('x (update-player-guarding shmup))
+ ((or 'up 'down 'left 'right)
+ (update-player-movement shmup key #t))
+ ('z (begin-shooting (& shmup player)))
+ ('x (begin-guarding (& shmup player)))
('return (pause-game shmup))
('r (reset-game shmup))
(_ #f)))
@@ -844,9 +839,10 @@
(match (state shmup)
('play
(match key
- ((or 'up 'down 'left 'right) (update-player-movement shmup))
- ('z (update-player-shooting shmup))
- ('x (update-player-guarding shmup))
+ ((or 'up 'down 'left 'right)
+ (update-player-movement shmup key #f))
+ ('z (end-shooting (& shmup player)))
+ ('x (end-guarding (& shmup player)))
(_ #f)))
(_ #f)))