多轮审查和修复
This commit is contained in:
@@ -4,13 +4,14 @@ 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;
|
||||
[SerializeField] private EnemyStatsSO _config;
|
||||
[SerializeField] private SpriteRenderer _spriteRenderer;
|
||||
|
||||
private Rigidbody2D _rb;
|
||||
private int _facingDir = 1;
|
||||
@@ -19,13 +20,13 @@ namespace BaseGames.Enemies
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Debug.Assert(_config != null, "[EnemyMovement] _config 未赋值,请在 Prefab Inspector 中指定 EnemyStatsSO。", this);
|
||||
_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);
|
||||
@@ -54,14 +55,42 @@ namespace BaseGames.Enemies
|
||||
_rb.velocity = new Vector2(0f, _rb.velocity.y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向目标位置抖跃(抛物线累加填充)。
|
||||
/// 计算初速使尔子到达目标,用 Impulse 施加力。
|
||||
/// </summary>
|
||||
public void JumpToTarget(Vector2 target)
|
||||
{
|
||||
if (_rb == null) return;
|
||||
Vector2 delta = target - (Vector2)transform.position;
|
||||
float gravMag = Mathf.Abs(Physics2D.gravity.y * _rb.gravityScale);
|
||||
float timeAloft = Mathf.Max(0.1f, delta.x != 0f
|
||||
? Mathf.Abs(delta.x) / _config.RunSpeed
|
||||
: 0.5f);
|
||||
|
||||
float vy = (delta.y - 0.5f * (-gravMag) * timeAloft * timeAloft) / timeAloft;
|
||||
float vx = delta.x / timeAloft;
|
||||
|
||||
_rb.velocity = new Vector2(vx, vy);
|
||||
UpdateFacing(vx);
|
||||
}
|
||||
|
||||
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);
|
||||
if (_spriteRenderer != null)
|
||||
{
|
||||
_spriteRenderer.flipX = newDir < 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// SpriteRenderer 未绑定时通过 localScale 翻转朝向
|
||||
Vector3 s = transform.localScale;
|
||||
transform.localScale = new Vector3(Mathf.Abs(s.x) * newDir, s.y, s.z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user