CTT/Server/Hotfix/Game/System/Map/MapSceneSystem.cs

219 lines
7.0 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
{
2021-04-18 15:54:51 +08:00
public class MapSceneAwakeSystem: AwakeSystem<MapScene>
2021-04-08 20:09:59 +08:00
{
public override void Awake(MapScene self)
{
self.Init();
self.UnitDic.Clear();
}
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
public static class MapSceneSystem
{
private const int Max_Tier_Count_Per_Map = 20;
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
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());
}
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
public static Tier GetTier(this MapScene self, int tierId)
{
2021-04-11 19:50:39 +08:00
self.tierDic.TryGetValue(tierId, out Tier tier);
2021-04-08 20:09:59 +08:00
return tier;
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
public static void AddUnit(this MapScene self, Unit unit)
{
2021-04-18 15:54:51 +08:00
try
{
MapUnitComponent.Instance.Add(unit);
self.UnitDic[unit.Id] = unit;
unit.Parent = self;
}
catch (Exception e)
{
Log.Error(e);
}
2021-04-08 20:09:59 +08:00
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
public static bool RemoveUnit(this MapScene self, Unit unit)
{
MapUnitComponent.Instance.Remove(unit.Id);
return self.UnitDic.Remove(unit.Id);
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
public static async ETTask Enter(this MapScene self, Unit unit)
{
if (unit.UnitType != UnitType.Player)
{
self.AddUnit(unit);
return;
}
2021-04-18 15:54:51 +08:00
Game.EventSystem.Publish(new ET.EventType.OnEnterMap { unit = unit }).Coroutine();
2021-04-08 20:09:59 +08:00
UnitScene unitScene = unit.GetComponent<UnitScene>();
int tierId = 0;
bool isEnter = false;
Tier tier;
do
{
tierId++;
tier = self.GetTier(tierId);
if (tier == null)
{
break;
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
if (tier.CanAdd()) isEnter = true;
2021-04-18 15:54:51 +08:00
}
while (!isEnter);
2021-04-08 20:09:59 +08:00
unitScene.TierId = tierId;
2021-04-18 15:54:51 +08:00
using var listComponent = ListComponent<Unit>.Create();
2021-04-11 19:50:39 +08:00
IEnumerable<Unit> unitList = self.GetTierAll(tierId);
2021-04-18 15:54:51 +08:00
listComponent.List.AddRange(unitList);
2021-04-08 20:09:59 +08:00
//广播
2021-04-18 15:54:51 +08:00
foreach (Unit u in listComponent.List)
2021-04-08 20:09:59 +08:00
{
try
{
if (!u)
continue;
2021-04-18 15:54:51 +08:00
M2C_EnterMap m2C_EnterMap = new M2C_EnterMap { UnitInfo = new UnitPosInfo(), };
2021-04-08 20:09:59 +08:00
//!加入到其他玩家的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);
}
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
//发送场景中已有的玩家
M2C_UnitsInMap unitsInMap = new M2C_UnitsInMap();
2021-04-18 15:54:51 +08:00
foreach (Unit u in listComponent.List)
2021-04-08 20:09:59 +08:00
{
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);
}
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
MessageHelper.SendActor(unit, unitsInMap);
// if (AppConfig.inst.isTest)
// Log.Debug($"【进入】{unit.ToCustomString()}进入地图[{self.mapId / 100}][{self.mapId % 100}]的{tierId}线,当前线玩家:{unitList.ToCustomString()}");
2021-04-08 20:09:59 +08:00
tier.Add(unit);
self.AddUnit(unit);
2021-04-18 15:54:51 +08:00
Game.EventSystem.Publish(new ET.EventType.AfterChangeMap { unit = unit, mapId = self.mapId }).Coroutine();
2021-04-08 20:09:59 +08:00
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
public static IEnumerable<Unit> GetAll(this MapScene self)
{
return self.UnitDic.Values;
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
public static IEnumerable<Unit> GetTierAll(this MapScene self, int tierId)
{
if (self.GetTier(tierId) is Tier tier)
{
return tier.GetAll();
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
return null;
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
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;
}
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
return false;
}
2021-04-18 15:54:51 +08:00
2021-04-08 20:09:59 +08:00
public static void Leave(this MapScene self, Unit unit, bool isOffLine = false)
{
if (unit.UnitType != UnitType.Player)
{
self.RemoveUnit(unit);
return;
}
2021-04-18 15:54:51 +08:00
if (!self.RemoveUnit(unit))
2021-04-08 20:09:59 +08:00
{
2021-04-18 15:54:51 +08:00
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()}");
2021-04-18 15:54:51 +08:00
//广播
using var listComponent = ListComponent<Unit>.Create();
listComponent.List.AddRange(unitList);
foreach (Unit u in listComponent.List)
2021-04-08 20:09:59 +08:00
{
2021-04-18 15:54:51 +08:00
try
2021-04-08 20:09:59 +08:00
{
if (!u)
continue;
2021-04-18 15:54:51 +08:00
//!离开其他玩家的aoi列表
2021-04-08 20:09:59 +08:00
u.GetComponent<BrocastComponent>()?.Remove(unit.Id);
2021-04-18 15:54:51 +08:00
//!其他玩家离开的aoi列表
2021-04-08 20:09:59 +08:00
unit.GetComponent<BrocastComponent>()?.Remove(u.Id);
MessageHelper.SendActor(u, new M2C_LeaveMap() { UnitId = unit.Id });
}
2021-04-18 15:54:51 +08:00
catch (Exception e)
{
Log.Error(e);
}
2021-04-08 20:09:59 +08:00
}
2021-04-18 15:54:51 +08:00
}
catch (Exception e)
{
Log.Error(e);
2021-04-08 20:09:59 +08:00
}
}
}
2021-04-18 15:54:51 +08:00
}