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

2
.gitignore vendored

@ -13,3 +13,5 @@ pom.xml.asc
.nrepl-port
figwheel_server.log
resources/public/js
*.iml
.idea

@ -34,16 +34,6 @@
:s (assoc obj :y (+ y pxs))
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]
(let [{:keys [x y w h d]} obj
{cx :x cy :y cw :w ch :h} container]
@ -55,12 +45,8 @@
obj)))
(defn pps->px [gamestate obj]
(let [prev (get-in gamestate [:timing :prev])
now (get-in gamestate [:timing :now])
secs (/ (- now prev) 1000)
secs (-> gamestate :timing :elapsed)
pps (:pps obj)]
(* pps secs)))
(* (:pps obj)
(-> gamestate :timing :elapsed)))
(defn move-inside [obj container pxs]
(let [moved (moved-object obj pxs)]

@ -18,7 +18,10 @@
;; difference between prev and now in seconds
:elapsed 0}
;; 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)}
:input {:dir :?}
;; currently active scene
@ -29,6 +32,8 @@
(def reloaded (atom false))
(defn nao [] (.now js/performance))
(defn curr-fps
"calculates the current fps using the elapsed time"
[elapsed]
@ -65,7 +70,7 @@
(defn update-step
"updates timing information and the current scene"
[gamestate]
(let [now (.now js/performance)
(let [now (nao)
secs (elapsed-seconds gamestate now)
scene (curr-scene gamestate)
continue? (continue-running? (:continue? gamestate))]
@ -78,7 +83,7 @@
(if continue?
(run-scene-update $ scene)
scene))
(assoc-in $ [:timing :prev] (.now js/performance)))))
(assoc-in $ [:timing :prev] (nao)))))
(defn draw-fps
"draws the current fps"

@ -90,10 +90,7 @@
(if (and (not (-> scenestate :map-def :def))
(some? @map-def))
(let [loaded-def (walk/keywordize-keys @map-def)
zoom (get-in scenestate [:map-def :zoom])
viewport (:viewport scenestate)
{mw :width mh :height
tw :tilewidth th :tileheight} loaded-def]
zoom (get-in scenestate [:map-def :zoom])]
(-> scenestate
(assoc-in [:map-def :def] loaded-def)
(update-in [:map-def :def]
@ -113,7 +110,6 @@
(defn draw-scene [gamestate scenestate]
(let [viewport (:viewport scenestate)
{:keys [x y w h background]} viewport
ctx (:ctx gamestate)]
(when (-> scenestate :map-def :def)
(tileset/draw-viewport (:map-def scenestate)

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

Loading…
Cancel
Save