diff --git a/desktop/src-common/advent/actions.clj b/desktop/src-common/advent/actions.clj index 131b1edb..b93ba5a7 100644 --- a/desktop/src-common/advent/actions.clj +++ b/desktop/src-common/advent/actions.clj @@ -47,10 +47,13 @@ (seq (set/intersection (set (get-in entities [:state :inventory])) (set items))) (seq (set/intersection (set (get-in @entities [:state :inventory])) (set items))))) +(def ^{:dynamic true} *fg-bg-key* :fg-actions) + + (defmacro run-action [entities & forms] `(let [c# (chan)] (do - (put! (get-in (deref ~entities) [:fg-actions :channel]) + (put! (get-in (deref ~entities) [*fg-bg-key* :channel]) (reify IAction (get-channel [_] c#) ~@forms)) @@ -62,26 +65,37 @@ (reset! ~entities result#)))))) (defn change-script-state [entities state] - (run-action entities - (begin [this screen entities] - (update-in entities [:fg-actions] assoc :script-running? state :last-skip-type (if state :end nil))) + (let [fg-bg-key *fg-bg-key*] + (run-action entities + (begin [this screen entities] + (update-in entities [fg-bg-key] assoc :script-running? state :last-skip-type (if state :end nil))) - (continue [this screen entities] entities) + (continue [this screen entities] entities) - (done? [this screen entities] - true) + (done? [this screen entities] + true) - (terminate [this screen entities] - entities) - (skip-type [this screen entities] - :none-but-arrow))) + (terminate [this screen entities] + entities) + (skip-type [this screen entities] + :none-but-arrow)))) + +(defmacro get-bg-script [entities & forms] + `(fn [starting-entities#] + (put! (get-in starting-entities# [:bg-actions :script-chan]) + (fn [starting-entities#] + (let [~entities (atom starting-entities#)] + (thread (binding [*fg-bg-key* :bg-actions] + ~@forms + (change-script-state ~entities false) + ))))))) (defmacro get-script [entities & forms] `(fn [starting-entities#] (put! (get-in starting-entities# [:fg-actions :script-chan]) (fn [starting-entities#] (let [~entities (atom starting-entities#)] - (thread (do + (thread (binding [*fg-bg-key* :fg-actions] ~@forms (change-script-state ~entities false) (utils/save @~entities :autosave "Autosave")))))))) @@ -91,7 +105,7 @@ (put! (get-in starting-entities# [:fg-actions :script-chan]) (fn [starting-entities#] (let [~entities (atom starting-entities#)] - (thread (do + (thread (binding [*fg-bg-key* :fg-actions] ~@forms (change-script-state ~entities false)))))))) diff --git a/desktop/src-common/advent/screens/rooms/inside_castle.clj b/desktop/src-common/advent/screens/rooms/inside_castle.clj index fe518c20..405b5a02 100644 --- a/desktop/src-common/advent/screens/rooms/inside_castle.clj +++ b/desktop/src-common/advent/screens/rooms/inside_castle.clj @@ -15,10 +15,12 @@ [play-clj.g2d :refer :all])) (defn brian [screen entities] + (when (and (not (get-in entities [:fg-actions :script-running?])) + (not (get-in entities [:bg-actions :script-running?])) (get-in entities [:state :active?]) (get-in entities [:room :entities :game-player])) - ((actions/get-script entities + ((actions/get-bg-script entities ((rand-nth [#(actions/do-dialogue entities :game-player "2 to the power of pi." :game-player "No, that's not right.") #(actions/do-dialogue entities :game-player "Eureka!" @@ -362,7 +364,7 @@ ] (rooms/make :music {:day :town-1 :night :night :sunrise :night} :name "Inside castle" - :timers {:brian [10.0 20.0 brian]} + :timers {:brian [10.0 2.0 brian]} :interactions {:right-door {:box [286 140 306 160] :cursor :right