diff options
Diffstat (limited to 'animation.scm')
| -rw-r--r-- | animation.scm | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/animation.scm b/animation.scm index 0d961a1..8843512 100644 --- a/animation.scm +++ b/animation.scm @@ -1,18 +1,25 @@ (module downstroke-animation * (import scheme (chicken base) - (chicken keyword) (chicken pretty-print) (only srfi-1 filter) downstroke-entity downstroke-world) -;; ---- Animation data accessors ---- +;; Animation definitions are alists (converted from plist form in the user's +;; prefab data file by load-prefabs). Each animation is an alist with keys +;; #:name, #:frames, optional #:duration. -(define (animation-frames anim) - (get-keyword #:frames anim)) -(define (animation-duration anim) - (get-keyword #:duration anim)) +;; Look up a key in an animation alist. Mirrors entity-ref: +;; a non-procedure default is returned as-is on miss; a procedure default +;; is invoked as a thunk. +(define (animation-ref anim key #!optional default) + (cond ((assq key anim) => cdr) + ((procedure? default) (default)) + (else default))) + +(define (animation-frames anim) (animation-ref anim #:frames)) +(define (animation-duration anim) (animation-ref anim #:duration)) (define (frame-by-idx frames frame-idx) (list-ref frames (modulo frame-idx (length frames)))) @@ -43,8 +50,10 @@ (define (animation-by-name animations name) - (let ((matching-anims (filter (lambda (anim) (eq? (get-keyword #:name anim) name)) animations))) - (if matching-anims + (let ((matching-anims + (filter (lambda (anim) (eq? (animation-ref anim #:name) name)) + animations))) + (if (pair? matching-anims) (car matching-anims) #f))) @@ -61,13 +70,13 @@ (if (>= tick duration) (let ((new-frame-id (modulo (+ frame 1) (length frames)))) (entity-set-many entity - (list (cons #:anim-tick 0) - (cons #:anim-frame new-frame-id) - (cons #:tile-id (frame->tile-id frames new-frame-id)) - (cons #:duration (frame->duration frames new-frame-id))))) + `((#:anim-tick . 0) + (#:anim-frame . ,new-frame-id) + (#:tile-id . ,(frame->tile-id frames new-frame-id)) + (#:duration . ,(frame->duration frames new-frame-id))))) (entity-set-many entity - (list (cons #:anim-tick tick) - (cons #:tile-id (frame->tile-id frames frame))))))) + `((#:anim-tick . ,tick) + (#:tile-id . ,(frame->tile-id frames frame))))))) (define (animate-entity entity animations) (let* ((anim-name (entity-ref entity #:anim-name #f)) |
