aboutsummaryrefslogtreecommitdiff
path: root/animation.scm
diff options
context:
space:
mode:
Diffstat (limited to 'animation.scm')
-rw-r--r--animation.scm33
1 files changed, 24 insertions, 9 deletions
diff --git a/animation.scm b/animation.scm
index f166bff..5753bc1 100644
--- a/animation.scm
+++ b/animation.scm
@@ -26,16 +26,27 @@
(define (frame-by-idx frames frame-idx)
(list-ref frames (modulo frame-idx (length frames))))
+;; A "timed" frame is =(tile-id tick-budget)= — a two-element proper list.
+;; A bare tile id is a number (or other atom).
+(define (timed-frame? frame-def)
+ (and (pair? frame-def)
+ (pair? (cdr frame-def))
+ (null? (cddr 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)
+ (if (timed-frame? frame-def)
(car frame-def)
frame-def)))
-(define (frame->duration animation frame-idx)
+;; Tick budget for one frame, in this order:
+;; 1. Per-frame duration from =(tile duration)= in #:frames
+;; 2. Else the animation alist's #:duration
+;; 3. Else +default-anim-duration+ (global default)
+(define (animation-frame-duration animation frame-idx)
(let ((frame-def (frame-by-idx (animation-frames animation) frame-idx)))
- (if (list? frame-def)
+ (if (timed-frame? frame-def)
(cadr frame-def)
(animation-ref animation #:duration +default-anim-duration+))))
@@ -63,22 +74,26 @@
;; 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.
+;; Tick threshold is always (animation-frame-duration anim frame) — per-frame pair,
+;; then animation #:duration, then +default-anim-duration+. #:anim-duration
+;; on the entity is set every step to that resolved value.
(define (advance-animation entity anim dt)
- (let ((tick (+ dt (entity-ref entity #:anim-tick 0)))
- (duration (entity-ref entity #:duration +default-anim-duration+))
- (frames (animation-frames anim))
- (frame (entity-ref entity #:anim-frame 0)))
+ (let* ((frame (entity-ref entity #:anim-frame 0))
+ (frames (animation-frames anim))
+ (duration (animation-frame-duration anim frame))
+ (tick (+ dt (entity-ref entity #:anim-tick 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 anim new-frame-id)))))
+ (#:anim-duration . ,(animation-frame-duration anim new-frame-id)))))
(entity-set-many entity
`((#:anim-tick . ,tick)
- (#:tile-id . ,(frame->tile-id frames frame)))))))
+ (#:tile-id . ,(frame->tile-id frames frame))
+ (#:anim-duration . ,duration))))))
(define (animate-entity entity animations dt)
(let* ((anim-name (entity-ref entity #:anim-name #f))