diff options
| author | Gene Pasquet <dev@etenil.net> | 2026-04-18 05:59:07 +0100 |
|---|---|---|
| committer | Gene Pasquet <dev@etenil.net> | 2026-04-18 05:59:07 +0100 |
| commit | 84f251ee6e829d33a4f29aa4043924023a378724 (patch) | |
| tree | ab03d18fa192303bf2e1758743ac16c11d9da87f /animation.scm | |
| parent | c2085be2dd2a0cb3da05991847e35080915e547e (diff) | |
Re-format
Diffstat (limited to 'animation.scm')
| -rw-r--r-- | animation.scm | 150 |
1 files changed, 75 insertions, 75 deletions
diff --git a/animation.scm b/animation.scm index 96718ec..1005868 100644 --- a/animation.scm +++ b/animation.scm @@ -1,92 +1,92 @@ (module (downstroke animation) * -(import scheme - (chicken base) - (only srfi-1 filter) - (downstroke entity) - (downstroke world)) + (import scheme + (chicken base) + (only srfi-1 filter) + (downstroke entity) + (downstroke world)) -;; 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. + ;; 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. -;; 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))) + ;; 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 (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)))) + (define (frame-by-idx frames frame-idx) + (list-ref frames (modulo frame-idx (length frames)))) -;; The tile ID is 1-indexed. -(define (frame->tile-id frames frame-idx) - (let ((frame-def (frame-by-idx frames frame-idx))) - (if (list? frame-def) - (car frame-def) - frame-def))) + ;; The tile ID is 1-indexed. + (define (frame->tile-id frames frame-idx) + (let ((frame-def (frame-by-idx frames frame-idx))) + (if (list? frame-def) + (car frame-def) + frame-def))) -(define (frame->duration frames frame-idx) - (let ((frame-def (frame-by-idx frames frame-idx))) - (if (list? frame-def) - (cadr frame-def) - 10))) + (define (frame->duration frames frame-idx) + (let ((frame-def (frame-by-idx frames frame-idx))) + (if (list? frame-def) + (cadr frame-def) + 10))) -;; ---- set-animation ---- -;; Switch to a new animation, resetting frame and tick counters. -;; No-op if the animation is already active (avoids restart mid-loop). + ;; ---- set-animation ---- + ;; Switch to a new animation, resetting frame and tick counters. + ;; No-op if the animation is already active (avoids restart mid-loop). -(define (set-animation entity name) - (if (eq? (entity-ref entity #:anim-name #f) name) - entity - (entity-set (entity-set (entity-set entity #:anim-name name) - #:anim-frame 0) - #:anim-tick 0))) + (define (set-animation entity name) + (if (eq? (entity-ref entity #:anim-name #f) name) + entity + (entity-set (entity-set (entity-set entity #:anim-name name) + #:anim-frame 0) + #:anim-tick 0))) -(define (animation-by-name animations name) - (let ((matching-anims - (filter (lambda (anim) (eq? (animation-ref anim #:name) name)) - animations))) - (if (pair? matching-anims) - (car matching-anims) - #f))) + (define (animation-by-name animations name) + (let ((matching-anims + (filter (lambda (anim) (eq? (animation-ref anim #:name) name)) + animations))) + (if (pair? matching-anims) + (car matching-anims) + #f))) -;; ---- animate-entity ---- -;; Advance the animation tick/frame counter for one game tick. -;; Pass the animation table for this entity's type. -;; Entities without #:anim-name are returned unchanged. + ;; ---- animate-entity ---- + ;; Advance the animation tick/frame counter for one game tick. + ;; Pass the animation table for this entity's type. + ;; Entities without #:anim-name are returned unchanged. -(define (advance-animation entity anim) - (let ((tick (+ 1 (entity-ref entity #:anim-tick 0))) - (duration (animation-duration anim)) - (frames (animation-frames anim)) - (frame (entity-ref entity #:anim-frame 0))) - (if (>= tick duration) - (let ((new-frame-id (modulo (+ frame 1) (length frames)))) + (define (advance-animation entity anim) + (let ((tick (+ 1 (entity-ref entity #:anim-tick 0))) + (duration (animation-duration anim)) + (frames (animation-frames anim)) + (frame (entity-ref entity #:anim-frame 0))) + (if (>= tick duration) + (let ((new-frame-id (modulo (+ frame 1) (length frames)))) + (entity-set-many entity + `((#: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 - `((#: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 - `((#:anim-tick . ,tick) - (#: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)) - (anim (and anim-name (animation-by-name animations anim-name)))) - (if anim - (advance-animation entity anim) - entity))) + (define (animate-entity entity animations) + (let* ((anim-name (entity-ref entity #:anim-name #f)) + (anim (and anim-name (animation-by-name animations anim-name)))) + (if anim + (advance-animation entity anim) + entity))) -(define-pipeline (apply-animation animation) (scene entity dt) - guard: (entity-ref entity #:animations #f) - (let ((animations (entity-ref entity #:animations #f))) - (animate-entity entity animations))) + (define-pipeline (apply-animation animation) (scene entity dt) + guard: (entity-ref entity #:animations #f) + (let ((animations (entity-ref entity #:animations #f))) + (animate-entity entity animations))) -) ;; End of animation module + ) ;; End of animation module |
