Skip to content

Commit 6b46e7a

Browse files
committed
Improve the code per review from Erik
1 parent 946f8c4 commit 6b46e7a

1 file changed

Lines changed: 25 additions & 13 deletions

File tree

src/scittle/games/asteroids.cljs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,31 @@
250250
"Hyperspace jump with risk"
251251
[]
252252
(when (<= (:hyperspace-cooldown @game-state) 0)
253-
(swap! game-state assoc-in [:ship :x] (rand-int canvas-width))
254-
(swap! game-state assoc-in [:ship :y] (rand-int canvas-height))
255-
(swap! game-state assoc-in [:ship :vx] 0)
256-
(swap! game-state assoc-in [:ship :vy] 0)
257-
(swap! game-state assoc :hyperspace-cooldown hyperspace-cooldown)
258-
;; 10% chance of explosion (risky!)
259-
(when (< (rand) 0.1)
260-
(swap! game-state update-in [:lives] dec)
261-
(swap! game-state update :particles
262-
#(vec (concat % (create-particles :x (:x (:ship @game-state))
263-
:y (:y (:ship @game-state))
264-
:count 12
265-
:color "#FFFFFF")))))))
253+
(let [new-x (rand-int canvas-width)
254+
new-y (rand-int canvas-height)
255+
died? (< (rand) 0.1)]
256+
(swap! game-state
257+
(fn [state]
258+
(-> state
259+
;; Teleport ship
260+
(assoc-in [:ship :x] new-x)
261+
(assoc-in [:ship :y] new-y)
262+
(assoc-in [:ship :vx] 0)
263+
(assoc-in [:ship :vy] 0)
264+
(assoc :hyperspace-cooldown hyperspace-cooldown)
265+
;; Conditionally handle death
266+
(#(if died?
267+
(-> %
268+
(update-in [:lives] dec)
269+
(update :particles
270+
(fn [particles]
271+
(vec (concat particles
272+
(create-particles
273+
:x new-x
274+
:y new-y
275+
:count 12
276+
:color "#FFFFFF"))))))
277+
%))))))))
266278

267279
;; ============================================================================
268280
;; Collision Detection

0 commit comments

Comments
 (0)