Files
vampire-like/Assets/GameMain/Scripts/Runtime/EntityLogic/Enemy/RemoteEnemy.cs
T
2026-09-15 11:07:01 +08:00

82 lines
2.4 KiB
C#

using Cysharp.Threading.Tasks;
using SepCore.AsyncTask;
using SepCore.CustomUtility;
using SepCore.Definition;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace SepCore.Entity
{
public class RemoteEnemy : EnemyBase
{
private float _attackRangeSquared;
protected override void OnShow(object userData)
{
base.OnShow(userData);
_attackRangeSquared = AttackRange * AttackRange;
}
protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
base.OnUpdate(elapseSeconds, realElapseSeconds);
if (_target == null)
{
Movement.SetMove(false);
return;
}
Vector3 toTarget = _target.position - CachedTransform.position;
toTarget.y = 0f;
float distanceSquared = toTarget.sqrMagnitude;
if (distanceSquared <= _attackRangeSquared)
{
Movement.SetMove(false);
if (_canAttack)
{
TryFireProjectile();
ResetAttackCooldown(AttackCooldown);
}
}
else
{
Movement.SetMove(true);
Movement.SetDirection(GetTargetDirection());
}
}
private void TryFireProjectile()
{
string groupName = GameEntry.Luban.Global.ProjectileGroupName;
if (!GameEntry.Entity.HasEntityGroup(groupName))
{
Log.Error("Entity group '{0}' does not exist.", groupName);
return;
}
float spawnHeight = GameEntry.Luban.Global.ProjectileHeightOffset / 1000f;
Vector3 spawnPosition = CachedTransform.position + Vector3.up * spawnHeight;
Vector3 direction = _target.position - spawnPosition;
direction.y = 0f;
if (direction.sqrMagnitude <= Mathf.Epsilon)
{
direction = CachedTransform.forward;
direction.y = 0f;
}
if (direction.sqrMagnitude <= Mathf.Epsilon)
{
direction = Vector3.forward;
}
else
{
direction.Normalize();
}
GameEntry.Entity.ShowProjectile(Id, EnemyData.Camp, AttackDamage, spawnPosition, direction).Forget();
}
}
}