Skip to content

Commit 2dd200e

Browse files
committed
Same thing with fire bullets of various sorts
1 parent f3220a0 commit 2dd200e

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

src/scittle/games/asteroids.cljs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -277,34 +277,33 @@
277277

278278
(defn fire-bullet!
279279
"Fires a bullet from the ship"
280-
[]
280+
[{:keys [ship] :as game-state}]
281281
(play-laser-sound) ; Play laser sound
282-
(let [{:keys [x y angle]} (:ship @game-state)
282+
(let [{:keys [x y angle]} ship
283283
bullet-vx (* bullet-speed (Math/cos (- angle (/ Math/PI 2))))
284284
bullet-vy (* bullet-speed (Math/sin (- angle (/ Math/PI 2))))]
285-
(swap! game-state update :bullets conj
286-
{:x x
287-
:y y
288-
:vx bullet-vx
289-
:vy bullet-vy
290-
: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})))
291291

292292
(defn ufo-fire!
293293
"UFO fires bullet at ship"
294-
[& {:keys [ufo]}]
295-
(let [{:keys [ship]} @game-state
296-
dx (- (:x ship) (:x ufo))
294+
[{:keys [ship] :as game-state} & {:keys [ufo]}]
295+
(let [dx (- (:x ship) (:x ufo))
297296
dy (- (:y ship) (:y ufo))
298297
angle (Math/atan2 dy dx)
299298
bullet-vx (* 5 (Math/cos angle))
300299
bullet-vy (* 5 (Math/sin angle))]
301-
(swap! game-state update :bullets conj
302-
{:x (:x ufo)
303-
:y (:y ufo)
304-
:vx bullet-vx
305-
:vy bullet-vy
306-
:life bullet-lifetime
307-
: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})))
308307

309308
;; ============================================================================
310309
;; Special Moves
@@ -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))))))))
@@ -786,7 +785,7 @@
786785
(swap! keys-pressed conj key)
787786
(when (= (:game-status @game-state) :playing)
788787
(case key
789-
" " (do (fire-bullet!) (.preventDefault e))
788+
" " (do (swap! game-state fire-bullet!) (.preventDefault e))
790789
"x" (swap! game-state hyperspace!)
791790
"X" (swap! game-state hyperspace!)
792791
nil)))))
@@ -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"

0 commit comments

Comments
 (0)