diff --git a/desktop/src-common/advent/actions.clj b/desktop/src-common/advent/actions.clj index 5de0601f..89429857 100644 --- a/desktop/src-common/advent/actions.clj +++ b/desktop/src-common/advent/actions.clj @@ -16,7 +16,8 @@ (begin [this screen entities]) (done? [this screen entities]) (continue [this screen entities]) - (terminate [this screen entities])) + (terminate [this screen entities]) + (get-channel [this])) (defmacro get-script [entities & forms] `(fn [starting-entities#] @@ -45,9 +46,17 @@ dy (- y1 y2)] (Math/sqrt (+ (* dx dx) (* dy dy))))) +(defmacro run-action [entities & forms] + `(let [c# (chan)] + (do + (put! (get-in (deref ~entities) [:actions :channel]) + (reify IAction + (get-channel [_] c#) + ~@forms)) + (reset! ~entities ( delta-x 0) right - :else (:anim %)))))))) + (continue [this screen entities] + (let [{from-x :x from-y :y :keys [left right] :as target-entity} (entities target-id) + [[target-x target-y] remainder] @targets-left] + (let [delta-x (- target-x from-x) + delta-y (- target-y from-y) + distance (dist from-x from-y target-x target-y) + moved-x (if (= 0.0 distance) + 0 + (* 1.5 (/ delta-x distance))) + moved-y (if (= 0.0 distance) + 0 + (* 1.5 (/ delta-y distance)))] + (if (< distance 1) + (do (swap! targets-left rest) + entities) + (update-in entities [target-id] + #(assoc (jump-to screen entities % [(+ moved-x from-x) (+ moved-y from-y)]) + :anim (cond (< delta-x 0) left + (> delta-x 0) right + :else (:anim %)))))))) - (done? [this screen entities] - (let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (entities target-id)] - (< (dist final-x final-y from-x from-y) 1))) + (done? [this screen entities] + (let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (entities target-id)] + (< (dist final-x final-y from-x from-y) 1))) - (terminate [this screen entities] - (let [entities (stop screen entities target-id)] - (put! c entities) - entities)))) - (reset! entities ( (- (:total-time screen) - @initial-time) - (get-text-duration text))) + (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)))) - (reset! entities ( entities - (update-in [target-id :inventory] #(conj % item)) - (assoc-in [:cursor :current] item))) + (-> entities + (update-in [target-id :inventory] #(conj % item)) + (assoc-in [:cursor :current] item))) - (continue [this screen entities] entities) + (continue [this screen entities] entities) - (done? [this screen entities] - true) + (done? [this screen entities] true) - (terminate [this screen entities] - (put! c entities) - entities))) - (reset! entities ( entities - (assoc-in [:background] (get-in entities [:backgrounds new-background])) - (assoc-in [:ego :x] x) - (assoc-in [:ego :y] y))) + (run-action entities + (begin [this screen entities] + (-> entities + (assoc-in [:background] (get-in entities [:backgrounds new-background])) + (assoc-in [:ego :x] x) + (assoc-in [:ego :y] y))) - (continue [this screen entities] entities) + (continue [this screen entities] entities) - (done? [this screen entities] - true) + (done? [this screen entities] true) - (terminate [this screen entities] - (put! c entities) - entities))) - (reset! entities (