67 lines
2.5 KiB
C#
67 lines
2.5 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|
|
}
|