-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.clj
57 lines (52 loc) · 2.23 KB
/
scene.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(ns quip.scene
(:require [quil.core :as q]))
(defn current-scene
[{:keys [current-scene] :as state}]
(get-in state [:scenes current-scene]))
(defn fade-to-black
[state progress max]
(q/fill 0 (int (* 255 (/ progress max))))
(q/rect 0 0 (q/width) (q/height)))
(defn transition
"Temporarily replace `parent-update-fn` and `parent-draw-fn` with
transition handdling versions which will set themselves back to the
originals on completion."
[{:keys [parent-update-fn parent-draw-fn] :as state}
target-scene
& {:keys [transition-fn
transition-length
init-fn]
:or {transition-fn fade-to-black
transition-length 20
init-fn identity}}]
(-> state
(assoc :transition-progress 0)
(assoc :input-enabled? false)
(assoc :parent-update-fn (fn [{:keys [transition-progress] :as state}]
(if (< (or transition-progress 0) transition-length)
(update state :transition-progress inc)
(-> state
(assoc :current-scene target-scene)
(assoc :parent-update-fn parent-update-fn)
(assoc :parent-draw-fn parent-draw-fn)
(assoc :input-enabled? true)
(dissoc :transition-progress)
init-fn))))
(assoc :parent-draw-fn (fn [{:keys [transition-progress] :as state}]
(transition-fn state transition-progress transition-length)))))
(defn update-scene-sprites
"Update each sprite in the current scene using its `:update-fn`."
[{:keys [current-scene] :as state}]
(update-in state [:scenes current-scene :sprites]
(fn [sprites]
(map (fn [s]
((:update-fn s) s))
sprites))))
(defn draw-scene-sprites
"Draw each sprite in the current scene using its `:draw-fn`."
[{:keys [current-scene] :as state}]
(let [sprites (get-in state [:scenes current-scene :sprites])]
(doall
(map (fn [s]
((:draw-fn s) s))
sprites))))