From 102a307af8931a0e074cd9ae25a5c67b4e1a64b2 Mon Sep 17 00:00:00 2001 From: Richard Harrington Date: Thu, 22 Aug 2013 15:25:36 -0400 Subject: [PATCH] changed shells to a map-indexed system, for identifying new ones (so far, we can't play the same sound at the same time anyway, so this change has no visible effect yet) --- public/js/main.js | 14 ++++++++++---- src/robotwar/register.clj | 10 ++++++---- src/robotwar/shell.clj | 5 +++-- src/robotwar/world.clj | 7 ++++--- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/public/js/main.js b/public/js/main.js index 761edde..8a9b6ae 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -151,13 +151,19 @@ var animateWorld = function(previousWorld, currentWorld) { ctx.clearRect(0, 0, width, height); - currentWorld.shells.forEach(function(shell) { - drawShell(shell); - }); + var shellMap = currentWorld.shells["shell-map"]; + for (key in shellMap) { + if (shellMap.hasOwnProperty(key)) { + drawShell(shellMap[key]); + } + } currentWorld.robots.forEach(function(robot, idx) { drawRobot(robot, ROBOT_COLORS[idx]); }); - if (currentWorld.shells.length > previousWorld.shells.length) { + console.log("next:", currentWorld.shells["next-id"]); + console.log("last:", previousWorld.shells["next-id"]); + + if (currentWorld.shells["next-id"] !== previousWorld.shells["next-id"]) { shotSound.play(); } } diff --git a/src/robotwar/register.clj b/src/robotwar/register.clj index 8f390c8..50eb894 100644 --- a/src/robotwar/register.clj +++ b/src/robotwar/register.clj @@ -97,20 +97,22 @@ ; adds a shell to the list of shells. ; It's a no-op if the shot clock hasn't reached zero yet. {:write-register - (fn [{:keys [robot-idx field-name]} world data] + (fn [{:keys [robot-idx field-name]} + {{:keys [shell-map next-id] :as shells} :shells :as world} + data] (let [{:keys [pos-x pos-y aim shot-timer] :as robot} (get-in world (path-to-robot robot-idx))] (if (> shot-timer 0) world - (let [shells (:shells world) - world-with-new-shot-timer (assoc-in + (let [world-with-new-shot-timer (assoc-in world (path-to-robot-field robot-idx :shot-timer) GAME-SECONDS-PER-SHOT)] (assoc world-with-new-shot-timer :shells - (conj shells (shell/init-shell pos-x pos-y aim data)))))))}) + {:shell-map (merge shell-map (shell/init-shell pos-x pos-y aim next-id data)) + :next-id (inc next-id)})))))}) (defn get-target-register "helper function for DataRegister record" diff --git a/src/robotwar/shell.clj b/src/robotwar/shell.clj index d7e3e56..cb5e0d4 100644 --- a/src/robotwar/shell.clj +++ b/src/robotwar/shell.clj @@ -3,11 +3,12 @@ (:require [robotwar.physics :as physics])) (defn init-shell - [pos-x pos-y aim distance] + [pos-x pos-y aim id distance] ; TODO: make the starting point dependent upon the robot radius, ; which should be in constants. (let [{unit-x :x unit-y :y} (physics/decompose-angle aim)] - {:pos-x pos-x + {:id id + :pos-x pos-x :pos-y pos-y :v-x (* unit-x SHELL-SPEED) :v-y (* unit-y SHELL-SPEED) diff --git a/src/robotwar/world.clj b/src/robotwar/world.clj index abb997c..567a3cb 100644 --- a/src/robotwar/world.clj +++ b/src/robotwar/world.clj @@ -10,7 +10,8 @@ (defn init-world "initialize all the variables for a robot world." [programs] - {:shells [] + {:shells {:next-id 0 + :shell-map {}} :robots (vec (map-indexed (fn [idx program] (robot/init-robot idx @@ -27,12 +28,12 @@ (robot/tick-robot (robots robot-idx) world)) starting-world (range (count (:robots starting-world)))) - ticked-shells (map shell/tick-shell shells) + ticked-shells (map shell/tick-shell (:shell-map 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 damaged-world :shells live-shells)))) + (assoc-in damaged-world [:shells :shell-map] live-shells)))) (def build-combined-worlds (partial iterate tick-combined-world))