From 0409b79a1852051dac4b301043cc49164426321b Mon Sep 17 00:00:00 2001 From: Cameron Date: Sun, 28 Apr 2024 03:41:19 -0700 Subject: [PATCH] making a test for these int grid values. looks to be working so far based on the test, found a small fix through this process --- .../Editor/Builders/LDtkBuilderLevel.cs | 4 +- .../LDtkComponentLayerIntGridValues.cs | 6 +- .../Tests/ComponentTilesetTilesTest.cs | 113 ++++++++++++++++++ .../Tests/ComponentTilesetTilesTest.cs.meta | 3 + 4 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 Assets/Tests/EditMode/Tests/ComponentTilesetTilesTest.cs create mode 100644 Assets/Tests/EditMode/Tests/ComponentTilesetTilesTest.cs.meta diff --git a/Assets/LDtkUnity/Editor/Builders/LDtkBuilderLevel.cs b/Assets/LDtkUnity/Editor/Builders/LDtkBuilderLevel.cs index 4801ce6bc..beead3fd7 100644 --- a/Assets/LDtkUnity/Editor/Builders/LDtkBuilderLevel.cs +++ b/Assets/LDtkUnity/Editor/Builders/LDtkBuilderLevel.cs @@ -253,7 +253,9 @@ private LDtkComponentLayer BuildLayerInstance(LayerInstance layer) LDtkComponentEntity[] entities = null; float layerScale = 1; - + + _layerIntGrid = null; + _layerTiles = null; void TryBuildLayerGameObject() { diff --git a/Assets/LDtkUnity/Runtime/Components/LDtkComponentLayerIntGridValues.cs b/Assets/LDtkUnity/Runtime/Components/LDtkComponentLayerIntGridValues.cs index 8e806b8a7..97016c245 100644 --- a/Assets/LDtkUnity/Runtime/Components/LDtkComponentLayerIntGridValues.cs +++ b/Assets/LDtkUnity/Runtime/Components/LDtkComponentLayerIntGridValues.cs @@ -69,17 +69,17 @@ internal void OnImport(LDtkDefinitionObjectsCache cache, LayerInstance instance) } /// - /// Get a IntGridValue tile at the coordinate for this layer + /// Get a IntGridValue tile at the coordinate for this layer. Returns -1 if a value at the coord doesn't exist. /// [PublicAPI] public int GetValue(Vector3Int coord) { LDtkDefinitionObjectIntGridValue def = GetValueDefinition(coord); - return def != null ? def.Value : 0; + return def != null ? def.Value : -1; } /// - /// Get a IntGridValue tile at the coordinate for this layer. Can be null. + /// Get a IntGridValue tile at the coordinate for this layer. Returns null if a value at the coord doesn't exist. /// [PublicAPI] public LDtkDefinitionObjectIntGridValue GetValueDefinition(Vector3Int coord) diff --git a/Assets/Tests/EditMode/Tests/ComponentTilesetTilesTest.cs b/Assets/Tests/EditMode/Tests/ComponentTilesetTilesTest.cs new file mode 100644 index 000000000..4628fa519 --- /dev/null +++ b/Assets/Tests/EditMode/Tests/ComponentTilesetTilesTest.cs @@ -0,0 +1,113 @@ +using System.Linq; +using NUnit.Framework; +using UnityEditor; +using UnityEngine; + +namespace LDtkUnity.Tests +{ + public class ComponentTilesTest + { + [Test] + public void TestExpectedCoordsTileset() + { + //const string lvlName = "Level"; + + string path = "Assets/Samples/Samples/Test_file_for_API_showing_all_features.ldtk"; + GameObject prefab = AssetDatabase.LoadAssetAtPath(path); + + LDtkComponentProject project = prefab.GetComponent(); + LDtkComponentLayer[] layers = project.Worlds[0].Levels[0].LayerInstances; + + foreach (LDtkComponentLayer layer in layers) + { + LDtkComponentLayerIntGridValues intGrid = layer.IntGrid; + + if (layer.Identifier == "IntGrid_without_rules") + { + Assert.NotNull(intGrid); + Assert.IsNull(layer.AutoLayerTiles); + Assert.IsNull(layer.GridTiles); + + Vector3Int[] allValidItems = new Vector3Int[] + { + new Vector3Int(3,28,0), + }; + + //test what doesnt exist + TestIntGridPosition(intGrid, new Vector3Int(0,0,0), false); + TestIntGridPosition(intGrid, new Vector3Int(1,0,0), false); + TestIntGridPosition(intGrid, new Vector3Int(1000,9999,0), false); + + //test what exists + + } + if (layer.Identifier == "IntGrid_with_rules") + { + Assert.NotNull(intGrid); + Assert.NotNull(layer.AutoLayerTiles); + Assert.IsNull(layer.GridTiles); + } + if (layer.Identifier == "PureAutoLayer") + { + Assert.IsNull(intGrid); + Assert.NotNull(layer.AutoLayerTiles); + Assert.IsNull(layer.GridTiles); + } + if (layer.Identifier == "Tiles") + { + Assert.IsNull(intGrid); + Assert.IsNull(layer.AutoLayerTiles); + Assert.NotNull(layer.GridTiles); + } + if (layer.Identifier == "IntGrid_8px_grid") + { + Assert.NotNull(intGrid); + Assert.IsNull(layer.AutoLayerTiles); + Assert.IsNull(layer.GridTiles); + } + } + + /*Level level = project.UnityWorlds.First().Levels.FirstOrDefault(); + Assert.NotNull(level, "null level"); + + //LayerInstance layer = level.LayerInstances.FirstOrDefault(p => p.IsIntGridLayer); + //Assert.NotNull(layer); + + Rect levelBounds = level.UnityWorldSpaceBounds(WorldLayout.Free, (int)16); + + + //Debug.Log(levelBounds);*/ + } + + private static void TestIntGridPosition(LDtkComponentLayerIntGridValues intGrid, Vector3Int pos, bool assertHas, int assertIs = -1, Vector3Int[] assertAllTilesExist = null) + { + LDtkDefinitionObjectIntGridValue valueObj = intGrid.GetValueDefinition(pos); + int value = intGrid.GetValue(pos); + + Assert.AreEqual(assertIs, value); + + if (assertHas) + { + Assert.NotNull(valueObj); + Assert.AreNotEqual(-1, value); + + Vector3Int[] positionsObj = intGrid.GetPositionsOfValueDefinition(valueObj); + Vector3Int[] positions = intGrid.GetPositionsOfValue(value); + + Assert.AreEqual(valueObj.Value, value); + Assert.True(positionsObj.SequenceEqual(positions)); + + Assert.True(ArraysContainSameValues(positionsObj, assertAllTilesExist)); + } + else + { + Assert.IsNull(valueObj); + } + } + + public static bool ArraysContainSameValues(T[] array1, T[] array2) + { + return array1.Length == array2.Length && array1.All(array2.Contains) && array2.All(array1.Contains); + } + } +} diff --git a/Assets/Tests/EditMode/Tests/ComponentTilesetTilesTest.cs.meta b/Assets/Tests/EditMode/Tests/ComponentTilesetTilesTest.cs.meta new file mode 100644 index 000000000..cfa1a9fd8 --- /dev/null +++ b/Assets/Tests/EditMode/Tests/ComponentTilesetTilesTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b66565a48be744baa1116ffe9bfa3d95 +timeCreated: 1714165668 \ No newline at end of file