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.
 
 
 
 
 
 

74 lines
2.8 KiB

using CoreGame;
using Entitas;
using UnityEngine;
namespace CoreGame.Render
{
public class GunAimEnemySystem : IExecuteSystem
{
private readonly IGroup<CombatEntity> m_Group;
public GunAimEnemySystem(Contexts contexts)
{
m_Group = contexts.combat.GetGroup(CombatMatcher.AimTarget);
}
public void Execute(float deltaTime)
{
m_Group.GetEntities(Contexts.s_CacheEntities);
if (Contexts.s_CacheEntities.Count == 0)
return;
var entities = Contexts.s_CacheEntities;
for (int i = 0; i < entities.Count; i++)
{
var entity = entities[i];
var aimTarget = entity.aimTarget;
if (aimTarget.rotationEid == aimTarget.targetEid)
continue;
var rotationEnt = Contexts.Combat.GetEntity(aimTarget.rotationEid);
if (rotationEnt == null)
continue;
var targetEntity = Contexts.Combat.GetEntity(aimTarget.targetEid);
if (targetEntity != null)
aimTarget.targetPos = MathViewUtils.ToVector2(TargetSelectSrv.GetCastTargetPos(targetEntity));
var bindPointProxy = rotationEnt.bindPointProxy;
if (bindPointProxy == null)
continue;
if (bindPointProxy.TryGetBindPointOrDefault(aimTarget.rotationCtrl, out var bindPoint) == false)
continue;
aimTarget.resetTime -= deltaTime;
if (aimTarget.resetTime < 0)
{
bindPoint.localEulerAngles = Vector3.zero;
entity.RemoveAimTarget();
continue;
}
var lookDir = aimTarget.targetPos - new Vector2(bindPoint.position.x, bindPoint.position.y);
lookDir.Normalize();
var gunDir = aimTarget.gunAimForwardDir;
var sign = Mathf.Sign(lookDir.x);
if (aimTarget.rotationEntChangeDir)
{
rotationEnt.transformProxy.SetDirection(lookDir.x < 0 ? Vector2.left : Vector2.right);
gunDir.x *= Mathf.Sign(lookDir.x);
sign *= Mathf.Sign(gunDir.x * lookDir.y - gunDir.y * lookDir.x);
}
var dot = Vector2.Dot(lookDir, gunDir);
var angle = Mathf.Acos(dot) * Mathf.Rad2Deg * sign;
// 主人的朝向也要变, 所以说这个系统要在LocationSyncSystem之后执行
var quaternion = Quaternion.Euler(0, 0, angle);
var lastQuat = bindPoint.localRotation;
bindPoint.localRotation = Quaternion.Slerp(lastQuat, quaternion, deltaTime * 20);
}
}
}
}