not stable yet, but you can use core.async for scripting.

This commit is contained in:
=
2014-09-15 16:26:14 -07:00
parent cf44afc6df
commit a9b5876d1f
3 changed files with 150 additions and 103 deletions

View File

@@ -7,10 +7,21 @@
[clojure.string :as s]
[advent.pathfind]
[advent.actions :as actions]
[advent.screens.dialogue :as dialogue])
[advent.screens.dialogue :as dialogue]
[clojure.core.async :refer [put! <! <!! >! >!! chan go thread take! alts!!]])
(:import [com.badlogic.gdx.graphics Pixmap Pixmap$Filter Texture Texture$TextureFilter]
[com.badlogic.gdx.graphics.g2d TextureRegion]))
(defprotocol IAction
(begin [this screen entities])
(done? [this screen entities])
(continue [this screen entities])
(terminate [this screen entities]))
(defmacro get-script [& forms]
`(fn [action-channel#] (thread ~@forms (put! action-channel# :end))))
(defn jump-to [screen entities entity [x y]]
(let [scale-fn (-> entities :background :scale-fn)
entity (assoc entity :x x
@@ -34,58 +45,81 @@
(assoc (jump-to screen entities target-entity [(+ moved-x from-x) (+ moved-y from-y)])
:anim (if (< moved-x 0) left right))))))))
(defn stop [target-id]
(fn [screen entities]
(let [target (target-id entities)]
(run! dialogue/talking-screen :stop-talk :target-id target-id)
(assoc-in entities [target-id] (merge target
{:anim nil
:actions (rest (:actions target))}
(when (:anim target)
(texture (animation! (:anim target) :get-key-frame 0.25))))))))
(defn stop [screen entities target-id]
(update-in entities [target-id] #(merge %
{:anim nil}
(when (:anim %)
(texture (animation! (:anim %) :get-key-frame 0.25))))))
(defn from-path [screen entities target-id [x y]]
(let [entity (target-id entities)
path (vec (take-nth 5 (advent.pathfind/visit-all
(:collision (:background entities))
[(int (:x entity)) (int (:y entity))]
[(int x) (int y)])))
actions (when (seq path)
(concat
[(stop target-id)]
(map #(walk-to % target-id) (conj path [x y]))
[(stop target-id)]))]
actions))
(defn walk-to [entities target-id [x y]]
(let [c (chan)
entity (entities target-id)
path (take-nth 5 (advent.pathfind/visit-all
(:collision (:background entities))
[(int (:x entity)) (int (:y entity))]
[(int x) (int y)]))]
(doseq [[target-x target-y] path]
(put! (get-in entities [:actions :channel])
(reify
IAction
(begin [this screen entities]
(let [{from-x :x from-y :y :keys [left right anim]} (entities target-id)]
(let [delta-x (- target-x from-x)]
(assoc-in entities [target-id :anim] (if (< delta-x 0) left right)))))
(continue [this screen entities]
(let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (entities target-id)]
(let [delta-x (- target-x from-x)
delta-y (- target-y from-y)
mag (Math/sqrt (+ (* delta-x delta-x) (* delta-y delta-y)))
moved-x (* 1.5 (/ delta-x mag))
moved-y (* 1.5 (/ delta-y mag))]
(assoc entities target-id
(jump-to screen entities target-entity [(+ moved-x from-x) (+ moved-y from-y)])))))
(done? [this screen entities]
(let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (entities target-id)]
(let [delta-x (- target-x from-x)
delta-y (- target-y from-y)
mag (Math/sqrt (+ (* delta-x delta-x) (* delta-y delta-y)))]
(< mag 1))))
(terminate [this screen entities]
(put! c entities)
(stop screen entities target-id)))))
(<!! c)))
(defn get-text-duration [text]
(* (count (s/split text #" ")) 0.75))
(defn talk [target-id text]
;; instead of tracking initial-time in an atom to determine when
;; this is done, this should probably be moved into a start and done?
;; function for an action
(let [initial-time (atom nil)]
(fn [screen entities]
(let [
begin? (not@initial-time)
_ (swap! initial-time #(or % (:total-time screen)))
target-y (get-in entities [target-id :y])
scale-fn (get-in entities [:background :scale-fn])
scale (scale-fn target-y)
height (* scale 36)
done? (> (- (:total-time screen)
@initial-time)
(get-text-duration text))]
(if done?
(do
(run! dialogue/talking-screen :stop-talk :target-id target-id)
(update-in entities [target-id :actions] rest))
(do
(when begin?
(run! dialogue/talking-screen :on-talk :text text
:x (get-in entities [target-id :x]) :y (+ (get-in entities [target-id :y]) height)
:target-id target-id
:scale scale))
(assoc-in entities [target-id :anim] (get-in entities [target-id :talk]))))))))
(defn talk [entities target-id text]
(let [c (chan)
initial-time (atom nil)]
(put! (get-in entities [:actions :channel])
(reify
IAction
(begin [this screen entities]
(let [_ (swap! initial-time #(or % (:total-time screen)))
target-y (get-in entities [target-id :y])
scale-fn (get-in entities [:background :scale-fn])
scale (scale-fn target-y)
height (* scale 36)]
(run! dialogue/talking-screen :on-talk :text text
:x (get-in entities [target-id :x]) :y (+ (get-in entities [target-id :y]) height)
:target-id target-id
:scale scale)
(assoc-in entities [target-id :anim] (get-in entities [target-id :talk]))))
(continue [this screen entities] entities)
(done? [this screen entities]
(> (- (:total-time screen)
@initial-time)
(get-text-duration text)))
(terminate [this screen entities]
(put! c entities)
(run! dialogue/talking-screen :stop-talk :target-id target-id)
(stop screen entities target-id))))
(<!! c)))