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)))))
(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]
(loop [zipper (make-zipper choices)]
(let [selected-choice (atom nil)
node (zip/node zipper)]
(let [selected-index (atom nil)
node (zip/node zipper)
dialogue-choices (partition 2 (:choices node))]
(run-action 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)
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]
(run! @(resolve 'advent.screens.scene/scene) :on-reactivate)
entities))
(let [zipper (nth-child zipper (-> node :choices keys (.indexOf @selected-choice)))
(let [zipper (nth-child zipper @selected-index)
node (zip/node zipper)]
(when-let [run (:run node)]
(run @selected-choice))
(run (-> dialogue-choices
(nth @selected-index)
first)))
(when-let [next-choices (:choices node)]
(if (fn? next-choices)
(recur (next-choices zipper))