From 499602baa6311e732b5c50f22d76bd595c8bbd4d Mon Sep 17 00:00:00 2001 From: Richard Harrington Date: Tue, 3 Sep 2013 12:34:45 -0400 Subject: [PATCH] moved shell and shell-id to top level of world --- public/js/main.js | 16 ++++++++-------- src/robotwar/browser.clj | 2 +- src/robotwar/register.clj | 7 +++---- src/robotwar/world.clj | 20 +++++++++----------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/public/js/main.js b/public/js/main.js index b2b0932..3eee7d1 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -195,15 +195,15 @@ var animateWorld = function(previousWorld, currentWorld) { ctx.clearRect(0, 0, width, height); - var currentShellMap = currentWorld.shells["shell-map"]; - var previousShellMap = previousWorld.shells["shell-map"]; - for (key in previousShellMap) { - if (previousShellMap.hasOwnProperty(key)) { - if (currentShellMap.hasOwnProperty(key)) { - drawShell(previousShellMap[key]); + var currentShells = currentWorld["shells"]; + var previousShells = previousWorld["shells"]; + for (key in previousShells) { + if (previousShells.hasOwnProperty(key)) { + if (currentShells.hasOwnProperty(key)) { + drawShell(previousShells[key]); } else { - explodeShell(previousShellMap[key]); + explodeShell(previousShells[key]); } } } @@ -216,7 +216,7 @@ } }); - if (currentWorld.shells["next-id"] !== previousWorld.shells["next-id"]) { + if (currentWorld["next-shell-id"] !== previousWorld["next-shell-id"]) { console.log("hoi"); nextSoundEl.get().play(); } diff --git a/src/robotwar/browser.clj b/src/robotwar/browser.clj index c5283a1..a5180b6 100644 --- a/src/robotwar/browser.clj +++ b/src/robotwar/browser.clj @@ -34,6 +34,6 @@ (compact-shells [world] (update-in world - [:shells :shell-map] + [:shells] #(map (comp three-sigs-map select-shell-keys) %)))] (map (comp compact-shells compact-robots) worlds))) diff --git a/src/robotwar/register.clj b/src/robotwar/register.clj index 6665375..14a3eaf 100644 --- a/src/robotwar/register.clj +++ b/src/robotwar/register.clj @@ -111,7 +111,7 @@ ; It's a no-op if the shot clock hasn't reached zero yet. {:write-register (fn [{:keys [robot-idx field-name]} - {{:keys [shell-map next-id] :as shells} :shells :as world} + {:keys [shells next-shell-id] :as world} data] (let [{:keys [pos-x pos-y aim shot-timer] :as robot} (get-in world (path-to-robot robot-idx))] @@ -123,9 +123,8 @@ GAME-SECONDS-PER-SHOT)] (assoc world-with-new-shot-timer - :shells - {:shell-map (merge shell-map (shell/init-shell pos-x pos-y aim next-id data)) - :next-id (inc next-id)})))))}) + :shells (merge shells (shell/init-shell pos-x pos-y aim next-shell-id data)) + :next-shell-id (inc next-shell-id))))))}) (defn get-target-register "helper function for DataRegister record" diff --git a/src/robotwar/world.clj b/src/robotwar/world.clj index a14b752..f57ed92 100644 --- a/src/robotwar/world.clj +++ b/src/robotwar/world.clj @@ -10,11 +10,8 @@ (defn init-world "initialize all the variables for a robot world." [programs] - ; TODO: have :shells and :next-shell-id be top-level fields, - ; and dispense with :shell-map. Need to changes stuff throughout - ; the project; search for shells and shell-map in clj and js. - {:shells {:next-id 0 - :shell-map {}} + {:shells {} + :next-shell-id 0 :robots (vec (map-indexed (fn [idx program] (robot/init-robot idx @@ -27,16 +24,17 @@ (defn tick-combined-world [starting-world] - (let [{shells :shells :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 (:shell-map shells)) + (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)] ; TODO: make this a real let-binding, that determines ; which robots were damaged. (let [damaged-world ticked-robots-world] - (assoc-in damaged-world [:shells :shell-map] live-shells)))) + (assoc damaged-world :shells live-shells)))) (def build-combined-worlds (partial iterate tick-combined-world))