diff --git a/public/js/main.js b/public/js/main.js index b62d5b0..9087951 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -238,7 +238,6 @@ drawRobot(robot, ROBOT_COLORS[idx]); } }); - if (currentWorld["next-shell-id"] !== previousWorld["next-shell-id"]) { soundPlayers.shellRelease.play(); } diff --git a/src/robotwar/browser.clj b/src/robotwar/browser.clj index a5180b6..2f28666 100644 --- a/src/robotwar/browser.clj +++ b/src/robotwar/browser.clj @@ -16,10 +16,8 @@ :damage :shot-timer])) (select-shell-keys [shell] - (select-keys shell [:id - :pos-x - :pos-y - :exploded])) + (select-keys shell [:pos-x + :pos-y])) (three-sigs-map [m] (zipmap (keys m) (map #(if (float? %) diff --git a/src/robotwar/register.clj b/src/robotwar/register.clj index 14a3eaf..ce526b8 100644 --- a/src/robotwar/register.clj +++ b/src/robotwar/register.clj @@ -123,7 +123,9 @@ GAME-SECONDS-PER-SHOT)] (assoc world-with-new-shot-timer - :shells (merge shells (shell/init-shell pos-x pos-y aim next-shell-id data)) + :shells (merge + shells + {next-shell-id (shell/init-shell pos-x pos-y aim data)}) :next-shell-id (inc next-shell-id))))))}) (defn get-target-register diff --git a/src/robotwar/shell.clj b/src/robotwar/shell.clj index cb5e0d4..30272f4 100644 --- a/src/robotwar/shell.clj +++ b/src/robotwar/shell.clj @@ -3,32 +3,23 @@ (:require [robotwar.physics :as physics])) (defn init-shell - [pos-x pos-y aim id distance] - ; TODO: make the starting point dependent upon the robot radius, - ; which should be in constants. + [pos-x pos-y aim distance] (let [{unit-x :x unit-y :y} (physics/decompose-angle aim)] - {:id id - :pos-x pos-x + {:pos-x pos-x :pos-y pos-y :v-x (* unit-x SHELL-SPEED) :v-y (* unit-y SHELL-SPEED) :dest-x (+ pos-x (* unit-x distance)) - :dest-y (+ pos-y (* unit-y distance)) - :exploded false})) + :dest-y (+ pos-y (* unit-y distance))})) (defn tick-shell - [{:keys [pos-x pos-y v-x v-y dest-x dest-y exploded] :as shell}] - (if exploded - nil - (let [delta-x (* v-x *GAME-SECONDS-PER-TICK*) - delta-y (* v-y *GAME-SECONDS-PER-TICK*) - remaining-x (- dest-x pos-x) - remaining-y (- dest-y pos-y)] - ; only need to check one dimension - (if (and (<= (Math/abs remaining-x) (Math/abs delta-x)) - (<= (Math/abs remaining-y) (Math/abs delta-y))) - (merge shell {:pos-x dest-x - :pos-y dest-y - :exploded true}) - (merge shell {:pos-x (+ pos-x delta-x) - :pos-y (+ pos-y delta-y)}))))) + [{:keys [pos-x pos-y v-x v-y dest-x dest-y] :as shell}] + (let [delta-x (* v-x *GAME-SECONDS-PER-TICK*) + delta-y (* v-y *GAME-SECONDS-PER-TICK*) + remaining-x (- dest-x pos-x) + remaining-y (- dest-y pos-y)] + (if (and (<= (Math/abs remaining-x) (Math/abs delta-x)) + (<= (Math/abs remaining-y) (Math/abs delta-y))) + nil + (merge shell {:pos-x (+ pos-x delta-x) + :pos-y (+ pos-y delta-y)})))) diff --git a/src/robotwar/world.clj b/src/robotwar/world.clj index f57ed92..fbc2575 100644 --- a/src/robotwar/world.clj +++ b/src/robotwar/world.clj @@ -25,16 +25,18 @@ (defn tick-combined-world [starting-world] (let [{:keys [shells next-shell-id] :as ticked-robots-world} - (reduce (fn [{robots :robots :as world} robot-idx] - (robot/tick-robot (robots robot-idx) world)) - starting-world - (range (count (:robots starting-world)))) - ticked-shells (map shell/tick-shell shells) - live-shells (remove :exploded ticked-shells) - exploded-shells (filter :exploded ticked-shells)] + (reduce (fn [{robots :robots :as world} robot-idx] + (robot/tick-robot (robots robot-idx) world)) + starting-world + (range (count (:robots starting-world)))) + ticked-shells (into {} (map (fn [shell-map-entry] + [(key shell-map-entry) + (shell/tick-shell (val shell-map-entry))]) + shells))] ; TODO: make this a real let-binding, that determines ; which robots were damaged. (let [damaged-world ticked-robots-world] - (assoc damaged-world :shells live-shells)))) + (pprint ticked-shells) + (assoc damaged-world :shells ticked-shells)))) (def build-combined-worlds (partial iterate tick-combined-world))