level manager et une ferme
pas de moyen d'accéder et de gérer et d'accéder aux unités / props sur la map - ajout d'un LevelManager qui contient tous les objet de niveau (ILevelObject) - ajout d'un ResourceTile qui s'ajoute lui-même au level manager, et contient ses informations. ResourceTile pas terminée puisque le genre de resource est dépendant du ResourceManager, pas encore contenu dans main.
This commit is contained in:
parent
bc57e9595d
commit
d615b9abe4
3
Assets/Scripts/GameAssembly.asmdef
Normal file
3
Assets/Scripts/GameAssembly.asmdef
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"name": "GameAssembly"
|
||||||
|
}
|
||||||
7
Assets/Scripts/GameAssembly.asmdef.meta
Normal file
7
Assets/Scripts/GameAssembly.asmdef.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6ee199c72d6db6244ac382d3d4d61bfc
|
||||||
|
AssemblyDefinitionImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
3
Assets/Scripts/ILevelObject.cs
Normal file
3
Assets/Scripts/ILevelObject.cs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
public interface ILevelObject
|
||||||
|
{
|
||||||
|
}
|
||||||
11
Assets/Scripts/ILevelObject.cs.meta
Normal file
11
Assets/Scripts/ILevelObject.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e8c55ebc87e041b419aa13dffc17b91f
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
58
Assets/Scripts/LevelManager.cs
Normal file
58
Assets/Scripts/LevelManager.cs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
public class LevelManager : Singleton<LevelManager>
|
||||||
|
{
|
||||||
|
public event System.Action<ILevelObject> Added;
|
||||||
|
public event System.Action<ILevelObject> Removed;
|
||||||
|
|
||||||
|
private List<ILevelObject> levelObjects;
|
||||||
|
public LevelManager()
|
||||||
|
{
|
||||||
|
levelObjects = new List<ILevelObject>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(ILevelObject levelObject)
|
||||||
|
{
|
||||||
|
if (levelObjects.Contains(levelObject)) return;
|
||||||
|
levelObjects.Add(levelObject);
|
||||||
|
Added?.Invoke(levelObject);
|
||||||
|
}
|
||||||
|
public void Remove(ILevelObject levelObject)
|
||||||
|
{
|
||||||
|
if (!levelObjects.Contains(levelObject)) return;
|
||||||
|
levelObjects.Remove(levelObject);
|
||||||
|
Removed?.Invoke(levelObject);
|
||||||
|
}
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
levelObjects.RemoveAll(obj =>
|
||||||
|
{
|
||||||
|
Removed?.Invoke(obj);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
public T Get<T>(System.Func<T, bool> predicate = default) where T : ILevelObject
|
||||||
|
{
|
||||||
|
if (predicate == default) predicate = (t) => true;
|
||||||
|
return (T)levelObjects.Find(t => t is T t1 && predicate(t1));
|
||||||
|
}
|
||||||
|
public List<T> GetAll<T>(System.Func<T, bool> predicate = default) where T : ILevelObject
|
||||||
|
{
|
||||||
|
if (predicate == default) predicate = (t) => true;
|
||||||
|
List<T> ret = new List<T>();
|
||||||
|
foreach (var t in levelObjects) if (t is T t1 && predicate(t1)) ret.Add(t1);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Count<T>(Func<T, bool> predicate = default) where T : ILevelObject
|
||||||
|
{
|
||||||
|
return GetAll(predicate).Count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Has<T>(System.Func<T, bool> predicate = default)
|
||||||
|
{
|
||||||
|
if (predicate == default) predicate = (t) => true;
|
||||||
|
return levelObjects.Exists(t => t is T && predicate((T)t));
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Scripts/LevelManager.cs.meta
Normal file
11
Assets/Scripts/LevelManager.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1af43055ac165604e992f071c5520910
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
42
Assets/Scripts/ResourceTile.cs
Normal file
42
Assets/Scripts/ResourceTile.cs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.Tilemaps;
|
||||||
|
|
||||||
|
|
||||||
|
[CreateAssetMenu(menuName = "Gather And Defend/Resource Tile")]
|
||||||
|
public class ResourceTile : TileBase
|
||||||
|
{
|
||||||
|
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]
|
||||||
|
private Sprite _sprite;
|
||||||
|
public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
|
||||||
|
{
|
||||||
|
if (!Application.isPlaying) return base.StartUp(position, tilemap, go);
|
||||||
|
if (position == new Vector3Int(-5, -5))
|
||||||
|
{
|
||||||
|
Debug.Log("Yo");
|
||||||
|
}
|
||||||
|
LevelManager.Instance.Add(new ResourceTileData((Vector3)position, name));
|
||||||
|
return base.StartUp(position, tilemap, go);
|
||||||
|
}
|
||||||
|
public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
|
||||||
|
{
|
||||||
|
tileData.sprite = _sprite;
|
||||||
|
tileData.transform.SetTRS(Vector3.zero, Quaternion.identity, Vector3.one);
|
||||||
|
tileData.color = Color.white;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Scripts/ResourceTile.cs.meta
Normal file
11
Assets/Scripts/ResourceTile.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 84b05f2a26f63da46a1028488482d079
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
12
Assets/Scripts/Singleton.cs
Normal file
12
Assets/Scripts/Singleton.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
public class Singleton<T> where T : Singleton<T>, new()
|
||||||
|
{
|
||||||
|
private static T _instance;
|
||||||
|
public static T Instance
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_instance == null) _instance = new T();
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Scripts/Singleton.cs.meta
Normal file
11
Assets/Scripts/Singleton.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7f3fd6219c44e484db1f5c351626bf3e
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
8
Assets/Tests.meta
Normal file
8
Assets/Tests.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: fc8abee0ea7bd1a47b2fcce297d01648
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
8
Assets/Tests/Playmode.meta
Normal file
8
Assets/Tests/Playmode.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 48410eca65be77149bf834b1df6f55f5
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
22
Assets/Tests/Playmode/Playmode.asmdef
Normal file
22
Assets/Tests/Playmode/Playmode.asmdef
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "Tests",
|
||||||
|
"rootNamespace": "",
|
||||||
|
"references": [
|
||||||
|
"UnityEngine.TestRunner",
|
||||||
|
"UnityEditor.TestRunner",
|
||||||
|
"GameAssembly"
|
||||||
|
],
|
||||||
|
"includePlatforms": [],
|
||||||
|
"excludePlatforms": [],
|
||||||
|
"allowUnsafeCode": false,
|
||||||
|
"overrideReferences": true,
|
||||||
|
"precompiledReferences": [
|
||||||
|
"nunit.framework.dll"
|
||||||
|
],
|
||||||
|
"autoReferenced": false,
|
||||||
|
"defineConstraints": [
|
||||||
|
"UNITY_INCLUDE_TESTS"
|
||||||
|
],
|
||||||
|
"versionDefines": [],
|
||||||
|
"noEngineReferences": false
|
||||||
|
}
|
||||||
7
Assets/Tests/Playmode/Playmode.asmdef.meta
Normal file
7
Assets/Tests/Playmode/Playmode.asmdef.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 35cea9d83a6dea3488659da5d3f262a9
|
||||||
|
AssemblyDefinitionImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
54
Assets/Tests/Playmode/TestLevelManager.cs
Normal file
54
Assets/Tests/Playmode/TestLevelManager.cs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.TestTools;
|
||||||
|
using UnityEngine.Tilemaps;
|
||||||
|
|
||||||
|
public class TestLevelManager
|
||||||
|
{
|
||||||
|
private ResourceTile farm;
|
||||||
|
private Tilemap tilemap;
|
||||||
|
const int size = 25;
|
||||||
|
const int sqrt_size = 5;
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
farm = ScriptableObject.CreateInstance<ResourceTile>();
|
||||||
|
farm.name = nameof(farm);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
Object.Destroy(tilemap.gameObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use
|
||||||
|
// `yield return null;` to skip a frame.
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator TestLevelManagerWithEnumeratorPasses()
|
||||||
|
{
|
||||||
|
yield return null;
|
||||||
|
Assert.AreEqual(LevelManager.Instance.Count<ResourceTile.ResourceTileData>(), size, "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));
|
||||||
|
Assert.True(tileExists, "there should be a tile at position " + pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
var newPos = new Vector3Int(-5, -5);
|
||||||
|
tilemap.SetTile(newPos, farm);
|
||||||
|
yield return null;
|
||||||
|
|
||||||
|
var newTileExists = LevelManager.Instance.Has<ResourceTile.ResourceTileData>(t => Mathf.Approximately(Vector3.Distance(t.Position, newPos), 0));
|
||||||
|
Assert.True(newTileExists, "new tile wasn't added to level manager");
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Tests/Playmode/TestLevelManager.cs.meta
Normal file
11
Assets/Tests/Playmode/TestLevelManager.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: c8b5cb265fff43142b83e2584ba2620e
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Loading…
x
Reference in New Issue
Block a user