aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/06-sprite.lisp
diff options
context:
space:
mode:
authorColin Okay <colin@cicadas.surf>2022-07-02 06:21:45 -0500
committerColin Okay <colin@cicadas.surf>2022-07-02 06:21:45 -0500
commit5495e46fcd79009389f12163df3f13b7efa11950 (patch)
tree28300a14d32f0b40cfc750dd82af7380159abd8b /examples/06-sprite.lisp
parent7036229a91e7360a4f40c7e9844d80812e3b8ae5 (diff)
[example] improved sprite example
Diffstat (limited to 'examples/06-sprite.lisp')
-rw-r--r--examples/06-sprite.lisp35
1 files changed, 25 insertions, 10 deletions
diff --git a/examples/06-sprite.lisp b/examples/06-sprite.lisp
index 2d737e5..91910f4 100644
--- a/examples/06-sprite.lisp
+++ b/examples/06-sprite.lisp
@@ -7,6 +7,9 @@
(in-package #:ww.examples/6)
(defclass sprite-example (ww::application) ())
+(defclass dude (ww::sprite)
+ ((walkingp :accessor walkingp :initform nil)))
+
(defun set-key-if-not (sprite key )
(unless (eql key (ww::frameset-key sprite))
@@ -15,26 +18,37 @@
(unless (ww::runningp (ww::current-frameset sprite))
(setf (ww::runningp (ww::current-frameset sprite)) t)))
-(define-symbol-macro +walking-speed+ 10)
+(define-symbol-macro +walking-speed+ 1)
+
+(ww::defhandler move-by-face
+ (ww::on-perframe (sprite)
+ (when (walkingp sprite)
+ (case (ww::frameset-key sprite)
+ (:left
+ (decf (ww::x sprite) +walking-speed+))
+ (:right
+ (incf (ww::x sprite) +walking-speed+))
+ (:back
+ (incf (ww::y sprite) +walking-speed+))
+ (:front
+ (decf (ww::y sprite) +walking-speed+))))))
(ww::defhandler move-dude
(ww::on-keydown (target scancode)
+ (setf (walkingp target) t)
(case scancode
(:scancode-left
- (set-key-if-not target :left)
- (decf (ww::x target) +walking-speed+))
+ (set-key-if-not target :left))
(:scancode-right
- (set-key-if-not target :right)
- (incf (ww::x target) +walking-speed+))
+ (set-key-if-not target :right))
(:scancode-up
- (set-key-if-not target :back)
- (incf (ww::y target) +walking-speed+))
+ (set-key-if-not target :back))
(:scancode-down
- (set-key-if-not target :front)
- (decf (ww::y target) +walking-speed+)))))
+ (set-key-if-not target :front)))))
(ww::defhandler stand
(ww::on-keyup (target)
+ (setf (walkingp target) nil)
(let ((current
(ww::current-frameset target)))
(setf (ww::runningp current) nil
@@ -72,13 +86,14 @@
:fps 3))
(dude
(make-instance
- 'ww::sprite
+ 'dude
:framesets (list :front front
:back back
:left left
:right right)
:frameset-key :front)))
+ (ww::add-handler dude #'move-by-face )
(ww::add-handler dude #'move-dude)
(ww::add-handler dude #'stand)
(ww::refocus-on dude)