Files
zeling_v2/Assets/_Game/Scripts/World/HazardZone.cs
Joywayer 68d4c699ae 修复内容:
PlayerMovement:新增 _facingLocked 字段 + LockFacing(bool) 方法;UpdateFacing() 锁定时直接返回
WallSlideState:OnStateEnter 调用 LockFacing(true) + FlipFacing(_wallDir);OnStateExit 调用 LockFacing(false) 解锁
WallJumpState:OnStateEnter 保险性再调一次 LockFacing(false);WallJumpAway/Toward 同步写入 _inputVelocityX,确保解锁后 UpdateFacing 朝向正确(背墙跳 = 离墙方向,对墙跳 = 朝墙方向)
2026-05-22 10:48:52 +08:00

71 lines
2.2 KiB
C#

using BaseGames.Player;
using UnityEngine;
namespace BaseGames.World
{
/// <summary>
/// 危险区域。玩家进入时触发即死(调用 Kill(),无视无敌帧)或持续伤害。
/// </summary>
[RequireComponent(typeof(Collider2D))]
public class HazardZone : MonoBehaviour
{
[SerializeField] private bool _isInstantKill = true;
[SerializeField] private int _damage = 1;
[Tooltip("持续伤害模式下每次造成伤害的间隔(秒)。仅 _isInstantKill = false 时生效。")]
[SerializeField] private float _damageInterval = 0.5f;
private PlayerStats _cachedStats;
private float _damageTimer;
private bool _playerInside;
private void OnTriggerEnter2D(Collider2D other)
{
if (!other.CompareTag("Player")) return;
_cachedStats = other.GetComponentInParent<PlayerStats>();
if (_cachedStats == null) return;
_playerInside = true;
ApplyDamage();
_damageTimer = _damageInterval;
}
private void OnTriggerExit2D(Collider2D other)
{
if (!other.CompareTag("Player")) return;
_playerInside = false;
_cachedStats = null;
}
private void Update()
{
if (!_playerInside || _isInstantKill || _cachedStats == null) return;
_damageTimer -= Time.deltaTime;
if (_damageTimer <= 0f)
{
ApplyDamage();
_damageTimer = _damageInterval;
}
}
private void ApplyDamage()
{
if (_cachedStats == null) return;
if (_isInstantKill)
_cachedStats.Kill();
else
_cachedStats.TakeDamage(_damage);
}
private void OnDrawGizmos()
{
var col = GetComponent<Collider2D>();
if (col == null) return;
Gizmos.color = new Color(1f, 0f, 0f, 0.3f);
Gizmos.DrawCube(transform.position, col.bounds.size);
Gizmos.color = new Color(1f, 0f, 0f, 0.8f);
Gizmos.DrawWireCube(transform.position, col.bounds.size);
}
}
}