Blobs are stored in gs and are movable with the mouse!
This commit is contained in:
parent
27fa774f7b
commit
4d5e8c2c43
|
@ -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)
|
||||
|
|
|
@ -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,13 +13,17 @@
|
|||
(slick/start-game "Test game" {:id :game}))
|
||||
|
||||
(defmethod scr/screen-from-spec :game [spec]
|
||||
(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})))
|
||||
:timestamp-last (u/timestamp)})
|
||||
: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
|
||||
|
@ -35,19 +40,50 @@
|
|||
(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]
|
||||
(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)))))
|
||||
(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)]
|
||||
|
|
Loading…
Reference in a new issue