started revamping actions some more for making interactions simple.

This commit is contained in:
=
2014-09-16 13:01:54 -07:00
parent c91e0d7f52
commit 0f7ac68195
2 changed files with 46 additions and 50 deletions

View File

@@ -18,8 +18,10 @@
(continue [this screen entities]) (continue [this screen entities])
(terminate [this screen entities])) (terminate [this screen entities]))
(defmacro get-script [& forms] (defmacro get-script [entities & forms]
`(fn [action-channel#] (thread ~@forms ))) `(fn [starting-entities#]
(let [~entities (atom starting-entities#)]
(thread ~@forms))))
(defn jump-to [screen entities entity [x y]] (defn jump-to [screen entities entity [x y]]
@@ -45,11 +47,11 @@
(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)
{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
(:collision (:background entities)) (:collision (:background @entities))
[(int start-x) (int start-y)] [(int start-x) (int start-y)]
[final-x final-y]))) [final-x final-y])))
path (if (seq path) path (if (seq path)
@@ -58,14 +60,11 @@
targets-left (atom path)] targets-left (atom path)]
(if (seq path) (if (seq path)
(do (do
(put! (get-in entities [:actions :channel]) (put! (get-in @entities [:actions :channel])
(reify (reify
IAction IAction
(begin [this screen entities] (begin [this screen entities]
entities 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] :as target-entity} (entities target-id) (let [{from-x :x from-y :y :keys [left right] :as target-entity} (entities target-id)
@@ -92,7 +91,7 @@
(let [entities (stop screen entities target-id)] (let [entities (stop screen entities target-id)]
(put! c entities) (put! c entities)
entities)))) entities))))
(<!! c)) (reset! entities (<!! c)))
entities))) entities)))
(defn get-text-duration [text] (defn get-text-duration [text]
@@ -101,7 +100,7 @@
(defn talk [entities target-id text] (defn talk [entities target-id text]
(let [c (chan) (let [c (chan)
initial-time (atom nil)] initial-time (atom nil)]
(put! (get-in entities [:actions :channel]) (put! (get-in @entities [:actions :channel])
(reify (reify
IAction IAction
(begin [this screen entities] (begin [this screen entities]
@@ -127,11 +126,11 @@
(put! c 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))))
(<!! c))) (reset! entities (<!! c))))
(defn give [entities target-id item] (defn give [entities target-id item]
(let [c (chan)] (let [c (chan)]
(put! (get-in entities [:actions :channel]) (put! (get-in @entities [:actions :channel])
(reify (reify
IAction IAction
(begin [this screen entities] (begin [this screen entities]
@@ -149,4 +148,4 @@
(terminate [this screen entities] (terminate [this screen entities]
(put! c entities) (put! c entities)
entities))) entities)))
(<!! c))) (reset! entities (<!! c))))

View File

@@ -24,18 +24,16 @@
(cursor-override [this])) (cursor-override [this]))
(defprotocol IInteractable (defprotocol IInteractable
(interact [this screen entities location])) (get-script [this location]))
(def default-interaction (def default-interaction
(reify (reify
IInteractable IInteractable
(interact [_ screen entities [x y]] (get-script [_ [x y]]
(actions/get-script (actions/get-script entities
(actions/walk-to entities :ego [x y]))))) (actions/walk-to entities :ego [x y])))))
(defn find-override [screen entities [x y]] (defn find-override [screen entities [x y]]
(first (filter #(and (mouse-in? % [x y]) (first (filter #(and (mouse-in? % [x y])
(satisfies? ICursorOverridable %)) (satisfies? ICursorOverridable %))
@@ -51,10 +49,10 @@
:actions {:channel (chan) :current nil :started? false}) :actions {:channel (chan) :current nil :started? false})
entities) entities)
script (or (when interaction script (or (when interaction
(interact interaction screen entities [x y])) (get-script interaction [x y]))
(interact default-interaction screen entities [x y]))] (get-script default-interaction [x y]))]
(script (get-in entities [:actions :channel])) (script entities)
entities)) entities))
(defn get-ego [screen] (defn get-ego [screen]
@@ -108,8 +106,8 @@
(mouse-in? [_ location] (mouse-in? [_ location]
(apply (apply zone/box (:box spec)) location)) (apply (apply zone/box (:box spec)) location))
IInteractable IInteractable
(interact [_ screen entities location] (get-script [_ location]
((:script spec) entities)) (:script spec))
ICursorOverridable ICursorOverridable
(cursor-override [this] (:cursor spec))) (cursor-override [this] (:cursor spec)))
)] )]
@@ -144,39 +142,38 @@
:sheep (assoc (animation->texture screen sheep) :x 38 :y 160 :baseline 160 :anim sheep) :sheep (assoc (animation->texture screen sheep) :x 38 :y 160 :baseline 160 :anim sheep)
:background (make-background :interactions :background (make-background :interactions
{:door {:box [258 100 281 160] {:door {:box [258 100 281 160]
:script (fn [entities] :script (actions/get-script
(actions/get-script entities
(actions/walk-to entities :ego [262 88]) (actions/walk-to entities :ego [262 88])
(actions/talk entities :ego (str "Anyone home?"))))} (actions/talk entities :ego (str "Anyone home?")))}
:sword {:box [274 55 305 88] :sword {:box [274 55 305 88]
:script (fn [entities] :script (actions/get-script
(actions/get-script entities
(actions/talk entities :ego (str "It's the coolest sword I've ever seen!")) (actions/talk entities :ego (str "It's the coolest sword I've ever seen!"))
(actions/walk-to entities :ego [290 66]) (actions/walk-to entities :ego [290 66])
(actions/talk entities :ego "Maybe I can pull it out.")))} (actions/talk entities :ego "Maybe I can pull it out."))}
:sheep {:box [38 160 71 181] :sheep {:box [38 160 71 181]
:script (fn [entities] :script (actions/get-script
(actions/get-script entities
(if ((get-in entities [:ego :inventory]) :wool) (if ((get-in @entities [:ego :inventory]) :wool)
(actions/talk entities :ego "The sheep has given me enough wool.") (actions/talk entities :ego "The sheep has given me enough wool.")
(do (actions/give entities :ego :wool) (do (actions/give entities :ego :wool)
(actions/talk entities :ego "I guess his wool is shedding.")))))} (actions/talk entities :ego "I guess his wool is shedding."))))}
:right-dir {:box [300 131 320 224] :right-dir {:box [300 131 320 224]
:script (fn [entities] :script (actions/get-script
(actions/get-script entities
(actions/walk-to entities :ego [319 160]))) (actions/walk-to entities :ego [319 160]))
:cursor :right} :cursor :right}
:up-dir {:box [60 180 224 240] :up-dir {:box [60 180 224 240]
:script (fn [entities] :script (actions/get-script
(actions/get-script entities
(actions/walk-to entities :ego [137 204]))) (actions/walk-to entities :ego [137 204]))
:cursor :up} :cursor :up}
:left-dir {:box [0 40 20 140] :left-dir {:box [0 40 20 140]
:script (fn [entities] :script (actions/get-script
(actions/get-script entities
(actions/walk-to entities :ego [0 80]))) (actions/walk-to entities :ego [0 80]))
:cursor :left}} :cursor :left}}
:layers [(assoc background :x 0 :y 0 :baseline 0) :layers [(assoc background :x 0 :y 0 :baseline 0)
(assoc house :x 0 :y 0 :baseline 122) (assoc house :x 0 :y 0 :baseline 122)