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