continued refactoring actions

This commit is contained in:
2014-09-16 23:18:36 -07:00
parent d86ea452ab
commit 8f32ec50d3
2 changed files with 85 additions and 97 deletions

View File

@@ -16,7 +16,8 @@
(begin [this screen entities]) (begin [this screen entities])
(done? [this screen entities]) (done? [this screen entities])
(continue [this screen entities]) (continue [this screen entities])
(terminate [this screen entities])) (terminate [this screen entities])
(get-channel [this]))
(defmacro get-script [entities & forms] (defmacro get-script [entities & forms]
`(fn [starting-entities#] `(fn [starting-entities#]
@@ -45,9 +46,17 @@
dy (- y1 y2)] dy (- y1 y2)]
(Math/sqrt (+ (* dx dx) (* dy dy))))) (Math/sqrt (+ (* dx dx) (* dy dy)))))
(defmacro run-action [entities & forms]
`(let [c# (chan)]
(do
(put! (get-in (deref ~entities) [:actions :channel])
(reify IAction
(get-channel [_] c#)
~@forms))
(reset! ~entities (<!! c#)))))
(defn walk-to [entities target-id [final-x final-y]] (defn walk-to [entities target-id [final-x final-y]]
(let [c (chan) (let [{start-x :x start-y :y} (@entities target-id)
{start-x :x start-y :y} (@entities target-id)
final-x (int final-x) final-x (int final-x)
final-y (int final-y) final-y (int final-y)
path (vec (take-nth 5 (advent.pathfind/visit-all path (vec (take-nth 5 (advent.pathfind/visit-all
@@ -59,118 +68,95 @@
[]) [])
targets-left (atom path)] targets-left (atom path)]
(if (seq path) (if (seq path)
(do (run-action entities
(put! (get-in @entities [:actions :channel]) (begin [this screen entities]
(reify entities)
IAction
(begin [this screen entities]
entities)
(continue [this screen entities] (continue [this screen entities]
(let [{from-x :x from-y :y :keys [left right] :as target-entity} (entities target-id) (let [{from-x :x from-y :y :keys [left right] :as target-entity} (entities target-id)
[[target-x target-y] remainder] @targets-left] [[target-x target-y] remainder] @targets-left]
(let [delta-x (- target-x from-x) (let [delta-x (- target-x from-x)
delta-y (- target-y from-y) delta-y (- target-y from-y)
distance (dist from-x from-y target-x target-y) distance (dist from-x from-y target-x target-y)
moved-x (if (= 0.0 distance) moved-x (if (= 0.0 distance)
0 0
(* 1.5 (/ delta-x distance))) (* 1.5 (/ delta-x distance)))
moved-y (if (= 0.0 distance) moved-y (if (= 0.0 distance)
0 0
(* 1.5 (/ delta-y distance)))] (* 1.5 (/ delta-y distance)))]
(if (< distance 1) (if (< distance 1)
(do (swap! targets-left rest) (do (swap! targets-left rest)
entities) entities)
(update-in entities [target-id] (update-in entities [target-id]
#(assoc (jump-to screen entities % [(+ moved-x from-x) (+ moved-y from-y)]) #(assoc (jump-to screen entities % [(+ moved-x from-x) (+ moved-y from-y)])
:anim (cond (< delta-x 0) left :anim (cond (< delta-x 0) left
(> delta-x 0) right (> delta-x 0) right
:else (:anim %)))))))) :else (:anim %))))))))
(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 final-x final-y from-x from-y) 1))) (< (dist final-x final-y from-x from-y) 1)))
(terminate [this screen entities] (terminate [this screen entities]
(let [entities (stop screen entities target-id)] (stop screen entities target-id)))
(put! c entities)
entities))))
(reset! entities (<!! c)))
@entities))) @entities)))
(defn get-text-duration [text] (defn get-text-duration [text]
(* (count (s/split text #" ")) 0.75)) (* (count (s/split text #" ")) 0.75))
(defn talk [entities target-id text] (defn talk [entities target-id text]
(let [c (chan) (let [initial-time (atom nil)]
initial-time (atom nil)] (run-action entities
(put! (get-in @entities [:actions :channel]) (begin [this screen entities]
(reify (let [_ (swap! initial-time #(or % (:total-time screen)))
IAction target-y (get-in entities [target-id :y])
(begin [this screen entities] scale-fn (get-in entities [:background :scale-fn])
(let [_ (swap! initial-time #(or % (:total-time screen))) scale (scale-fn target-y)
target-y (get-in entities [target-id :y]) height (* scale 36)]
scale-fn (get-in entities [:background :scale-fn]) (run! dialogue/talking-screen :on-talk :text text
scale (scale-fn target-y) :x (get-in entities [target-id :x]) :y (+ (get-in entities [target-id :y]) height)
height (* scale 36)] :target-id target-id
(run! dialogue/talking-screen :on-talk :text text :scale scale)
:x (get-in entities [target-id :x]) :y (+ (get-in entities [target-id :y]) height) (assoc-in entities [target-id :anim] (get-in entities [target-id :talk]))))
:target-id target-id
:scale scale)
(assoc-in entities [target-id :anim] (get-in entities [target-id :talk]))))
(continue [this screen entities] entities) (continue [this screen entities] entities)
(done? [this screen entities] (done? [this screen entities]
(> (- (:total-time screen) (> (- (:total-time screen)
@initial-time) @initial-time)
(get-text-duration text))) (get-text-duration text)))
(terminate [this screen entities] (terminate [this screen entities]
(put! c entities) (run! dialogue/talking-screen :stop-talk :target-id target-id)
(run! dialogue/talking-screen :stop-talk :target-id target-id) (stop screen entities target-id)))))
(stop screen entities target-id))))
(reset! entities (<!! c))))
(defn give [entities target-id item] (defn give [entities target-id item]
(let [c (chan)] (run-action entities
(put! (get-in @entities [:actions :channel]) (begin [this screen entities]
(reify (sound! (sound "pickup.mp3") :play)
IAction
(begin [this screen entities]
(sound! (sound "pickup.mp3") :play)
(-> entities (-> entities
(update-in [target-id :inventory] #(conj % item)) (update-in [target-id :inventory] #(conj % item))
(assoc-in [:cursor :current] item))) (assoc-in [:cursor :current] item)))
(continue [this screen entities] entities) (continue [this screen entities] entities)
(done? [this screen entities] (done? [this screen entities] true)
true)
(terminate [this screen entities] (terminate [this screen entities]
(put! c entities) entities)))
entities)))
(reset! entities (<!! c))))
(defn transition-background [entities new-background [x y]] (defn transition-background [entities new-background [x y]]
(let [c (chan)] (run-action entities
(put! (get-in @entities [:actions :channel]) (begin [this screen entities]
(reify (-> entities
IAction (assoc-in [:background] (get-in entities [:backgrounds new-background]))
(begin [this screen entities] (assoc-in [:ego :x] x)
(-> entities (assoc-in [:ego :y] y)))
(assoc-in [:background] (get-in entities [:backgrounds new-background]))
(assoc-in [:ego :x] x)
(assoc-in [:ego :y] y)))
(continue [this screen entities] entities) (continue [this screen entities] entities)
(done? [this screen entities] (done? [this screen entities] true)
true)
(terminate [this screen entities] (terminate [this screen entities]
(put! c entities) entities)))
entities)))
(reset! entities (<!! c))))

View File

@@ -79,8 +79,10 @@
(let [entities (if started? entities (actions/begin current screen entities)) (let [entities (if started? entities (actions/begin current screen entities))
entities (actions/continue current screen entities)] entities (actions/continue current screen entities)]
(if (actions/done? current screen entities) (if (actions/done? current screen entities)
(recur screen (assoc (actions/terminate current screen entities) (let [terminated (actions/terminate current screen entities)]
:actions {:channel channel :current nil :started? false})) (put! (actions/get-channel current) terminated)
(recur screen (assoc terminated
:actions {:channel channel :current nil :started? false})))
(assoc-in entities [:actions :started?] true))) (assoc-in entities [:actions :started?] true)))
(let [[current _] (alts!! [channel] :default nil)] (let [[current _] (alts!! [channel] :default nil)]
(assoc entities :actions {:channel channel :current current :started? false})))) (assoc entities :actions {:channel channel :current current :started? false}))))