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

View File

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

View File

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

View File

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