chore: initial commit
This commit is contained in:
67
Assets/Scripts/Enemies/EnemyMovement.cs
Normal file
67
Assets/Scripts/Enemies/EnemyMovement.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace BaseGames.Enemies
|
||||
{
|
||||
/// <summary>
|
||||
/// 敌人移动组件(架构 07_EnemyModule §3)。
|
||||
/// Phase 1 实现:水平移动、面向目标、击退。
|
||||
/// ⚠️ 使用 Rigidbody2D.velocity(Unity 2022 LTS)。
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
public class EnemyMovement : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private EnemyStatsSO _config;
|
||||
|
||||
private Rigidbody2D _rb;
|
||||
private int _facingDir = 1;
|
||||
|
||||
public bool IsGrounded { get; private set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_rb = GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
/// <summary>按 SO 配置速度水平移动。dir: +1 右 / -1 左 / 0 停止。</summary>
|
||||
public void MoveHorizontal(float dir)
|
||||
{
|
||||
if (_config == null) return;
|
||||
float speed = _config.WalkSpeed;
|
||||
_rb.velocity = new Vector2(dir * speed, _rb.velocity.y);
|
||||
UpdateFacing(dir);
|
||||
}
|
||||
|
||||
/// <summary>显式指定速度(BD 追击任务调用)。</summary>
|
||||
public void MoveWithSpeed(float dir, float speed)
|
||||
{
|
||||
_rb.velocity = new Vector2(dir * speed, _rb.velocity.y);
|
||||
UpdateFacing(dir);
|
||||
}
|
||||
|
||||
public void FaceTarget(Vector2 targetPos)
|
||||
{
|
||||
float dir = targetPos.x < transform.position.x ? -1f : 1f;
|
||||
UpdateFacing(dir);
|
||||
}
|
||||
|
||||
public void ApplyKnockback(Vector2 dir, float force)
|
||||
{
|
||||
_rb.velocity = dir.normalized * force;
|
||||
}
|
||||
|
||||
public void StopHorizontal()
|
||||
{
|
||||
_rb.velocity = new Vector2(0f, _rb.velocity.y);
|
||||
}
|
||||
|
||||
private void UpdateFacing(float dir)
|
||||
{
|
||||
if (Mathf.Approximately(dir, 0f)) return;
|
||||
int newDir = dir > 0f ? 1 : -1;
|
||||
if (newDir == _facingDir) return;
|
||||
_facingDir = newDir;
|
||||
Vector3 s = transform.localScale;
|
||||
transform.localScale = new Vector3(Mathf.Abs(s.x) * newDir, s.y, s.z);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user