From eca484c72320218ba7057bda38814877eb1b1184 Mon Sep 17 00:00:00 2001 From: Cameron Date: Sun, 28 Apr 2024 01:39:39 -0700 Subject: [PATCH] made intgrid values cached in a better way to get values quickly in runtime --- .../LDtkComponentLayerIntGridValues.cs | 87 +++++++++---------- 1 file changed, 42 insertions(+), 45 deletions(-) diff --git a/Assets/LDtkUnity/Runtime/Components/LDtkComponentLayerIntGridValues.cs b/Assets/LDtkUnity/Runtime/Components/LDtkComponentLayerIntGridValues.cs index 4ef91199f..8e806b8a7 100644 --- a/Assets/LDtkUnity/Runtime/Components/LDtkComponentLayerIntGridValues.cs +++ b/Assets/LDtkUnity/Runtime/Components/LDtkComponentLayerIntGridValues.cs @@ -15,7 +15,8 @@ public sealed class LDtkComponentLayerIntGridValues : MonoBehaviour { [SerializeField] internal IntGridValuePositions[] _values; - private Dictionary _valuesDict; + private Dictionary _valueOfPositionDict; + private Dictionary _positionsOfValueDict; [Serializable] internal class IntGridValuePositions @@ -66,79 +67,75 @@ internal void OnImport(LDtkDefinitionObjectsCache cache, LayerInstance instance) } _values = valuePositions.Select(pair => pair.Value).ToArray(); } - - private void TryInitialize() + + /// + /// Get a IntGridValue tile at the coordinate for this layer + /// + [PublicAPI] + public int GetValue(Vector3Int coord) { - if (_valuesDict == null) - { - Initialize(); - } + LDtkDefinitionObjectIntGridValue def = GetValueDefinition(coord); + return def != null ? def.Value : 0; } /// - /// Indexes the serialized data. Call this to re-cache data if needed. + /// Get a IntGridValue tile at the coordinate for this layer. Can be null. /// [PublicAPI] - public void Initialize() + public LDtkDefinitionObjectIntGridValue GetValueDefinition(Vector3Int coord) { - _valuesDict = new Dictionary(_values.Sum(p => p._positions.Count)); + TryCacheValueOfPositionDict(); + return _valueOfPositionDict.TryGetValue(coord, out LDtkDefinitionObjectIntGridValue def) ? def : null; + } + private void TryCacheValueOfPositionDict() + { + if (_valueOfPositionDict != null) + { + return; + } + + _valueOfPositionDict = new Dictionary(_values.Sum(p => p._positions.Count)); foreach (IntGridValuePositions value in _values) { foreach (Vector3Int position in value._positions) { - if (_valuesDict.ContainsKey(position)) + if (!_valueOfPositionDict.ContainsKey(position)) { - GameObject obj = gameObject; - LDtkDebug.LogWarning($"Duplicate entry found for {obj.name}, ", obj); + _valueOfPositionDict.Add(position, value._value); continue; } - _valuesDict.Add(position, value._value); + + GameObject obj = gameObject; + LDtkDebug.LogWarning($"Duplicate entry found for {obj.name}, ", obj); } } } - - /// - /// Get a IntGridValue tile at the coordinate for this layer. Can be null. - /// - //todo needs a unit test - public LDtkDefinitionObjectIntGridValue GetValueDefinition(Vector3Int coord) - { - TryInitialize(); - return _valuesDict.TryGetValue(coord, out LDtkDefinitionObjectIntGridValue def) ? def : null; - } - - /// - /// Get a IntGridValue tile at the coordinate for this layer - /// - //todo needs a unit test - public int GetValue(Vector3Int coord) - { - LDtkDefinitionObjectIntGridValue def = GetValueDefinition(coord); - return def != null ? def.Value : 0; - } /// /// Get all Tilemap coordinates of a specific IntGrid value. /// - //todo needs a unit test - public Vector3Int[] GetPositionsOfValue(LDtkDefinitionObjectIntGridValue value) + [PublicAPI] + public Vector3Int[] GetPositionsOfValueDefinition(LDtkDefinitionObjectIntGridValue value) { - TryInitialize(); - return _valuesDict - .Where(p => p.Value == value) - .Select(p => p.Key).ToArray(); + if (value == null) + { + LDtkDebug.LogError("Argument null when trying to get IntGrid positions", gameObject); + return null; + } + return GetPositionsOfValue(value.Value); } /// /// Get all Tilemap coordinates of a specific IntGrid value. /// - //todo needs a unit test + [PublicAPI] public Vector3Int[] GetPositionsOfValue(int value) { - TryInitialize(); - return _valuesDict - .Where(p => p.Value.Value == value) - .Select(p => p.Key).ToArray(); + if (_positionsOfValueDict == null) + { + _positionsOfValueDict = _values.ToDictionary(obj => obj._value.Value, obj => obj._positions.ToArray()); + } + return _positionsOfValueDict[value]; } } }