From 59f5cb6c28399c84221b728daf4bd8461ab1e230 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Fri, 1 Jul 2022 10:53:47 -0500 Subject: [add] sprite class and example --- src/interactive/sprite.lisp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/interactive/sprite.lisp (limited to 'src/interactive/sprite.lisp') diff --git a/src/interactive/sprite.lisp b/src/interactive/sprite.lisp new file mode 100644 index 0000000..81f5715 --- /dev/null +++ b/src/interactive/sprite.lisp @@ -0,0 +1,42 @@ +;;;; sprite.lisp + +(in-package #:wheelwork) + +(defclass/std sprite (unit interactive) + ((framesets :with :doc "A PLIST whose values are framesets.") + (frameset-key))) + +(defun current-frameset (sprite) + (getf (sprite-framesets sprite) + (frameset-key sprite))) + +(defmethod initialize-instance :after ((sprite sprite) &key) + (with-slots (framesets frameset-key) sprite + (loop for (name fs . more) on framesets by #'cddr + do (setf (unit-container fs) sprite)) + (unless frameset-key + (setf frameset-key (first framesets))))) + +(defmethod cleanup ((sprite sprite)) + (mapc #'cleanup (sprite-framesets sprite))) + +(defmethod render ((sprite sprite)) + (when-let (fs (current-frameset sprite)) + (render fs))) + +(macrolet + ((def-sprite-accessors (&rest accessor-names) + (let ((defs + (loop for accessor-name in accessor-names + collect + `(defmethod ,accessor-name ((sprite sprite)) + (,accessor-name (current-frameset sprite))) + + collect + `(defmethod (setf ,accessor-name) (newval (sprite sprite)) + (loop for (key fs . more) on (sprite-framesets sprite) by #'cddr + do (setf (,accessor-name fs) newval)) + newval)))) + `(progn ,@defs)))) + + (def-sprite-accessors x y scale-x scale-y width height rotation)) -- cgit v1.2.3