one action for walking between points.
This commit is contained in:
@@ -45,44 +45,53 @@
|
|||||||
|
|
||||||
(defn walk-to [entities target-id [final-x final-y]]
|
(defn walk-to [entities target-id [final-x final-y]]
|
||||||
(let [c (chan)
|
(let [c (chan)
|
||||||
entity (entities target-id)
|
{start-x :x start-y :y} (entities target-id)
|
||||||
|
final-x (int final-x)
|
||||||
|
final-y (int final-y)
|
||||||
path (vec (take-nth 5 (advent.pathfind/visit-all
|
path (vec (take-nth 5 (advent.pathfind/visit-all
|
||||||
(:collision (:background entities))
|
(:collision (:background entities))
|
||||||
[(int (:x entity)) (int (:y entity))]
|
[(int start-x) (int start-y)]
|
||||||
[(int final-x) (int final-y)])))
|
[final-x final-y])))
|
||||||
path (if (seq path)
|
path (if (seq path)
|
||||||
(conj path [(int final-x) (int final-y)])
|
(conj path [final-x final-y])
|
||||||
[])]
|
[])
|
||||||
(doseq [[target-x target-y] path]
|
targets-left (atom path)]
|
||||||
(put! (get-in entities [:actions :channel])
|
(if (seq path)
|
||||||
(reify
|
(do
|
||||||
IAction
|
(put! (get-in entities [:actions :channel])
|
||||||
(begin [this screen entities]
|
(reify
|
||||||
(let [{from-x :x from-y :y :keys [left right anim]} (entities target-id)]
|
IAction
|
||||||
(let [delta-x (- target-x from-x)]
|
(begin [this screen entities]
|
||||||
(assoc-in entities [target-id :anim] (if (< delta-x 0) left right)))))
|
entities
|
||||||
|
#_(let [{from-x :x from-y :y :keys [left right anim]} (entities target-id)]
|
||||||
|
(let [delta-x (- target-x from-x)]
|
||||||
|
(assoc-in entities [target-id :anim] (if (< delta-x 0) left right)))))
|
||||||
|
|
||||||
(continue [this screen entities]
|
(continue [this screen entities]
|
||||||
(let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (entities target-id)]
|
(let [{from-x :x from-y :y :keys [left right] :as target-entity} (entities target-id)
|
||||||
(let [delta-x (- target-x from-x)
|
[[target-x target-y] remainder] @targets-left]
|
||||||
delta-y (- target-y from-y)
|
(let [delta-x (- target-x from-x)
|
||||||
mag (Math/sqrt (+ (* delta-x delta-x) (* delta-y delta-y)))
|
delta-y (- target-y from-y)
|
||||||
moved-x (* 1.5 (/ delta-x mag))
|
distance (dist from-x from-y target-x target-y)
|
||||||
moved-y (* 1.5 (/ delta-y mag))]
|
moved-x (* 1.5 (/ delta-x distance))
|
||||||
(assoc entities target-id
|
moved-y (* 1.5 (/ delta-y distance))]
|
||||||
(jump-to screen entities target-entity [(+ moved-x from-x) (+ moved-y from-y)])))))
|
(when (< distance 1)
|
||||||
|
(swap! targets-left rest))
|
||||||
|
(update-in entities [target-id]
|
||||||
|
#(assoc (jump-to screen entities % [(+ moved-x from-x) (+ moved-y from-y)])
|
||||||
|
:anim (if (< delta-x 0) left right))))))
|
||||||
|
|
||||||
(done? [this screen entities]
|
(done? [this screen entities]
|
||||||
(let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (entities target-id)]
|
(let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (entities target-id)]
|
||||||
(< (dist target-x target-y from-x from-y) 1)))
|
(println final-x final-y from-x from-y (dist final-x final-y from-x from-y))
|
||||||
|
(doto (< (dist final-x final-y from-x from-y) 1) println)))
|
||||||
|
|
||||||
(terminate [this screen entities]
|
(terminate [this screen entities]
|
||||||
(doto (let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (entities target-id)]
|
(let [entities (stop screen entities target-id)]
|
||||||
(if (<= (dist final-x final-y from-x from-y) 1)
|
(put! c entities)
|
||||||
(stop screen entities target-id)
|
entities))))
|
||||||
entities))
|
(<!! c))
|
||||||
#(put! c %))))))
|
entities)))
|
||||||
(<!! c)))
|
|
||||||
|
|
||||||
(defn get-text-duration [text]
|
(defn get-text-duration [text]
|
||||||
(* (count (s/split text #" ")) 0.75))
|
(* (count (s/split text #" ")) 0.75))
|
||||||
|
|||||||
Reference in New Issue
Block a user