Skip to content

Commit 7f75311

Browse files
authored
Merge pull request #140 from burinc/main
Extract anon-fn out to pure fn
2 parents e01d7a5 + c75168c commit 7f75311

1 file changed

Lines changed: 70 additions & 71 deletions

File tree

src/scittle/games/asteroids.cljs

Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -240,27 +240,26 @@
240240

241241
(defn init-level!
242242
"Initializes a new level"
243-
[& {:keys [level]}]
243+
[{:keys [level ship] :as game-state}]
244244
(let [num-asteroids (+ 3 level)]
245-
(swap! game-state assoc
246-
:asteroids (vec (for [_ (range num-asteroids)]
247-
(let [edge (rand-int 4)
248-
x (case edge
249-
0 (rand-int canvas-width)
250-
1 canvas-width
251-
2 (rand-int canvas-width)
252-
(rand-int canvas-height))
253-
y (case edge
254-
0 0
255-
1 (rand-int canvas-height)
256-
2 canvas-height
257-
0)]
258-
(create-asteroid :x x :y y :size-type :large))))
259-
:bullets []
260-
:particles []
261-
:ufo-timer (+ 600 (rand-int 600))
262-
:ship (assoc (:ship @game-state)
263-
:invulnerable 120))))
245+
(merge game-state
246+
{:asteroids (vec (for [_ (range num-asteroids)]
247+
(let [edge (rand-int 4)
248+
x (case edge
249+
0 (rand-int canvas-width)
250+
1 canvas-width
251+
2 (rand-int canvas-width)
252+
(rand-int canvas-height))
253+
y (case edge
254+
0 0
255+
1 (rand-int canvas-height)
256+
2 canvas-height
257+
0)]
258+
(create-asteroid :x x :y y :size-type :large))))
259+
:bullets []
260+
:particles []
261+
:ufo-timer (+ 600 (rand-int 600))
262+
:ship (assoc ship :invulnerable 120)})))
264263

265264
(defn reset-ship!
266265
"Resets ship to center"
@@ -278,69 +277,69 @@
278277

279278
(defn fire-bullet!
280279
"Fires a bullet from the ship"
281-
[]
280+
[{:keys [ship] :as game-state}]
282281
(play-laser-sound) ; Play laser sound
283-
(let [{:keys [x y angle]} (:ship @game-state)
282+
(let [{:keys [x y angle]} ship
284283
bullet-vx (* bullet-speed (Math/cos (- angle (/ Math/PI 2))))
285284
bullet-vy (* bullet-speed (Math/sin (- angle (/ Math/PI 2))))]
286-
(swap! game-state update :bullets conj
287-
{:x x
288-
:y y
289-
:vx bullet-vx
290-
:vy bullet-vy
291-
:life bullet-lifetime})))
285+
(update game-state :bullets conj
286+
{:x x
287+
:y y
288+
:vx bullet-vx
289+
:vy bullet-vy
290+
:life bullet-lifetime})))
292291

293292
(defn ufo-fire!
294293
"UFO fires bullet at ship"
295-
[& {:keys [ufo]}]
296-
(let [{:keys [ship]} @game-state
297-
dx (- (:x ship) (:x ufo))
294+
[{:keys [ship] :as game-state} & {:keys [ufo]}]
295+
(let [dx (- (:x ship) (:x ufo))
298296
dy (- (:y ship) (:y ufo))
299297
angle (Math/atan2 dy dx)
300298
bullet-vx (* 5 (Math/cos angle))
301299
bullet-vy (* 5 (Math/sin angle))]
302-
(swap! game-state update :bullets conj
303-
{:x (:x ufo)
304-
:y (:y ufo)
305-
:vx bullet-vx
306-
:vy bullet-vy
307-
:life bullet-lifetime
308-
:from-ufo true})))
300+
(update game-state :bullets conj
301+
{:x (:x ufo)
302+
:y (:y ufo)
303+
:vx bullet-vx
304+
:vy bullet-vy
305+
:life bullet-lifetime
306+
:from-ufo true})))
309307

310308
;; ============================================================================
311309
;; Special Moves
312310
;; ============================================================================
313311

312+
(defn hyperspace [state new-x new-y died?]
313+
(-> state
314+
;; Teleport ship
315+
(assoc-in [:ship :x] new-x)
316+
(assoc-in [:ship :y] new-y)
317+
(assoc-in [:ship :vx] 0)
318+
(assoc-in [:ship :vy] 0)
319+
(assoc :hyperspace-cooldown hyperspace-cooldown)
320+
;; Conditionally handle death
321+
(#(if died?
322+
(-> %
323+
(update-in [:lives] dec)
324+
(update :particles
325+
(fn [particles]
326+
(vec (concat particles
327+
(create-particles
328+
:x new-x
329+
:y new-y
330+
:count 12
331+
:color "#FFFFFF"))))))
332+
%))))
333+
314334
(defn hyperspace!
315335
"Hyperspace jump with risk"
316-
[]
317-
(when (<= (:hyperspace-cooldown @game-state) 0)
318-
(play-hyperspace-sound) ; Play hyperspace sound
336+
[game-state]
337+
(when (<= (:hyperspace-cooldown game-state) 0)
338+
(play-hyperspace-sound) ; Play hyperspace sound
319339
(let [new-x (rand-int canvas-width)
320340
new-y (rand-int canvas-height)
321341
died? (< (rand) 0.1)]
322-
(swap! game-state
323-
(fn [state]
324-
(-> state
325-
;; Teleport ship
326-
(assoc-in [:ship :x] new-x)
327-
(assoc-in [:ship :y] new-y)
328-
(assoc-in [:ship :vx] 0)
329-
(assoc-in [:ship :vy] 0)
330-
(assoc :hyperspace-cooldown hyperspace-cooldown)
331-
;; Conditionally handle death
332-
(#(if died?
333-
(-> %
334-
(update-in [:lives] dec)
335-
(update :particles
336-
(fn [particles]
337-
(vec (concat particles
338-
(create-particles
339-
:x new-x
340-
:y new-y
341-
:count 12
342-
:color "#FFFFFF"))))))
343-
%))))))))
342+
(hyperspace game-state new-x new-y died?))))
344343

345344
;; ============================================================================
346345
;; Collision Detection
@@ -427,7 +426,7 @@
427426
(do
428427
(when (and (= (mod (:frame-count @game-state) 60) 0)
429428
(< (rand) 0.3))
430-
(ufo-fire! :ufo u))
429+
(swap! game-state ufo-fire! :ufo u))
431430
(-> u
432431
(update :x + (:vx u))
433432
(update :y + (:vy u))))))))
@@ -558,7 +557,7 @@
558557
(when (empty? asteroids)
559558
(play-level-complete-sound) ; Play victory sound
560559
(swap! game-state update :level inc)
561-
(init-level! :level (:level @game-state))))))
560+
(swap! game-state init-level!)))))
562561

563562
;; ============================================================================
564563
;; Drawing Functions
@@ -764,7 +763,7 @@
764763
:distance 0}
765764
:fire-button false
766765
:hyperspace-button false})
767-
(init-level! :level 1))
766+
(init-level! {:level 1 :ship {}}))
768767

769768
;; ============================================================================
770769
;; Canvas Component
@@ -786,9 +785,9 @@
786785
(swap! keys-pressed conj key)
787786
(when (= (:game-status @game-state) :playing)
788787
(case key
789-
" " (do (fire-bullet!) (.preventDefault e))
790-
"x" (hyperspace!)
791-
"X" (hyperspace!)
788+
" " (do (swap! game-state fire-bullet!) (.preventDefault e))
789+
"x" (swap! game-state hyperspace!)
790+
"X" (swap! game-state hyperspace!)
792791
nil)))))
793792

794793
(.addEventListener js/window "keyup"
@@ -1033,7 +1032,7 @@
10331032
:color "76, 175, 80"
10341033
:on-press #(do
10351034
(swap! game-state assoc-in [:touch-controls :fire-button] true)
1036-
(fire-bullet!))
1035+
(swap! game-state fire-bullet!))
10371036
:on-release #(swap! game-state assoc-in [:touch-controls :fire-button] false)]
10381037
[touch-action-button
10391038
:label "HYPER"
@@ -1042,7 +1041,7 @@
10421041
:color "255, 152, 0"
10431042
:on-press #(do
10441043
(swap! game-state assoc-in [:touch-controls :hyperspace-button] true)
1045-
(hyperspace!))
1044+
(swap! game-state hyperspace!))
10461045
:on-release #(swap! game-state assoc-in [:touch-controls :hyperspace-button] false)]]]))
10471046

10481047
;; ============================================================================

0 commit comments

Comments
 (0)