diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/06-sprite.lisp | 35 |
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) |