slightly better tileset code

master
Josha von Gizycki 6 years ago
parent edcfd29de8
commit 635c391e54

@ -54,15 +54,22 @@
(assoc :tiles-y (/ (:tilecount tileset) (assoc :tiles-y (/ (:tilecount tileset)
(:columns 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] (defn init-map-def [scenestate]
(if (and (not (get-in scenestate [:tileset :def])) (if (and (not (get-in scenestate [:tileset :def]))
(some? @map-def)) (some? @map-def))
(assoc-in scenestate (assoc-in
scenestate
[:tileset :def] [:tileset :def]
(update (walk/keywordize-keys @map-def) (-> @map-def
:tilesets (walk/keywordize-keys)
(fn [tilesets] (update :tilesets #(map map-tileset-size %))
(map map-tileset-size tilesets)))) map-layer-tiles))
scenestate)) scenestate))
(defn update-scene [gamestate scenestate] (defn update-scene [gamestate scenestate]

@ -39,7 +39,7 @@
[(dec x) [(dec x)
(/ (- ix x) width)])) (/ (- ix x) width)]))
(defn layer->dest [layer tileset data] (defn layer->dest [tileset layer]
(map-indexed (map-indexed
(fn [ix tile] (fn [ix tile]
(let [{:keys [width height]} layer (let [{:keys [width height]} layer
@ -50,7 +50,7 @@
:y (* dyc tileheight) :y (* dyc tileheight)
:w tilewidth :w tilewidth
:h tileheight})) :h tileheight}))
data)) (:data layer)))
(defn draw-tile [ctx tileset image tileid dest] (defn draw-tile [ctx tileset image tileid dest]
(let [{:keys [tileheight tilewidth (let [{:keys [tileheight tilewidth
@ -70,11 +70,8 @@
tileset-id (:tileset viewport) tileset-id (:tileset viewport)
image (tileset-id images) image (tileset-id images)
tileset (map-def->tileset def (:tileset viewport))] tileset (map-def->tileset def (:tileset viewport))]
(doseq [layer layers (doseq [layer layers]
:let [destinations (->> (:data layer) (doseq [dest layer
(layer->dest layer tileset)
(filter #(not= 0 (:tile %))))]]
(doseq [dest destinations
:let [dx (- (:x dest) (:x viewport)) :let [dx (- (:x dest) (:x viewport))
dy (- (:y dest) (:y viewport))]] dy (- (:y dest) (:y viewport))]]
(draw-tile ctx tileset image (draw-tile ctx tileset image

Loading…
Cancel
Save