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