From 635c391e54c20f6904f6ae2cb3bfcfb59508d86d Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Fri, 19 Oct 2018 17:57:25 +0200 Subject: [PATCH] slightly better tileset code --- src/cljs/topdown2d/demoscene.cljs | 19 +++++++++++++------ src/cljs/topdown2d/tileset.cljs | 11 ++++------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/cljs/topdown2d/demoscene.cljs b/src/cljs/topdown2d/demoscene.cljs index c216bc8..3765c2a 100644 --- a/src/cljs/topdown2d/demoscene.cljs +++ b/src/cljs/topdown2d/demoscene.cljs @@ -54,15 +54,22 @@ (assoc :tiles-y (/ (:tilecount tileset) (:columns tileset))))) +(defn map-layer-tiles [map-def] + (let [ftileset (first (:tilesets map-def))] + (-> map-def + (update :layers + #(map (partial tileset/layer->dest ftileset) %))))) + (defn init-map-def [scenestate] (if (and (not (get-in scenestate [:tileset :def])) (some? @map-def)) - (assoc-in scenestate - [:tileset :def] - (update (walk/keywordize-keys @map-def) - :tilesets - (fn [tilesets] - (map map-tileset-size tilesets)))) + (assoc-in + scenestate + [:tileset :def] + (-> @map-def + (walk/keywordize-keys) + (update :tilesets #(map map-tileset-size %)) + map-layer-tiles)) scenestate)) (defn update-scene [gamestate scenestate] diff --git a/src/cljs/topdown2d/tileset.cljs b/src/cljs/topdown2d/tileset.cljs index cdfdf0a..e0341c2 100644 --- a/src/cljs/topdown2d/tileset.cljs +++ b/src/cljs/topdown2d/tileset.cljs @@ -39,7 +39,7 @@ [(dec x) (/ (- ix x) width)])) -(defn layer->dest [layer tileset data] +(defn layer->dest [tileset layer] (map-indexed (fn [ix tile] (let [{:keys [width height]} layer @@ -50,7 +50,7 @@ :y (* dyc tileheight) :w tilewidth :h tileheight})) - data)) + (:data layer))) (defn draw-tile [ctx tileset image tileid dest] (let [{:keys [tileheight tilewidth @@ -70,11 +70,8 @@ tileset-id (:tileset viewport) image (tileset-id images) tileset (map-def->tileset def (:tileset viewport))] - (doseq [layer layers - :let [destinations (->> (:data layer) - (layer->dest layer tileset) - (filter #(not= 0 (:tile %))))]] - (doseq [dest destinations + (doseq [layer layers] + (doseq [dest layer :let [dx (- (:x dest) (:x viewport)) dy (- (:y dest) (:y viewport))]] (draw-tile ctx tileset image