Skip to content

Commit 38f821e

Browse files
committed
Extract anon-fn out to pure fn
- push swap! towards the edges
1 parent e01d7a5 commit 38f821e

1 file changed

Lines changed: 29 additions & 28 deletions

File tree

src/scittle/games/asteroids.cljs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -311,36 +311,37 @@
311311
;; Special Moves
312312
;; ============================================================================
313313

314+
(defn hyperspace [state new-x new-y died?]
315+
(-> state
316+
;; Teleport ship
317+
(assoc-in [:ship :x] new-x)
318+
(assoc-in [:ship :y] new-y)
319+
(assoc-in [:ship :vx] 0)
320+
(assoc-in [:ship :vy] 0)
321+
(assoc :hyperspace-cooldown hyperspace-cooldown)
322+
;; Conditionally handle death
323+
(#(if died?
324+
(-> %
325+
(update-in [:lives] dec)
326+
(update :particles
327+
(fn [particles]
328+
(vec (concat particles
329+
(create-particles
330+
:x new-x
331+
:y new-y
332+
:count 12
333+
:color "#FFFFFF"))))))
334+
%))))
335+
314336
(defn hyperspace!
315337
"Hyperspace jump with risk"
316-
[]
317-
(when (<= (:hyperspace-cooldown @game-state) 0)
318-
(play-hyperspace-sound) ; Play hyperspace sound
338+
[game-state]
339+
(when (<= (:hyperspace-cooldown game-state) 0)
340+
(play-hyperspace-sound) ; Play hyperspace sound
319341
(let [new-x (rand-int canvas-width)
320342
new-y (rand-int canvas-height)
321343
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-
%))))))))
344+
(hyperspace game-state new-x new-y died?))))
344345

345346
;; ============================================================================
346347
;; Collision Detection
@@ -787,8 +788,8 @@
787788
(when (= (:game-status @game-state) :playing)
788789
(case key
789790
" " (do (fire-bullet!) (.preventDefault e))
790-
"x" (hyperspace!)
791-
"X" (hyperspace!)
791+
"x" (swap! game-state hyperspace!)
792+
"X" (swap! game-state hyperspace!)
792793
nil)))))
793794

794795
(.addEventListener js/window "keyup"
@@ -1042,7 +1043,7 @@
10421043
:color "255, 152, 0"
10431044
:on-press #(do
10441045
(swap! game-state assoc-in [:touch-controls :hyperspace-button] true)
1045-
(hyperspace!))
1046+
(swap! game-state hyperspace!))
10461047
:on-release #(swap! game-state assoc-in [:touch-controls :hyperspace-button] false)]]]))
10471048

10481049
;; ============================================================================

0 commit comments

Comments
 (0)