ajouter la tile directement au LevelManager
au lieu d'ajouter une classe data intermédiaire. l'idée est que ça rend les modifications complexes d'avoir un intermédiaire. Autant ajouter la tuile direct.
This commit is contained in:
parent
c7132af42f
commit
dec5aeb857
@ -5,28 +5,24 @@ using UnityEngine.Tilemaps;
|
||||
|
||||
|
||||
[CreateAssetMenu(menuName = "Gather And Defend/Resource Tile")]
|
||||
public class ResourceTile : TileBase
|
||||
public class ResourceTile : TileBase, ILevelObject
|
||||
{
|
||||
public class ResourceTileData : ILevelObject
|
||||
{
|
||||
private Vector3 _position;
|
||||
private string _resourceType;
|
||||
|
||||
public Vector3 Position => _position;
|
||||
public string ResourceType => _resourceType;
|
||||
|
||||
public ResourceTileData(Vector2 position, string resourceType)
|
||||
{
|
||||
this._position = position;
|
||||
this._resourceType = resourceType;
|
||||
}
|
||||
}
|
||||
[SerializeField]
|
||||
[Tooltip("the prefab of the currency that will be spawned when mining this resource")]
|
||||
private GameObject _yieldPrefab;
|
||||
[SerializeField]
|
||||
private Sprite _sprite;
|
||||
public Vector3 Position { get; private set; }
|
||||
|
||||
public Sprite Sprite { get => _sprite; set => _sprite = value; }
|
||||
public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
|
||||
{
|
||||
if (!Application.isPlaying) return base.StartUp(position, tilemap, go);
|
||||
LevelManager.Instance.Add(new ResourceTileData((Vector3)position, name));
|
||||
|
||||
//need to create an instance of the tile, otherwise the position will change for all tiles instead of only this one.
|
||||
var instance = Instantiate(this);
|
||||
instance.Position = position;
|
||||
LevelManager.Instance.Add(instance);
|
||||
return base.StartUp(position, tilemap, go);
|
||||
}
|
||||
public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
|
||||
|
||||
@ -7,27 +7,29 @@ using UnityEngine.Tilemaps;
|
||||
|
||||
public class TestLevelManager
|
||||
{
|
||||
private ResourceTile farm;
|
||||
private Tilemap tilemap;
|
||||
private ResourceTile _farm;
|
||||
private Tilemap _tilemap;
|
||||
const int size = 25;
|
||||
const int sqrt_size = 5;
|
||||
|
||||
private YieldInstruction Timing => new WaitForSeconds(0.1f);
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
farm = ScriptableObject.CreateInstance<ResourceTile>();
|
||||
farm.name = nameof(farm);
|
||||
_farm = ScriptableObject.CreateInstance<ResourceTile>();
|
||||
_farm.name = nameof(_farm);
|
||||
|
||||
tilemap = new GameObject("Tilemap").AddComponent<Tilemap>();
|
||||
_tilemap = new GameObject("Tilemap").AddComponent<Tilemap>();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
var pos = new Vector3Int(i % sqrt_size, i / sqrt_size);
|
||||
tilemap.SetTile(pos, farm);
|
||||
_tilemap.SetTile(pos, _farm);
|
||||
}
|
||||
}
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
Object.Destroy(tilemap.gameObject);
|
||||
Object.Destroy(_tilemap.gameObject);
|
||||
}
|
||||
|
||||
// A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use
|
||||
@ -35,20 +37,20 @@ public class TestLevelManager
|
||||
[UnityTest]
|
||||
public IEnumerator TestLevelManagerWithEnumeratorPasses()
|
||||
{
|
||||
yield return null;
|
||||
Assert.AreEqual(LevelManager.Instance.Count<ResourceTile.ResourceTileData>(), size, "there should be " + size + " tiles");
|
||||
yield return Timing;
|
||||
Assert.AreEqual(size, LevelManager.Instance.Count<ResourceTile>(), "there should be " + size + " tiles");
|
||||
for (int i = 0; i < 25; i++)
|
||||
{
|
||||
var pos = new Vector3(i % sqrt_size, i / sqrt_size);
|
||||
var tileExists = LevelManager.Instance.Has<ResourceTile.ResourceTileData>(t => Mathf.Approximately(Vector2.Distance(pos, t.Position), 0));
|
||||
var tileExists = LevelManager.Instance.Has<ResourceTile>(t => Mathf.Approximately(Vector2.Distance(pos, t.Position), 0));
|
||||
Assert.True(tileExists, "there should be a tile at position " + pos);
|
||||
}
|
||||
|
||||
var newPos = new Vector3Int(-5, -5);
|
||||
tilemap.SetTile(newPos, farm);
|
||||
yield return null;
|
||||
_tilemap.SetTile(newPos, _farm);
|
||||
yield return Timing;
|
||||
|
||||
var newTileExists = LevelManager.Instance.Has<ResourceTile.ResourceTileData>(t => Mathf.Approximately(Vector3.Distance(t.Position, newPos), 0));
|
||||
var newTileExists = LevelManager.Instance.Has<ResourceTile>(t => Mathf.Approximately(Vector3.Distance(t.Position, newPos), 0));
|
||||
Assert.True(newTileExists, "new tile wasn't added to level manager");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user