using System.Collections;
using UnityEngine;
///
/// 盾卫技能:坚固壁垒
/// 1级:生成相当于自身 25% 最大生命值的护盾(持续 5 秒),播放 MagicShield 护盾特效
/// 3级:护盾值提升至 35% 最大生命值,护盾期间减伤 +15%
///
public class ShieldGuardSkill : UnitSkill
{
[Header("护盾配置")]
[Tooltip("护盾持续时间(秒)")]
public float shieldDuration = 5.0f;
[Tooltip("护盾特效预制体")]
public GameObject shieldVfxPrefab;
private GameObject activeVfxInstance;
private Coroutine activeShieldCoroutine;
private void EnsureUnit()
{
if (unit == null)
{
unit = GetComponent();
}
}
public override void ReleaseSkill()
{
EnsureUnit();
if (unit == null) return;
WorldHealthBar healthBar = unit.GetComponent();
if (healthBar == null || healthBar.IsDead) return;
int level = unit.level; // 从 UnitBase 字段读取 level
// 1级:25% maxHealth,3级+:35% maxHealth
float ratio = level >= 3 ? 0.35f : 0.25f;
float shieldAmount = healthBar.maxHealth * ratio;
// 1. 添加护盾值
healthBar.AddShield(shieldAmount);
// 2. 播放 MagicShield 护盾特效
PlayShieldVfx();
// 3. 开启超时自动清盾协程
if (activeShieldCoroutine != null)
{
StopCoroutine(activeShieldCoroutine);
}
activeShieldCoroutine = StartCoroutine(ShieldTimeoutRoutine(shieldDuration, shieldAmount));
Debug.Log($"盾卫释放技能【坚固壁垒】 获得 {shieldAmount:F1} 护盾(比例: {ratio * 100}%,等级: {level})");
}
public override float ModifyIncomingDamage(float damage, GameObject attacker)
{
EnsureUnit();
// 3级解锁:护盾激活期间享受额外 15% 减伤
if (unit != null && unit.level >= 3)
{
WorldHealthBar healthBar = unit.GetComponent();
if (healthBar != null && healthBar.currentShield > 0f)
{
return damage * (1f - 0.15f);
}
}
return damage;
}
private void PlayShieldVfx()
{
if (activeVfxInstance != null)
{
Destroy(activeVfxInstance);
}
GameObject prefab = shieldVfxPrefab;
if (prefab == null)
{
prefab = Resources.Load("Prefabs/Combat/Magic/Shield/MagicShieldBlue");
}
#if UNITY_EDITOR
if (prefab == null)
{
prefab = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/TJGeneratorLibEffects/Prefabs/Combat/Magic/Shield/MagicShieldBlue.prefab");
}
#endif
if (prefab != null)
{
activeVfxInstance = Instantiate(prefab, unit.transform.position + Vector3.up * 1.0f, Quaternion.identity);
activeVfxInstance.transform.SetParent(unit.transform, true);
}
}
private IEnumerator ShieldTimeoutRoutine(float duration, float amount)
{
float timer = duration;
WorldHealthBar healthBar = unit != null ? unit.GetComponent() : null;
while (timer > 0f)
{
if (unit == null || healthBar == null || healthBar.IsDead || healthBar.currentShield <= 0f)
{
break;
}
timer -= Time.deltaTime;
yield return null;
}
// 超时或护盾为 0 清理特效与护盾值
if (activeVfxInstance != null)
{
Destroy(activeVfxInstance);
activeVfxInstance = null;
}
if (healthBar != null && healthBar.currentShield > 0f && timer <= 0f)
{
healthBar.RemoveShield(amount);
}
}
private void OnDestroy()
{
if (activeVfxInstance != null)
{
Destroy(activeVfxInstance);
}
}
}