46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class UnitManager : MonoBehaviour {
|
|
public static UnitManager Instance { get; private set; }
|
|
public List<Unit> UnitList { get; private set; } = new();
|
|
public List<Unit> FriendlyUnitList{ get; private set; } = new();
|
|
public List<Unit> EnemyUnitList { get; private set; } = new();
|
|
|
|
private void Awake() {
|
|
if (Instance is not null) {
|
|
Debug.LogError($"There is more than one UnitManager! {transform} - {Instance}");
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
Instance = this;
|
|
}
|
|
private void Start() {
|
|
Unit.OnAnyUnitSpawned += Unit_OnAnyUnitSpawned;
|
|
Unit.OnAnyUnitDead += Unit_OnAnyUnitDead;
|
|
}
|
|
|
|
private void Unit_OnAnyUnitSpawned(object sender, EventArgs e) {
|
|
if (sender is not Unit unit) return;
|
|
UnitList.Add(unit);
|
|
if (unit.IsEnemy) {
|
|
EnemyUnitList.Add(unit);
|
|
}
|
|
else {
|
|
FriendlyUnitList.Add(unit);
|
|
}
|
|
}
|
|
|
|
private void Unit_OnAnyUnitDead(object sender, EventArgs e) {
|
|
if (sender is not Unit unit) return;
|
|
UnitList.Remove(unit);
|
|
if (unit.IsEnemy) {
|
|
EnemyUnitList.Remove(unit);
|
|
}
|
|
else {
|
|
FriendlyUnitList.Remove(unit);
|
|
}
|
|
}
|
|
}
|