From d9d5b7b5c58a7b044b78ee7993f0a15e5b79f689 Mon Sep 17 00:00:00 2001 From: miepzerino <o.skotnik@gmail.com> Date: Thu, 03 Apr 2025 18:28:15 +0000 Subject: [PATCH] #42 started performance fixes for chunk generation --- Assets/Scripts/CustomRuleTile.cs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 50 insertions(+), 10 deletions(-) diff --git a/Assets/Scripts/CustomRuleTile.cs b/Assets/Scripts/CustomRuleTile.cs index e80cac2..1043714 100644 --- a/Assets/Scripts/CustomRuleTile.cs +++ b/Assets/Scripts/CustomRuleTile.cs @@ -10,25 +10,65 @@ { public List<TileBase> siblings = new List<TileBase>(); + private Dictionary<int, bool> ruleMatchCache = new Dictionary<int, bool>(); public class Neighbor : RuleTile.TilingRule.Neighbor { public const int Sibling = 3; } public override bool RuleMatch(int neighbor, TileBase tile) { + // Create a unique key for caching based on neighbor and tile + int cacheKey = neighbor; + if (tile != null) + { + cacheKey = cacheKey * 23 + tile.GetInstanceID(); // Use prime number to reduce collisions + } + + // Check if we have a cached result + if (ruleMatchCache.TryGetValue(cacheKey, out bool cachedResult)) + { + return cachedResult; + } + + // If not cached, compute the result + bool result; + // Handle null tiles if (tile == null) - return neighbor == RuleTile.TilingRule.Neighbor.NotThis; - - // Always allow connections to siblings or self, regardless of surrounding tiles - if (tile == this || siblings.Contains(tile)) - return neighbor == RuleTile.TilingRule.Neighbor.This; - + { + result = neighbor == RuleTile.TilingRule.Neighbor.NotThis; + } + // Always allow connections to siblings or self + else if (tile == this || siblings.Contains(tile)) + { + result = neighbor == RuleTile.TilingRule.Neighbor.This; + } // For Sibling type explicitly - if (neighbor == Neighbor.Sibling) - return siblings.Contains(tile); - + else if (neighbor == Neighbor.Sibling) + { + result = siblings.Contains(tile); + } // For any other case, use base behavior - return base.RuleMatch(neighbor, tile); + else + { + result = base.RuleMatch(neighbor, tile); + } + + // Cache the result + ruleMatchCache[cacheKey] = result; + return result; + } + + // Add method to clear cache when needed (e.g., when rules change or on chunk unload) + public void ClearRuleCache() + { + ruleMatchCache.Clear(); + } + + // Override RefreshTile to clear cache for that specific tile position + public override void RefreshTile(Vector3Int position, ITilemap tilemap) + { + ClearRuleCache(); // Clear cache before refresh to ensure proper updates + base.RefreshTile(position, tilemap); } } \ No newline at end of file -- Gitblit v1.9.3