separated out tick-robot code into helper functions: move-robot and update-shot-timer

This commit is contained in:
Richard Harrington 2013-08-21 13:12:42 -04:00
parent 36ea96826f
commit 1a5b3e3727

View File

@ -21,6 +21,35 @@
:shot-timer 0.0
:brain (brain/init-brain src-code (register/init-registers idx))}))
(defn move-robot
"takes a robot and returns it, moved through space.
helper function for tick-robot."
[{:keys [pos-x pos-y v-x v-y desired-v-x desired-v-y] :as robot}]
(let [max-accel-x (if (neg? desired-v-x) (- MAX-ACCEL) MAX-ACCEL)
max-accel-y (if (neg? desired-v-y) (- MAX-ACCEL) MAX-ACCEL)
{new-pos-x :d, new-v-x :v} (phys/d-and-v-given-desired-v
pos-x
v-x
desired-v-x
max-accel-x
*GAME-SECONDS-PER-TICK*)
{new-pos-y :d, new-v-y :v} (phys/d-and-v-given-desired-v
pos-y
v-y
desired-v-y
max-accel-y
*GAME-SECONDS-PER-TICK*)]
(merge robot {:pos-x new-pos-x
:pos-y new-pos-y
:v-x new-v-x
:v-y new-v-y})))
(defn update-shot-timer
"takes a robot and returns one with the :shot-timer updated"
[{shot-timer :shot-timer :as robot}]
(merge robot {:shot-timer
(max (- shot-timer *GAME-SECONDS-PER-TICK*) 0.0)}))
(defn tick-robot
"takes a robot and a world and returns the new state of the world
after the robot has taken its turn.
@ -30,34 +59,9 @@
[{robot-idx :idx :as robot} world]
(if (<= (:damage robot) 0)
world
(let [new-world (brain/tick-brain
robot
world
register/read-register
register/write-register)
new-robot (get-in new-world [:robots robot-idx])
{:keys [pos-x pos-y v-x v-y desired-v-x desired-v-y shot-timer]} new-robot
max-accel-x (if (pos? desired-v-x) MAX-ACCEL (- MAX-ACCEL))
max-accel-y (if (pos? desired-v-y) MAX-ACCEL (- MAX-ACCEL))
{new-pos-x :d new-v-x :v} (phys/d-and-v-given-desired-v
pos-x
v-x
desired-v-x
max-accel-x
*GAME-SECONDS-PER-TICK*)
{new-pos-y :d new-v-y :v} (phys/d-and-v-given-desired-v
pos-y
v-y
desired-v-y
max-accel-y
*GAME-SECONDS-PER-TICK*)]
(assoc-in
new-world
[:robots robot-idx]
(into new-robot {:pos-x new-pos-x
:pos-y new-pos-y
:v-x new-v-x
:v-y new-v-y
:shot-timer (max (- shot-timer *GAME-SECONDS-PER-TICK*)
0.0)})))))
(let [ticked-world (brain/tick-brain
robot
world
register/read-register
register/write-register)]
(update-in ticked-world [:robots robot-idx] (comp update-shot-timer move-robot)))))