zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Server/Hotfix/Game/System/Map/MapSceneSystem.cs

193 lines
6.5 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using ET.EventType;
using System;
using System.Collections.Generic;
namespace ET
{
public class MapSceneAwakeSystem : AwakeSystem<MapScene>
{
public override void Awake(MapScene self)
{
self.Init();
self.UnitDic.Clear();
}
}
public static class MapSceneSystem
{
private const int Max_Tier_Count_Per_Map = 20;
public static void Init(this MapScene self)
{
self.tierDic.Clear();
for (int i = 1; i <= Max_Tier_Count_Per_Map; i++)
{
//!Tier 没有其他逻辑
self.tierDic.Add(i, new Tier());
}
}
public static Tier GetTier(this MapScene self, int tierId)
{
self.tierDic.TryGetValue(tierId, out var tier);
return tier;
}
public static void AddUnit(this MapScene self, Unit unit)
{
MapUnitComponent.Instance.Add(unit);
self.UnitDic[unit.Id] = unit;
unit.Parent = self;
}
public static bool RemoveUnit(this MapScene self, Unit unit)
{
MapUnitComponent.Instance.Remove(unit.Id);
return self.UnitDic.Remove(unit.Id);
}
public static async ETTask Enter(this MapScene self, Unit unit)
{
if (unit.UnitType != UnitType.Player)
{
self.AddUnit(unit);
return;
}
Game.EventSystem.Publish(new ET.EventType.OnEnterMap
{
unit = unit
}).Coroutine();
UnitScene unitScene = unit.GetComponent<UnitScene>();
int tierId = 0;
bool isEnter = false;
Tier tier;
do
{
tierId++;
tier = self.GetTier(tierId);
if (tier == null)
{
break;
}
if (tier.CanAdd()) isEnter = true;
} while (!isEnter);
unitScene.TierId = tierId;
var unitList = self.GetTierAll(tierId);
//广播
foreach (var u in unitList)
{
try
{
if (!u)
continue;
M2C_EnterMap m2C_EnterMap = new M2C_EnterMap
{
UnitInfo = new UnitPosInfo(),
};
//!加入到其他玩家的aoi列表
u.GetComponent<BrocastComponent>()?.Add(unit);
//!其他玩家加入到刚进入的aoi列表
unit.GetComponent<BrocastComponent>()?.Add(u);
UnitHelper.SetUnitInfo(unit, m2C_EnterMap.UnitInfo);
m2C_EnterMap.PvpUnitCharacter = await CharacterHelper.GetUnitCharacter(unit);
MessageHelper.SendActor(u, m2C_EnterMap);
}
catch (Exception e)
{
Log.Error(e);
}
}
//发送场景中已有的玩家
M2C_UnitsInMap unitsInMap = new M2C_UnitsInMap();
foreach (var u in unitList)
{
try
{
if (!u)
continue;
UnitPosInfo unitInfo = new UnitPosInfo();
UnitHelper.SetUnitInfo(u, unitInfo);
unitsInMap.Units.Add(unitInfo);
unitsInMap.PvpUnits.Add(await CharacterHelper.GetUnitCharacter(u));
}
catch (Exception e)
{
Log.Error(e);
}
}
MessageHelper.SendActor(unit, unitsInMap);
if (AppConfig.inst.isTest)
Log.Debug($"【进入】{unit.ToCustomString()}进入地图[{self.mapId / 100}][{self.mapId % 100}]的{tierId}线,当前线玩家:{unitList.ToCustomString()}");
tier.Add(unit);
self.AddUnit(unit);
Game.EventSystem.Publish(new ET.EventType.AfterChangeMap
{
unit = unit,
mapId = self.mapId
}).Coroutine();
}
public static IEnumerable<Unit> GetAll(this MapScene self)
{
return self.UnitDic.Values;
}
public static IEnumerable<Unit> GetTierAll(this MapScene self, int tierId)
{
if (self.GetTier(tierId) is Tier tier)
{
return tier.GetAll();
}
return null;
}
public static bool ChangeTier(this MapScene self, Unit unit, int tierId)
{
UnitScene unitScene = unit.GetComponent<UnitScene>();
if (self.GetTier(tierId) is Tier tier)
{
if (tier.CanAdd())
{
tier.Add(unit);
self.GetTier(unitScene.TierId).Remove(unit.Id);
return true;
}
}
return false;
}
public static void Leave(this MapScene self, Unit unit, bool isOffLine = false)
{
if (unit.UnitType != UnitType.Player)
{
self.RemoveUnit(unit);
return;
}
if (self.RemoveUnit(unit))
{
try
{
UnitScene unitScene = unit.GetComponent<UnitScene>();
int tierId = unitScene.TierId;
self.GetTier(tierId).Remove(unit.Id);
var unitList = self.GetTierAll(tierId);
if (AppConfig.inst.isTest)
Log.Debug($"【离开】{unit.ToCustomString()}离开地图[{self.mapId / 100}][{self.mapId % 100}]的{tierId}线,剩余线玩家:{unitList.ToCustomString()}");
//广播
foreach (var u in unitList)
{
if (!u)
continue;
//!加入到其他玩家的aoi列表
u.GetComponent<BrocastComponent>()?.Remove(unit.Id);
//!其他玩家加入到刚进入的aoi列表
unit.GetComponent<BrocastComponent>()?.Remove(u.Id);
MessageHelper.SendActor(u, new M2C_LeaveMap() { UnitId = unit.Id });
}
}
catch (Exception e)
{
Log.Error(e);
}
}
}
}
}