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

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

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

Loading…
Cancel
Save