Files
zeling_v2/Assets/_Game/Scripts/World/DestructibleTile.cs

67 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using BaseGames.Combat;
using UnityEngine;
namespace BaseGames.World
{
/// <summary>
/// 可破坏物(墙壁/地块)。实现 IDamageable受击后破坏并在 WorldStateRegistry 记录状态。
/// </summary>
public class DestructibleTile : MonoBehaviour, IDamageable
{
#pragma warning disable CS0414
[SerializeField] private int _maxHP = 1;
#pragma warning restore CS0414
[SerializeField] private string _destructedId;
/// <summary>
/// ScriptableObject 注入(非静态 Instance
/// 在 Inspector 中手动拖入场景级 WorldStateRegistry 资产。
/// </summary>
[SerializeField] private WorldStateRegistry _worldState;
private bool _isDestroyed;
// ── IDamageable ───────────────────────────────────────────────────────
public bool IsAlive => !_isDestroyed;
public bool IsInvincible => _isDestroyed;
public int Defense => 0;
public void TakeDamage(DamageInfo info)
{
if (_isDestroyed) return;
if (!CheckDestroyCondition(info)) return;
_isDestroyed = true;
DestroyTile();
}
// ── Virtual Hook ──────────────────────────────────────────────────────
/// <summary>
/// 子类覆盖此方法添加额外破坏条件(如方向校验)。
/// </summary>
protected virtual bool CheckDestroyCondition(DamageInfo info) => true;
// ── Implementation ────────────────────────────────────────────────────
private void DestroyTile()
{
if (!string.IsNullOrEmpty(_destructedId))
_worldState?.MarkDestroyed(_destructedId);
// 播放破坏 VFX、移除 Tilemap 格子等由子类扩展实现
gameObject.SetActive(false);
}
private void Start()
{
// 读档恢复:若已被摧毁则直接隐藏
if (!string.IsNullOrEmpty(_destructedId) && _worldState != null
&& _worldState.IsDestroyed(_destructedId))
{
_isDestroyed = true;
gameObject.SetActive(false);
}
}
}
}