Skip to content
Permalink
Browse files

Add playback controls to animated-sprite-subject

  • Loading branch information...
Shinmera committed Jun 27, 2019
1 parent b76aefa commit 51672f976b88d638de1e3048533f0ca168096bf2
Showing with 30 additions and 11 deletions.
  1. +3 −0 package.lisp
  2. +27 −11 sprite.lisp
@@ -652,8 +652,11 @@
#:animated-sprite-subject
#:animations
#:clock
#:playback-speed
#:playback-direction
#:frame
#:animation
#:reset-animation
#:update-sprite-animation)
;; ssao,lisp
(:export
@@ -39,7 +39,9 @@ void main(){
(define-shader-subject animated-sprite-subject (sprite-entity)
((animations :accessor animations)
(animation :initform NIL :accessor animation)
(clock :initform 0.0d0 :accessor clock)))
(clock :initform 0.0d0 :accessor clock)
(playback-direction :initform +1 :accessor playback-direction)
(playback-speed :initform 1.0 :accessor playback-speed)))

(defmethod shared-initialize :after ((subject animated-sprite-subject) slots &key animation frame animations)
(when animations (setf (animations subject) animations))
@@ -78,27 +80,41 @@ void main(){
(defmethod (setf frame) (value (subject animated-sprite-subject))
(setf (vx (tile subject)) value))

(defmethod reset-animation ((subject animated-sprite-subject))
(setf (vx (tile subject)) (sprite-animation-start (animation subject)))
(setf (clock subject) 0.0d0)
(setf (playback-speed subject) 1.0)
(setf (playback-direction subject) +1))

(defmethod (setf animation) ((index integer) (subject animated-sprite-subject))
(setf (animation subject) (aref (animations subject) index)))

(defmethod (setf animation) ((name symbol) (subject animated-sprite-subject))
(setf (animation subject) (find name (animations subject) :key #'sprite-animation-name)))

(defmethod (setf animation) :before ((animation sprite-animation) (subject animated-sprite-subject))
(defmethod (setf animation) ((animation sprite-animation) (subject animated-sprite-subject))
(unless (eql animation (animation subject))
(setf (vx (tile subject)) (sprite-animation-start animation))))
(setf (slot-value subject 'animation) animation)
(reset-animation subject))
animation)

(define-handler (animated-sprite-subject update-sprite-animation tick) (ev dt)
(let* ((tile (tile animated-sprite-subject))
(animations (animations animated-sprite-subject))
(animation (animation animated-sprite-subject)))
(incf (clock animated-sprite-subject) dt)
(incf (clock animated-sprite-subject) (* (playback-speed animated-sprite-subject) dt))
(when (<= (sprite-animation-step animation) (clock animated-sprite-subject))
(decf (clock animated-sprite-subject) (sprite-animation-step animation))
(incf (vx tile)))
(when (<= (sprite-animation-end animation) (vx tile))
(let ((next (aref animations (sprite-animation-next animation))))
(cond ((eq animation next)
(setf (vx tile) (sprite-animation-loop animation)))
(T
(setf (animation animated-sprite-subject) next)))))))
(incf (vx tile) (playback-direction animated-sprite-subject)))
(cond ((<= (sprite-animation-end animation) (vx tile))
(let ((next (aref animations (sprite-animation-next animation))))
(cond ((eq animation next)
(setf (vx tile) (sprite-animation-loop animation)))
(T
(setf (animation animated-sprite-subject) next)))))
((< (vx tile) (sprite-animation-start animation))
(let ((next (aref animations (sprite-animation-next animation))))
(cond ((eq animation next)
(setf (vx tile) (1- (sprite-animation-end animation))))
(T
(setf (animation animated-sprite-subject) next))))))))

0 comments on commit 51672f9

Please sign in to comment.
You can’t perform that action at this time.