Skip to content

Commit

Permalink
making a test for these int grid values. looks to be working so far b…
Browse files Browse the repository at this point in the history
…ased on the test, found a small fix through this process
  • Loading branch information
Cammin committed Apr 28, 2024
1 parent eca484c commit 0409b79
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Assets/LDtkUnity/Editor/Builders/LDtkBuilderLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ private LDtkComponentLayer BuildLayerInstance(LayerInstance layer)

LDtkComponentEntity[] entities = null;
float layerScale = 1;


_layerIntGrid = null;
_layerTiles = null;

void TryBuildLayerGameObject()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ internal void OnImport(LDtkDefinitionObjectsCache cache, LayerInstance instance)
}

/// <summary>
/// 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.
/// </summary>
[PublicAPI]
public int GetValue(Vector3Int coord)
{
LDtkDefinitionObjectIntGridValue def = GetValueDefinition(coord);
return def != null ? def.Value : 0;
return def != null ? def.Value : -1;
}

/// <summary>
/// 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.
/// </summary>
[PublicAPI]
public LDtkDefinitionObjectIntGridValue GetValueDefinition(Vector3Int coord)
Expand Down
113 changes: 113 additions & 0 deletions Assets/Tests/EditMode/Tests/ComponentTilesetTilesTest.cs
Original file line number Diff line number Diff line change
@@ -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<GameObject>(path);

LDtkComponentProject project = prefab.GetComponent<LDtkComponentProject>();
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>(T[] array1, T[] array2)
{
return array1.Length == array2.Length && array1.All(array2.Contains) && array2.All(array1.Contains);
}
}
}
3 changes: 3 additions & 0 deletions Assets/Tests/EditMode/Tests/ComponentTilesetTilesTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0409b79

Please sign in to comment.