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

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

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

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

Loading…
Cancel
Save