using UnityEngine;
[System.Flags]
public enum BloonProperties
{
None = 0,
Black = 1 << 0, // 免疫爆炸
White = 1 << 1, // 免疫冰冻
Purple = 1 << 2, // 免疫能量、魔法、等离子
Lead = 1 << 3, // 免疫锐利、能量
Camo = 1 << 4, // 需要迷彩侦测
Regrow = 1 << 5, // 会再生
Fortified = 1 << 6, // 强化装甲
MoabClass = 1 << 7 // MOAB级别
}
public enum DamageType
{
Sharp, // 锐利
Explosion, // 爆炸
Energy, // 能量
Magic, // 魔法
Fire, // 火焰
Plasma, // 等离子
Projectile, // 弹丸
Impact, // 撞击
Normal // 普通
}
public class DamageInfo
{
public DamageType damageType;
public float amount;
public bool canHitCamo;
public bool canHitLead;
public DamageInfo(DamageType type, float amount)
{
this.damageType = type;
this.amount = amount;
this.canHitCamo = false;
this.canHitLead = false;
}
}
public class Bloon : MonoBehaviour
{
[SerializeField] private BloonProperties properties;
[SerializeField] private float health = 1.0f;
[SerializeField] private GameObject[] childrenPrefabs; // 子气球预制体
private static readonly Dictionary<DamageType, BloonProperties> immunityMatrix =
new Dictionary<DamageType, BloonProperties>()
{
{ DamageType.Explosion, BloonProperties.Black },
{ DamageType.Energy, BloonProperties.Lead | BloonProperties.Purple },
{ DamageType.Magic, BloonProperties.Purple },
{ DamageType.Plasma, BloonProperties.Purple },
{ DamageType.Sharp, BloonProperties.Lead }
};
public void TakeDamage(DamageInfo damageInfo)
{
// 检查免疫关系
if (IsImmuneTo(damageInfo.damageType))
{
ShowImmuneEffect(damageInfo.damageType);
return; // 0伤害!
}
// 检查迷彩侦测
if (HasProperty(BloonProperties.Camo) && !damageInfo.canHitCamo)
{
ShowMissEffect(); // 未命中迷彩气球
return;
}
// 应用伤害
health -= damageInfo.amount;
ShowDamageEffect(damageInfo.amount);
if (health <= 0)
{
PopBloon();
}
}
private bool IsImmuneTo(DamageType damageType)
{
if (immunityMatrix.ContainsKey(damageType))
{
BloonProperties requiredImmunity = immunityMatrix[damageType];
return (properties & requiredImmunity) != 0;
}
return false;
}
private bool HasProperty(BloonProperties property)
{
return (properties & property) != 0;
}
private void ShowImmuneEffect(DamageType damageType)
{
// 显示免疫特效
// 播放"叮"的音效
// 显示"0"伤害数字
Debug.Log($"{name} 免疫了 {damageType} 伤害!");
// 视觉反馈:显示免疫图标
ShowFloatingIcon(GetImmunityIcon(damageType));
}
private void ShowFloatingIcon(Sprite icon)
{
// 在气球上方显示免疫图标
// 使用DOTween上浮渐隐
}
private Sprite GetImmunityIcon(DamageType damageType)
{
// 根据伤害类型返回对应的免疫图标
return null; // 实际项目中加载对应图标
}
private void PopBloon()
{
// 播放爆破音效
// 显示爆破特效
// 生成子气球(如果有)
SpawnChildren();
// 奖励金钱
GameManager.Instance.AddMoney(GetRewardAmount());
// 销毁当前气球
Destroy(gameObject);
}
private void SpawnChildren()
{
if (childrenPrefabs != null && childrenPrefabs.Length > 0)
{
foreach (GameObject childPrefab in childrenPrefabs)
{
// 随机位置生成子气球
Vector3 randomOffset = new Vector3(
Random.Range(-0.5f, 0.5f),
0,
Random.Range(-0.5f, 0.5f)
);
Instantiate(childPrefab, transform.position + randomOffset, Quaternion.identity);
}
}
}
private int GetRewardAmount()
{
// 根据气球类型返回奖励金额
return 1; // 基础奖励
}
}