Blobs are stored in gs and are movable with the mouse!

This commit is contained in:
Jeremy Penner 2013-05-04 00:20:13 -04:00
parent 27fa774f7b
commit 4d5e8c2c43
2 changed files with 69 additions and 19 deletions

View file

@ -12,6 +12,20 @@
(def blobbuf (BufferUtils/createFloatBuffer (* 4 10))) (def blobbuf (BufferUtils/createFloatBuffer (* 4 10)))
(def emptyblob {:x 0 :y 0 :scalex 0 :scaley 0}) (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] (defn with-blob-shader [f & args]
(let [shader (res/get-shader "res/shader/blob")] (let [shader (res/get-shader "res/shader/blob")]
(.bind shader) (.bind shader)

View file

@ -1,6 +1,7 @@
(ns hottub.core (ns hottub.core
(:require [hottub.slick :as slick] (:require [hottub.slick :as slick]
[hottub.gs :as gs] [hottub.gs :as gs]
[hottub.gui :as gui]
[hottub.resource :as res] [hottub.resource :as res]
[hottub.timeline :as tln] [hottub.timeline :as tln]
[hottub.screen :as scr] [hottub.screen :as scr]
@ -12,13 +13,17 @@
(slick/start-game "Test game" {:id :game})) (slick/start-game "Test game" {:id :game}))
(defmethod scr/screen-from-spec :game [spec] (defmethod scr/screen-from-spec :game [spec]
{:tln (tln/timeline-insert (tln/timeline-create) 0 (let [blobmap {0 {:x 0.3 :y 0.4 :scalex 0.2 :scaley 0.3}
(gs/update-gs gs/gs-empty 1 {:x -0.5 :y 0.1 :scalex 0.4 :scaley 0.1}}]
(gs/add-index :name) {:tln (tln/timeline-insert (tln/timeline-create) 0
(gs/add-index :type) (gs/update-gs gs/gs-empty
(gs/set-entity (gs/gen-id) {:type :sprite :x 50 :y 50 (gs/add-index :name)
:image "res/man.png" :name :man}))) (gs/add-index :type)
:timestamp-last (u/timestamp)}) (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] (defn do-sim [gs tsstart tsend fntln fncontinuous fndiscrete]
(gs/update-gs gs (gs/update-gs gs
@ -35,19 +40,50 @@
(defn update-gs-from [tsstart tsend] (defn update-gs-from [tsstart tsend]
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] (defn update-gs-at [ts inputs]
(let [input (last inputs) (do
man (first (gs/q :name :man))] (update-blobs inputs)
(gs/update-entity (assoc man :x (:mousex input) :y (:mousey input))))) (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] (defn render-gs [gs g]
(gs/with-gs gs (gs/with-gs gs
(doseq [sprite (gs/q :type :sprite)] (doseq [sprite (gs/q :type :sprite)]
(if-let [image (res/get-image (:image sprite))] (if-let [image (res/get-image (:image sprite))]
(.draw image (:x sprite) (:y sprite)))) (.draw image (:x sprite) (:y sprite))))
(blob/with-blob-shader blob/draw-blobs (draw-blobs g)))
[{: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)))
(defmethod slick/update-game :game [screen inputtln delta] (defmethod slick/update-game :game [screen inputtln delta]
(if-let [input (tln/timeline-last-value inputtln)] (if-let [input (tln/timeline-last-value inputtln)]