newlon/scripts/components/gui/PlantSlot.cs

67 lines
1.4 KiB
C#

using Godot;
using System;
public partial class PlantSlot : TextureButton
{
[Export] private PlantResource _resource;
[Export] private Label _cost;
[Export] private TextureRect _icon;
[Export] private Timer _timer;
private bool _recharging;
public override void _Ready()
{
if (_resource != null)
UpdateContents();
}
public override void _Process(double delta)
{
if (_resource != null)
{
Disabled = _recharging || LevelController.Instance.LevelData.SunCount < _resource.Cost;
FocusMode = Disabled ? FocusModeEnum.None : FocusModeEnum.All;
}
}
public void SetPlantResource( PlantResource resource )
{
_resource = resource;
UpdateContents();
}
private void UpdateContents()
{
_cost.Text = _resource.Cost.ToString();
_icon.Texture = _resource.Preview;
_timer.WaitTime = _resource.ReloadTime;
}
private void OnPressed()
{
if (_timer.TimeLeft == 0)
{
LevelController.Instance.PlantField.SetPlant(this, _resource);
}
}
private void OnFocusExited()
{
LevelController.Instance.PlantField.SetPlant(null, null);
}
public void Recharge()
{
_recharging = true;
ReleaseFocus();
_timer.Start();
}
private void Timeout()
{
_recharging = false;
}
}