From 7033ac5c9b9ee45c7f78da60fc0ccd3d78e0b924 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Sat, 23 Mar 2013 23:08:46 -0400 Subject: [PATCH] Create do-sim function which integrates discrete and continuous processing broken up by external events --- src/hottub/core.clj | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/hottub/core.clj b/src/hottub/core.clj index d67e641..c4c87fd 100644 --- a/src/hottub/core.clj +++ b/src/hottub/core.clj @@ -19,8 +19,24 @@ :image "res/man.png" :name :man}))) :timestamp-last (u/timestamp)}) -(defn update-gs [input] - (let [man (first (gs/q :name :man))] +(defn do-sim [gs tsstart tsend fntln fncontinuous fndiscrete] + (gs/update-gs gs + (loop [ts tsstart eventhandled false] + (let [[tsev events] (-> (fntln) (subseq (if eventhandled > >=) ts) first)] + (if (= tsev ts) + (do + (fndiscrete tsev events) + (recur ts true)) + (if-not (= ts tsend) + (let [tsstop (fncontinuous ts (or tsev tsend))] + (recur tsstop false)))))))) + +(defn update-gs-from [tsstart tsend] + tsend) + +(defn update-gs-at [ts inputs] + (let [input (last inputs) + man (first (gs/q :name :man))] (gs/update-entity (assoc man :x (:mousex input) :y (:mousey input))))) (defn render-gs [gs] @@ -41,7 +57,7 @@ screents (+ screentslast (- ts (:timestamp-last screen))) tln (:tln screen) gslast (tln/timeline-last-value tln) - gsnext (gs/update-gs gslast (update-gs input)) + gsnext (do-sim gslast screentslast screents (fn [] inputtln) update-gs-from update-gs-at) tlnnext (tln/timeline-insert tln screents gsnext) screennext (assoc screen :timestamp-last ts :tln tlnnext)] (scr/update-screen screen screennext)))))