put inputs into gamestate, api changes, sprite cycle resets when no input is given, simplifications

master
Josha von Gizycki 7 years ago
parent 43da44c0ab
commit 9be3f6b848

@ -8,7 +8,7 @@
(def gamestate { (def gamestate {
:canvas (.getElementById js/document "gamecanvas") :canvas (.getElementById js/document "gamecanvas")
:ctx (.getContext (.getElementById js/document "gamecanvas") "2d") :ctx (.getContext (.getElementById js/document "gamecanvas") "2d")
:target-fps 30 :target-fps 40
:timing { :timing {
; msecs of previous frame ; msecs of previous frame
:prev 0 :prev 0
@ -24,6 +24,9 @@
:w 600 :w 600
:h 400 :h 400
} }
:input {
:dir :?
}
; currently active scene ; currently active scene
:scene :demo :scene :demo
:scenes { :scenes {
@ -68,12 +71,13 @@
scenedata (get-in gamestate [:scenes scenekey]) scenedata (get-in gamestate [:scenes scenekey])
updatefunc (:update scenedata) updatefunc (:update scenedata)
newdata (updatefunc gamestate scenedata)] newdata (updatefunc gamestate scenedata)]
(update-in gamestate [:scenes scenekey] (fn [] newdata)))) (assoc-in gamestate [:scenes scenekey] newdata)))
(defn update-step (defn update-step
"updates timing information and the current scene" "updates timing information and the current scene"
[gamestate] [gamestate]
(-> gamestate (-> gamestate
(assoc-in [:input :dir] (input/dir))
(set-timing :now) (set-timing :now)
(set-elapsed-seconds) (set-elapsed-seconds)
(set-fps) (set-fps)

@ -11,11 +11,10 @@
: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 :? :d :s
:sprite { :sprite {
:image (.getElementById js/document "demo-player") :image (.getElementById js/document "demo-player")
:size 64 :size 64
:d :s
:rows { :rows {
:w 1 :e 3 :w 1 :e 3
:n 0 :s 2 :n 0 :s 2
@ -45,16 +44,13 @@
})) }))
(defn update-player [gamestate player dir] (defn update-player [gamestate player dir]
(let [player-dir (get-in player [:sprite :d]) (let [old-dir (:d player)
sprite-dir (if (= :? dir) player-dir dir)] new-dir (if (= :? dir) old-dir dir)]
(as-> player p (as-> player p
(assoc p :d dir) (assoc p :d new-dir)
(assoc-in p (if (= :? dir)
[:sprite :d] (sprites/reset-cycle player)
sprite-dir) (sprites/proc-cycle gamestate p)))))
(if-not (= :? dir)
(sprites/proc-cycle gamestate p)
p))))
(defn update-viewport [gamestate viewport dir] (defn update-viewport [gamestate viewport dir]
(collision/move-inside (collision/move-inside
@ -65,7 +61,7 @@
(defn update-scene [gamestate scenedata] (defn update-scene [gamestate scenedata]
(let [player (get-in scenedata [:data :player]) (let [player (get-in scenedata [:data :player])
viewport (get-in scenedata [:data :viewport]) viewport (get-in scenedata [:data :viewport])
dir (input/dirinput)] dir (get-in gamestate [:input :dir])]
(-> scenedata (-> scenedata
(assoc-in [:data :player] (assoc-in [:data :player]
(update-player gamestate player dir)) (update-player gamestate player dir))
@ -81,5 +77,5 @@
x y w h x y w h
0 0 w h)) 0 0 w h))
(sprites/draw (sprites/draw
gamestate (:ctx gamestate)
(get-in scenedata [:data :player]))) (get-in scenedata [:data :player])))

@ -18,7 +18,7 @@
#(assoc % (.-code event) false)) #(assoc % (.-code event) false))
false)) false))
(defn dirinput [] (defn dir []
(cond (cond
(keydown? :ArrowLeft) :w (keydown? :ArrowLeft) :w
(keydown? :ArrowRight) :e (keydown? :ArrowRight) :e

@ -6,8 +6,8 @@
from (:from sprite-cycle) from (:from sprite-cycle)
maxpos (:count sprite-cycle) maxpos (:count sprite-cycle)
{:keys [pos spc last-cycle]} sprite-cycle {:keys [pos spc last-cycle]} sprite-cycle
elapsed (get-in gamestate [:timing :elapsed]) restart? (> (inc pos) maxpos)
restart? (> (inc pos) maxpos)] elapsed (get-in gamestate [:timing :elapsed])]
; new sprite frame? ; new sprite frame?
(if (> (+ last-cycle elapsed) spc) (if (> (+ last-cycle elapsed) spc)
; start cycle from new? ; start cycle from new?
@ -23,19 +23,24 @@
[:sprite :cycle :last-cycle] [:sprite :cycle :last-cycle]
#(+ % elapsed))))) #(+ % elapsed)))))
(defn pos-in-sprite [sprite] (defn reset-cycle [obj]
(let [{:keys [d size rows]} sprite (assoc-in
obj
[:sprite :cycle :pos]
0))
(defn pos-in-sprite [sprite d]
(let [{:keys [size rows]} sprite
pos (get-in sprite [:cycle :pos]) pos (get-in sprite [:cycle :pos])
row (d rows)] row (d rows)]
{:y (* row size) :x (* pos size)})) {:y (* row size) :x (* pos size)}))
(defn draw [gamestate obj] (defn draw [ctx obj]
(let [{:keys [x y w h d sprite]} obj (let [{:keys [x y w h d sprite]} obj
image (:image sprite) image (:image sprite)
sprite-size (:size sprite) sprite-size (:size sprite)
{:keys [ctx]} gamestate
sprite-cycle (:cycle sprite) sprite-cycle (:cycle sprite)
pos (pos-in-sprite sprite)] pos (pos-in-sprite sprite d)]
(.drawImage ctx (.drawImage ctx
image image
(:x pos) (:y pos) sprite-size sprite-size (:x pos) (:y pos) sprite-size sprite-size

Loading…
Cancel
Save