proper stopping.
This commit is contained in:
@@ -19,7 +19,7 @@
|
|||||||
(terminate [this screen entities]))
|
(terminate [this screen entities]))
|
||||||
|
|
||||||
(defmacro get-script [& forms]
|
(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]]
|
(defn jump-to [screen entities entity [x y]]
|
||||||
@@ -31,20 +31,6 @@
|
|||||||
(assoc entity :scale-x (scale-fn y) :scale-y (scale-fn y))
|
(assoc entity :scale-x (scale-fn y) :scale-y (scale-fn y))
|
||||||
entity)))
|
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]
|
(defn stop [screen entities target-id]
|
||||||
(update-in entities [target-id] #(merge %
|
(update-in entities [target-id] #(merge %
|
||||||
{:anim nil}
|
{:anim nil}
|
||||||
@@ -52,13 +38,19 @@
|
|||||||
(texture (animation! (:anim %) :get-key-frame 0.25))))))
|
(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)
|
(let [c (chan)
|
||||||
entity (entities target-id)
|
entity (entities target-id)
|
||||||
path (take-nth 5 (advent.pathfind/visit-all
|
path (conj (vec (take-nth 5 (advent.pathfind/visit-all
|
||||||
(:collision (:background entities))
|
(:collision (:background entities))
|
||||||
[(int (:x entity)) (int (:y entity))]
|
[(int (:x entity)) (int (:y entity))]
|
||||||
[(int x) (int y)]))]
|
[(int final-x) (int final-y)])))
|
||||||
|
[(int final-x) (int final-y)])]
|
||||||
(doseq [[target-x target-y] path]
|
(doseq [[target-x target-y] path]
|
||||||
(put! (get-in entities [:actions :channel])
|
(put! (get-in entities [:actions :channel])
|
||||||
(reify
|
(reify
|
||||||
@@ -80,14 +72,14 @@
|
|||||||
|
|
||||||
(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)]
|
||||||
(let [delta-x (- target-x from-x)
|
(< (dist target-x target-y from-x from-y) 1)))
|
||||||
delta-y (- target-y from-y)
|
|
||||||
mag (Math/sqrt (+ (* delta-x delta-x) (* delta-y delta-y)))]
|
|
||||||
(< mag 1))))
|
|
||||||
|
|
||||||
(terminate [this screen entities]
|
(terminate [this screen entities]
|
||||||
(put! c entities)
|
(doto (let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (entities target-id)]
|
||||||
(stop screen entities target-id)))))
|
(if (<= (dist final-x final-y from-x from-y) 1)
|
||||||
|
(stop screen entities target-id)
|
||||||
|
entities))
|
||||||
|
#(put! c %))))))
|
||||||
(<!! c)))
|
(<!! c)))
|
||||||
|
|
||||||
(defn get-text-duration [text]
|
(defn get-text-duration [text]
|
||||||
|
|||||||
@@ -106,17 +106,15 @@
|
|||||||
(merge (texture (animation! (:right ego) :get-key-frame 0.25)) ego)))
|
(merge (texture (animation! (:right ego) :get-key-frame 0.25)) ego)))
|
||||||
|
|
||||||
(defn update-from-script [screen {{:keys [current started? channel]} :actions :as entities}]
|
(defn update-from-script [screen {{:keys [current started? channel]} :actions :as entities}]
|
||||||
(if (= :end current)
|
(if current
|
||||||
(assoc entities :actions {:channel channel :current nil :started? false})
|
(let [entities (if started? entities (actions/begin current screen entities))
|
||||||
(if current
|
entities (actions/continue current screen entities)]
|
||||||
(let [entities (if started? entities (actions/begin current screen entities))
|
(if (actions/done? current screen entities)
|
||||||
entities (actions/continue current screen entities)]
|
(recur screen (assoc (actions/terminate current screen entities)
|
||||||
(if (actions/done? current screen entities)
|
:actions {:channel channel :current nil :started? false}))
|
||||||
(assoc (actions/terminate current screen entities)
|
(assoc-in entities [:actions :started?] true)))
|
||||||
:actions {:channel channel :current nil :started? false})
|
(let [[current _] (alts!! [channel] :default nil)]
|
||||||
(assoc-in entities [:actions :started?] true)))
|
(assoc entities :actions {:channel channel :current current :started? false}))))
|
||||||
(let [[current _] (alts!! [channel] :default nil)]
|
|
||||||
(assoc entities :actions {:channel channel :current current :started? false})))))
|
|
||||||
|
|
||||||
(defn scaler-fn-with-baseline [baseline minimum-size & [maximum-size]]
|
(defn scaler-fn-with-baseline [baseline minimum-size & [maximum-size]]
|
||||||
(let [maximum-size (or maximum-size 1.0)]
|
(let [maximum-size (or maximum-size 1.0)]
|
||||||
|
|||||||
Reference in New Issue
Block a user