-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.clj
286 lines (257 loc) · 8.07 KB
/
sprite.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
(ns quip.sprite
(:require [quil.core :as q]
[quip.utils :as qpu]))
(defn offset-pos
[[x y] w h]
[(- x (/ w 2))
(- y (/ h 2))])
(defn update-pos
[{:keys [pos vel] :as s}]
(assoc s :pos (map + pos vel)))
(defn update-frame-delay
[{:keys [current-animation] :as s}]
(let [animation (current-animation (:animations s))
frame-delay (:frame-delay animation)]
(update s :delay-count #(mod (inc %) frame-delay))))
(defn update-animation
[{:keys [current-animation delay-count] :as s}]
(if (zero? delay-count)
(let [animation (current-animation (:animations s))
max-frame (:frames animation)]
(update s :animation-frame #(mod (inc %) max-frame)))
s))
(defn update-image-sprite
[s]
(some-> s
update-pos))
(defn update-animated-sprite
[s]
(some-> s
update-frame-delay
update-animation
update-pos))
(defn draw-image-sprite
[{:keys [pos w h image]}]
(let [[x y] (offset-pos pos w h)]
(q/image image x y)))
(def memo-graphics (memoize (fn [w h] (q/create-graphics w h))))
(defn draw-animated-sprite
[{:keys [pos rotation w h spritesheet current-animation animation-frame] :as s}]
(let [animation (current-animation (:animations s))
x-offset (* animation-frame w)
y-offset (* (:y-offset animation) h)
g (memo-graphics w h)]
(q/with-graphics g
(.clear g)
(q/image spritesheet (- x-offset) (- y-offset)))
(qpu/wrap-trans-rot pos rotation
#(q/image g (- (/ w 2)) (- (/ h 2))))))
(defn set-animation
[s animation]
(-> s
(assoc :current-animation animation)
(assoc :animation-frame 0)))
(defn default-bounding-poly
"Generates a bounding polygon based off the `w` by `h` rectangle of
the sprite."
[{:keys [w h]}]
[[0 0]
[w 0]
[w h]
[0 h]])
;;; Basic Sprite types
(defn static-sprite
[sprite-group pos w h image-file &
{:keys [rotation
update-fn
draw-fn
points
bounds-fn]
:or {rotation 0
update-fn identity
draw-fn draw-image-sprite}}]
{:sprite-group sprite-group
:uuid (java.util.UUID/randomUUID)
:pos pos
:rotation rotation
:w w
:h h
:animated? false
:static? true
:update-fn update-fn
:draw-fn draw-fn
:points points
:bounds-fn (or bounds-fn
(if (seq points)
:points
default-bounding-poly))
:image (q/load-image image-file)})
(defn image-sprite
[sprite-group pos w h image-file &
{:keys [rotation
vel
update-fn
draw-fn
points
bounds-fn]
:or {rotation 0
vel [0 0]
update-fn update-image-sprite
draw-fn draw-image-sprite}}]
{:sprite-group sprite-group
:uuid (java.util.UUID/randomUUID)
:pos pos
:rotation rotation
:vel vel
:w w
:h h
:animated? false
:static? false
:update-fn update-fn
:draw-fn draw-fn
:points points
:bounds-fn (or bounds-fn
(if (seq points)
:points
default-bounding-poly))
:image (q/load-image image-file)})
(defn animated-sprite
[sprite-group pos w h spritesheet-file &
{:keys [rotation
vel
update-fn
draw-fn
animations
current-animation
points
bounds-fn]
:or {rotation 0
vel [0 0]
update-fn update-animated-sprite
draw-fn draw-animated-sprite
animations {:none {:frames 1
:y-offset 0
:frame-delay 100}}
current-animation :none}}]
{:sprite-group sprite-group
:uuid (java.util.UUID/randomUUID)
:pos pos
:rotation rotation
:vel vel
:w w
:h h
:animated? true
:static? false
:update-fn update-fn
:draw-fn draw-fn
:points points
:bounds-fn (or bounds-fn
(if (seq points)
:points
default-bounding-poly))
:spritesheet (q/load-image spritesheet-file)
:animations animations
:current-animation current-animation
:delay-count 0
:animation-frame 0})
(defn draw-text-sprite
[{:keys [content pos offsets font size color]}]
(let [[x y] pos]
(apply q/text-align offsets)
(q/text-font font)
(qpu/fill color)
(q/text content x y)))
(defn text-sprite
[content pos &
{:keys [offsets
sprite-group
font
size
color
update-fn
draw-fn]
:or {offsets [:center]
sprite-group :text
font qpu/default-font
size qpu/default-text-size
color qpu/black
update-fn identity
draw-fn draw-text-sprite}}]
{:sprite-group sprite-group
:uuid (java.util.UUID/randomUUID)
:content content
:pos pos
:offsets offsets
:font (q/create-font font size)
:color color
:update-fn update-fn
:draw-fn draw-fn})
(defn update-scene-sprites
"Update each sprite in the current scene using its `:update-fn`.
Optionally accepts a key specifying the name of the sprite
collection on the scene."
[{:keys [current-scene] :as state} &
{:keys [sprite-key] :or {sprite-key :sprites}}]
(update-in state [:scenes current-scene sprite-key]
(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`.
Optionally accepts a key specifying the name of the sprite
collection on the scene."
[{:keys [current-scene] :as state} &
{:keys [sprite-key] :or {sprite-key :sprites}}]
(let [sprites (get-in state [:scenes current-scene sprite-key])]
(doall
(map (fn [s]
((:draw-fn s) s))
sprites))))
(defn draw-scene-sprites-by-layers
"Draw each sprite in the current scene using its `:draw-fn` in the
order their `:sprite-group` appears in the `layers` list.
Any sprites with groups not found in `layers` will be drawn last.
Optionally accepts a key specifying the name of the sprite
collection on the scene."
[{:keys [current-scene] :as state} layers &
{:keys [sprite-key] :or {sprite-key :sprites}}]
(let [sprites (get-in state [:scenes current-scene sprite-key])
unspecified (filter #(not ((set layers) (:sprite-group %))) sprites)]
(doall
(map (fn [group]
(doall
(map (fn [s]
((:draw-fn s) s))
(filter #(= group (:sprite-group %))
sprites))))
layers))
(doall
(map (fn [s]
((:draw-fn s) s))
unspecified))))
(defn update-sprites-by-pred
"Update sprites in the current scene with the update function `f`
filtering by a predicate function `pred`.
Optionally accepts a key specifying the name of the sprite
collection on the scene."
[{:keys [current-scene] :as state} pred f &
{:keys [sprite-key] :or {sprite-key :sprites}}]
(update-in state [:scenes current-scene sprite-key]
(fn [sprites]
(pmap (fn [s]
(if (pred s)
(f s)
s))
sprites))))
(defn group-pred
"Defines a predicate that filters sprites based on their
sprite-group.
Commonly used alongside `update-sprites-by-pred`:
(qpsprite/update-sprites-by-pred
state
(qpsprite/group-pred :asteroids)
sprite-update-fn)"
[sprite-group]
(fn [s]
(= sprite-group (:sprite-group s))))