-
Notifications
You must be signed in to change notification settings - Fork 3
/
animation.clj
33 lines (30 loc) · 1.02 KB
/
animation.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(ns gdl.graphics.animation
"An immutable animation defrecord (plain map) for passing image objects to 'frames'.")
(defprotocol Animation
(tick [_ delta])
(restart [_])
(stopped? [_])
(current-frame [_]))
(defrecord ImmutableAnimation [frames frame-duration looping? cnt maxcnt]
Animation
(tick [this delta]
(let [maxcnt (float maxcnt)
newcnt (+ (float cnt) (float delta))]
(assoc this :cnt (cond (< newcnt maxcnt) newcnt
looping? (min maxcnt (- newcnt maxcnt))
:else maxcnt))))
(restart [this]
(assoc this :cnt 0))
(stopped? [_]
(and (not looping?) (>= cnt maxcnt)))
(current-frame [this]
(frames (min (int (/ (float cnt) (float frame-duration)))
(dec (count frames))))))
(defn create
[frames & {:keys [frame-duration looping?]}]
(map->ImmutableAnimation
{:frames (vec frames)
:frame-duration frame-duration
:looping? looping?
:cnt 0
:maxcnt (* (count frames) (float frame-duration))}))