Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
TilemapSystem.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
5#include "../core/Types.hpp"
6#include "TilesetTable.hpp"
7#include "TileGrid.hpp"
8
9#include <glm/glm.hpp>
10#include <string>
11#include <unordered_map>
12#include <vector>
14
15namespace esengine {
16namespace tilemap {
17
18static constexpr u16 EMPTY_TILE = 0;
20static constexpr u32 CHUNK_SIZE = 16;
21
22ES_CONST(hex)
23static constexpr u16 TILE_ID_MASK = 0x1FFF;
24ES_CONST(hex)
25static constexpr u16 TILE_FLIP_H = 0x2000;
26ES_CONST(hex)
27static constexpr u16 TILE_FLIP_V = 0x4000;
28ES_CONST(hex)
29static constexpr u16 TILE_FLIP_D = 0x8000;
30
35
37 std::vector<AnimFrame> frames;
39};
40
41struct TileRange {
46 bool empty() const { return min_x >= max_x || min_y >= max_y; }
47};
48
49TileRange computeVisibleRange(f32 camLeft, f32 camBottom, f32 camRight, f32 camTop,
50 f32 originX, f32 originY,
51 f32 tileWidth, f32 tileHeight,
52 u32 mapWidth, u32 mapHeight);
53
54struct ChunkCoord {
56 bool operator==(const ChunkCoord& o) const { return x == o.x && y == o.y; }
57};
58
60 size_t operator()(const ChunkCoord& c) const {
61 return std::hash<u64>()(static_cast<u64>(static_cast<u32>(c.x))
62 | (static_cast<u64>(static_cast<u32>(c.y)) << 32));
63 }
64};
65
66struct ChunkData {
68 // Edit stamp copied from the owning LayerData::edit_revision on each change.
69 // The renderer compares this against the revision it last built per chunk and
70 // NEVER writes it back — rendering reads a read-only snapshot instead of
71 // flipping a content-side `dirty` flag (RC5: abstractions are one-way).
73};
74
76public:
77 void initLayer(Entity entity, u32 width, u32 height,
78 f32 tileWidth, f32 tileHeight);
79 void initInfiniteLayer(Entity entity, f32 tileWidth, f32 tileHeight);
80 void setChunkTiles(Entity entity, i32 chunkX, i32 chunkY,
81 const u16* tiles, u32 width, u32 height);
84 void removeChunk(Entity entity, i32 chunkX, i32 chunkY);
85 void destroyLayer(Entity entity);
86 bool hasLayer(Entity entity) const;
87
88 void setTile(Entity entity, i32 x, i32 y, u16 tileId);
89 u16 getTile(Entity entity, i32 x, i32 y) const;
90 void fillRect(Entity entity, i32 x, i32 y, u32 w, u32 h, u16 tileId);
91 void setTiles(Entity entity, const u16* tiles, u32 count);
92
93 void setTileAnimation(Entity entity, u16 tileId,
94 const AnimFrame* frames, u32 frameCount);
95 void clearTileAnimations(Entity entity);
96 void advanceAnimations(Entity entity, f32 dtMs);
97 u16 resolveAnimatedTile(Entity entity, u16 tileId) const;
98
99 void setTileProperty(Entity entity, u16 tileId,
100 const std::string& key, const std::string& value);
101 std::string getTileProperty(Entity entity, u16 tileId,
102 const std::string& key) const;
103
104 void flipTile(Entity entity, i32 x, i32 y, bool flipH, bool flipV, bool flipD);
105 void rotateTile(Entity entity, i32 x, i32 y, i32 degrees);
106
107 void setGridType(Entity entity, GridType type);
108
111 void setHexParams(Entity entity, f32 sideLength, bool staggerAxisX, bool staggerIndexEven);
112
113 struct LayerData {
115 u32 width = 0; // finite-layer bounds (0 = infinite); tiles live in `chunks`.
119
120 // Single-tileset fields (kept for painted layers / the single-tileset
121 // fallback). Multi-tileset layers (e.g. Tiled imports) instead populate
122 // `tilesets` below; when it is non-empty the renderer uses it and these
123 // are ignored.
128
129 // Multi-tileset table (sorted by first_id). Empty = single-tileset layer.
130 std::vector<TilesetSlot> tilesets;
133 glm::vec4 tint{1.0f, 1.0f, 1.0f, 1.0f};
134 f32 opacity = 1.0f;
135 glm::vec2 parallax_factor{1.0f, 1.0f};
136 bool visible = true;
142 bool hex_stagger_axis_x = false;
144
149
150 std::unordered_map<ChunkCoord, ChunkData, ChunkCoordHash> chunks;
151 bool infinite = false;
152
153 // Monotonic counter bumped on every chunk edit; stamped into
154 // ChunkData::revision so the renderer detects changes without the content
155 // system exposing a mutable dirty flag.
157 // Bumped when a chunk is taken out, so the renderer knows to drop what it cached for chunks that are gone.
159
160 std::unordered_map<u16, TileAnimation> tile_animations;
161 f64 elapsed_ms = 0; // f64: an f32 accumulator stalls (dt < ULP) after ~day-scale uptime
162 // Bumped only when some animation's CURRENT frame flips — ANIMATED chunk
163 // meshes rebuild on this, not every frame.
165 // Bumped when the animation TABLE changes (a tile gains/loses an
166 // animation). This forces EVERY chunk to re-evaluate once, so a chunk that
167 // was all-static picks up a tile that just became animated — the
168 // anim_revision check above is gated on has_animated_tiles and would miss
169 // that false→true transition.
171
172 std::unordered_map<u16, std::unordered_map<std::string, std::string>> tile_properties;
173 };
174
175 const LayerData* getLayerData(Entity entity) const;
177
178 void setRenderProps(Entity entity, u32 textureHandle, u32 tilesetColumns,
179 f32 uvTileW, f32 uvTileH,
180 i32 sortLayer, f32 depth,
181 f32 parallaxX, f32 parallaxY);
182
183 // Replace the layer's multi-tileset table (sorted ascending by first_id).
184 // Passing an empty table reverts the layer to the single-tileset fields.
185 void setTilesets(Entity entity, std::vector<TilesetSlot> slots);
186 void setTint(Entity entity, f32 r, f32 g, f32 b, f32 a, f32 opacity);
187 void setVisible(Entity entity, bool visible);
188 void setOriginEntity(Entity layerKey, Entity originEntity);
189
190 void tileToWorld(Entity entity, i32 tx, i32 ty,
191 f32 originX, f32 originY, f32& outX, f32& outY) const;
192 void worldToTile(Entity entity, f32 wx, f32 wy,
193 f32 originX, f32 originY, i32& outTx, i32& outTy) const;
194
195 using LayerMap = std::unordered_map<Entity, LayerData>;
196 const LayerMap& allLayers() const { return layers_; }
197
198private:
199 u32 next_generation_ = 0;
200 LayerMap layers_;
201};
202
203} // namespace tilemap
204} // namespace esengine
Reflection and metadata system for automatic binding generation.
#define ES_CONST(...)
Mark a constant that CROSSES the boundary, so EHT emits its TS twin.
Local tile placement and polygon containment shared by rendering and queries.
Per-layer tileset table + GID→tileset resolution (dependency-free).
Core type definitions and utilities for ESEngine.
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 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
const LayerMap & allLayers() 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
std::unordered_map< Entity, LayerData > LayerMap
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.
Definition Types.hpp:54
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
Definition Types.hpp:174
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint16_t u16
Unsigned 16-bit integer.
Definition Types.hpp:43
double f64
64-bit floating point
Definition Types.hpp:61
float f32
32-bit floating point
Definition Types.hpp:59
Entity identifier with packed index + generation.
Definition Types.hpp:135
size_t operator()(const ChunkCoord &c) const
bool operator==(const ChunkCoord &o) const
u16 tiles[CHUNK_SIZE *CHUNK_SIZE]
std::vector< AnimFrame > frames
std::unordered_map< u16, std::unordered_map< std::string, std::string > > tile_properties
std::unordered_map< u16, TileAnimation > tile_animations
bool hex_stagger_axis_x
true: columns stagger (staggeraxis=x)
std::unordered_map< ChunkCoord, ChunkData, ChunkCoordHash > chunks
bool hex_stagger_index_even
true: even rows/cols carry the shift