;;;; sprite.lisp (in-package #:wheelwork) (defclass/std sprite (unit interactive) ((framesets :with :doc "A PLIST whose values are framesets.") (frameset-key :with :doc "The current name of the frameset being displayed."))) (defun current-frameset (sprite) "Returns the current FRAMESET instance being displayed on SRPITE." (getf (sprite-framesets sprite) (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-in-scene-p 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)) (defmethod get-rect ((sprite sprite)) (get-rect (current-frameset sprite)))