44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
using System.Collections.Generic;
|
|
namespace Gameplay.Unit.Data
|
|
{
|
|
public class UnitContainerWithTroop : UnitContainer
|
|
{
|
|
|
|
private Dictionary<int, List<GameUnit>> _troopDict = new Dictionary<int, List<GameUnit>>();
|
|
|
|
protected override void _OnAdd(GameUnit unit)
|
|
{
|
|
base._OnAdd(unit);
|
|
var troopId = unit.commonData.troopId;
|
|
if (_troopDict.TryGetValue(troopId, out List<GameUnit> list))
|
|
{
|
|
list.Add(unit);
|
|
}
|
|
else
|
|
{
|
|
list = new List<GameUnit>();
|
|
list.Add(unit);
|
|
_troopDict.Add(troopId, list);
|
|
}
|
|
}
|
|
|
|
protected override void _OnRemove(GameUnit unit)
|
|
{
|
|
base._OnRemove(unit);
|
|
var troopId = unit.commonData.troopId;
|
|
// 理论上来说必然存在, 出现报错了再说
|
|
_troopDict[troopId].Remove(unit);
|
|
}
|
|
|
|
public List<GameUnit> GetListByTroop(int troopId)
|
|
{
|
|
if (_troopDict.TryGetValue(troopId, out List<GameUnit> list))
|
|
{
|
|
return list;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|