moved shell and shell-id to top level of world

This commit is contained in:
Richard Harrington 2013-09-03 12:34:45 -04:00
parent d0ab2bd8b8
commit 499602baa6
4 changed files with 21 additions and 24 deletions

View File

@ -195,15 +195,15 @@
var animateWorld = function(previousWorld, currentWorld) { var animateWorld = function(previousWorld, currentWorld) {
ctx.clearRect(0, 0, width, height); ctx.clearRect(0, 0, width, height);
var currentShellMap = currentWorld.shells["shell-map"]; var currentShells = currentWorld["shells"];
var previousShellMap = previousWorld.shells["shell-map"]; var previousShells = previousWorld["shells"];
for (key in previousShellMap) { for (key in previousShells) {
if (previousShellMap.hasOwnProperty(key)) { if (previousShells.hasOwnProperty(key)) {
if (currentShellMap.hasOwnProperty(key)) { if (currentShells.hasOwnProperty(key)) {
drawShell(previousShellMap[key]); drawShell(previousShells[key]);
} }
else { 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"); console.log("hoi");
nextSoundEl.get().play(); nextSoundEl.get().play();
} }

View File

@ -34,6 +34,6 @@
(compact-shells [world] (compact-shells [world]
(update-in (update-in
world world
[:shells :shell-map] [:shells]
#(map (comp three-sigs-map select-shell-keys) %)))] #(map (comp three-sigs-map select-shell-keys) %)))]
(map (comp compact-shells compact-robots) worlds))) (map (comp compact-shells compact-robots) worlds)))

View File

@ -111,7 +111,7 @@
; It's a no-op if the shot clock hasn't reached zero yet. ; It's a no-op if the shot clock hasn't reached zero yet.
{:write-register {:write-register
(fn [{:keys [robot-idx field-name]} (fn [{:keys [robot-idx field-name]}
{{:keys [shell-map next-id] :as shells} :shells :as world} {:keys [shells next-shell-id] :as world}
data] data]
(let [{:keys [pos-x pos-y aim shot-timer] :as robot} (let [{:keys [pos-x pos-y aim shot-timer] :as robot}
(get-in world (path-to-robot robot-idx))] (get-in world (path-to-robot robot-idx))]
@ -123,9 +123,8 @@
GAME-SECONDS-PER-SHOT)] GAME-SECONDS-PER-SHOT)]
(assoc (assoc
world-with-new-shot-timer world-with-new-shot-timer
:shells :shells (merge shells (shell/init-shell pos-x pos-y aim next-shell-id data))
{:shell-map (merge shell-map (shell/init-shell pos-x pos-y aim next-id data)) :next-shell-id (inc next-shell-id))))))})
:next-id (inc next-id)})))))})
(defn get-target-register (defn get-target-register
"helper function for DataRegister record" "helper function for DataRegister record"

View File

@ -10,11 +10,8 @@
(defn init-world (defn init-world
"initialize all the variables for a robot world." "initialize all the variables for a robot world."
[programs] [programs]
; TODO: have :shells and :next-shell-id be top-level fields, {:shells {}
; and dispense with :shell-map. Need to changes stuff throughout :next-shell-id 0
; the project; search for shells and shell-map in clj and js.
{:shells {:next-id 0
:shell-map {}}
:robots (vec (map-indexed (fn [idx program] :robots (vec (map-indexed (fn [idx program]
(robot/init-robot (robot/init-robot
idx idx
@ -27,16 +24,17 @@
(defn tick-combined-world (defn tick-combined-world
[starting-world] [starting-world]
(let [{shells :shells :as ticked-robots-world} (reduce (fn [{robots :robots :as world} robot-idx] (let [{:keys [shells next-shell-id] :as ticked-robots-world}
(robot/tick-robot (robots robot-idx) world)) (reduce (fn [{robots :robots :as world} robot-idx]
starting-world (robot/tick-robot (robots robot-idx) world))
(range (count (:robots starting-world)))) starting-world
ticked-shells (map shell/tick-shell (:shell-map shells)) (range (count (:robots starting-world))))
ticked-shells (map shell/tick-shell shells)
live-shells (remove :exploded ticked-shells) live-shells (remove :exploded ticked-shells)
exploded-shells (filter :exploded ticked-shells)] exploded-shells (filter :exploded ticked-shells)]
; TODO: make this a real let-binding, that determines ; TODO: make this a real let-binding, that determines
; which robots were damaged. ; which robots were damaged.
(let [damaged-world ticked-robots-world] (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)) (def build-combined-worlds (partial iterate tick-combined-world))