master
Josha von Gizycki 5 years ago
parent e1163421c5
commit 0e6aca8071

@ -1,6 +1,5 @@
(ns topdown2d.demoscene (ns topdown2d.demoscene
(:require [topdown2d.collision :as collision] (:require [topdown2d.collision :as collision]
[topdown2d.input :as input]
[topdown2d.sprites :as sprites] [topdown2d.sprites :as sprites]
[topdown2d.dom :refer [by-id]] [topdown2d.dom :refer [by-id]]
[topdown2d.tileset :as tileset] [topdown2d.tileset :as tileset]
@ -56,11 +55,12 @@
(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 zoom :zoom} (:map-def scenestate)
zoom :zoom} (:map-def scenestate)
viewport (:viewport scenestate) viewport (:viewport scenestate)
{mw :width mh :height {mw :width
tw :tilewidth th :tileheight} loaded-def] mh :height
tw :tilewidth
th :tileheight} loaded-def]
(-> scenestate (-> scenestate
(assoc-in [:viewport :keep-in :w] (* (dec mw) zoom tw)) (assoc-in [:viewport :keep-in :w] (* (dec mw) zoom tw))
(assoc-in [:viewport :keep-in :h] (* (dec mh) zoom th)) (assoc-in [:viewport :keep-in :h] (* (dec mh) zoom th))

@ -36,11 +36,26 @@
[(dec x) [(dec x)
(/ (- ix x) width)])) (/ (- 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 (map-indexed
(fn [ix tile] (fn [ix tile]
(let [[dxc dyc] (ix->coords ix (:width layer)) (let [[dxc dyc] (ix->coords ix (:width layer))]
{:keys [target-width target-height]} tileset]
{:tile tile {:tile tile
:x (* dxc target-width) :x (* dxc target-width)
:y (* dyc target-height) :y (* dyc target-height)
@ -48,6 +63,17 @@
:h target-height})) :h target-height}))
(:data layer))) (: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] (defn draw-tile [ctx tileset image tileid dest]
(let [{:keys [tileheight tilewidth (let [{:keys [tileheight tilewidth
tiles-x target-width target-height]} tileset tiles-x target-width target-height]} tileset
@ -59,31 +85,12 @@
(int (:x dest)) (int (:y dest)) (int (:x dest)) (int (:y dest))
target-width target-height))) 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] (defn draw-viewport [map-def ctx viewport]
(let [def (:def map-def) (let [def (:def map-def)
image (:image viewport) image (:image viewport)
tileset (:tileset-def viewport)] tileset (:tileset-def viewport)]
(doseq [layer (:layers def)] (doseq [layers (:layers def)]
(doseq [dest layer (doseq [dest layers
: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