simplify scenestates, code indentation

the :data key in scenestates sucks. the whole scene map is now the scenestate
master
Josha von Gizycki 7 years ago
parent 1493b0f1a1
commit 9b61fdc888

@ -4,39 +4,35 @@
(let [{:keys [x y w h]} obj (let [{:keys [x y w h]} obj
{cx :x cy :y cw :w ch :h} container] {cx :x cy :y cw :w ch :h} container]
(and (and
(> x cx) (> x cx)
(> y cy) (> y cy)
(< (+ x w) (+ cx cw)) (< (+ x w) (+ cx cw))
(< (+ y h) (+ cy ch))))) (< (+ y h) (+ cy ch)))))
(defn collide? [obj obj2] (defn collide? [obj obj2]
(let [{:keys [x y w h]} obj (let [{:keys [x y w h]} obj
{ox :x oy :y ow :w oh :h} obj2] {ox :x oy :y ow :w oh :h} obj2]
(or (or
; top left corner ;; top left corner
(and (>= x ox) (>= y oy) (and (>= x ox) (>= y oy)
(<= x (+ ox ow)) (<= y (+ oy oh))) (<= x (+ ox ow)) (<= y (+ oy oh)))
; top right corner ;; top right corner
(and (>= (+ x h) ox) (>= y oy) (and (>= (+ x h) ox) (>= y oy)
(<= (+ x h) (+ ox ow)) (<= y (+ oy oh))) (<= (+ x h) (+ ox ow)) (<= y (+ oy oh)))
; bottom left corner ;; bottom left corner
(and (>= x ox) (>= (+ y h) oy) (and (>= x ox) (>= (+ y h) oy)
(<= x (+ ox ow)) (<= (+ y h) (+ oy oh))) (<= x (+ ox ow)) (<= (+ y h) (+ oy oh)))
; bottom right corner ;; bottom right corner
(and (>= (+ x w) ox) (>= (+ y h) oy) (and (>= (+ x w) ox) (>= (+ y h) oy)
(<= (+ x w) (+ ox ow)) (<= (+ y h) (+ oy oh)))))) (<= (+ x w) (+ ox ow)) (<= (+ y h) (+ oy oh))))))
(defn moved-object [obj pxs] (defn moved-object [obj pxs]
(let [{:keys [x y d]} obj] (let [{:keys [x y d]} obj]
(case d (case d
:w (assoc obj :w (assoc obj :x (- x pxs))
:x (- x pxs)) :e (assoc obj :x (+ x pxs))
:e (assoc obj :n (assoc obj :y (- y pxs))
:x (+ x pxs)) :s (assoc obj :y (+ y pxs))
:n (assoc obj
:y (- y pxs))
:s (assoc obj
:y (+ y pxs))
obj))) obj)))
(defn bump-into [obj obj2] (defn bump-into [obj obj2]

@ -26,8 +26,7 @@
:scene :demo :scene :demo
:scenes {:demo {:update demoscene/update-scene :scenes {:demo {:update demoscene/update-scene
:draw demoscene/draw-scene :draw demoscene/draw-scene
:init demoscene/init :init demoscene/init}}})
:data {}}}})
(defn set-timing (defn set-timing
"sets the current time at the given key" "sets the current time at the given key"
@ -60,10 +59,10 @@
(if-not (:continue? gamestate) (if-not (:continue? gamestate)
gamestate gamestate
(let [scenekey (:scene gamestate) (let [scenekey (:scene gamestate)
scenedata (get-in gamestate [:scenes scenekey]) scenestate (get-in gamestate [:scenes scenekey])
updatefunc (:update scenedata) updatefunc (:update scenestate)
newdata (updatefunc gamestate scenedata)] newstate (updatefunc gamestate scenestate)]
(assoc-in gamestate [:scenes scenekey] newdata)))) (assoc-in gamestate [:scenes scenekey] newstate))))
(defn continue-running? (defn continue-running?
"checks if the gameloop should keep running, based on input" "checks if the gameloop should keep running, based on input"
@ -119,8 +118,7 @@
(get-in gamestate [:dimensions :w]) (get-in gamestate [:dimensions :w])
(get-in gamestate [:dimensions :h])) (get-in gamestate [:dimensions :h]))
(let [scenekey (:scene gamestate) (let [scenekey (:scene gamestate)
scene (scenekey (:scenes gamestate)) drawfunc (get-in gamestate [:scenes scenekey :draw])]
drawfunc (:draw scene)]
(drawfunc gamestate scene)) (drawfunc gamestate scene))
(draw-fps gamestate)) (draw-fps gamestate))
@ -155,12 +153,10 @@
gamestate gamestate
:scenes :scenes
(reduce (reduce
(fn [scenes [scenekey scenedata]] (fn [scenes [scenekey scenestate]]
(let [initfunc (:init scenedata) (let [initfunc (:init scenestate)
newdata (initfunc gamestate scenedata)] newstate (initfunc gamestate scenestate)]
(assoc (assoc scenes scenekey newstate)))
scenes
scenekey newdata)))
{} {}
(:scenes gamestate)))) (:scenes gamestate))))

@ -4,35 +4,36 @@
[topdown2d.input :as input] [topdown2d.input :as input]
[topdown2d.sprites :as sprites])) [topdown2d.sprites :as sprites]))
(defn init [gamestate scenedata] (defn init [gamestate scenestate]
(assoc (assoc
scenedata scenestate
:data :player
{:player {:x (- (/ (get-in gamestate [:dimensions :w]) 2) 32) {:x (- (/ (get-in gamestate [:dimensions :w]) 2) 32)
:y (- (/ (get-in gamestate [:dimensions :h]) 2) 32) :y (- (/ (get-in gamestate [:dimensions :h]) 2) 32)
:w 64 :h 64 :w 64 :h 64
:d :s :d :s
:sprite :sprite
{:image (.getElementById js/document "demo-player") {:image (.getElementById js/document "demo-player")
:size 64 :size 64
:rows {:w 1 :e 3 :rows {:w 1 :e 3
:n 0 :s 2 :n 0 :s 2
:? 2} :? 2}
:cycle {:pos 0 :cycle {:pos 0
:from 1 :from 1
:count 8 :count 8
:last-cycle 0 :last-cycle 0
;; seconds per cycle ;; seconds per cycle
:spc 0.08}}} :spc 0.08}}}
:viewport {:image (.getElementById js/document "demo-background") :viewport
:keep-in {:x 0 :y 0 {:image (.getElementById js/document "demo-background")
:w 2239 :h 2235} :keep-in {:x 0 :y 0
:x 1 :y 1 :w 2239 :h 2235}
:d :? :x 1 :y 1
:pps 350 :d :?
:w (get-in gamestate [:dimensions :w]) ;; pixels per second
:h (get-in gamestate [:dimensions :h]) :pps 350
}})) :w (get-in gamestate [:dimensions :w])
:h (get-in gamestate [:dimensions :h])}))
(defn update-player [gamestate player dir] (defn update-player [gamestate player dir]
(let [old-dir (:d player) (let [old-dir (:d player)
@ -49,18 +50,16 @@
(:keep-in viewport) (:keep-in viewport)
(collision/pps->px gamestate viewport))) (collision/pps->px gamestate viewport)))
(defn update-scene [gamestate scenedata] (defn update-scene [gamestate scenestate]
(let [player (get-in scenedata [:data :player]) (let [player (:player scenestate)
viewport (get-in scenedata [:data :viewport]) viewport (:viewport scenestate)
dir (get-in gamestate [:input :dir])] dir (get-in gamestate [:input :dir])]
(-> scenedata (assoc scenedata
(assoc-in [:data :player] :player (update-player gamestate player dir)
(update-player gamestate player dir)) :viewport (update-viewport gamestate viewport dir))))
(assoc-in [:data :viewport]
(update-viewport gamestate viewport dir)))))
(defn draw-scene [gamestate scenedata] (defn draw-scene [gamestate scenestate]
(let [viewport (get-in scenedata [:data :viewport]) (let [viewport (:viewport scenestate)
{:keys [x y w h background]} viewport] {:keys [x y w h background]} viewport]
(.drawImage (.drawImage
(:ctx gamestate) (:ctx gamestate)
@ -69,4 +68,4 @@
0 0 w h)) 0 0 w h))
(sprites/draw (sprites/draw
(:ctx gamestate) (:ctx gamestate)
(get-in scenedata [:data :player]))) (:player scenestate)))

Loading…
Cancel
Save