2023-08-22 19:08:45 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using Framework;
|
|
|
|
|
|
namespace Gameplay.Common
|
|
|
|
|
|
{
|
|
|
|
|
|
public class GameUnitMapper
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
private static GameUnitMapper _instance;
|
|
|
|
|
|
|
|
|
|
|
|
public static GameUnitMapper Inst
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return _instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static GameUnitMapper CreateInstance()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_instance != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("请勿重复创建: GameUnitMapper");
|
|
|
|
|
|
return _instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
_instance = new GameUnitMapper();
|
|
|
|
|
|
return _instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
|
private readonly Dictionary<uint, GameUnit> _gameUnitDict;
|
|
|
|
|
|
private readonly List<GameUnit> _unitList;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
public List<GameUnit> unitList
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return _unitList;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private GameUnitMapper()
|
|
|
|
|
|
{
|
|
|
|
|
|
_unitList = new List<GameUnit>();
|
|
|
|
|
|
_gameUnitDict = new Dictionary<uint, GameUnit>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public GameUnit GetUnitById(uint uId)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_gameUnitDict.TryGetValue(uId, out var unit))
|
|
|
|
|
|
{
|
|
|
|
|
|
return unit;
|
|
|
|
|
|
}
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.Log("不存在的UnitId: {0}", uId);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void RemoveUnitById(uint uId)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_gameUnitDict.TryGetValue(uId, out var unit))
|
|
|
|
|
|
{
|
|
|
|
|
|
_gameUnitDict.Remove(uId);
|
|
|
|
|
|
_unitList.Remove(unit);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.Log("不存在的UnitId: {0}", uId);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
|
{
|
|
|
|
|
|
EventManager.Instance.Register(EventManager.EventName.UnitCreate, (Action<GameUnit>)_AddUnit);
|
|
|
|
|
|
EventManager.Instance.Register(EventManager.EventName.UnitDispose, (Action<uint>)_RemoveUnit);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
EventManager.Instance.Unregister(EventManager.EventName.UnitCreate, (Action<GameUnit>)_AddUnit);
|
|
|
|
|
|
EventManager.Instance.Unregister(EventManager.EventName.UnitDispose, (Action<uint>)_RemoveUnit);
|
|
|
|
|
|
|
|
|
|
|
|
_gameUnitDict.Clear();
|
|
|
|
|
|
_unitList.Clear();
|
|
|
|
|
|
_instance = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void _AddUnit(GameUnit unit)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_gameUnitDict.ContainsKey(unit.GetID()))
|
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.LogError("重复的UnitId: {0}",unit.GetID());
|
2023-08-22 19:08:45 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
_unitList.Add(unit);
|
|
|
|
|
|
_gameUnitDict.Add(unit.GetID(), unit);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void _RemoveUnit(uint id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_gameUnitDict.ContainsKey(id))
|
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.LogError("不存在的UnitId: {0}", id);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
_unitList.Remove(_gameUnitDict[id]);
|
|
|
|
|
|
_gameUnitDict.Remove(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|