mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] 接入ET8服务端
[+] 接入ET8服务端
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6047e4105251a574695322acaa6ab8b3
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,31 @@
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
[Event(SceneType.Map)]
|
||||
public class ChangePosition_NotifyAOI: AEvent<Scene, ET.EventType.ChangePosition>
|
||||
{
|
||||
protected override async ETTask Run(Scene scene, ET.EventType.ChangePosition args)
|
||||
{
|
||||
Unit unit = args.Unit;
|
||||
float3 oldPos = args.OldPos;
|
||||
int oldCellX = (int) (oldPos.x * 1000) / AOIManagerComponent.CellSize;
|
||||
int oldCellY = (int) (oldPos.z * 1000) / AOIManagerComponent.CellSize;
|
||||
int newCellX = (int) (unit.Position.x * 1000) / AOIManagerComponent.CellSize;
|
||||
int newCellY = (int) (unit.Position.z * 1000) / AOIManagerComponent.CellSize;
|
||||
if (oldCellX == newCellX && oldCellY == newCellY)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AOIEntity aoiEntity = unit.GetComponent<AOIEntity>();
|
||||
if (aoiEntity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
unit.Scene().GetComponent<AOIManagerComponent>().Move(aoiEntity, newCellX, newCellY);
|
||||
await ETTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 071b14f170a41df40be9d446a366f90e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
[ActorMessageLocationHandler(SceneType.Map)]
|
||||
public class C2M_TestRobotCaseHandler : ActorMessageLocationHandler<Unit, C2M_TestRobotCase, M2C_TestRobotCase>
|
||||
{
|
||||
protected override async ETTask Run(Unit unit, C2M_TestRobotCase request, M2C_TestRobotCase response)
|
||||
{
|
||||
response.N = request.N;
|
||||
await ETTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5779b882127d20f41a9e4d9e48bb8dcf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,13 @@
|
||||
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
[ActorMessageLocationHandler(SceneType.Map)]
|
||||
public class G2M_SessionDisconnectHandler : ActorMessageLocationHandler<Unit, G2M_SessionDisconnect>
|
||||
{
|
||||
protected override async ETTask Run(Unit unit, G2M_SessionDisconnect message)
|
||||
{
|
||||
await ETTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba011d050583a53499952147c5fad840
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3aa2b5e0132eb714ab53a38e8c6f7e10
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,13 @@
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
[ActorMessageLocationHandler(SceneType.Map)]
|
||||
public class C2M_PathfindingResultHandler : ActorMessageLocationHandler<Unit, C2M_PathfindingResult>
|
||||
{
|
||||
protected override async ETTask Run(Unit unit, C2M_PathfindingResult message)
|
||||
{
|
||||
unit.FindPathMoveToAsync(message.Position).Coroutine();
|
||||
await ETTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bedd38d8adf8fb147925d5091bdc6ec7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,12 @@
|
||||
namespace ET.Server
|
||||
{
|
||||
[ActorMessageLocationHandler(SceneType.Map)]
|
||||
public class C2M_StopHandler: ActorMessageLocationHandler<Unit, C2M_Stop>
|
||||
{
|
||||
protected override async ETTask Run(Unit unit, C2M_Stop message)
|
||||
{
|
||||
unit.Stop(1);
|
||||
await ETTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 82030ce8c55952d4281c067ab368ef72
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,58 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
public static partial class MoveHelper
|
||||
{
|
||||
// 可以多次调用,多次调用的话会取消上一次的协程
|
||||
public static async ETTask FindPathMoveToAsync(this Unit unit, float3 target)
|
||||
{
|
||||
float speed = unit.GetComponent<NumericComponent>().GetAsFloat(NumericType.Speed);
|
||||
if (speed < 0.01)
|
||||
{
|
||||
unit.SendStop(2);
|
||||
return;
|
||||
}
|
||||
|
||||
M2C_PathfindingResult m2CPathfindingResult = new();
|
||||
unit.GetComponent<PathfindingComponent>().Find(unit.Position, target, m2CPathfindingResult.Points);
|
||||
|
||||
if (m2CPathfindingResult.Points.Count < 2)
|
||||
{
|
||||
unit.SendStop(3);
|
||||
return;
|
||||
}
|
||||
|
||||
// 广播寻路路径
|
||||
m2CPathfindingResult.Id = unit.Id;
|
||||
MessageHelper.Broadcast(unit, m2CPathfindingResult);
|
||||
|
||||
MoveComponent moveComponent = unit.GetComponent<MoveComponent>();
|
||||
|
||||
bool ret = await moveComponent.MoveToAsync(m2CPathfindingResult.Points, speed);
|
||||
if (ret) // 如果返回false,说明被其它移动取消了,这时候不需要通知客户端stop
|
||||
{
|
||||
unit.SendStop(0);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Stop(this Unit unit, int error)
|
||||
{
|
||||
unit.GetComponent<MoveComponent>().Stop(error == 0);
|
||||
unit.SendStop(error);
|
||||
}
|
||||
|
||||
// error: 0表示协程走完正常停止
|
||||
public static void SendStop(this Unit unit, int error)
|
||||
{
|
||||
MessageHelper.Broadcast(unit, new M2C_Stop()
|
||||
{
|
||||
Error = error,
|
||||
Id = unit.Id,
|
||||
Position = unit.Position,
|
||||
Rotation = unit.Rotation,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1126c2d3180a6f1419a44335fc0f0f12
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 976f21a6abc312d42ad4bf8078438054
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
[ActorMessageLocationHandler(SceneType.Map)]
|
||||
public class C2M_TransferMapHandler : ActorMessageLocationHandler<Unit, C2M_TransferMap, M2C_TransferMap>
|
||||
{
|
||||
protected override async ETTask Run(Unit unit, C2M_TransferMap request, M2C_TransferMap response)
|
||||
{
|
||||
await ETTask.CompletedTask;
|
||||
|
||||
string currentMap = unit.Scene().Name;
|
||||
string toMap = null;
|
||||
if (currentMap == "Map1")
|
||||
{
|
||||
toMap = "Map2";
|
||||
}
|
||||
else
|
||||
{
|
||||
toMap = "Map1";
|
||||
}
|
||||
|
||||
StartSceneConfig startSceneConfig = StartSceneConfigCategory.Instance.GetBySceneName(unit.Fiber().Zone, toMap);
|
||||
|
||||
TransferHelper.TransferAtFrameFinish(unit, startSceneConfig.ActorId, toMap).Coroutine();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 09e1381365665c14aaa410513c72dbb1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
[ActorMessageHandler(SceneType.Map)]
|
||||
public class M2M_UnitTransferRequestHandler: ActorMessageHandler<Scene, M2M_UnitTransferRequest, M2M_UnitTransferResponse>
|
||||
{
|
||||
protected override async ETTask Run(Scene scene, M2M_UnitTransferRequest request, M2M_UnitTransferResponse response)
|
||||
{
|
||||
UnitComponent unitComponent = scene.GetComponent<UnitComponent>();
|
||||
Unit unit = MongoHelper.Deserialize<Unit>(request.Unit);
|
||||
|
||||
unitComponent.AddChild(unit);
|
||||
unitComponent.Add(unit);
|
||||
|
||||
foreach (byte[] bytes in request.Entitys)
|
||||
{
|
||||
Entity entity = MongoHelper.Deserialize<Entity>(bytes);
|
||||
unit.AddComponent(entity);
|
||||
}
|
||||
|
||||
unit.AddComponent<MoveComponent>();
|
||||
unit.AddComponent<PathfindingComponent, string>(scene.Name);
|
||||
unit.Position = new float3(-10, 0, -10);
|
||||
|
||||
unit.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.OrderedMessage);
|
||||
|
||||
// 通知客户端开始切场景
|
||||
M2C_StartSceneChange m2CStartSceneChange = new() { SceneInstanceId = scene.InstanceId, SceneName = scene.Name };
|
||||
MessageHelper.SendToClient(unit, m2CStartSceneChange);
|
||||
|
||||
// 通知客户端创建My Unit
|
||||
M2C_CreateMyUnit m2CCreateUnits = new();
|
||||
m2CCreateUnits.Unit = UnitHelper.CreateUnitInfo(unit);
|
||||
MessageHelper.SendToClient(unit, m2CCreateUnits);
|
||||
|
||||
// 加入aoi
|
||||
unit.AddComponent<AOIEntity, int, float3>(9 * 1000, unit.Position);
|
||||
|
||||
// 解锁location,可以接收发给Unit的消息
|
||||
await scene.Root().GetComponent<LocationProxyComponent>().UnLock(LocationType.Unit, unit.Id, request.OldActorId, unit.GetActorId());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e8e9b12c47eb5ac41b42a6b83817f1d2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,39 @@
|
||||
using System.Collections.Generic;
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
public static partial class TransferHelper
|
||||
{
|
||||
public static async ETTask TransferAtFrameFinish(Unit unit, ActorId sceneInstanceId, string sceneName)
|
||||
{
|
||||
await unit.Fiber().WaitFrameFinish();
|
||||
|
||||
await TransferHelper.Transfer(unit, sceneInstanceId, sceneName);
|
||||
}
|
||||
|
||||
|
||||
public static async ETTask Transfer(Unit unit, ActorId sceneInstanceId, string sceneName)
|
||||
{
|
||||
Scene root = unit.Root();
|
||||
|
||||
// location加锁
|
||||
long unitId = unit.Id;
|
||||
|
||||
M2M_UnitTransferRequest request = M2M_UnitTransferRequest.Create();
|
||||
request.OldActorId = unit.GetActorId();
|
||||
request.Unit = unit.ToBson();
|
||||
foreach (Entity entity in unit.Components.Values)
|
||||
{
|
||||
if (entity is ITransfer)
|
||||
{
|
||||
request.Entitys.Add(entity.ToBson());
|
||||
}
|
||||
}
|
||||
unit.Dispose();
|
||||
|
||||
await root.GetComponent<LocationProxyComponent>().Lock(LocationType.Unit, unitId, request.OldActorId);
|
||||
await root.GetComponent<ActorSenderComponent>().Call(sceneInstanceId, request);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6fe1b54e7a5a88243a60c501d7a71979
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 90f2084c2789e40468c6eb5642275748
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,29 @@
|
||||
namespace ET.Server
|
||||
{
|
||||
// 进入视野通知
|
||||
[Event(SceneType.Map)]
|
||||
public class UnitEnterSightRange_NotifyClient: AEvent<Scene, EventType.UnitEnterSightRange>
|
||||
{
|
||||
protected override async ETTask Run(Scene scene, EventType.UnitEnterSightRange args)
|
||||
{
|
||||
AOIEntity a = args.A;
|
||||
AOIEntity b = args.B;
|
||||
if (a.Id == b.Id)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Unit ua = a.GetParent<Unit>();
|
||||
if (ua.Type != UnitType.Player)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Unit ub = b.GetParent<Unit>();
|
||||
|
||||
MessageHelper.NoticeUnitAdd(ua, ub);
|
||||
|
||||
await ETTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 92a7bfbd50423e64b994beb9fe3ddad2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
public static partial class UnitFactory
|
||||
{
|
||||
public static Unit Create(Scene scene, long id, UnitType unitType)
|
||||
{
|
||||
UnitComponent unitComponent = scene.GetComponent<UnitComponent>();
|
||||
switch (unitType)
|
||||
{
|
||||
case UnitType.Player:
|
||||
{
|
||||
Unit unit = unitComponent.AddChildWithId<Unit, int>(id, 1001);
|
||||
unit.AddComponent<MoveComponent>();
|
||||
unit.Position = new float3(-10, 0, -10);
|
||||
|
||||
NumericComponent numericComponent = unit.AddComponent<NumericComponent>();
|
||||
numericComponent.Set(NumericType.Speed, 6f); // 速度是6米每秒
|
||||
numericComponent.Set(NumericType.AOI, 15000); // 视野15米
|
||||
|
||||
unitComponent.Add(unit);
|
||||
// 加入aoi
|
||||
unit.AddComponent<AOIEntity, int, float3>(9 * 1000, unit.Position);
|
||||
return unit;
|
||||
}
|
||||
default:
|
||||
throw new Exception($"not such unit type: {unitType}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 359d93a0f7fdfa44bbc572328e9742f3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,49 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace ET.Server
|
||||
{
|
||||
[FriendOf(typeof(MoveComponent))]
|
||||
[FriendOf(typeof(NumericComponent))]
|
||||
public static partial class UnitHelper
|
||||
{
|
||||
public static UnitInfo CreateUnitInfo(Unit unit)
|
||||
{
|
||||
UnitInfo unitInfo = new();
|
||||
NumericComponent nc = unit.GetComponent<NumericComponent>();
|
||||
unitInfo.UnitId = unit.Id;
|
||||
unitInfo.ConfigId = unit.ConfigId;
|
||||
unitInfo.Type = (int)unit.Type;
|
||||
unitInfo.Position = unit.Position;
|
||||
unitInfo.Forward = unit.Forward;
|
||||
|
||||
MoveComponent moveComponent = unit.GetComponent<MoveComponent>();
|
||||
if (moveComponent != null)
|
||||
{
|
||||
if (!moveComponent.IsArrived())
|
||||
{
|
||||
unitInfo.MoveInfo = new MoveInfo();
|
||||
unitInfo.MoveInfo.Points.Add(unit.Position);
|
||||
for (int i = moveComponent.N; i < moveComponent.Targets.Count; ++i)
|
||||
{
|
||||
float3 pos = moveComponent.Targets[i];
|
||||
unitInfo.MoveInfo.Points.Add(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ((int key, long value) in nc.NumericDic)
|
||||
{
|
||||
unitInfo.KV.Add(key, value);
|
||||
}
|
||||
|
||||
return unitInfo;
|
||||
}
|
||||
|
||||
// 获取看见unit的玩家,主要用于广播
|
||||
public static Dictionary<long, AOIEntity> GetBeSeePlayers(this Unit self)
|
||||
{
|
||||
return self.GetComponent<AOIEntity>().GetBeSeePlayers();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bcdc9f9c96c0ff146ac609ff97b4903a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,20 @@
|
||||
namespace ET.Server
|
||||
{
|
||||
// 离开视野
|
||||
[Event(SceneType.Map)]
|
||||
public class UnitLeaveSightRange_NotifyClient: AEvent<Scene, EventType.UnitLeaveSightRange>
|
||||
{
|
||||
protected override async ETTask Run(Scene scene, EventType.UnitLeaveSightRange args)
|
||||
{
|
||||
await ETTask.CompletedTask;
|
||||
AOIEntity a = args.A;
|
||||
AOIEntity b = args.B;
|
||||
if (a.Unit.Type != UnitType.Player)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
MessageHelper.NoticeUnitRemove(a.GetParent<Unit>(), b.GetParent<Unit>());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10a2365c686050546995d6a3897b9038
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user