aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorColin Okay <okay@toyful.space>2022-07-09 15:50:13 -0500
committerColin Okay <okay@toyful.space>2022-07-09 15:50:13 -0500
commit8c86602ed04bf3731b39b6170176a7ebe4b89e3b (patch)
tree3cfb66ed4e3cb38e8b4d01f0bb24987a94c6e303 /examples
parent62d23b98785538b6e1d212a8acc086d5c0a65c63 (diff)
[example] add gravity toggle
Diffstat (limited to 'examples')
-rw-r--r--examples/09-ghoulspree.lisp41
1 files changed, 30 insertions, 11 deletions
diff --git a/examples/09-ghoulspree.lisp b/examples/09-ghoulspree.lisp
index 05bd4a3..35986fa 100644
--- a/examples/09-ghoulspree.lisp
+++ b/examples/09-ghoulspree.lisp
@@ -11,7 +11,8 @@
(defclass/std ghoulspree (ww::application)
((ghouls-per-click :std 10)
- (collision-on-p :std t)))
+ (collision-on-p :std t)
+ (gravity-on-p :std nil)))
(defclass/std ghoul (ww:bitmap)
((dx dy dr :std)))
@@ -70,6 +71,10 @@ on which boundary VAL is outside of."
dy2 tdy
dr2 tdr)))))
+(defun apply-gravity-to (thing acc)
+ (with-slots (dx dy) thing
+ (decf dy acc)))
+
(ww:defhandler moveghouls
(ww:on-perframe (app)
;; first handle collisions
@@ -83,12 +88,17 @@ on which boundary VAL is outside of."
(advance-pos g1)
(advance-pos g2)
(advance-pos g2))))
-
;; then update positions and remove the out of bounds
- (loop for ghoul in (ww:container-units app)
- do (advance-pos ghoul)
- when (out-of-bounds-p ghoul)
- do (ww:drop-unit ghoul))))
+ (let ((gravity
+ (gravity-on-p app))
+ (accelleration
+ (/ 9.8 (ww:fps app))))
+ (loop for ghoul in (ww:container-units app)
+ do (advance-pos ghoul)
+ when gravity
+ do (apply-gravity-to ghoul accelleration)
+ when (out-of-bounds-p ghoul)
+ do (ww:drop-unit ghoul)))))
(defun random-sign ()
(if (zerop (random 2)) -1 1))
@@ -107,14 +117,23 @@ on which boundary VAL is outside of."
(ww:defhandler toggle-collision
- (ww:on-keydown ()
- (format t "collision: ~a~%"
- (setf (collision-on-p target)
- (not (collision-on-p target))))))
+ (ww:on-keydown (app scancode)
+ (case scancode
+ (:scancode-c
+ (format t "collision: ~a~%"
+ (setf (collision-on-p app)
+ (not (collision-on-p app)))))
+ (:scancode-g
+ (format t "gravity: ~a~%"
+ (setf (gravity-on-p app)
+ (not (gravity-on-p app))))))))
+
(defmethod ww::boot ((app ghoulspree))
"Adds the intro text and sets up the start button handler."
- (format t "Click to add ghouls to the screen.~%Press any key to toggle collision detection.~%")
+ (format t "Click to add ~a ghouls to the screen.~%" (ghouls-per-click app))
+ (format t "Press c to toggle collision handling.~%")
+ (format t "Press g to toggle gravity.~%")
(ww:add-handler app #'add-ghouls)
(ww:add-handler app #'moveghouls)