[+] 接入ET8服务端

[+] 接入ET8服务端
This commit is contained in:
ALEXTANG
2023-07-13 12:23:48 +08:00
parent e0be062006
commit 336d4b2eb9
1316 changed files with 130657 additions and 626 deletions

View File

@@ -0,0 +1,179 @@
using System.Collections.Generic;
using Unity.Mathematics;
namespace ET.Server
{
[EntitySystemOf(typeof(AOIEntity))]
[FriendOf(typeof(AOIEntity))]
public static partial class AOIEntitySystem2
{
[EntitySystem]
private static void Awake(this AOIEntity self, int distance, float3 pos)
{
self.ViewDistance = distance;
self.Scene().GetComponent<AOIManagerComponent>().Add(self, pos.x, pos.z);
}
[EntitySystem]
private static void Destroy(this AOIEntity self)
{
self.Scene().GetComponent<AOIManagerComponent>()?.Remove(self);
self.ViewDistance = 0;
self.SeeUnits.Clear();
self.SeePlayers.Clear();
self.BeSeePlayers.Clear();
self.BeSeeUnits.Clear();
self.SubEnterCells.Clear();
self.SubLeaveCells.Clear();
}
}
[FriendOf(typeof(AOIEntity))]
[FriendOf(typeof(Cell))]
public static partial class AOIEntitySystem
{
// 获取在自己视野中的对象
public static Dictionary<long, AOIEntity> GetSeeUnits(this AOIEntity self)
{
return self.SeeUnits;
}
public static Dictionary<long, AOIEntity> GetBeSeePlayers(this AOIEntity self)
{
return self.BeSeePlayers;
}
public static Dictionary<long, AOIEntity> GetSeePlayers(this AOIEntity self)
{
return self.SeePlayers;
}
// cell中的unit进入self的视野
public static void SubEnter(this AOIEntity self, Cell cell)
{
cell.SubsEnterEntities.Add(self.Id, self);
foreach (KeyValuePair<long, AOIEntity> kv in cell.AOIUnits)
{
if (kv.Key == self.Id)
{
continue;
}
self.EnterSight(kv.Value);
}
}
public static void UnSubEnter(this AOIEntity self, Cell cell)
{
cell.SubsEnterEntities.Remove(self.Id);
}
public static void SubLeave(this AOIEntity self, Cell cell)
{
cell.SubsLeaveEntities.Add(self.Id, self);
}
// cell中的unit离开self的视野
public static void UnSubLeave(this AOIEntity self, Cell cell)
{
foreach (KeyValuePair<long, AOIEntity> kv in cell.AOIUnits)
{
if (kv.Key == self.Id)
{
continue;
}
self.LeaveSight(kv.Value);
}
cell.SubsLeaveEntities.Remove(self.Id);
}
// enter进入self视野
public static void EnterSight(this AOIEntity self, AOIEntity enter)
{
// 有可能之前在Enter后来出了Enter还在LeaveCell这样仍然没有删除继续进来Enter这种情况不需要处理
if (self.SeeUnits.ContainsKey(enter.Id))
{
return;
}
if (!AOISeeCheckHelper.IsCanSee(self, enter))
{
return;
}
if (self.Unit.Type == UnitType.Player)
{
if (enter.Unit.Type == UnitType.Player)
{
self.SeeUnits.Add(enter.Id, enter);
enter.BeSeeUnits.Add(self.Id, self);
self.SeePlayers.Add(enter.Id, enter);
enter.BeSeePlayers.Add(self.Id, self);
}
else
{
self.SeeUnits.Add(enter.Id, enter);
enter.BeSeeUnits.Add(self.Id, self);
enter.BeSeePlayers.Add(self.Id, self);
}
}
else
{
if (enter.Unit.Type == UnitType.Player)
{
self.SeeUnits.Add(enter.Id, enter);
enter.BeSeeUnits.Add(self.Id, self);
self.SeePlayers.Add(enter.Id, enter);
}
else
{
self.SeeUnits.Add(enter.Id, enter);
enter.BeSeeUnits.Add(self.Id, self);
}
}
EventSystem.Instance.Publish(self.Scene(), new EventType.UnitEnterSightRange() { A = self, B = enter });
}
// leave离开self视野
public static void LeaveSight(this AOIEntity self, AOIEntity leave)
{
if (self.Id == leave.Id)
{
return;
}
if (!self.SeeUnits.ContainsKey(leave.Id))
{
return;
}
self.SeeUnits.Remove(leave.Id);
if (leave.Unit.Type == UnitType.Player)
{
self.SeePlayers.Remove(leave.Id);
}
leave.BeSeeUnits.Remove(self.Id);
if (self.Unit.Type == UnitType.Player)
{
leave.BeSeePlayers.Remove(self.Id);
}
EventSystem.Instance.Publish(self.Scene(), new EventType.UnitLeaveSightRange { A = self, B = leave });
}
/// <summary>
/// 是否在Unit视野范围内
/// </summary>
/// <param name="self"></param>
/// <param name="unitId"></param>
/// <returns></returns>
public static bool IsBeSee(this AOIEntity self, long unitId)
{
return self.BeSeePlayers.ContainsKey(unitId);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 652ae649b0d965d44b15b66a2b2a15b2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,41 @@
using System.Collections.Generic;
namespace ET.Server
{
[FriendOf(typeof(AOIEntity))]
public static partial class AOIHelper
{
public static long CreateCellId(int x, int y)
{
return (long) ((ulong) x << 32) | (uint) y;
}
public static void CalcEnterAndLeaveCell(AOIEntity aoiEntity, int cellX, int cellY, HashSet<long> enterCell, HashSet<long> leaveCell)
{
enterCell.Clear();
leaveCell.Clear();
int r = (aoiEntity.ViewDistance - 1) / AOIManagerComponent.CellSize + 1;
int leaveR = r;
if (aoiEntity.Unit.Type == UnitType.Player)
{
leaveR += 1;
}
for (int i = cellX - leaveR; i <= cellX + leaveR; ++i)
{
for (int j = cellY - leaveR; j <= cellY + leaveR; ++j)
{
long cellId = CreateCellId(i, j);
leaveCell.Add(cellId);
if (i > cellX + r || i < cellX - r || j > cellY + r || j < cellY - r)
{
continue;
}
enterCell.Add(cellId);
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 37db3276e119c254295c2b6e57aeee57
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,187 @@
using System.Collections.Generic;
namespace ET.Server
{
[FriendOf(typeof(AOIManagerComponent))]
[FriendOf(typeof(AOIEntity))]
[FriendOf(typeof(Cell))]
public static partial class AOIManagerComponentSystem
{
public static void Add(this AOIManagerComponent self, AOIEntity aoiEntity, float x, float y)
{
int cellX = (int)(x * 1000) / AOIManagerComponent.CellSize;
int cellY = (int)(y * 1000) / AOIManagerComponent.CellSize;
if (aoiEntity.ViewDistance == 0)
{
aoiEntity.ViewDistance = 1;
}
AOIHelper.CalcEnterAndLeaveCell(aoiEntity, cellX, cellY, aoiEntity.SubEnterCells, aoiEntity.SubLeaveCells);
// 遍历EnterCell
foreach (long cellId in aoiEntity.SubEnterCells)
{
Cell cell = self.GetCell(cellId);
aoiEntity.SubEnter(cell);
}
// 遍历LeaveCell
foreach (long cellId in aoiEntity.SubLeaveCells)
{
Cell cell = self.GetCell(cellId);
aoiEntity.SubLeave(cell);
}
// 自己加入的Cell
Cell selfCell = self.GetCell(AOIHelper.CreateCellId(cellX, cellY));
aoiEntity.Cell = selfCell;
selfCell.Add(aoiEntity);
// 通知订阅该Cell Enter的Unit
foreach (KeyValuePair<long, AOIEntity> kv in selfCell.SubsEnterEntities)
{
kv.Value.EnterSight(aoiEntity);
}
}
public static void Remove(this AOIManagerComponent self, AOIEntity aoiEntity)
{
if (aoiEntity.Cell == null)
{
return;
}
// 通知订阅该Cell Leave的Unit
aoiEntity.Cell.Remove(aoiEntity);
foreach (KeyValuePair<long, AOIEntity> kv in aoiEntity.Cell.SubsLeaveEntities)
{
kv.Value.LeaveSight(aoiEntity);
}
// 通知自己订阅的Enter Cell清理自己
foreach (long cellId in aoiEntity.SubEnterCells)
{
Cell cell = self.GetCell(cellId);
aoiEntity.UnSubEnter(cell);
}
foreach (long cellId in aoiEntity.SubLeaveCells)
{
Cell cell = self.GetCell(cellId);
aoiEntity.UnSubLeave(cell);
}
// 检查
if (aoiEntity.SeeUnits.Count > 1)
{
Log.Error($"aoiEntity has see units: {aoiEntity.SeeUnits.Count}");
}
if (aoiEntity.BeSeeUnits.Count > 1)
{
Log.Error($"aoiEntity has beSee units: {aoiEntity.BeSeeUnits.Count}");
}
}
private static Cell GetCell(this AOIManagerComponent self, long cellId)
{
Cell cell = self.GetChild<Cell>(cellId);
if (cell == null)
{
cell = self.AddChildWithId<Cell>(cellId);
}
return cell;
}
public static void Move(AOIEntity aoiEntity, Cell newCell, Cell preCell)
{
aoiEntity.Cell = newCell;
preCell.Remove(aoiEntity);
newCell.Add(aoiEntity);
// 通知订阅该newCell Enter的Unit
foreach (KeyValuePair<long, AOIEntity> kv in newCell.SubsEnterEntities)
{
if (kv.Value.SubEnterCells.Contains(preCell.Id))
{
continue;
}
kv.Value.EnterSight(aoiEntity);
}
// 通知订阅preCell leave的Unit
foreach (KeyValuePair<long, AOIEntity> kv in preCell.SubsLeaveEntities)
{
// 如果新的cell仍然在对方订阅的subleave中
if (kv.Value.SubLeaveCells.Contains(newCell.Id))
{
continue;
}
kv.Value.LeaveSight(aoiEntity);
}
}
public static void Move(this AOIManagerComponent self, AOIEntity aoiEntity, int cellX, int cellY)
{
long newCellId = AOIHelper.CreateCellId(cellX, cellY);
if (aoiEntity.Cell.Id == newCellId) // cell没有变化
{
return;
}
// 自己加入新的Cell
Cell newCell = self.GetCell(newCellId);
Move(aoiEntity, newCell, aoiEntity.Cell);
AOIHelper.CalcEnterAndLeaveCell(aoiEntity, cellX, cellY, aoiEntity.enterHashSet, aoiEntity.leaveHashSet);
// 算出自己leave新Cell
foreach (long cellId in aoiEntity.leaveHashSet)
{
if (aoiEntity.SubLeaveCells.Contains(cellId))
{
continue;
}
Cell cell = self.GetCell(cellId);
aoiEntity.SubLeave(cell);
}
// 算出需要通知离开的Cell
aoiEntity.SubLeaveCells.ExceptWith(aoiEntity.leaveHashSet);
foreach (long cellId in aoiEntity.SubLeaveCells)
{
Cell cell = self.GetCell(cellId);
aoiEntity.UnSubLeave(cell);
}
// 这里交换两个HashSet,提高性能
ObjectHelper.Swap(ref aoiEntity.SubLeaveCells, ref aoiEntity.leaveHashSet);
// 算出自己看到的新Cell
foreach (long cellId in aoiEntity.enterHashSet)
{
if (aoiEntity.SubEnterCells.Contains(cellId))
{
continue;
}
Cell cell = self.GetCell(cellId);
aoiEntity.SubEnter(cell);
}
// 离开的Enter
aoiEntity.SubEnterCells.ExceptWith(aoiEntity.enterHashSet);
foreach (long cellId in aoiEntity.SubEnterCells)
{
Cell cell = self.GetCell(cellId);
aoiEntity.UnSubEnter(cell);
}
// 这里交换两个HashSet,提高性能
ObjectHelper.Swap(ref aoiEntity.SubEnterCells, ref aoiEntity.enterHashSet);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ba627657b5b945f42879a685aa491be8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,10 @@
namespace ET.Server
{
public static partial class AOISeeCheckHelper
{
public static bool IsCanSee(AOIEntity a, AOIEntity b)
{
return true;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 13b8112b4a2e587479d6fe24b8c39c1a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Text;
namespace ET.Server
{
[EntitySystemOf(typeof(Cell))]
[FriendOf(typeof(Cell))]
public static partial class CellSystem
{
[EntitySystem]
private static void Awake(this ET.Server.Cell self)
{
}
[EntitySystem]
private static void Destroy(this Cell self)
{
self.AOIUnits.Clear();
self.SubsEnterEntities.Clear();
self.SubsLeaveEntities.Clear();
}
public static void Add(this Cell self, AOIEntity aoiEntity)
{
self.AOIUnits.Add(aoiEntity.Id, aoiEntity);
}
public static void Remove(this Cell self, AOIEntity aoiEntity)
{
self.AOIUnits.Remove(aoiEntity.Id);
}
public static string CellIdToString(this long cellId)
{
int y = (int)(cellId & 0xffffffff);
int x = (int)((ulong)cellId >> 32);
return $"{x}:{y}";
}
public static string CellIdToString(this HashSet<long> cellIds)
{
StringBuilder sb = new StringBuilder();
foreach (long cellId in cellIds)
{
sb.Append(cellId.CellIdToString());
sb.Append(",");
}
return sb.ToString();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1286ef0bf56acc14dad6dd9a105a2bfc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: