14 f32 tileWidth,
f32 tileHeight,
15 u32 mapWidth,
u32 mapHeight) {
16 f32 localLeft = camLeft - originX;
17 f32 localBottom = camBottom - originY;
18 f32 localRight = camRight - originX;
19 f32 localTop = camTop - originY;
21 i32 minX =
static_cast<i32>(std::floor(localLeft / tileWidth));
22 i32 minY =
static_cast<i32>(std::floor(localBottom / tileHeight));
23 i32 maxX =
static_cast<i32>(std::ceil(localRight / tileWidth));
24 i32 maxY =
static_cast<i32>(std::ceil(localTop / tileHeight));
26 minX = std::max(minX, 0);
27 minY = std::max(minY, 0);
28 maxX = std::min(maxX,
static_cast<i32>(mapWidth));
29 maxY = std::min(maxY,
static_cast<i32>(mapHeight));
31 return {minX, minY, maxX, maxY};
40 return a / n -
static_cast<i32>(a % n < 0);
44inline bool inBounds(
const TilemapSystem::LayerData& layer,
i32 x,
i32 y) {
45 if (layer.width == 0 && layer.height == 0)
return true;
46 return x >= 0 && y >= 0
47 &&
static_cast<u32>(x) < layer.width &&
static_cast<u32>(y) < layer.height;
51 const i32 cx = chunkOf(x), cy = chunkOf(y);
52 auto it = layer.chunks.find({cx, cy});
53 if (it == layer.chunks.end())
return EMPTY_TILE;
60 const i32 cx = chunkOf(x), cy = chunkOf(y);
64 auto it = layer.chunks.find({cx, cy});
65 if (it == layer.chunks.end()) {
67 it = layer.chunks.try_emplace(
ChunkCoord{cx, cy}).first;
70 if (chunk.tiles[index] == value)
return;
71 chunk.
tiles[index] = value;
72 chunk.revision = ++layer.edit_revision;
78 if (!layer.chunks.empty()) ++layer.edit_revision;
82 for (
u32 cy = 0; cy < chunksY; ++cy) {
83 for (
u32 cx = 0; cx < chunksX; ++cx) {
88 if (ty >= height)
break;
91 if (tx >= width)
break;
92 const usize i =
static_cast<usize>(ty) * width + tx;
99 chunk.revision = ++layer.edit_revision;
100 layer.chunks[{
static_cast<i32>(cx),
static_cast<i32>(cy)}] = chunk;
109 f32 tileWidth,
f32 tileHeight) {
116 layers_[entity] = std::move(layer);
127 layers_[entity] = std::move(layer);
131 const u16* tiles,
u32 width,
u32 height) {
136 auto& chunk = layer->chunks[coord];
137 chunk.revision = ++layer->edit_revision;
138 std::memset(chunk.tiles, 0,
sizeof(chunk.tiles));
142 chunk.tiles[ly *
CHUNK_SIZE + lx] = tiles[ly * width + lx];
150 if (layer->chunks.erase(
ChunkCoord{chunkX, chunkY}) == 0)
return;
151 ++layer->edit_revision;
152 ++layer->chunks_erased;
156 layers_.erase(entity);
160 return layers_.count(entity) > 0;
164 auto it = layers_.find(entity);
165 if (it == layers_.end())
return nullptr;
170 auto it = layers_.find(entity);
171 if (it == layers_.end())
return;
172 auto& layer = it->second;
173 if (!inBounds(layer, x, y))
return;
174 writeTile(layer, x, y, tileId);
178 auto it = layers_.find(entity);
180 const auto& layer = it->second;
181 if (!inBounds(layer, x, y))
return EMPTY_TILE;
182 return readTile(layer, x, y);
187 auto it = layers_.find(entity);
188 if (it == layers_.end())
return;
189 auto& layer = it->second;
190 const i64 coordinateEnd =
static_cast<i64>(std::numeric_limits<i32>::max()) + 1;
191 i64 startX = x, startY = y;
192 i64 endX = std::min(
static_cast<i64>(x) + w, coordinateEnd);
193 i64 endY = std::min(
static_cast<i64>(y) + h, coordinateEnd);
194 if (layer.width != 0 || layer.height != 0) {
195 startX = std::max(startX,
i64{0});
196 startY = std::max(startY,
i64{0});
197 endX = std::min(endX,
static_cast<i64>(layer.width));
198 endY = std::min(endY,
static_cast<i64>(layer.height));
200 if (startX >= endX || startY >= endY)
return;
201 for (
i64 ty = startY; ty < endY; ++ty) {
202 for (
i64 tx = startX; tx < endX; ++tx) {
203 writeTile(layer,
static_cast<i32>(tx),
static_cast<i32>(ty), tileId);
209 auto it = layers_.find(entity);
210 if (it == layers_.end())
return;
211 auto& layer = it->second;
212 buildChunksFromFlat(layer, tiles, layer.width, layer.height, count);
216 auto it = layers_.find(entity);
217 if (it == layers_.end())
return nullptr;
224 f32 parallaxX,
f32 parallaxY) {
227 layer->texture_handle = textureHandle;
228 layer->tileset_columns = tilesetColumns;
229 layer->uv_tile_width = uvTileW;
230 layer->uv_tile_height = uvTileH;
231 layer->sort_layer = sortLayer;
232 layer->depth = depth;
233 layer->parallax_factor = {parallaxX, parallaxY};
239 std::sort(slots.begin(), slots.end(),
241 layer->tilesets = std::move(slots);
247 layer->tint = {r, g, b, a};
248 layer->opacity = opacity;
254 layer->visible = visible;
260 layer->origin_entity = originEntity;
266 if (!layer || frameCount == 0)
return;
269 anim.
frames.assign(frames, frames + frameCount);
271 for (
u32 i = 0; i < frameCount; i++) {
275 layer->tile_animations[tileId] = std::move(anim);
276 layer->anim_table_revision++;
281 if (!layer || layer->tile_animations.empty())
return;
284 layer->tile_animations.clear();
285 layer->anim_table_revision++;
293 for (
u32 i = 0; i < anim.
frames.size(); ++i) {
294 acc += anim.
frames[i].duration_ms;
295 if (t < acc)
return i;
297 return static_cast<u32>(anim.
frames.size()) - 1;
303 if (!layer || layer->tile_animations.empty())
return;
304 const f64 prev = layer->elapsed_ms;
305 layer->elapsed_ms +=
static_cast<f64>(dtMs);
309 for (
const auto& [
id, anim] : layer->tile_animations) {
310 if (frameIndexAt(anim, prev) != frameIndexAt(anim, layer->elapsed_ms)) {
311 layer->anim_revision++;
319 if (!layer)
return tileId;
321 auto it = layer->tile_animations.find(tileId);
322 if (it == layer->tile_animations.end())
return tileId;
324 const auto& anim = it->second;
325 return anim.
frames[frameIndexAt(anim, layer->elapsed_ms)].tile_id;
329 const std::string& key,
const std::string& value) {
332 layer->tile_properties[tileId][key] = value;
336 const std::string& key)
const {
338 if (!layer)
return {};
340 auto tileIt = layer->tile_properties.find(tileId);
341 if (tileIt == layer->tile_properties.end())
return {};
343 auto propIt = tileIt->second.find(key);
344 if (propIt == tileIt->second.end())
return {};
346 return propIt->second;
350 bool flipH,
bool flipV,
bool flipD) {
352 if (!layer || !inBounds(*layer, x, y))
return;
360 writeTile(*layer, x, y,
id | flags);
365 if (!layer || !inBounds(*layer, x, y))
return;
369 i32 rot = ((degrees % 360) + 360) % 360;
377 writeTile(*layer, x, y,
id | flags);
383 layer->grid_type = type;
389 layer->hex_side_length = sideLength;
390 layer->hex_stagger_axis_x = staggerAxisX;
391 layer->hex_stagger_index_even = staggerIndexEven;
396 f32& outX,
f32& outY)
const {
398 if (!layer) { outX = 0; outY = 0;
return; }
400 const auto anchor = layer->grid().anchor(tx, ty);
401 outX = originX + anchor.x;
402 outY = originY + anchor.y;
407 i32& outTx,
i32& outTy)
const {
409 if (!layer) { outTx = 0; outTy = 0;
return; }
411 const auto cell = layer->grid().cellAt(wx - originX, wy - originY);
void setOriginEntity(Entity layerKey, Entity originEntity)
void rotateTile(Entity entity, i32 x, i32 y, i32 degrees)
void setChunkTiles(Entity entity, i32 chunkX, i32 chunkY, const u16 *tiles, u32 width, u32 height)
void destroyLayer(Entity entity)
void setTiles(Entity entity, const u16 *tiles, u32 count)
void setVisible(Entity entity, bool visible)
void setHexParams(Entity entity, f32 sideLength, bool staggerAxisX, bool staggerIndexEven)
void setTileProperty(Entity entity, u16 tileId, const std::string &key, const std::string &value)
void removeChunk(Entity entity, i32 chunkX, i32 chunkY)
void setTileAnimation(Entity entity, u16 tileId, const AnimFrame *frames, u32 frameCount)
bool hasLayer(Entity entity) const
void setTile(Entity entity, i32 x, i32 y, u16 tileId)
void setTilesets(Entity entity, std::vector< TilesetSlot > slots)
void worldToTile(Entity entity, f32 wx, f32 wy, f32 originX, f32 originY, i32 &outTx, i32 &outTy) const
void clearTileAnimations(Entity entity)
void initLayer(Entity entity, u32 width, u32 height, f32 tileWidth, f32 tileHeight)
u16 getTile(Entity entity, i32 x, i32 y) const
void advanceAnimations(Entity entity, f32 dtMs)
void fillRect(Entity entity, i32 x, i32 y, u32 w, u32 h, u16 tileId)
void flipTile(Entity entity, i32 x, i32 y, bool flipH, bool flipV, bool flipD)
void setRenderProps(Entity entity, u32 textureHandle, u32 tilesetColumns, f32 uvTileW, f32 uvTileH, i32 sortLayer, f32 depth, f32 parallaxX, f32 parallaxY)
const LayerData * getLayerData(Entity entity) const
void tileToWorld(Entity entity, i32 tx, i32 ty, f32 originX, f32 originY, f32 &outX, f32 &outY) const
u16 resolveAnimatedTile(Entity entity, u16 tileId) const
std::string getTileProperty(Entity entity, u16 tileId, const std::string &key) const
void setGridType(Entity entity, GridType type)
void setTint(Entity entity, f32 r, f32 g, f32 b, f32 a, f32 opacity)
LayerData * getLayerDataMut(Entity entity)
void initInfiniteLayer(Entity entity, f32 tileWidth, f32 tileHeight)
static constexpr u32 CHUNK_SIZE
TileRange computeVisibleRange(f32 camLeft, f32 camBottom, f32 camRight, f32 camTop, f32 originX, f32 originY, f32 tileWidth, f32 tileHeight, u32 mapWidth, u32 mapHeight)
static constexpr u16 TILE_FLIP_D
static constexpr u16 TILE_FLIP_H
static constexpr u16 EMPTY_TILE
static constexpr u16 TILE_FLIP_V
static constexpr u16 TILE_ID_MASK
std::int32_t i32
Signed 32-bit integer.
std::size_t usize
Size type for indexing and sizes.
std::uint32_t u32
Unsigned 32-bit integer.
std::int64_t i64
Signed 64-bit integer.
std::uint16_t u16
Unsigned 16-bit integer.
double f64
64-bit floating point
float f32
32-bit floating point
Entity identifier with packed index + generation.
u16 tiles[CHUNK_SIZE *CHUNK_SIZE]
std::vector< AnimFrame > frames