ordered dialogue trees

This commit is contained in:
2014-09-27 10:19:20 -07:00
parent 9da703004a
commit fd3b07bf12
3 changed files with 17 additions and 14 deletions

View File

@@ -163,30 +163,33 @@
(recur (inc so-far) (zip/right zipper))))) (recur (inc so-far) (zip/right zipper)))))
(defn make-zipper [tree] (defn make-zipper [tree]
(zip/zipper map? (comp vals :choices) (fn [n c] nil) tree)) (zip/zipper map? (comp #(map second %) #(partition 2 %) :choices) (fn [n c] nil) tree))
(defn present-choices [entities choices] (defn present-choices [entities choices]
(loop [zipper (make-zipper choices)] (loop [zipper (make-zipper choices)]
(let [selected-choice (atom nil) (let [selected-index (atom nil)
node (zip/node zipper)] node (zip/node zipper)
dialogue-choices (partition 2 (:choices node))]
(run-action entities (run-action entities
(begin [this screen entities] (begin [this screen entities]
(run! dialogue/choice-screen :on-present-choices :choices (:choices node) :callback #(reset! selected-choice %)) (run! dialogue/choice-screen :on-present-choices :choices dialogue-choices :callback #(reset! selected-index %))
(run! @(resolve 'advent.screens.scene/scene) :on-deactivate) (run! @(resolve 'advent.screens.scene/scene) :on-deactivate)
entities) entities)
(continue [this screen entities] entities) (continue [this screen entities] entities)
(done? [this screen entities] (not (nil? @selected-choice))) (done? [this screen entities] (not (nil? @selected-index)))
(terminate [this screen entities] (terminate [this screen entities]
(run! @(resolve 'advent.screens.scene/scene) :on-reactivate) (run! @(resolve 'advent.screens.scene/scene) :on-reactivate)
entities)) entities))
(let [zipper (nth-child zipper (-> node :choices keys (.indexOf @selected-choice))) (let [zipper (nth-child zipper @selected-index)
node (zip/node zipper)] node (zip/node zipper)]
(when-let [run (:run node)] (when-let [run (:run node)]
(run @selected-choice)) (run (-> dialogue-choices
(nth @selected-index)
first)))
(when-let [next-choices (:choices node)] (when-let [next-choices (:choices node)]
(if (fn? next-choices) (if (fn? next-choices)
(recur (next-choices zipper)) (recur (next-choices zipper))

View File

@@ -75,7 +75,7 @@
tx (.getTexture tr) tx (.getTexture tr)
_ (texture! tx :set-filter Texture$TextureFilter/Linear Texture$TextureFilter/Linear)] _ (texture! tx :set-filter Texture$TextureFilter/Linear Texture$TextureFilter/Linear)]
(-> entities (-> entities
(into (for [[text i] (map vector (keys choices) (range))] (into (for [[[text] i] (map vector choices (range))]
[i (assoc (label text (style :label font (color :white))) :x 30 :y (* 30 i))])) [i (assoc (label text (style :label font (color :white))) :x 30 :y (* 30 i))]))
(assoc :state {:object nil :callback callback :choices choices})))) (assoc :state {:object nil :callback callback :choices choices}))))
@@ -83,7 +83,7 @@
(let [{:keys [x y]} (input->screen screen {:x (:input-x screen) :y (:input-y screen)})] (let [{:keys [x y]} (input->screen screen {:x (:input-x screen) :y (:input-y screen)})]
(when (seq entities) (when (seq entities)
(when (< y (* 30 (count entities))) (when (< y (* 30 (count entities)))
((get-in entities [:state :callback]) (nth (keys (get-in entities [:state :choices])) (int (/ y 30)))) ((get-in entities [:state :callback]) (int (/ y 30)))
{})))) {}))))
:on-mouse-moved (fn [screen [entities]] :on-mouse-moved (fn [screen [entities]]

View File

@@ -160,9 +160,9 @@
(defn wizard-dialogue [entities] (defn wizard-dialogue [entities]
{ {
:choices {"What do you mean, \"Not you again?\"" :choices ["What do you mean, \"Not you again?\""
{:run #(respond entities % :wizard "I mean, you've wrecked my life and I never want to see you again.") {:run #(respond entities % :wizard "I mean, you've wrecked my life and I never want to see you again.")
:choices {"You mean the time I set your house on fire with a fire mint?" :choices ["You mean the time I set your house on fire with a fire mint?"
{:run #(do {:run #(do
(respond entities % (respond entities %
:wizard "That was you!?" :wizard "That was you!?"
@@ -185,16 +185,16 @@
(actions/transition-background entities :outside-house [262 88]))} (actions/transition-background entities :outside-house [262 88]))}
"Even an old hoot like you needs a kick in the pants every now and again." "Even an old hoot like you needs a kick in the pants every now and again."
{:run #(respond entities % :wizard "What gives you the right to try to teach me a lesson?") {:run #(respond entities % :wizard "What gives you the right to try to teach me a lesson?")
:choices {"My good looks?" {:choices actions/previous-choices} :choices ["My good looks?" {:choices actions/previous-choices}
"My good standing within the community?" {:choices actions/previous-choices} "My good standing within the community?" {:choices actions/previous-choices}
"My respectful attitude?" {:choices actions/previous-choices}}}}} "My respectful attitude?" {:choices actions/previous-choices}]}]}
"You're not happy to see me, Mr. Fangald?" "You're not happy to see me, Mr. Fangald?"
{:run #(respond entities % :wizard "Of course not, you little brat. You've made my life a living hell!") {:run #(respond entities % :wizard "Of course not, you little brat. You've made my life a living hell!")
:choices #(-> % zip/left)} :choices #(-> % zip/left)}
"Good bye, Mr. Fangald!" "Good bye, Mr. Fangald!"
{:run #(do {:run #(do
(respond entities % :wizard "Now scram!") (respond entities % :wizard "Now scram!")
(actions/transition-background entities :outside-house [262 88]))}}}) (actions/transition-background entities :outside-house [262 88]))}]})
(defn backgrounds [screen] (defn backgrounds [screen]
(let [sheep-sheet (texture! (texture "outsidehouse/sheep-anim.png") :split 33 21) (let [sheep-sheet (texture! (texture "outsidehouse/sheep-anim.png") :split 33 21)