Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(render): tilemap picks up an animation added to a placed tile

esengine6fe64de4f42f
diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
index c9cf12d2a..58622fd9c 100644
--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
@@ -284,12 +284,14 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {
auto& cache = chunkCaches[coord];
if (chunkData.revision != cache.built_revision
+ || layer.anim_table_revision != cache.built_anim_table_revision
|| (cache.has_animated_tiles && layer.anim_revision != cache.built_anim_revision)
|| cache.slots.size() != slots.size()) {
rebuildChunk(layer, chunkData, coord,
slots, resolved, entity, cache);
cache.built_revision = chunkData.revision;
cache.built_anim_revision = layer.anim_revision;
+ cache.built_anim_table_revision = layer.anim_table_revision;
}
for (usize si = 0; si < slots.size() && si < cache.slots.size(); ++si) {
diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp
index 2743d2fb9..0cbe01230 100644
--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp
+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp
@@ -43,6 +43,9 @@ private:
// Animation-frame revision this cache was built at; an animated chunk
// rebuilds when a visible frame flips, not every frame.
u32 built_anim_revision = 0;
+ // Animation-TABLE revision this cache was built at; every chunk rebuilds
+ // once when the table changes so a static chunk can become animated.
+ u32 built_anim_table_revision = 0;
};
// A layer slot with its texture resolved for the current frame.
diff --git a/src/esengine/tilemap/TilemapSystem.cpp b/src/esengine/tilemap/TilemapSystem.cpp
index 5e8420804..87ff5e0a9 100644
--- a/src/esengine/tilemap/TilemapSystem.cpp
+++ b/src/esengine/tilemap/TilemapSystem.cpp
@@ -243,7 +243,7 @@ void TilemapSystem::setTileAnimation(Entity entity, u16 tileId,
}
if (anim.total_duration_ms == 0) return;
layer->tile_animations[tileId] = std::move(anim);
- layer->anim_revision++; // the frame table changed — cached meshes are stale
+ layer->anim_table_revision++; // the table changed — every chunk re-evaluates once
}
namespace {
diff --git a/src/esengine/tilemap/TilemapSystem.hpp b/src/esengine/tilemap/TilemapSystem.hpp
index 8703a3d47..1a6617e05 100644
--- a/src/esengine/tilemap/TilemapSystem.hpp
+++ b/src/esengine/tilemap/TilemapSystem.hpp
@@ -147,10 +147,15 @@ public:
std::unordered_map<u16, TileAnimation> tile_animations;
f32 elapsed_ms = 0;
- // Bumped only when some animation's CURRENT frame changes (or the
- // animation table itself does) — animated chunk meshes rebuild on this,
- // not every frame.
+ // Bumped only when some animation's CURRENT frame flips — ANIMATED chunk
+ // meshes rebuild on this, not every frame.
u32 anim_revision = 0;
+ // Bumped when the animation TABLE changes (a tile gains/loses an
+ // animation). This forces EVERY chunk to re-evaluate once, so a chunk that
+ // was all-static picks up a tile that just became animated — the
+ // anim_revision check above is gated on has_animated_tiles and would miss
+ // that false→true transition.
+ u32 anim_table_revision = 0;
std::unordered_map<u16, std::unordered_map<std::string, std::string>> tile_properties;
};
diff --git a/tests/tilemap/test_tilemap.cpp b/tests/tilemap/test_tilemap.cpp
index faf3e12c5..d82abc17f 100644
--- a/tests/tilemap/test_tilemap.cpp
+++ b/tests/tilemap/test_tilemap.cpp
@@ -329,3 +329,27 @@ TEST_CASE("tilemap_hex_tile_to_world") {
CHECK_EQ(ty, 0);
}
}
+
+TEST_CASE("tilemap_animation_revision_separation") {
+ // The render cache must distinguish a TABLE change (a tile GAINS an animation
+ // → every chunk re-evaluates once, so an all-static chunk can become animated)
+ // from a per-frame FLIP (only already-animated chunks rebuild). A single
+ // conflated revision let a static chunk miss a tile that just became animated.
+ TilemapSystem sys;
+ sys.initLayer(E(0), 8, 8, 32.0f, 32.0f);
+
+ REQUIRE(sys.getLayerData(E(0)) != nullptr);
+ CHECK(sys.getLayerData(E(0))->anim_table_revision == 0);
+ CHECK(sys.getLayerData(E(0))->anim_revision == 0);
+
+ // Adding an animation is a TABLE change: anim_table_revision bumps, anim_revision does not.
+ AnimFrame frames[2] = {{10, 100}, {11, 100}}; // two 100ms frames
+ sys.setTileAnimation(E(0), 10, frames, 2);
+ CHECK(sys.getLayerData(E(0))->anim_table_revision == 1);
+ CHECK(sys.getLayerData(E(0))->anim_revision == 0);
+
+ // Advancing past a frame boundary is a FLIP: anim_revision bumps, the table revision holds.
+ sys.advanceAnimations(E(0), 150.0f); // crosses the 100ms boundary → frame 0→1
+ CHECK(sys.getLayerData(E(0))->anim_revision == 1);
+ CHECK(sys.getLayerData(E(0))->anim_table_revision == 1);
+}