This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathasteroids.clj
More file actions
387 lines (337 loc) · 11.3 KB
/
asteroids.clj
File metadata and controls
387 lines (337 loc) · 11.3 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
;; Copyright (c) Zachary Tellman. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns example.game.asteroids
(:use [penumbra opengl]
[penumbra.seq :only (separate)] ;;modified to drop contrib.
[cantor])
(:require [penumbra.app :as app]
[penumbra.text :as text]
[penumbra.time :as time]
[penumbra.data :as data]))
;;;
(def *dim* (vec2 10 10))
(defn wrap
"Makes the position wrap around from right to left, bottom to top"
[pos]
(sub
(map*
mod
(add pos *dim*)
(mul *dim* 2))
*dim*))
(defn expired? [x]
((:expired? x)))
(defn render [x]
((:render x)))
(defn radius [x]
(if (number? (:radius x))
(:radius x)
((:radius x))))
(defn position [x]
(if (cartesian? (:position x))
(:position x)
((:position x))))
(defn intersects? [a b]
(let [min-dist (+ (radius a) (radius b))
dist (sub (position a) (position b))]
(>= (* min-dist min-dist) (length-squared dist))))
(defn rand-color [a b]
(map #(+ %1 (rand (- %2 %1))) a b))
;;asteroids
(defn sphere-vertices
[lod]
(for [theta (range 0 361 (/ 360 lod))]
(for [phi (range -90 91 (/ 180 (/ lod 2)))]
(cartesian (polar3 theta phi)))))
(defn rand-vector []
(normalize (cartesian (polar3 (rand 360) (rand 360)))))
(defn offset-vertex
"Expand if on one side of a plane, contract if on the other"
[v vertex]
(if (neg? (dot v (normalize vertex)))
(mul vertex 1.01)
(mul vertex 0.99)))
(defn offset-sphere [v vertices]
(map (fn [arc] (map #(offset-vertex v %) arc)) vertices))
(defn gen-asteroid-vertices
"Procedurally generate perturbed sphere"
[lod iterations]
(let [s (iterate #(offset-sphere (rand-vector) %) (sphere-vertices lod))]
(nth s iterations)))
(defn draw-asteroid [vertices]
(doseq [arcs (partition 2 1 vertices)]
(draw-quad-strip
(doseq [[a b] (map list (first arcs) (second arcs))]
(vertex a) (vertex b)))))
(defn gen-asteroid-geometry [lod iterations]
(create-display-list (draw-asteroid (gen-asteroid-vertices lod iterations))))
(defn init-asteroids []
(def asteroid-meshes (doall (take 20 (repeatedly #(gen-asteroid-geometry 12 100))))))
(defn gen-asteroid [& args]
(let [params (merge
{:position (vec2 0 0)
:radius 1
:theta (rand 360)
:speed 1}
(apply hash-map args))
birth (app/now)
elapsed #(- (app/now) birth)
asteroid (nth asteroid-meshes (rand-int 20))
velocity (mul (cartesian (:theta params)) (:speed params))
position #(wrap (add (:position params) (mul velocity (elapsed))))]
{:expired? #(< (:radius params) 0.25)
:position position
:radius (:radius params)
:render #(push-matrix
(translate (position))
(rotate (* (:speed params) (elapsed) -50) 1 0 0)
(rotate (:theta params) 0 1 0)
(apply scale (->> params :radius repeat (take 3)))
(color 0.6 0.6 0.6)
(call-display-list asteroid))}))
;;particles
(defn textured-quad []
(draw-quads
(texture 0 0) (vertex -1 -1)
(texture 1 0) (vertex 1 -1)
(texture 1 1) (vertex 1 1)
(texture 0 1) (vertex -1 1)))
(defn init-particles []
(def particle-tex
(let [[w h] [32 32]
dim (vec2 w h)
tex (create-byte-texture w h)]
(data/overwrite!
tex
(apply concat
(for [x (range w) y (range h)]
(let [pos (div (vec2 x y) dim)
i (Math/exp (* 16 (- (length-squared (sub pos (vec2 0.5 0.5))))))]
[1 1 1 i]))))
tex))
(def particle-quad
(create-display-list (textured-quad))))
(defn draw-particle [position radius tint]
(push-matrix
(apply color tint)
(translate position)
(scale radius radius)
(call-display-list particle-quad)))
(defn gen-particle [& args]
(let [params (merge
{:position (vec2 0 0)
:theta (rand 360)
:speed 1
:radius (+ 0.25 (rand 0.5))
:color [1 1 1]
:lifespan 1}
(apply hash-map args))
birth (app/now)
velocity (mul (cartesian (polar2 (:theta params))) (:speed params))
elapsed #(- (app/now) birth)
position #(wrap (add (:position params) (mul velocity (elapsed))))]
{:expired? #(> (- (app/now) birth) (:lifespan params))
:position position
:radius (:radius params)
:render #(draw-particle
(position)
(:radius params)
(concat (:color params) [(max 0 (- 1 (Math/pow (/ (elapsed) (:lifespan params)) 3)))]))}))
;;spaceship
(defn draw-fuselage [] ;;should be hung in the Louvre
(color 1 1 1)
(draw-triangles
(vertex -0.4 -0.5) (vertex 0 -0.4) (vertex 0 0.5)
(vertex 0.4 -0.5) (vertex 0 -0.4) (vertex 0 0.5)))
(defn init-spaceship []
(def fuselage (create-display-list (draw-fuselage))))
(defn fire-bullet [state]
(let [ship (:spaceship state)]
(assoc state
:bullets
(conj
(:bullets state)
(gen-particle
:position (:position ship)
:theta (:theta ship)
:speed 15
:radius 0.25
:color [0 0 1]
:lifespan 2)))))
(defn emit-flame [state]
(when (app/key-pressed? :up)
(let [ship (:spaceship state)
offset (- (rand 30) 15)
theta (+ 180 (:theta ship) offset)
particles (:particles state)
position (add (:position ship) (cartesian (polar2 theta 0.3)))
velocity (polar (add (:velocity ship) (cartesian theta)))]
(assoc state
:particles (conj particles
(gen-particle
:position position
:theta (.theta velocity)
:speed (.r velocity)
:radius 0.25
:color (rand-color [1 0.5 0.7] [1 1 1])
:lifespan (/ (Math/cos (radians (* 3 offset))) 2.5)))))))
(defn update-spaceship [dt ship]
(let [p (:position ship)
v (:velocity ship)
theta (:theta ship)
theta (condp (fn [x _] (app/key-pressed? x)) nil
:left (rem (+ theta (* 360 dt)) 360)
:right (rem (- theta (* 360 dt)) 360)
theta)
a (if (app/key-pressed? :up)
(mul (cartesian theta) 3)
(vec2 0 0))
v (add v (mul a dt))
p (wrap (add p (mul v dt)))]
(assoc ship
:theta theta
:position p
:velocity v)))
(defn draw-spaceship [ship]
(push-matrix
(translate (:position ship))
(rotate (- (:theta ship) 90) 0 0 1)
(call-display-list fuselage)))
(defn gen-spaceship []
{:position (vec2 0 0)
:radius 0.5
:velocity (vec2 0 0)
:theta 0
:birth (app/now)})
;;game state
(defn reset
"Reset to initial game state."
[state]
(assoc state
:spaceship (gen-spaceship)
:asteroids (take 4 (repeatedly
#(let [theta (rand 360)
pos (cartesian (polar2 theta 2))]
(gen-asteroid :position pos :theta theta :speed (rand)))))))
(defn split-asteroid
"Turn asteroid into four sub-asteroids."
[asteroid]
(when (< 0.25 (radius asteroid))
(take 4
(repeatedly
#(gen-asteroid
:position (position asteroid)
:radius (/ (radius asteroid) 2)
:speed (/ 1.5 (radius asteroid)))))))
(defn gen-explosion
"Create particles within a given color range."
[num object [lo-color hi-color] speed]
(take num
(repeatedly
#(gen-particle
:position (position object)
:speed (rand speed)
:color (rand-color lo-color hi-color)
:lifespan 2))))
(defn explode-asteroids
"Turn asteroid into sub-asteroids and explosion particles."
[asteroids state]
(assoc state
:asteroids (concat
(:asteroids state)
(mapcat split-asteroid asteroids))
:particles (concat
(:particles state)
(mapcat #(gen-explosion (* (radius %) 100) % [[1 0.5 0] [1 1 0.2]] 2) asteroids))))
(defn check-complete
"Are all the asteroids gone?"
[state]
(if (zero? (count (:asteroids state)))
(reset state)
state))
(defn check-ship
"Has the ship collided with any asteroids?"
[state]
(let [ship (:spaceship state)
asteroids (:asteroids state)
[hit missed] (separate #(intersects? ship %) asteroids)]
(if (some #(intersects? ship %) asteroids)
(assoc state
:asteroids (concat missed (mapcat split-asteroid hit))
:spaceship (gen-spaceship)
:particles (concat (:particles state) (gen-explosion 300 ship [[0 0 0.6] [0.5 0.5 1]] 7)))
state)))
(defn check-asteroids
"Have the asteroids collided with any bullets?"
[state]
(let [bullets (:bullets state)
asteroids (:asteroids state)
collisions (for [a asteroids, b bullets :when (intersects? a b)] [a b])
[hit missed] (separate (set (map first collisions)) asteroids)
bullets (remove (set (map second collisions)) bullets)
particles (:particles state)]
(explode-asteroids
hit
(assoc state
:particles (remove expired? particles)
:bullets (remove expired? bullets)
:asteroids missed))))
(defn update-collisions [state]
(binding [*dim* (:dim state)]
(-> state check-asteroids check-ship check-complete)))
;;game loop
(defn init [state]
(app/title! "Asteroids")
(app/vsync! true)
(app/key-repeat! false)
(init-asteroids)
(init-particles)
(init-spaceship)
(enable :blend)
(blend-func :src-alpha :one-minus-src-alpha)
(app/periodic-update! 25 update-collisions)
(app/periodic-update! 50 emit-flame)
(reset state))
(defn reshape [[x y w h] state]
(let [dim (vec2 (* (/ w h) 10) 10)]
(frustum-view 45 (/ w h) 0.1 100)
(load-identity)
(translate 0 0 (- (* 2.165 (.y dim))))
(assoc state
:dim dim)))
(defn key-press [key state]
(cond
(= key " ") (fire-bullet state)
(= key :escape) (app/pause!)
(= key "1") (app/speed! 0.5)
(= key "2") (app/speed! 1)
(= key "3") (app/speed! 2)
:else state))
(defn update [[dt time] state]
(binding [*dim* (:dim state)]
(assoc state
:spaceship (update-spaceship dt (:spaceship state)))))
(defn display [[dt time] state]
(text/write-to-screen (str (int (/ 1 dt)) " fps") 0 0)
(binding [*dim* (:dim state)]
(with-enabled :texture-2d
(with-texture particle-tex
(doseq [p (concat (:particles state) (:bullets state))]
(render p))))
(with-disabled :texture-2d
(with-render-mode :wireframe
(doseq [a (:asteroids state)]
(render a)))
(draw-spaceship (:spaceship state)))
(app/repaint!)))
;;tom
;;Doesn't work for me, probably not supported shader stuff on my hardware.
(defn start []
(app/start
{:reshape reshape, :init init, :key-press key-press, :update update, :display display}
{:dim *dim*}))