problem : tiles dont have an update loop, neither do they have a start / destroy method solution : finished working on a way to create a custom start/update/destroy pipeline for the projects custom tiles. it's using LevelManagerScript as to Update. note : also created a database and a start of serialization system so we can save and load stuff.
117 lines
3.5 KiB
C#
117 lines
3.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Unity.Plastic.Newtonsoft.Json;
|
|
using UnityEngine;
|
|
|
|
public class LevelManager : Singleton<LevelManager>
|
|
{
|
|
public delegate void LevelAction(ILevelObject levelObject);
|
|
public delegate bool LevelPredicate<T>(T levelObject) where T : ILevelObject;
|
|
public event LevelAction Added;
|
|
public event LevelAction Removed;
|
|
|
|
private readonly List<ILevelObject> toAdd;
|
|
private readonly List<ILevelObject> toRemove;
|
|
private readonly List<ILevelObject> levelObjects;
|
|
|
|
public Transform LevelTransform
|
|
{
|
|
get;private set;
|
|
}
|
|
public LevelManager()
|
|
{
|
|
toAdd = new List<ILevelObject>();
|
|
toRemove = new List<ILevelObject>();
|
|
levelObjects = new List<ILevelObject>();
|
|
|
|
var mgrScript = Object.FindObjectOfType<LevelManagerScript>();
|
|
if (!mgrScript) mgrScript = new GameObject(nameof(LevelManager)).AddComponent<LevelManagerScript>();
|
|
LevelTransform = mgrScript.transform;
|
|
}
|
|
|
|
public void Add(ILevelObject levelObject)
|
|
{
|
|
toAdd.Add(levelObject);
|
|
}
|
|
public void Remove(ILevelObject levelObject)
|
|
{
|
|
toRemove.Add(levelObject);
|
|
}
|
|
public void Clear()
|
|
{
|
|
toAdd.Clear();
|
|
toRemove.Clear();
|
|
levelObjects.Clear();
|
|
}
|
|
public T Get<T>(LevelPredicate<T> predicate = null) where T : ILevelObject
|
|
{
|
|
if (predicate == null) predicate = (generic) => true;
|
|
return (T)levelObjects.Find(levelObject => levelObject is T generic && predicate(generic));
|
|
}
|
|
public List<T> GetAll<T>(LevelPredicate<T> predicate = null) where T : ILevelObject
|
|
{
|
|
if (predicate == null) predicate = (generic) => true;
|
|
List<T> ret = new();
|
|
foreach (var levelObject in levelObjects) if (levelObject is T generic && predicate(generic)) ret.Add(generic);
|
|
return ret;
|
|
}
|
|
|
|
public int Count<T>(LevelPredicate<T> predicate = null) where T : ILevelObject
|
|
{
|
|
return GetAll(predicate).Count;
|
|
}
|
|
|
|
public bool Has<T>(LevelPredicate<T> predicate = null) where T : ILevelObject
|
|
{
|
|
if (predicate == null) predicate = (generic) => true;
|
|
return levelObjects.Exists(levelObject => levelObject is T generic && predicate(generic));
|
|
}
|
|
|
|
public void UpdateLevel()
|
|
{
|
|
levelObjects.ForEach(levelObject => levelObject.LevelUpdate());
|
|
|
|
var toAdd = new List<ILevelObject>(this.toAdd);
|
|
toAdd.ForEach(addedObject =>
|
|
{
|
|
this.toAdd.Remove(addedObject);
|
|
levelObjects.Add(addedObject);
|
|
Added?.Invoke(addedObject);
|
|
addedObject.LevelStart();
|
|
});
|
|
|
|
var toRemove = new List<ILevelObject>(this.toRemove);
|
|
toRemove.ForEach(removedObject =>
|
|
{
|
|
this.toRemove.Remove(removedObject);
|
|
levelObjects.Remove(removedObject);
|
|
Removed?.Invoke(removedObject);
|
|
removedObject.LevelDestroy();
|
|
});
|
|
toRemove.Clear();
|
|
}
|
|
void SaveLevel()
|
|
{
|
|
var list = levelObjects.Select(obj => obj.ToDictionary()).ToList();
|
|
Debug.Log(JsonConvert.SerializeObject(list));
|
|
}
|
|
void LoadLevel()
|
|
{
|
|
|
|
}
|
|
}
|
|
public class LevelManagerScript : MonoBehaviour
|
|
{
|
|
private static LevelManagerScript _instance;
|
|
void Awake()
|
|
{
|
|
if (!_instance) _instance = this;
|
|
else Destroy(gameObject);
|
|
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
void Update()
|
|
{
|
|
LevelManager.Instance.UpdateLevel();
|
|
}
|
|
} |