You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

65 lines
2.2 KiB

using System.Collections.Generic;
using Entitas;
using GAS.Runtime;
using UnityEngine;
namespace CoreGame.Render
{
public class NavSys : IExecuteSystem
{
private readonly IGroup<CombatEntity> m_NavGroup;
private readonly List<CombatEntity> m_CacheEntities = new List<CombatEntity>();
public NavSys(CombatContext contexts)
{
var navMatcher = (Matcher<CombatEntity>)Matcher<CombatEntity>.AllOf(CombatComponentsLookup.NavAgentProxy)
.NoneOf(CombatComponentsLookup.Dead);
m_NavGroup = contexts.GetGroup(navMatcher);
}
public void Execute(float deltaTime)
{
m_NavGroup.GetEntities(m_CacheEntities);
if (m_CacheEntities.Count == 0)
return;
for (int i = 0; i < m_CacheEntities.Count; i++)
{
var ent = m_CacheEntities[i];
if (ent.HasComponent(CombatComponentsLookup.NavState) == false)
continue;
var navAgentProxy = ent.navAgentProxy;
navAgentProxy.ChangeSpeedParam(deltaTime <= BattleConst.Epsilon ? 0 : 1);
if (ent.TagCountContainer.HasAnyTags(GTagLib.State_Death, GTagLib.State_ForceMove))
continue;
if (ent.TagCountContainer.HasTag(GTagLib.State_Stun))
{
navAgentProxy.StopNav();
ent.RemoveNavState();
continue;
}
if (navAgentProxy.IsArrived())
{
navAgentProxy.StopNav();
ent.RemoveNavState();
continue;
}
var logicPos = navAgentProxy.GetLogicPos();
var transformProxy = ent.transformProxy;
// 抖动 水平速度小于0.4f时,不转向
float dis = deltaTime * 0.4f;
if (Mathf.Abs(logicPos.x - transformProxy.position.x) > dis)
{
transformProxy.SetDirection((logicPos - transformProxy.position).normalized);
}
transformProxy.SetPosition(logicPos);
}
}
}
}