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

249 lines
8.2 KiB
C#
Executable File

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 Tier tier);
return tier;
}
public static void AddUnit(this MapScene self, Unit unit)
{
try
{
MapUnitComponent.Instance.Add(unit);
self.UnitDic[unit.Id] = unit;
unit.Parent = self;
}
catch (Exception e)
{
Log.Error(e);
}
}
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, bool isReEnter = false)
{
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;
Tier tier = null;
if (!isReEnter)
{
bool isEnter = false;
do
{
tierId++;
tier = self.GetTier(tierId);
if (tier == null)
{
break;
}
if (tier.CanAdd()) isEnter = true;
}
while (!isEnter);
unitScene.TierId = tierId;
}
using (var listComponent = ListComponent<Unit>.Create())
{
IEnumerable<Unit> unitList = self.GetTierAll(tierId);
if (unitList != null)
listComponent.List.AddRange(unitList);
//广播
foreach (Unit u in listComponent.List)
{
try
{
if (!u)
continue;
BrocastMessage(unit, u).Coroutine();
}
catch (Exception e)
{
Log.Error(e);
}
}
static async ETVoid BrocastMessage(Unit enterUnit, Unit targetUnit)
{
M2C_EnterMap m2C_EnterMap = new M2C_EnterMap { UnitInfo = new UnitPosInfo(), };
//!加入到其他玩家的aoi列表
targetUnit.GetComponent<BrocastComponent>()?.Add(enterUnit);
//!其他玩家加入到刚进入的aoi列表
enterUnit.GetComponent<BrocastComponent>()?.Add(targetUnit);
UnitHelper.SetUnitInfo(enterUnit, m2C_EnterMap.UnitInfo);
m2C_EnterMap.PvpUnitCharacter = await CharacterHelper.GetUnitCharacter(enterUnit);
MessageHelper.SendActor(targetUnit, m2C_EnterMap);
}
//发送场景中已有的玩家
M2C_UnitsInMap unitsInMap = new M2C_UnitsInMap();
using (var taskList = ListComponent<ETTask>.Create())
{
foreach (Unit u in listComponent.List)
{
try
{
if (!u)
continue;
taskList.List.Add(GetUnitInfo(u, unitsInMap));
}
catch (Exception e)
{
Log.Error(e);
}
}
static async ETTask GetUnitInfo(Unit u, M2C_UnitsInMap unitsInMap)
{
UnitPosInfo unitInfo = new UnitPosInfo();
UnitHelper.SetUnitInfo(u, unitInfo);
unitsInMap.Units.Add(unitInfo);
unitsInMap.PvpUnits.Add(await CharacterHelper.GetUnitCharacter(u));
}
await ETTaskHelper.WaitAll(taskList.List);
MessageHelper.SendActor(unit, unitsInMap);
}
if (!isReEnter)
BattleHelper.UpdateChangeMapTaskStateEvent(unitScene, listComponent.List);
}
if (!isReEnter)
{
if (tier != null)
tier.Add(unit);
self.AddUnit(unit);
}
// if (AppConfig.inst.isTest)
// Log.Debug($"【进入】{unit.ToCustomString()}进入地图[{self.mapId / 100}][{self.mapId % 100}]的{tierId}线,当前线玩家:{unitList.ToCustomString()}");
//!触发通关任务事件
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)
{
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)
{
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))
{
return;
}
try
{
UnitScene unitScene = unit.GetComponent<UnitScene>();
int tierId = unitScene.TierId;
self.GetTier(tierId).Remove(unit.Id);
IEnumerable<Unit> unitList = self.GetTierAll(tierId);
// if (AppConfig.inst.isTest)
// Log.Debug($"【离开】{unit.ToCustomString()}离开地图[{self.mapId / 100}][{self.mapId % 100}]的{tierId}线,剩余线玩家:{unitList.ToCustomString()}");
//广播
using var listComponent = ListComponent<Unit>.Create();
listComponent.List.AddRange(unitList);
foreach (Unit u in listComponent.List)
{
try
{
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);
}
}
}
catch (Exception e)
{
Log.Error(e);
}
}
}
}