aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGene Pasquet <dev@etenil.net>2026-04-19 11:24:39 +0100
committerGene Pasquet <dev@etenil.net>2026-04-19 11:24:39 +0100
commit986564f050496dd78667eb37b6e66bd540968905 (patch)
tree3a5b67eeb73f236caaf5cd5473e24912759bff9a
parentb3f2fe63c2781d3faf20dc4d6709e4d9bc8755c8 (diff)
Set individual key frame animation time, make animations run on dt
-rw-r--r--animation.scm28
-rw-r--r--demo/assets/animation-prefabs.scm2
2 files changed, 17 insertions, 13 deletions
diff --git a/animation.scm b/animation.scm
index ea13125..2466cc8 100644
--- a/animation.scm
+++ b/animation.scm
@@ -1,6 +1,7 @@
(module (downstroke animation) *
(import scheme
(chicken base)
+ (chicken pretty-print)
(only srfi-1 filter)
(downstroke entity)
(downstroke world))
@@ -17,7 +18,7 @@
((procedure? default) (default))
(else default)))
-(define +default-anim-duration+ 10)
+(define +default-anim-duration+ 100) ; 100ms
(define (animation-frames anim) (animation-ref anim #:frames 1))
(define (animation-duration anim) (animation-ref anim #:duration +default-anim-duration+))
@@ -32,11 +33,11 @@
(car frame-def)
frame-def)))
-(define (frame->duration frames frame-idx)
- (let ((frame-def (frame-by-idx frames frame-idx)))
+(define (frame->duration animation frame-idx)
+ (let ((frame-def (frame-by-idx (animation-frames animation) frame-idx)))
(if (list? frame-def)
(cadr frame-def)
- 10)))
+ (animation-ref animation #:duration +default-anim-duration+))))
;; ---- set-animation ----
;; Switch to a new animation, resetting frame and tick counters.
@@ -63,9 +64,9 @@
;; 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))
+(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)))
(if (>= tick duration)
@@ -74,21 +75,24 @@
`((#:anim-tick . 0)
(#:anim-frame . ,new-frame-id)
(#:tile-id . ,(frame->tile-id frames new-frame-id))
- (#:duration . ,(frame->duration frames new-frame-id)))))
+ (#:duration . ,(frame->duration anim new-frame-id)))))
(entity-set-many entity
`((#:anim-tick . ,tick)
(#:tile-id . ,(frame->tile-id frames frame)))))))
-(define (animate-entity entity animations)
+(define (animate-entity entity animations dt)
(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)
+ (advance-animation entity anim dt)
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)))
+ (let* ((animations (entity-ref entity #:animations #f))
+ (updated-entity (animate-entity entity animations dt)))
+ (when (eq? (entity-ref entity #:type) 'timed-frames)
+ (pp (list (entity-ref updated-entity #:tile-id) (entity-ref updated-entity #:duration) (entity-ref entity #:anim-tick))))
+ updated-entity))
) ;; End of animation module
diff --git a/demo/assets/animation-prefabs.scm b/demo/assets/animation-prefabs.scm
index f965c81..ac9558d 100644
--- a/demo/assets/animation-prefabs.scm
+++ b/demo/assets/animation-prefabs.scm
@@ -3,4 +3,4 @@
(timed-frames animated #:type timed-frames #:anim-name walk
#:animations ((#:name walk #:frames ((28 10) (29 1000)))))
(std-frames animated #:type std-frames #:anim-name attack
- #:animations ((#:name attack #:frames (28 29) #:duration 10)))))
+ #:animations ((#:name attack #:frames (28 29) #:duration 200)))))