feat(buildings): 添加金矿建筑功能并优化建筑健康系统 - 为兵营和金矿添加伤害数字显示功能,包括持续时间、上升高度等参数 - 将金矿脚本重构为GoldMine组件,实现定时产出金币机制 - 为所有单位添加NavMeshAgent组件以支持导航寻路 - 为弓箭手、法师等单位添加分离半径和目标扩散参数 - 为建筑添加碰撞体和标签设置 - 修复当前能量字段命名规范(currentEnergy -> _currentEnergy) - 调整金矿健康值从100提升至500 - 统一伤害数字显示相关属性配置 ```
85 lines
2.0 KiB
C#
85 lines
2.0 KiB
C#
using UnityEngine;
|
|
using System;
|
|
|
|
/// <summary>
|
|
/// 金币管理器,全局单例
|
|
/// </summary>
|
|
public class GoldManager : MonoBehaviour
|
|
{
|
|
public static GoldManager Instance { get; private set; }
|
|
|
|
[Header("金币设置")]
|
|
[SerializeField] private int initialGold = 500;
|
|
[SerializeField] private int currentGold = 500;
|
|
|
|
/// <summary>
|
|
/// 当前金币数量(只读)
|
|
/// </summary>
|
|
public int CurrentGold => currentGold;
|
|
|
|
/// <summary>
|
|
/// 金币变化事件(参数:新的金币数量)
|
|
/// </summary>
|
|
public event Action<int> OnGoldChanged;
|
|
|
|
private void Awake()
|
|
{
|
|
// 单例模式
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
Instance = this;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
currentGold = initialGold;
|
|
OnGoldChanged?.Invoke(currentGold);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 增加金币
|
|
/// </summary>
|
|
public void AddGold(int amount)
|
|
{
|
|
if (amount <= 0) return;
|
|
|
|
currentGold += amount;
|
|
OnGoldChanged?.Invoke(currentGold);
|
|
Debug.Log($"获得金币 +{amount},当前金币: {currentGold}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 尝试消费金币,如果金币足够则扣除并返回 true,否则返回 false
|
|
/// </summary>
|
|
public bool TrySpendGold(int amount)
|
|
{
|
|
if (amount <= 0)
|
|
{
|
|
Debug.LogWarning("消费金币数量必须大于0");
|
|
return false;
|
|
}
|
|
|
|
if (currentGold < amount)
|
|
{
|
|
Debug.LogWarning($"金币不足!需要 {amount},当前只有 {currentGold}");
|
|
return false;
|
|
}
|
|
|
|
currentGold -= amount;
|
|
OnGoldChanged?.Invoke(currentGold);
|
|
Debug.Log($"消费金币 -{amount},当前金币: {currentGold}");
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查是否有足够的金币
|
|
/// </summary>
|
|
public bool HasEnoughGold(int amount)
|
|
{
|
|
return currentGold >= amount;
|
|
}
|
|
}
|