Merge branch 'background_scripts'

This commit is contained in:
Bryce Covert
2015-11-22 15:37:45 -08:00
10 changed files with 150 additions and 87 deletions

View File

@@ -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) [:actions :channel])
(put! (get-in (deref ~entities) [*fg-bg-key* :channel])
(reify IAction
(get-channel [_] c#)
~@forms))
@@ -62,36 +65,55 @@
(reset! ~entities result#))))))
(defn change-script-state [entities state]
(run-action entities
(begin [this screen entities]
(update-in entities [: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# [:actions :script-chan])
(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"))))))))
(defn force-end [entities current-action key]
(do (put! (get-channel current-action) :end)
(screen! dialogue/talking-screen :stop-talk :id key)
(-> entities
(assoc-in [key :script-running?] false)
(assoc-in [key :current] nil)
(assoc-in [key :started?] false))))
(defmacro get-unsaved-script [entities & forms]
`(fn [starting-entities#]
(put! (get-in starting-entities# [:actions :script-chan])
(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))))))))
@@ -183,6 +205,19 @@
(skip-type [this screen entities]
:skip)))
(defn do-force-end [entities key]
(run-action entities
(begin [this screen entities]
(if (get-in entities [key :script-running?])
(force-end entities (get-in entities [key :current]) key)
entities))
(continue [this screen entities] entities)
(done? [this screen entities] true)
(terminate [this screen entities]
entities)
(skip-type [this screen entities]
:none)))
(defn walk-straight-to [entities target-id [final-x final-y] & {:keys [update-baseline? face speed anim override-dir stop?]}]
@@ -352,7 +387,8 @@
(defn talk [entities target-id text & {:keys [stop? animate? anim wait color] :or {wait true}}]
(let [initial-time (atom nil)
stop? (if (nil? stop?) true stop?)
animate? (if (nil? animate?) true animate?)]
animate? (if (nil? animate?) true animate?)
fg-or-bg *fg-bg-key*]
(run-action entities
(begin [this screen entities]
(let [_ (swap! initial-time #(or % (:total-time screen)))
@@ -369,6 +405,7 @@
scale (get-in entities [:room :entities target-id :scale-y] 1)
height (* scale height)]
(screen! dialogue/talking-screen :on-talk :text text
:id fg-or-bg
:scene-viewport (:viewport screen)
:x target-x :y (+ target-y height)
:color (or color (get-in entities [:room :entities target-id :talk-color]))
@@ -391,7 +428,7 @@
(terminate [this screen entities]
(if wait
(do
(screen! dialogue/talking-screen :stop-talk :target-id target-id)
(screen! dialogue/talking-screen :stop-talk :id fg-or-bg)
(if stop?
(stop screen entities target-id)
entities))
@@ -648,12 +685,16 @@
(assoc-in [:state :next-time] nil))
entities)
:ego
:face face)]
:face face)
entities (if (get-in entities [:bg-actions :script-running?])
(force-end entities (get-in entities [:bg-actions :current]) :bg-actions)
entities)]
(if-let [stop-fn (get-in entities [:room :stop-fn])]
(stop-fn screen entities)
entities)))
(skip-type [this screen entities]
:none))
(screen! dialogue/talking-screen :stop-talk :id :bg-actions)
(run-action entities
(begin [this screen entities]
(utils/stop-all-sounds! entities)