simplifying script api.

This commit is contained in:
=
2014-09-15 12:50:35 -07:00
parent 0c9ed24ecc
commit cf44afc6df

View File

@@ -1,5 +1,5 @@
(ns advent.action-test2 (ns advent.action-test2
(:require [clojure.core.async :refer [put! <! <!! >! chan go take! alts!!]])) (:require [clojure.core.async :refer [put! <! <!! >! chan go thread take! alts!!]]))
(defprotocol IAction (defprotocol IAction
(begin [this state]) (begin [this state])
@@ -30,7 +30,7 @@
(terminate [this state] (terminate [this state]
(put! c state) (put! c state)
state))) state)))
c)) (<!! c)))
(defn give-item [action-channel item] (defn give-item [action-channel item]
(let [c (chan)] (let [c (chan)]
@@ -47,7 +47,7 @@
(terminate [this state] (terminate [this state]
(put! c state) (put! c state)
state))) state)))
c)) (<!! c)))
(defn walk-to [action-channel who & targets ] (defn walk-to [action-channel who & targets ]
@@ -67,24 +67,28 @@
(terminate [this state] (terminate [this state]
(put! c state) (put! c state)
state)))) state))))
c)) (<!! c)))
(defmacro get-script [& forms]
`(fn [action-channel#] (thread ~@forms (put! action-channel# :end))))
(defn run-script [state action-channel] (defn run-script [state action-channel]
(let [random-loc (+ 3 (rand-int 10))] (let [random-loc (+ 3 (rand-int 10))]
(go (get-script
(if (= 1 (rand-int 2)) (if (= 1 (rand-int 2))
(<! (give-item action-channel :gold)) (give-item action-channel :gold)
(<! (give-item action-channel :candy))) (give-item action-channel :candy))
(let [state (<! (walk-to action-channel :ego [3 3] [random-loc random-loc] ))] (let [state (walk-to action-channel :ego [3 3] [random-loc random-loc] )]
(if ((:items state) :gold) (if ((:items state) :gold)
(<! (talk action-channel :ego "I have enough money to buy something")) (talk action-channel :ego "I have enough money to buy something")
(<! (talk action-channel :ego "I'm broke.")))) (talk action-channel :ego "I'm broke."))))))
(>! action-channel :end))))
(defn test-run [] (defn test-run []
(let [state {:x 0 :y 0 :time 0 :items #{} :current-action nil} (let [state {:x 0 :y 0 :time 0 :items #{} :current-action nil}
action-channel (chan) action-channel (chan)
actions (run-script state action-channel)] script (run-script state action-channel)
]
(script action-channel)
(loop [state state (loop [state state
current-action nil current-action nil
started? false] started? false]