Files
zfxaction25/DonkeysAndDroids.Godot/CurrencyBar.cs
2026-04-19 00:43:27 +02:00

132 lines
5.7 KiB
C#

using System;
using Godot;
using RobotAndDonkey.Game.GameState;
public partial class CurrencyBar : Control
{
public override void _Ready()
{
m_EnergyContainer = GetNode<Control>("%Energy");
m_CarryContainer = GetNode<Control>("%Carry");
m_DeliveryContainer = GetNode<Control>("%Delivery");
m_TapeContainer = GetNode<Control>("%Tape");
m_ProgramContainer = GetNode<Control>("%Program");
m_HandContainer = GetNode<Control>("%Hand");
m_EnergyLabel = GetNode<Label>("%Energy/Label");
m_CarryLabel = GetNode<Label>("%Carry/Label");
m_DeliveryLabel = GetNode<Label>("%Delivery/Label");
m_TapeLabel = GetNode<Label>("%Tape/Label");
m_ProgramLabel = GetNode<Label>("%Program/Label");
m_HandLabel = GetNode<Label>("%Hand/Label");
ResetVisuals();
}
private void ResetVisuals()
{
m_EnergyContainer.Scale = Vector2.One;
m_CarryContainer.Scale = Vector2.One;
m_DeliveryContainer.Scale = Vector2.One;
m_TapeContainer.Scale = Vector2.One;
m_ProgramContainer.Scale = Vector2.One;
m_HandContainer.Scale = Vector2.One;
m_EnergyLabel.Modulate = s_BaseColor;
m_CarryLabel.Modulate = s_BaseColor;
m_DeliveryLabel.Modulate = s_BaseColor;
m_TapeLabel.Modulate = s_BaseColor;
m_ProgramLabel.Modulate = s_BaseColor;
m_HandLabel.Modulate = s_BaseColor;
}
public void Configure(CoreLoop coreLoop, int programCount, Currency currency, Tween tween)
{
if (!m_HasCurrency)
{
m_CurrentCurrency = currency;
m_CurrentProgramCount = programCount;
m_HasCurrency = true;
SetAllTexts(coreLoop, programCount, currency);
ResetVisuals();
return;
}
AnimateStat(m_EnergyContainer, m_EnergyLabel, m_CurrentCurrency.Energy, currency.Energy, tween, currency.Energy > m_CurrentCurrency.Energy ? MusicManager.ESound.Rest : MusicManager.ESound.Energy, value => value.ToString());
var oldCarryCombined = m_CurrentCurrency.Carry + m_CurrentCurrency.MaxCarry * 10000;
var newCarryCombined = currency.Carry + currency.MaxCarry * 10000;
AnimateStat(m_CarryContainer, m_CarryLabel, oldCarryCombined, newCarryCombined, tween, MusicManager.ESound.Carry, _ => $"{currency.Carry}/{currency.MaxCarry}");
AnimateStat(m_DeliveryContainer, m_DeliveryLabel, m_CurrentCurrency.Delivery, currency.Delivery, tween, MusicManager.ESound.Delivery, value => $"{value}/{coreLoop.Board.TargetDeliveryAmount}");
AnimateStat(m_TapeContainer, m_TapeLabel, m_CurrentCurrency.TapeLength, currency.TapeLength, tween, MusicManager.ESound.TapeLength, value => value.ToString());
AnimateStat(m_HandContainer, m_HandLabel, m_CurrentCurrency.HandSize, currency.HandSize, tween, MusicManager.ESound.Hand, value => value.ToString());
AnimateStat(m_ProgramContainer, m_ProgramLabel, m_CurrentProgramCount, programCount, tween, MusicManager.ESound.Program, value => $"Program {(value == 0 ? 0 : 1 + coreLoop.Robot.ProgramCount - value)}/{coreLoop.Robot.ProgramCount}");
m_CurrentCurrency = currency;
m_CurrentProgramCount = programCount;
}
private void SetAllTexts(CoreLoop coreLoop, int programCount, Currency currency)
{
m_EnergyLabel.Text = currency.Energy.ToString();
m_CarryLabel.Text = $"{currency.Carry}/{currency.MaxCarry}";
m_DeliveryLabel.Text = $"{currency.Delivery}/{coreLoop.Board.TargetDeliveryAmount}";
m_TapeLabel.Text = currency.TapeLength.ToString();
m_HandLabel.Text = currency.HandSize.ToString();
var programNumber = (programCount == 0 ? 0 : 1 + coreLoop.Robot.ProgramCount - programCount);
m_ProgramLabel.Text = $"Program {programNumber}/{coreLoop.Robot.ProgramCount}";
}
private void AnimateStat(Control container, Label label, int oldValue, int newValue, Tween tween, MusicManager.ESound sound, Func<int, string> formatter)
{
if (oldValue == newValue || formatter == null)
{
label.Text = formatter?.Invoke(newValue) ?? label.Text;
return;
}
if (tween != null)
{
var increased = newValue > oldValue;
label.Modulate = increased ? s_GoodColor : s_BadColor;
tween.SetParallel();
tween.TweenCallback(Callable.From(() =>
{
container.Scale = new(2, 2);
label.Text = formatter(newValue);
Main.Instance.Music.Play(sound);
}));
tween.TweenProperty(container, "scale", Vector2.One, 0.25f).SetTrans(Tween.TransitionType.Back).SetEase(Tween.EaseType.Out);
tween.TweenProperty(label, "modulate", s_BaseColor, 0.25f).SetTrans(Tween.TransitionType.Cubic).SetEase(Tween.EaseType.Out);
tween.SetParallel(false);
}
else
{
label.Text = formatter(newValue);
}
}
private static readonly Color s_BadColor = new(0.94f, 0.60f, 0.60f);
private static readonly Color s_BaseColor = new(1f, 1f, 1f);
private static readonly Color s_GoodColor = new(0.65f, 0.84f, 0.65f);
private Control m_CarryContainer;
private Label m_CarryLabel;
private Currency m_CurrentCurrency;
private int m_CurrentProgramCount;
private Control m_DeliveryContainer;
private Label m_DeliveryLabel;
private Control m_EnergyContainer;
private Label m_EnergyLabel;
private bool m_HasCurrency;
private Control m_ProgramContainer;
private Label m_ProgramLabel;
private Control m_TapeContainer;
private Label m_TapeLabel;
private Control m_HandContainer;
private Label m_HandLabel;
}