using UnityEngine;
///
/// 主角金币产出组件,挂载到主角单位上
///
public class HeroGoldProducer : MonoBehaviour
{
[Header("金币产出")]
[Tooltip("每次产出的金币数量")]
[SerializeField] private int goldPerProduction = 30;
[Tooltip("产出间隔(秒)")]
[SerializeField] private float productionInterval = 8f;
[Header("可选:血条和进度条")]
[SerializeField] private WorldHealthBar healthBar;
private float productionTimer = 0f;
private void Awake()
{
// 如果没有手动指定,尝试自动获取
if (healthBar == null)
{
healthBar = GetComponent();
}
}
private void Start()
{
// 初始化进度条(使用能量条显示产出进度)
if (healthBar != null)
{
healthBar.maxEnergy = 100f;
healthBar.currentEnergy = 0f;
}
}
private void Update()
{
if (GoldManager.Instance == null) return;
productionTimer += Time.deltaTime;
// 更新进度条
if (healthBar != null)
{
float progress = Mathf.Clamp01(productionTimer / productionInterval);
healthBar.currentEnergy = progress * 100f;
}
// 达到产出时间
if (productionTimer >= productionInterval)
{
ProduceGold();
productionTimer = 0f;
}
}
private void ProduceGold()
{
if (GoldManager.Instance != null)
{
GoldManager.Instance.AddGold(goldPerProduction);
Debug.Log($"主角 {gameObject.name} 产出金币 +{goldPerProduction}");
}
}
}