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) {
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();
}

View File

@ -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)))

View File

@ -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"

View File

@ -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))