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)
This commit is contained in:
Richard Harrington 2013-08-22 15:25:36 -04:00
parent 1511a41670
commit 102a307af8
4 changed files with 23 additions and 13 deletions

View File

@ -151,13 +151,19 @@
var animateWorld = function(previousWorld, currentWorld) { var animateWorld = function(previousWorld, currentWorld) {
ctx.clearRect(0, 0, width, height); ctx.clearRect(0, 0, width, height);
currentWorld.shells.forEach(function(shell) { var shellMap = currentWorld.shells["shell-map"];
drawShell(shell); for (key in shellMap) {
}); if (shellMap.hasOwnProperty(key)) {
drawShell(shellMap[key]);
}
}
currentWorld.robots.forEach(function(robot, idx) { currentWorld.robots.forEach(function(robot, idx) {
drawRobot(robot, ROBOT_COLORS[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(); shotSound.play();
} }
} }

View File

@ -97,20 +97,22 @@
; adds a shell to the list of shells. ; adds a shell to the list of shells.
; 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]} 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} (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))]
(if (> shot-timer 0) (if (> shot-timer 0)
world world
(let [shells (:shells world) (let [world-with-new-shot-timer (assoc-in
world-with-new-shot-timer (assoc-in
world world
(path-to-robot-field robot-idx :shot-timer) (path-to-robot-field robot-idx :shot-timer)
GAME-SECONDS-PER-SHOT)] GAME-SECONDS-PER-SHOT)]
(assoc (assoc
world-with-new-shot-timer world-with-new-shot-timer
:shells :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 (defn get-target-register
"helper function for DataRegister record" "helper function for DataRegister record"

View File

@ -3,11 +3,12 @@
(:require [robotwar.physics :as physics])) (:require [robotwar.physics :as physics]))
(defn init-shell (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, ; TODO: make the starting point dependent upon the robot radius,
; which should be in constants. ; which should be in constants.
(let [{unit-x :x unit-y :y} (physics/decompose-angle aim)] (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 :pos-y pos-y
:v-x (* unit-x SHELL-SPEED) :v-x (* unit-x SHELL-SPEED)
:v-y (* unit-y SHELL-SPEED) :v-y (* unit-y SHELL-SPEED)

View File

@ -10,7 +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]
{:shells [] {: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,12 +28,12 @@
(robot/tick-robot (robots robot-idx) world)) (robot/tick-robot (robots robot-idx) world))
starting-world starting-world
(range (count (:robots 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) 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 damaged-world :shells live-shells)))) (assoc-in damaged-world [:shells :shell-map] live-shells))))
(def build-combined-worlds (partial iterate tick-combined-world)) (def build-combined-worlds (partial iterate tick-combined-world))