finishing refactor.

This commit is contained in:
=
2014-09-04 23:10:12 -07:00
parent cc57ab54d4
commit 46d5399caf

View File

@@ -40,26 +40,26 @@
(assoc entity :scale-x (scale-fn y) :scale-y (scale-fn y))
entity)))
(defn walk-to-fn [[x y] target-id]
(defn walk-to-fn [[target-x target-y] target-id]
(fn [screen entities]
(let [{:keys [left right anim] :as target-entity} (entities target-id)
target-loc {:x x :y y}]
(let [delta-x (- x (:x target-entity))
delta-y (- y (:y target-entity))
(let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (entities target-id)]
(let [delta-x (- target-x from-x)
delta-y (- target-y from-y)
mag (Math/sqrt (+ (* delta-x delta-x) (* delta-y delta-y)))
vector-x (* 1.5 (/ delta-x mag))
vector-y (* 1.5 (/ delta-y mag))]
moved-x (* 1.5 (/ delta-x mag))
moved-y (* 1.5 (/ delta-y mag))]
(if (< mag 1)
(assoc entities target-id (assoc target-entity :actions (rest (:actions target-entity)) :anim nil))
(assoc entities target-id
(assoc (move-to screen entities target-entity [(+ vector-x (:x target-entity)) (+ vector-y (:y target-entity))])
:anim (if (< vector-x 0) left right))))))))
(assoc (move-to screen entities target-entity [(+ moved-x from-x) (+ moved-y from-y)])
:anim (if (< moved-x 0) left right))))))))
(defn stop-fn [target-id]
(fn [screen entities]
(let [target (target-id entities)]
(assoc-in entities [target-id] (merge target
{:anim nil}
{:anim nil
:actions (rest (:actions target))}
(when (:anim target)
(texture (animation! (:anim target) :get-key-frame 0.25))))))))
@@ -68,11 +68,12 @@
path (vec (take-nth 2 (advent.pathfind/visit-all
(:collision (:background entities))
[(int (:x (:ego entities))) (int (:y (:ego entities)))]
[(int x) (int y)])))]
(assoc-in entities [:ego :actions] (when (seq path)
(concat
(vec (map #(walk-to-fn % :ego) (conj path [x y])))
[(stop-fn :ego)])))))
[(int x) (int y)])))
actions (when (seq path)
(conj
(vec (map #(walk-to-fn % :ego) (conj path [x y])))
(stop-fn :ego)))]
(assoc-in entities [:ego :actions] actions)))
(defn get-ego [screen]
(let [player-sheet (texture! (texture "player.png") :split 18 36)
ego {:right (animation 0.075 (for [i (range 8)]