From 73c273afad436b198569812d08aa00d4b5959ac7 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 15 Sep 2014 16:52:10 -0700 Subject: [PATCH] proper stopping. --- desktop/src-common/advent/actions.clj | 44 +++++++++------------ desktop/src-common/advent/screens/scene.clj | 20 +++++----- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/desktop/src-common/advent/actions.clj b/desktop/src-common/advent/actions.clj index 6da482d0..07bd9ab2 100644 --- a/desktop/src-common/advent/actions.clj +++ b/desktop/src-common/advent/actions.clj @@ -19,7 +19,7 @@ (terminate [this screen entities])) (defmacro get-script [& forms] - `(fn [action-channel#] (thread ~@forms (put! action-channel# :end)))) + `(fn [action-channel#] (thread ~@forms ))) (defn jump-to [screen entities entity [x y]] @@ -31,20 +31,6 @@ (assoc entity :scale-x (scale-fn y) :scale-y (scale-fn y)) entity))) -(defn walk-to [[target-x target-y] target-id] - (fn [screen entities] - (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))) - 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 (jump-to screen entities target-entity [(+ moved-x from-x) (+ moved-y from-y)]) - :anim (if (< moved-x 0) left right)))))))) - (defn stop [screen entities target-id] (update-in entities [target-id] #(merge % {:anim nil} @@ -52,13 +38,19 @@ (texture (animation! (:anim %) :get-key-frame 0.25)))))) -(defn walk-to [entities target-id [x y]] +(defn dist [x1 y1 x2 y2] + (let [dx (- x1 x2) + dy (- y1 y2)] + (Math/sqrt (+ (* dx dx) (* dy dy))))) + +(defn walk-to [entities target-id [final-x final-y]] (let [c (chan) entity (entities target-id) - path (take-nth 5 (advent.pathfind/visit-all - (:collision (:background entities)) - [(int (:x entity)) (int (:y entity))] - [(int x) (int y)]))] + path (conj (vec (take-nth 5 (advent.pathfind/visit-all + (:collision (:background entities)) + [(int (:x entity)) (int (:y entity))] + [(int final-x) (int final-y)]))) + [(int final-x) (int final-y)])] (doseq [[target-x target-y] path] (put! (get-in entities [:actions :channel]) (reify @@ -80,14 +72,14 @@ (done? [this screen entities] (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)))] - (< mag 1)))) + (< (dist target-x target-y from-x from-y) 1))) (terminate [this screen entities] - (put! c entities) - (stop screen entities target-id))))) + (doto (let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (entities target-id)] + (if (<= (dist final-x final-y from-x from-y) 1) + (stop screen entities target-id) + entities)) + #(put! c %)))))) (