~lthms/fairy.lisp

56ce5a76dda422b78c18acb0193d568f668aa7f0 — Thomas Letan 6 years ago 25995d4
feature: Add a new animation called goto
2 files changed, 32 insertions(+), 3 deletions(-)

M fairy.lisp
M tiled.lisp
M fairy.lisp => fairy.lisp +30 -2
@@ 22,6 22,7 @@
           update
           element
           origin
           goto
           scale
           visible
           rectangle


@@ 44,6 45,8 @@
(defgeneric update (element dt)
  (:documentation "Update the element after ~dt~ ms."))

(defstruct goto-animation from to in progress then)

(defclass element ()
  ((origin :initarg :origin
           :initform (gamekit:vec2 0 0)


@@ 53,10 56,35 @@
          :accessor :scale)
   (visible :initarg :visible
            :initform t
            :accessor visible)))
            :accessor visible)
   (goto :initform nil
         :accessor %goto)))

(defmethod update ((el element) dt)
  ())
  (let ((anim (%goto el)))
    (when anim
      (setf (goto-animation-progress anim)
            (+ (goto-animation-progress anim) dt))
      (let* ((progress-ratio (min 1
                                  (/ (goto-animation-progress anim)
                                     (goto-animation-in anim))))
             (vec (gamekit:mult progress-ratio
                                (gamekit:subt (goto-animation-to anim)
                                              (goto-animation-from anim)))))
        (setf (fairy:origin el) (gamekit:add (goto-animation-from anim)
                                             vec))
        (when (>= progress-ratio 1)
          (setf (%goto el) nil)
          (when (goto-animation-then anim)
            (funcall (goto-animation-then anim))))))))

(defmethod goto ((el element) to in &key then)
  (let ((anim (make-goto-animation :from (fairy:origin el)
                                   :to to
                                   :in in
                                   :progress 0
                                   :then then)))
    (setf (%goto el) anim)))

(defmacro defdraw ((el class) &rest body)
  `(defmethod draw ((,el ,class))

M tiled.lisp => tiled.lisp +2 -1
@@ 101,7 101,8 @@
             (frame-duration (/ animation-period (length frames)))
             (new-current (mod (truncate time frame-duration) (length frames))))
        (setf (current el) (nth new-current frames))
        (setf (animation-started-since el) time)))))
        (setf (animation-started-since el) time))))
  (call-next-method))

(defclass tile-map (fairy:layer)
  ((tmx :initarg :path