71 lines
2.2 KiB
C#
71 lines
2.2 KiB
C#
using UnityEngine;
|
|
using BaseGames.Combat;
|
|
|
|
namespace BaseGames.Enemies
|
|
{
|
|
/// <summary>
|
|
/// 飞行敌人。不依赖 PathBerserker2d 导航,直接通过 Rigidbody2D 向玩家移动。
|
|
/// 接触玩家时造成伤害。
|
|
/// </summary>
|
|
public class FlyingEnemy : EnemyBase
|
|
{
|
|
[SerializeField] private float _chaseSpeed = 3f;
|
|
[SerializeField] private DamageSourceSO _contactDamageSource;
|
|
|
|
private Rigidbody2D _rb;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
_rb = GetComponent<Rigidbody2D>();
|
|
if (_rb != null)
|
|
{
|
|
_rb.gravityScale = 0f;
|
|
_rb.constraints = RigidbodyConstraints2D.FreezeRotation;
|
|
}
|
|
}
|
|
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
|
|
if (_playerTransform == null || _rb == null) return;
|
|
if (CurrentState == EnemyStateType.Dead ||
|
|
CurrentState == EnemyStateType.Stagger) return;
|
|
|
|
// 向玩家移动
|
|
Vector2 targetPos = _playerTransform.position;
|
|
Vector2 myPos = _rb.position;
|
|
Vector2 newPos = Vector2.MoveTowards(myPos, targetPos, _chaseSpeed * Time.deltaTime);
|
|
_rb.MovePosition(newPos);
|
|
}
|
|
|
|
public override void StopMovement()
|
|
{
|
|
if (_rb != null) _rb.velocity = Vector2.zero;
|
|
}
|
|
|
|
public override void MoveInDirection(float dir)
|
|
{
|
|
if (_rb != null) _rb.velocity = new Vector2(dir, 0f) * _chaseSpeed;
|
|
}
|
|
|
|
private void OnTriggerStay2D(Collider2D other)
|
|
{
|
|
if (_contactDamageSource == null) return;
|
|
if (CurrentState == EnemyStateType.Dead) return;
|
|
|
|
var hurtBox = other.GetComponent<HurtBox>();
|
|
if (hurtBox == null) return;
|
|
|
|
Vector2 knockDir = ((Vector2)other.bounds.center - _rb.position).normalized;
|
|
var info = DamageInfo.From(
|
|
_contactDamageSource,
|
|
knockDir,
|
|
transform.position,
|
|
gameObject.layer);
|
|
hurtBox.ReceiveDamage(info);
|
|
}
|
|
}
|
|
}
|