| | |
| | | { |
| | | |
| | | 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); |
| | | } |
| | | } |