master
Josha von Gizycki 5 years ago
parent 6e5bfecf21
commit e1163421c5

4
.gitignore vendored

@ -12,4 +12,6 @@ pom.xml.asc
.lein-failures .lein-failures
.nrepl-port .nrepl-port
figwheel_server.log figwheel_server.log
resources/public/js resources/public/js
*.iml
.idea

@ -34,16 +34,6 @@
:s (assoc obj :y (+ y pxs)) :s (assoc obj :y (+ y pxs))
obj))) obj)))
(defn bump-into [obj obj2]
(let [{:keys [x y w h d]} obj
{ox :x oy :y ow :w oh :h} obj2]
(case d
:w (assoc obj :x (+ ox ow 1))
:e (assoc obj :x (dec (- ox w)))
:n (assoc obj :y (+ oy oh 1))
:s (assoc obj :y (dec (- oy h)))
obj)))
(defn bump-inside-container [obj container] (defn bump-inside-container [obj container]
(let [{:keys [x y w h d]} obj (let [{:keys [x y w h d]} obj
{cx :x cy :y cw :w ch :h} container] {cx :x cy :y cw :w ch :h} container]
@ -55,12 +45,8 @@
obj))) obj)))
(defn pps->px [gamestate obj] (defn pps->px [gamestate obj]
(let [prev (get-in gamestate [:timing :prev]) (* (:pps obj)
now (get-in gamestate [:timing :now]) (-> gamestate :timing :elapsed)))
secs (/ (- now prev) 1000)
secs (-> gamestate :timing :elapsed)
pps (:pps obj)]
(* pps secs)))
(defn move-inside [obj container pxs] (defn move-inside [obj container pxs]
(let [moved (moved-object obj pxs)] (let [moved (moved-object obj pxs)]

@ -18,7 +18,10 @@
;; difference between prev and now in seconds ;; difference between prev and now in seconds
:elapsed 0} :elapsed 0}
;; width and height of the canvas ;; width and height of the canvas
:dimensions {:w (* 16 11 3) :dimensions {;; 16px tile size
;; 11 tiles wide, 9 high
;; 3 times zoom
:w (* 16 11 3)
:h (* 16 9 3)} :h (* 16 9 3)}
:input {:dir :?} :input {:dir :?}
;; currently active scene ;; currently active scene
@ -29,6 +32,8 @@
(def reloaded (atom false)) (def reloaded (atom false))
(defn nao [] (.now js/performance))
(defn curr-fps (defn curr-fps
"calculates the current fps using the elapsed time" "calculates the current fps using the elapsed time"
[elapsed] [elapsed]
@ -65,7 +70,7 @@
(defn update-step (defn update-step
"updates timing information and the current scene" "updates timing information and the current scene"
[gamestate] [gamestate]
(let [now (.now js/performance) (let [now (nao)
secs (elapsed-seconds gamestate now) secs (elapsed-seconds gamestate now)
scene (curr-scene gamestate) scene (curr-scene gamestate)
continue? (continue-running? (:continue? gamestate))] continue? (continue-running? (:continue? gamestate))]
@ -78,7 +83,7 @@
(if continue? (if continue?
(run-scene-update $ scene) (run-scene-update $ scene)
scene)) scene))
(assoc-in $ [:timing :prev] (.now js/performance))))) (assoc-in $ [:timing :prev] (nao)))))
(defn draw-fps (defn draw-fps
"draws the current fps" "draws the current fps"

@ -13,47 +13,47 @@
(.then #(.json %)) (.then #(.json %))
(.then #(reset! map-def (js->clj %)))) (.then #(reset! map-def (js->clj %))))
(merge (merge
scenestate scenestate
{:map-def {:images {:pokelike (by-id "demoscene-tileset")} {:map-def {:images {:pokelike (by-id "demoscene-tileset")}
:def nil :def nil
:zoom 3} :zoom 3}
:player {:x (/ (get-in gamestate [:dimensions :w]) 2) :player {:x (/ (get-in gamestate [:dimensions :w]) 2)
:y (/ (get-in gamestate [:dimensions :h]) 2) :y (/ (get-in gamestate [:dimensions :h]) 2)
:w 0 :h 0 :w 0 :h 0
:d :s :d :s
:animation {:ids {:s 778 :n 778 :animation {:ids {:s 778 :n 778
:e 819 :w 819} :e 819 :w 819}
:last-cycle 0 :last-cycle 0
:curr-id 778 :curr-id 778
:tileset :pokelike :tileset :pokelike
:tileset-def nil :tileset-def nil
:image (by-id "demoscene-tileset")}} :image (by-id "demoscene-tileset")}}
:viewport {:keep-in {:x 0 :y 0 :viewport {:keep-in {:x 0 :y 0
:w 0 :h 0} :w 0 :h 0}
:tileset :pokelike :tileset :pokelike
:image (by-id "demoscene-tileset") :image (by-id "demoscene-tileset")
:tileset-def nil :tileset-def nil
:x 6 :y 9 :x 6 :y 9
:w (get-in gamestate [:dimensions :w]) :w (get-in gamestate [:dimensions :w])
:h (get-in gamestate [:dimensions :h]) :h (get-in gamestate [:dimensions :h])
:d :? :d :?
;; pixels per second ;; pixels per second
:pps 150}})) :pps 150}}))
(defn update-player [gamestate player dir] (defn update-player [gamestate player dir]
(let [old-dir (:d player) (let [old-dir (:d player)
new-dir (if (= :? dir) old-dir dir)] new-dir (if (= :? dir) old-dir dir)]
(as-> player p (as-> player p
(assoc p :d new-dir) (assoc p :d new-dir)
(if (= :? dir) (if (= :? dir)
(sprites/reset player) (sprites/reset player)
(sprites/proc gamestate p))))) (sprites/proc gamestate p)))))
(defn update-viewport [gamestate viewport dir] (defn update-viewport [gamestate viewport dir]
(collision/move-inside (collision/move-inside
(assoc viewport :d dir) (assoc viewport :d dir)
(:keep-in viewport) (:keep-in viewport)
(collision/pps->px gamestate viewport))) (collision/pps->px gamestate viewport)))
(defn init-viewport [scenestate] (defn init-viewport [scenestate]
(let [{loaded-def :def (let [{loaded-def :def
@ -90,10 +90,7 @@
(if (and (not (-> scenestate :map-def :def)) (if (and (not (-> scenestate :map-def :def))
(some? @map-def)) (some? @map-def))
(let [loaded-def (walk/keywordize-keys @map-def) (let [loaded-def (walk/keywordize-keys @map-def)
zoom (get-in scenestate [:map-def :zoom]) zoom (get-in scenestate [:map-def :zoom])]
viewport (:viewport scenestate)
{mw :width mh :height
tw :tilewidth th :tileheight} loaded-def]
(-> scenestate (-> scenestate
(assoc-in [:map-def :def] loaded-def) (assoc-in [:map-def :def] loaded-def)
(update-in [:map-def :def] (update-in [:map-def :def]
@ -108,12 +105,11 @@
viewport (:viewport new-scenestate) viewport (:viewport new-scenestate)
dir (-> gamestate :input :dir)] dir (-> gamestate :input :dir)]
(assoc new-scenestate (assoc new-scenestate
:player (update-player gamestate player dir) :player (update-player gamestate player dir)
:viewport (update-viewport gamestate viewport dir)))) :viewport (update-viewport gamestate viewport dir))))
(defn draw-scene [gamestate scenestate] (defn draw-scene [gamestate scenestate]
(let [viewport (:viewport scenestate) (let [viewport (:viewport scenestate)
{:keys [x y w h background]} viewport
ctx (:ctx gamestate)] ctx (:ctx gamestate)]
(when (-> scenestate :map-def :def) (when (-> scenestate :map-def :def)
(tileset/draw-viewport (:map-def scenestate) (tileset/draw-viewport (:map-def scenestate)

@ -6,19 +6,19 @@
(get @keysdown (name code) false)) (get @keysdown (name code) false))
(.addEventListener (.addEventListener
js/document js/document
"keydown" "keydown"
(fn [event] (fn [event]
(swap! keysdown #(assoc % (.-code event) true)) (swap! keysdown #(assoc % (.-code event) true))
false)) false))
(.addEventListener (.addEventListener
js/document js/document
"keyup" "keyup"
(fn [event] (fn [event]
(swap! keysdown (swap! keysdown
#(assoc % (.-code event) false)) #(assoc % (.-code event) false))
false)) false))
(defn dir [] (defn dir []
(cond (cond

@ -39,7 +39,7 @@
{:keys [image size cycle] :as sprite} :sprite} obj {:keys [image size cycle] :as sprite} :sprite} obj
pos (pos-in-sprite sprite d)] pos (pos-in-sprite sprite d)]
(.drawImage (.drawImage
ctx ctx
image image
(:x pos) (:y pos) size size (:x pos) (:y pos) size size
x y size size))) x y size size)))

@ -1,5 +1,4 @@
(ns topdown2d.tileset (ns topdown2d.tileset)
(:require [topdown2d.collision :as coll]))
(defn next-animation-cycle [animation curr-cycle] (defn next-animation-cycle [animation curr-cycle]
(let [curr-ix (.indexOf animation curr-cycle) (let [curr-ix (.indexOf animation curr-cycle)
@ -15,7 +14,7 @@
(defn tick-animation [tileset-def anim-def now] (defn tick-animation [tileset-def anim-def now]
(let [map-def (:def tileset-def) (let [map-def (:def tileset-def)
{:keys [ids last-cycle curr-id tileset]} anim-def {:keys [last-cycle curr-id tileset]} anim-def
animation (->> (map-def->tileset map-def tileset) animation (->> (map-def->tileset map-def tileset)
:tiles :tiles
(filter #(= curr-id (:id %))) (filter #(= curr-id (:id %)))
@ -32,7 +31,7 @@
:curr-id (:tileid next-cycle)) :curr-id (:tileid next-cycle))
anim-def))) anim-def)))
(defn ix->coords [ix width height] (defn ix->coords [ix width]
(let [x (rem ix width)] (let [x (rem ix width)]
[(dec x) [(dec x)
(/ (- ix x) width)])) (/ (- ix x) width)]))
@ -40,8 +39,7 @@
(defn layer->dest [tileset layer] (defn layer->dest [tileset layer]
(map-indexed (map-indexed
(fn [ix tile] (fn [ix tile]
(let [{:keys [width height]} layer (let [[dxc dyc] (ix->coords ix (:width layer))
[dxc dyc] (ix->coords ix width height)
{:keys [target-width target-height]} tileset] {:keys [target-width target-height]} tileset]
{:tile tile {:tile tile
:x (* dxc target-width) :x (* dxc target-width)
@ -52,9 +50,8 @@
(defn draw-tile [ctx tileset image tileid dest] (defn draw-tile [ctx tileset image tileid dest]
(let [{:keys [tileheight tilewidth (let [{:keys [tileheight tilewidth
tiles-x tiles-y tiles-x target-width target-height]} tileset
target-width target-height]} tileset [x y] (ix->coords tileid tiles-x)
[x y] (ix->coords tileid tiles-x tiles-y)
sx (* x tilewidth) sx (* x tilewidth)
sy (* y tileheight)] sy (* y tileheight)]
(.drawImage ctx image (.drawImage ctx image
@ -83,12 +80,9 @@
(defn draw-viewport [map-def ctx viewport] (defn draw-viewport [map-def ctx viewport]
(let [def (:def map-def) (let [def (:def map-def)
{:keys [tileheight tilewidth
height width layers]} def
tileset-id (:tileset viewport)
image (:image viewport) image (:image viewport)
tileset (:tileset-def viewport)] tileset (:tileset-def viewport)]
(doseq [layer layers] (doseq [layer (:layers def)]
(doseq [dest layer (doseq [dest layer
:let [dx (- (:x dest) (:x viewport)) :let [dx (- (:x dest) (:x viewport))
dy (- (:y dest) (:y viewport))]] dy (- (:y dest) (:y viewport))]]

Loading…
Cancel
Save