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

@ -13,30 +13,26 @@
(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,11 +4,11 @@
[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
@ -24,15 +24,16 @@
: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
{:image (.getElementById js/document "demo-background")
:keep-in {:x 0 :y 0 :keep-in {:x 0 :y 0
:w 2239 :h 2235} :w 2239 :h 2235}
:x 1 :y 1 :x 1 :y 1
:d :? :d :?
;; pixels per second
:pps 350 :pps 350
:w (get-in gamestate [:dimensions :w]) :w (get-in gamestate [:dimensions :w])
:h (get-in gamestate [:dimensions :h]) :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