proper stopping.

This commit is contained in:
=
2014-09-15 16:52:10 -07:00
parent fd91c7cc55
commit 73c273afad
2 changed files with 27 additions and 37 deletions

View File

@@ -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 %))))))
(<!! c)))
(defn get-text-duration [text]

View File

@@ -106,17 +106,15 @@
(merge (texture (animation! (:right ego) :get-key-frame 0.25)) ego)))
(defn update-from-script [screen {{:keys [current started? channel]} :actions :as entities}]
(if (= :end current)
(assoc entities :actions {:channel channel :current nil :started? false})
(if current
(let [entities (if started? entities (actions/begin current screen entities))
entities (actions/continue current screen entities)]
(if (actions/done? current screen entities)
(assoc (actions/terminate current screen entities)
:actions {:channel channel :current nil :started? false})
(assoc-in entities [:actions :started?] true)))
(let [[current _] (alts!! [channel] :default nil)]
(assoc entities :actions {:channel channel :current current :started? false})))))
(if current
(let [entities (if started? entities (actions/begin current screen entities))
entities (actions/continue current screen entities)]
(if (actions/done? current screen entities)
(recur screen (assoc (actions/terminate current screen entities)
:actions {:channel channel :current nil :started? false}))
(assoc-in entities [:actions :started?] true)))
(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]]
(let [maximum-size (or maximum-size 1.0)]