57 lines
1.6 KiB
Scheme
57 lines
1.6 KiB
Scheme
![]() |
(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))))
|