Add animations.

This commit is contained in:
David Thompson 2024-05-25 06:52:59 -04:00
parent 6896b49848
commit faf4fa7124
5 changed files with 128 additions and 21 deletions

View file

@ -0,0 +1,56 @@
(define-module (game animation)
#:use-module (game tileset)
#:use-module (ice-9 match)
#:use-module (srfi srfi-9)
#:export (make-frame
frame?
frame-tile
frame-duration
make-animation
animation?
animation-tileset
animation-time
animation-frames
update-animation
draw-animation))
(define-record-type <frame>
(make-frame tile duration)
frame?
(tile frame-tile)
(duration frame-duration))
(define-record-type <animation>
(%make-animation tileset frames time frame-idx)
animation?
(tileset animation-tileset)
(frames animation-frames)
(time animation-time set-animation-time!)
(frame-idx animation-frame-idx set-animation-frame-idx!))
(define (make-animation tileset frames)
(%make-animation tileset frames 0.0 0))
(define (flonum? x)
(and (real? x) (inexact? x)))
(define (update-animation anim dt)
(unless (flonum? dt)
(error "not a flonum" dt))
(match anim
(($ <animation> tileset frames (? flonum? time) idx)
(let ((time* (+ time dt)))
(match (vector-ref frames idx)
(($ <frame> tile duration)
(cond
((>= time* duration)
(set-animation-frame-idx! anim (modulo (1+ idx) (vector-length frames)))
(set-animation-time! anim (- time* duration)))
(else
(set-animation-time! anim time*)))))))))
(define (draw-animation context anim x y)
(match anim
(($ <animation> tileset frames time idx)
(draw-tile context tileset (frame-tile (vector-ref frames idx)) x y))))

View file

@ -1,5 +1,6 @@
(define-module (game effects)
#:use-module (dom canvas)
#:use-module (game time)
#:use-module (ice-9 match)
#:use-module (math)
#:use-module (srfi srfi-9)
@ -24,9 +25,6 @@
(define (ease:linear t) t)
(define (current-time*)
(/ (exact->inexact (current-jiffy)) (jiffies-per-second)))
(define-record-type <effect>
(%make-effect type draw ease duration start)
effect?

6
modules/game/time.scm Normal file
View file

@ -0,0 +1,6 @@
(define-module (game time)
#:use-module (scheme time)
#:export (current-time*))
(define (current-time*)
(/ (exact->inexact (current-jiffy)) (jiffies-per-second)))