58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
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));
|
|
}
|
|
} |