Skip to content

Commit

Permalink
ref:<寻路系统>重构寻路与状态系统交互部分,新增MoveTodoSomething
Browse files Browse the repository at this point in the history
  • Loading branch information
wqaetly committed Dec 11, 2020
1 parent c9839d3 commit aec7a58
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 121 deletions.
Binary file modified Bin/Hotfix.dll
Binary file not shown.
Binary file modified Bin/Model.dll
Binary file not shown.
1 change: 1 addition & 0 deletions Server/.idea/.idea.Server/.idea/contentModel.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Server/Hotfix/NKGMOBA/Battle/Bat_SendMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class UnitPathComponentInvoke: AEvent<long, Vector3>
{
public override void Run(long a, Vector3 b)
{
UnitComponent.Instance.Get(a).GetComponent<UnitPathComponent>().MoveTo(b).Coroutine();
UnitComponent.Instance.Get(a).GetComponent<UnitPathComponent>().CommonNavigate(b);
}
}
}
15 changes: 8 additions & 7 deletions Server/Hotfix/NKGMOBA/Factory/UnitFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ public static Unit CreateDarius()
NP_RuntimeTreeFactory.CreateSkillNpRuntimeTree(unit, configComponent.Get<Server_SkillCanvasConfig>(10002).NPBehaveId,
configComponent.Get<Server_SkillCanvasConfig>(10002).BelongToSkillId)
.Start();

//默认升一级技能
unit.GetComponent<SkillCanvasManagerComponent>().AddSkillLevel(configComponent.Get<Server_SkillCanvasConfig>(10002).BelongToSkillId);
//Log.Info("行为树创建完成");

//添加栈式状态机组件
unit.AddComponent<StackFsmComponent>();
unit.AddComponent<CommonAttackComponent>();

//设置英雄位置
unit.Position = new Vector3(-10, 0, -10);
return unit;
Expand All @@ -100,7 +100,7 @@ public static Unit CreateSpiling(long parentId)
//Log.Info($"服务端响应木桩请求,父id为{message.ParentUnitId}");
UnitComponent.Instance.Get(parentId).GetComponent<ChildrenUnitComponent>().AddUnit(unit);
//Log.Info("确认找到了请求的父实体");

unit.AddComponent<B2S_UnitColliderManagerComponent>().CreateCollider(unit,
Game.Scene.GetComponent<ConfigComponent>().Get<Server_B2SCollisionRelationConfig>(10001).B2S_CollisionRelationId, 10006);
unit.AddComponent<HeroDataComponent, long>(10001);
Expand All @@ -112,11 +112,12 @@ public static Unit CreateSpiling(long parentId)
unit.AddComponent<UnitPathComponent>();

unit.AddComponent<NP_RuntimeTreeManager>();

ConfigComponent configComponent = Game.Scene.GetComponent<ConfigComponent>();
//Log.Info("开始创建行为树");
NP_RuntimeTreeFactory.CreateNpRuntimeTree(unit, configComponent.Get<Server_AICanvasConfig>(10001).NPBehaveId)
.Start();
if (RandomHelper.RandomNumber(0, 2) == 0)
//Log.Info("开始创建行为树");
NP_RuntimeTreeFactory.CreateNpRuntimeTree(unit, configComponent.Get<Server_AICanvasConfig>(10001).NPBehaveId)
.Start();

return unit;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using ETModel;
using ETModel.NKGMOBA.Battle.Fsm;
using ETModel.NKGMOBA.Battle.State;
using UnityEngine;

namespace ETHotfix
Expand All @@ -9,7 +11,8 @@ public class Frame_ClickMapHandler: AMActorLocationHandler<Unit, Frame_ClickMap>
protected override async ETTask Run(Unit unit, Frame_ClickMap message)
{
Vector3 target = new Vector3(message.X, message.Y, message.Z);
unit.GetComponent<UnitPathComponent>().MoveTo(target).Coroutine();
unit.GetComponent<UnitPathComponent>().CommonNavigate(target);

await ETTask.CompletedTask;
}
}
Expand Down
44 changes: 21 additions & 23 deletions Server/Hotfix/NKGMOBA/System/CommonAttackComponentSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public override void Run(long a)
}
}

[Event(EventIdType.CancelAttackWithOutResetAttackTarget)]
public class CancelAttackWithOutResetAttackTargetEvent: AEvent<long>
{
public override void Run(long a)
{
UnitComponent.Instance.Get(a).GetComponent<CommonAttackComponent>().CancelCommonAttackWithOutResetTarget();
}
}

[ObjectSystem]
public class CommonAttackComponentUpdateSystem: UpdateSystem<CommonAttackComponent>
{
Expand All @@ -37,7 +46,7 @@ public static void SetAttackTarget(this CommonAttackComponent self, Unit targetU
{
if (targetUnit == null)
{
Log.Error("普攻组件接受到的targetUnit为null");
Log.Error("普攻组件接收到的targetUnit为null");
return;
}

Expand All @@ -49,7 +58,8 @@ public static void SetAttackTarget(this CommonAttackComponent self, Unit targetU
}

self.CachedUnitForAttack = targetUnit;
self.Entity.GetComponent<StackFsmComponent>().ChangeState<CommonAttackState>(StateTypes.CommonAttack, "CommonAttack", 1);

self.m_StackFsmComponent.ChangeState<CommonAttackState>(StateTypes.CommonAttack, "CommonAttack", 1);
}
}

Expand Down Expand Up @@ -87,7 +97,7 @@ private static async ETTask CommonAttack_Internal(this CommonAttackComponent sel

Game.EventSystem.Run(EventIdType.ChangeHP, self.CachedUnitForAttack.Id, -50.0f);

CDComponent.Instance.TriggerCD(self.Entity.Id,"CommonAttack");
CDComponent.Instance.TriggerCD(self.Entity.Id, "CommonAttack");
CDInfo commonAttackCDInfo = CDComponent.Instance.GetCDData(self.Entity.Id, "CommonAttack");
commonAttackCDInfo.Interval = (long) (1 / attackSpeed - attackPre) * 1000;

Expand All @@ -101,36 +111,24 @@ public static void Update(this CommonAttackComponent self)
if (self.CachedUnitForAttack != null && !self.CachedUnitForAttack.IsDisposed)
{
Vector3 selfUnitPos = (self.Entity as Unit).Position;
float distance = Vector3.Distance(selfUnitPos, self.CachedUnitForAttack.Position);
double distance = Vector3.Distance(selfUnitPos, self.CachedUnitForAttack.Position);
//目标距离大于当前攻击距离会先进行寻路,这里的1.75为175码
if (distance >= 1.75)
if (distance >= 1.75f)
{
self.CancelCommonAttackWithOutResetTarget();
if (!CDComponent.Instance.GetCDResult(self.Entity.Id, "MoveToAttack")) return;

CDComponent.Instance.TriggerCD(self.Entity.Id, "MoveToAttack");

self.IsMoveToTarget = true;

//移动完进入攻击状态
CommonAttackState commonAttackState = ReferencePool.Acquire<CommonAttackState>();
commonAttackState.SetData(StateTypes.CommonAttack, "CommonAttack", 1);

self.Entity.GetComponent<UnitPathComponent>()
.MoveTo_InternalWithOutStateChange(self.CachedUnitForAttack.Position)
.Coroutine();
.NavigateTodoSomething(self.CachedUnitForAttack.Position, 1.75f, commonAttackState);
}
else
{
if (self.IsMoveToTarget)
{
self.Entity.GetComponent<UnitPathComponent>().CancelMove();
M2C_PathfindingResult pathfindingResult = new M2C_PathfindingResult()
{
X = selfUnitPos.x, Y = selfUnitPos.y, Z = selfUnitPos.z, Id = self.Entity.Id
};
pathfindingResult.Xs.Clear();
pathfindingResult.Ys.Clear();
pathfindingResult.Zs.Clear();
MessageHelper.Broadcast(pathfindingResult);
self.IsMoveToTarget = false;
}

//目标不为空,且处于攻击状态,且上次攻击已完成或取消
if ((self.CancellationTokenSource == null || self.CancellationTokenSource.IsCancellationRequested))
{
Expand Down
137 changes: 113 additions & 24 deletions Server/Hotfix/NKGMOBA/System/UnitPathComponentSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,39 @@

namespace ETHotfix
{
[Event(EventIdType.CancelMove)]
public class CancelMoveEvent: AEvent<long>
{
public override void Run(long a)
{
UnitComponent.Instance.Get(a).GetComponent<UnitPathComponent>().CancelMove();
}
}

[ObjectSystem]
public class UnitPathComponentUpdateSystem: UpdateSystem<UnitPathComponent>
{
public override void Update(UnitPathComponent self)
{
self.Update();
}
}

public static class UnitPathComponentHelper
{
public static void Update(this UnitPathComponent self)
{
if (self.CancellationTokenSource != null && self.TargetRange >= 0 &&
Vector3.Distance((self.Entity as Unit).Position, self.Target) <= self.TargetRange)
{
self.CancellationTokenSource?.Cancel();
self.CancellationTokenSource = null;

if (self.NextState == null) return;
self.Entity.GetComponent<StackFsmComponent>().ChangeState(self.NextState);
}
}

private static async ETTask MoveAsync(this UnitPathComponent self, List<Vector3> path)
{
if (path.Count == 0)
Expand All @@ -30,19 +61,67 @@ private static async ETTask MoveAsync(this UnitPathComponent self, List<Vector3>
}
}

public static async ETVoid MoveTo(this UnitPathComponent self, Vector3 target)
/// <summary>
/// 正常寻路,寻路完成后进入Idle状态
/// </summary>
/// <param name="self"></param>
/// <param name="target"></param>
public static void CommonNavigate(this UnitPathComponent self, Vector3 target)
{
if (!self.Entity.GetComponent<StackFsmComponent>().ChangeState<NavigateState>(StateTypes.Run, "Navigate", 1))
{
return;
}

if ((self.Target - target).magnitude < 0.1f)
IdleState idleState = ReferencePool.Acquire<IdleState>();
idleState.SetData(StateTypes.Idle, "Idle", 1);

self.MoveTodoSomething(target, self.Entity.GetComponent<StackFsmComponent>().GetCurrentFsmState(), idleState).Coroutine();
}

/// <summary>
/// 寻路到某个点然后做某事,如果当前距离小于目标距离则直接进入状态,否则就寻路到适合的地点再进行寻路
/// </summary>
/// <param name="self"></param>
/// <param name="target">目标地点</param>
/// <param name="targetRange">目标距离</param>
/// <param name="targetState">目标状态</param>
public static void NavigateTodoSomething(this UnitPathComponent self, Vector3 target, float targetRange, AFsmStateBase targetState)
{
if (Vector3.Distance((self.Entity as Unit).Position, target) >= targetRange)
{
return;
if (!self.Entity.GetComponent<StackFsmComponent>().ChangeState<NavigateState>(StateTypes.Run, "Navigate", 1))
{
ReferencePool.Release(targetState);
return;
}

self.MoveTodoSomething(target, self.Entity.GetComponent<StackFsmComponent>().GetCurrentFsmState(), targetState, targetRange)
.Coroutine();
}
else
{
self.Entity.GetComponent<StackFsmComponent>().ChangeState(targetState);
}
}

/// <summary>
/// 移动到目标点,随后进入指定状态(如果有的话)
/// </summary>
/// <param name="self"></param>
/// <param name="target"></param>
/// <param name="bindState"></param>
/// <param name="nextState"></param>
/// <param name="targetRange">目标范围,当自身与目标位置小于等于此范围时,则停止寻路,进入NextState</param>
/// <returns></returns>
private static async ETVoid MoveTodoSomething(this UnitPathComponent self, Vector3 target, AFsmStateBase bindState,
AFsmStateBase nextState = null, float targetRange = 0)
{
self.BindState = bindState;
self.NextState = nextState;

self.Target = target;
self.TargetRange = targetRange;

Unit unit = self.GetParent<Unit>();

Expand All @@ -60,35 +139,45 @@ public static async ETVoid MoveTo(this UnitPathComponent self, Vector3 target)
await self.MoveAsync(self.RecastPath.Results);
self.CancellationTokenSource.Dispose();
self.CancellationTokenSource = null;
self.Entity.GetComponent<StackFsmComponent>().RemoveState("Navigate");

if (nextState != null)
{
self.Entity.GetComponent<StackFsmComponent>().ChangeState(nextState);
}

self.Entity.GetComponent<StackFsmComponent>().RemoveState(bindState.StateName);
}

public static async ETVoid MoveTo_InternalWithOutStateChange(this UnitPathComponent self, Vector3 target)
public static void CancelMove(this UnitPathComponent self)
{
if ((self.Target - target).magnitude < 0.1f)
self.CancellationTokenSource?.Cancel();
self.CancellationTokenSource = null;

self.TargetRange = 0;
self.Target = Vector3.zero;

if (self.BindState != null)
{
return;
self.Entity.GetComponent<StackFsmComponent>().RemoveState(self.BindState.StateName);
self.BindState = null;
}

self.Target = target;

Unit unit = self.GetParent<Unit>();
if (self.NextState != null)
{
ReferencePool.Release(self.NextState);
self.NextState = null;
}

RecastPathComponent recastPathComponent = Game.Scene.GetComponent<RecastPathComponent>();
RecastPath recastPath = ReferencePool.Acquire<RecastPath>();
recastPath.StartPos = unit.Position;
recastPath.EndPos = new Vector3(target.x, target.y, target.z);
self.RecastPath = recastPath;
//TODO 因为目前阶段只有一张地图,所以默认mapId为10001
recastPathComponent.SearchPath(10001, self.RecastPath);
//Log.Debug($"find result: {self.ABPath.Result.ListToString()}");
Vector3 selfUnitPos = (self.Entity as Unit).Position;

self.CancellationTokenSource?.Cancel();
self.CancellationTokenSource = new CancellationTokenSource();
await self.MoveAsync(self.RecastPath.Results);
self.CancellationTokenSource.Dispose();
self.CancellationTokenSource = null;
self.Entity.GetComponent<StackFsmComponent>().RemoveState("Navigate");
M2C_PathfindingResult pathfindingResult = new M2C_PathfindingResult()
{
X = selfUnitPos.x, Y = selfUnitPos.y, Z = selfUnitPos.z, Id = self.Entity.Id
};
pathfindingResult.Xs.Clear();
pathfindingResult.Ys.Clear();
pathfindingResult.Zs.Clear();
MessageHelper.Broadcast(pathfindingResult);
}

// 从index找接下来3个点,广播
Expand Down
47 changes: 28 additions & 19 deletions Server/Model/Base/Event/EventIdType.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
namespace ETModel
{
public static class EventIdType
{
public const string NumbericChange = "NumbericChange";
public const string TestBehavior = "TestBehavior";
public static class EventIdType
{
public const string NumbericChange = "NumbericChange";
public const string TestBehavior = "TestBehavior";


public const string CreateCollider = "CreateCollider";


public const string ChangeHP = "ChangeHP";
public const string ChangeMP = "ChangeMP";
public const string CreateCollider = "CreateCollider";

public const string SendBuffInfoToClient = "SendBuffInfoToClient";

//移动到随机位置
public const string MoveToRandomPos = "MoveToRandomPos";
/// <summary>
/// 取消攻击
/// </summary>
public const string CancelAttack = "CancelAttack";
}
public const string ChangeHP = "ChangeHP";
public const string ChangeMP = "ChangeMP";

public const string SendBuffInfoToClient = "SendBuffInfoToClient";

//移动到随机位置
public const string MoveToRandomPos = "MoveToRandomPos";

/// <summary>
/// 取消移动
/// </summary>
public const string CancelMove = "CancelMove";

/// <summary>
/// 取消攻击
/// </summary>
public const string CancelAttack = "CancelAttack";

/// <summary>
/// 取消攻击但不重置攻击对象
/// </summary>
public const string CancelAttackWithOutResetAttackTarget = "CancelAttackWithOutResetAttackTarget";
}
}
Loading

0 comments on commit aec7a58

Please sign in to comment.