diff --git a/src/celldiv/blob.clj b/src/celldiv/blob.clj index cede98e..0a1b5da 100644 --- a/src/celldiv/blob.clj +++ b/src/celldiv/blob.clj @@ -12,6 +12,20 @@ (def blobbuf (BufferUtils/createFloatBuffer (* 4 10))) (def emptyblob {:x 0 :y 0 :scalex 0 :scaley 0}) +(defn blob-pos-to-screen [blob {:keys [x y w h]}] + (let [scalex (/ w (- maxx minx)) + scaley (/ h (- maxy miny)) + screenx (+ (* (- (:x blob) minx) scalex) x) + screeny (+ (* (- (:y blob) miny) scaley) y)] + {:x screenx :y screeny})) + +(defn screen-pos-to-blob [sx sy {:keys [x y w h]}] + (let [scalex (/ (- maxx minx) w) + scaley (/ (- maxy miny) h) + blobx (+ (* (- sx x) scalex) minx) + bloby (+ (* (- sy y) scaley) miny)] + {:x blobx :y bloby})) + (defn with-blob-shader [f & args] (let [shader (res/get-shader "res/shader/blob")] (.bind shader) diff --git a/src/hottub/core.clj b/src/hottub/core.clj index 11e6122..d06f0a7 100644 --- a/src/hottub/core.clj +++ b/src/hottub/core.clj @@ -1,6 +1,7 @@ (ns hottub.core (:require [hottub.slick :as slick] [hottub.gs :as gs] + [hottub.gui :as gui] [hottub.resource :as res] [hottub.timeline :as tln] [hottub.screen :as scr] @@ -12,50 +13,85 @@ (slick/start-game "Test game" {:id :game})) (defmethod scr/screen-from-spec :game [spec] - {:tln (tln/timeline-insert (tln/timeline-create) 0 - (gs/update-gs gs/gs-empty - (gs/add-index :name) - (gs/add-index :type) - (gs/set-entity (gs/gen-id) {:type :sprite :x 50 :y 50 - :image "res/man.png" :name :man}))) - :timestamp-last (u/timestamp)}) + (let [blobmap {0 {:x 0.3 :y 0.4 :scalex 0.2 :scaley 0.3} + 1 {:x -0.5 :y 0.1 :scalex 0.4 :scaley 0.1}}] + {:tln (tln/timeline-insert (tln/timeline-create) 0 + (gs/update-gs gs/gs-empty + (gs/add-index :name) + (gs/add-index :type) + (gs/set-entity (gs/gen-id) {:type :sprite :x 50 :y 50 + :image "res/man.png" :name :man}) + (gs/set-entity (gs/gen-id) {:name :blobs :gui (gui/make-gui) + :blobs blobmap :x 0 :y 0 :w 100 :h 100}))) + :timestamp-last (u/timestamp)})) (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 + (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] +(defn update-gs-from [tsstart tsend] tsend) +(defn- blob-to-region [id blob entity] + (let [{:keys [x y]} (blob/blob-pos-to-screen blob entity)] + {:id id :x (- x 10) :y (- y 10) :w 20 :h 20})) + +(defn- blob-event [entity blobid input event] + (case event + :drag (gs/update-entity + (update-in entity [:blobs blobid] + #(into % (blob/screen-pos-to-blob (:mousex input) (:mousey input) entity)))) + (println event))) + +(defn- update-blobs [inputs] + (doseq [input inputs] + (let [blobs (first (gs/q :name :blobs)) + blobmap (:blobs blobs) + regions (map #(blob-to-region % (get blobmap %) blobs) (keys blobmap)) + gui (gui/gui-input (:gui blobs) input regions #(blob-event blobs (:id %1) %2 %3))] + (gs/assoc-in-entity (::gs/id blobs) [:gui] gui)))) + +(defn- draw-blobs [g] + (let [blobs (first (gs/q :name :blobs))] + (blob/with-blob-shader blob/draw-blobs (vals (:blobs blobs)) + (:x blobs) (:y blobs) (:w blobs) (:h blobs)) + (.setColor g (org.newdawn.slick.Color. 1.0 0.0 0.0 0.2)) + (doseq [blob (vals (:blobs blobs))] + (let [{:keys [x y]} + (blob/blob-pos-to-screen blob blobs)] + ;(println x y) + (.fill g (org.newdawn.slick.geom.Circle. x y 5))) + ))) + (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))))) + (do + (update-blobs 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 g] (gs/with-gs gs (doseq [sprite (gs/q :type :sprite)] (if-let [image (res/get-image (:image sprite))] (.draw image (:x sprite) (:y sprite)))) - (blob/with-blob-shader blob/draw-blobs - [{:x 0.3 :y 0.4 :scalex 0.2 :scaley 0.3} - {:x -0.3 :y -0.4 :scalex 0.5 :scaley 0.3}] 0 0 300 300))) + (draw-blobs g))) (defmethod slick/update-game :game [screen inputtln delta] (if-let [input (tln/timeline-last-value inputtln)] (if (contains? (:keys input) ",") - (scr/push-modal-screen screen + (scr/push-modal-screen screen {:id :timeshift :ts (-> (:tln screen) last key)}) - + (let [ts (u/timestamp) screentslast (-> (:tln screen) last key) screents (+ screentslast (- ts (:timestamp-last screen))) @@ -104,11 +140,11 @@ (if @*newscreen* (slick/game-setscreen! game @*newscreen*)))) (defmethod slick/eval-with-bindings :game [screen game f] - (simple-eval screen game + (simple-eval screen game (fn [] (let [tln (:tln screen) gsnew (gs/update-gs (tln/timeline-last-value tln) (f))] - (if-not @*newscreen* + (if-not @*newscreen* (let [tslast (-> tln last key) tlnnew (tln/timeline-insert tln tslast gsnew) screennew (assoc screen :tln tlnnew)]