diff --git a/src/cljs/topdown2d/demoscene.cljs b/src/cljs/topdown2d/demoscene.cljs index e8cf824..3e5c16c 100644 --- a/src/cljs/topdown2d/demoscene.cljs +++ b/src/cljs/topdown2d/demoscene.cljs @@ -1,6 +1,5 @@ (ns topdown2d.demoscene (:require [topdown2d.collision :as collision] - [topdown2d.input :as input] [topdown2d.sprites :as sprites] [topdown2d.dom :refer [by-id]] [topdown2d.tileset :as tileset] @@ -56,11 +55,12 @@ (collision/pps->px gamestate viewport))) (defn init-viewport [scenestate] - (let [{loaded-def :def - zoom :zoom} (:map-def scenestate) + (let [{loaded-def :def zoom :zoom} (:map-def scenestate) viewport (:viewport scenestate) - {mw :width mh :height - tw :tilewidth th :tileheight} loaded-def] + {mw :width + mh :height + tw :tilewidth + th :tileheight} loaded-def] (-> scenestate (assoc-in [:viewport :keep-in :w] (* (dec mw) zoom tw)) (assoc-in [:viewport :keep-in :h] (* (dec mh) zoom th)) diff --git a/src/cljs/topdown2d/tileset.cljs b/src/cljs/topdown2d/tileset.cljs index daa9f3b..7cd9985 100644 --- a/src/cljs/topdown2d/tileset.cljs +++ b/src/cljs/topdown2d/tileset.cljs @@ -27,8 +27,8 @@ (next-animation-cycle animation curr-cycle))] (if trigger-cycle? (assoc anim-def - :last-cycle now - :curr-id (:tileid next-cycle)) + :last-cycle now + :curr-id (:tileid next-cycle)) anim-def))) (defn ix->coords [ix width] @@ -36,17 +36,43 @@ [(dec x) (/ (- ix x) width)])) -(defn layer->dest [tileset layer] +(defn map-tileset + [zoom {:keys [tilewidth tileheight columns tilecount] + :as tileset}] + (assoc tileset + :target-width (* zoom tilewidth) + :target-height (* zoom tileheight) + :tiles-x columns + :tiles-y (/ tilecount columns))) + +(defn map-tilesets [zoom map-def] + (update map-def + :tilesets + (fn [tilesets] + (map (partial map-tileset zoom) + tilesets)))) + +(defn layer->dest [{:keys [target-width target-height]} layer] (map-indexed - (fn [ix tile] - (let [[dxc dyc] (ix->coords ix (:width layer)) - {:keys [target-width target-height]} tileset] - {:tile tile - :x (* dxc target-width) - :y (* dyc target-height) - :w target-width - :h target-height})) - (:data layer))) + (fn [ix tile] + (let [[dxc dyc] (ix->coords ix (:width layer))] + {:tile tile + :x (* dxc target-width) + :y (* dyc target-height) + :w target-width + :h target-height})) + (:data layer))) + +(defn map-layers [map-def] + (let [first-tileset (-> map-def :tilesets first)] + (update map-def + :layers + (fn [layers] + (map (partial layer->dest first-tileset) + layers))))) + +(defn prepare-map-def [zoom map-def] + (map-layers (map-tilesets zoom map-def))) (defn draw-tile [ctx tileset image tileid dest] (let [{:keys [tileheight tilewidth @@ -59,31 +85,12 @@ (int (:x dest)) (int (:y dest)) target-width target-height))) -(defn map-tilesets [zoom tileset] - (-> tileset - (assoc :target-width (* zoom (:tilewidth tileset))) - (assoc :target-height (* zoom (:tileheight tileset))) - (assoc :tiles-x (:columns tileset)) - (assoc :tiles-y (/ (:tilecount tileset) - (:columns tileset))))) - -(defn map-layers [map-def] - (let [ftileset (first (:tilesets map-def))] - (update map-def :layers - #(map (partial layer->dest ftileset) - %)))) - -(defn prepare-map-def [zoom map-def] - (-> map-def - (update :tilesets #(map (partial map-tilesets zoom) %)) - map-layers)) - (defn draw-viewport [map-def ctx viewport] (let [def (:def map-def) image (:image viewport) tileset (:tileset-def viewport)] - (doseq [layer (:layers def)] - (doseq [dest layer + (doseq [layers (:layers def)] + (doseq [dest layers :let [dx (- (:x dest) (:x viewport)) dy (- (:y dest) (:y viewport))]] (draw-tile ctx tileset image