ChooseYourSeeds menu

This commit is contained in:
Фёдор Веселов 2024-09-22 20:37:00 +05:00
commit c9dd4cf175
23 changed files with 471 additions and 95 deletions

View file

@ -0,0 +1,16 @@
using Godot;
using System;
public partial class LevelGUIElements : Control
{
public static LevelGUIElements Instance;
[Export]
public HBoxContainer SeedpacketsHotbar;
[Export]
public TextureRect SunCounter;
public override void _Ready()
{
Instance = this;
}
}

View file

@ -1,67 +0,0 @@
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;
}
}

View file

@ -0,0 +1,28 @@
using Godot;
using System;
public partial class GridLoader : GridContainer
{
private const string PLANT_RESOURCE_PATH = "res://resources/plants/";
private PackedScene _plantCard;
public override void _Ready()
{
_plantCard = ResourceLoader.Load<PackedScene>("res://scenes/gui/seedpacket.tscn");
string[] files = DirAccess.GetFilesAt(PLANT_RESOURCE_PATH);
foreach(var file in files)
{
if(ResourceLoader.Exists(PLANT_RESOURCE_PATH+file))
{
Seedpacket slot = _plantCard.Instantiate<Seedpacket>();
AddChild(slot);
slot.SetPlantResource(ResourceLoader.Load<PlantResource>(PLANT_RESOURCE_PATH+file));
slot.SetHandler(new ChoosableHandler(slot));
}
}
}
}

View file

@ -0,0 +1,13 @@
using Godot;
public partial class LevelRunButton : Button
{
[Export] private AnimationPlayer _player;
public override void _Pressed()
{
RuntimeLevelData.Instance.SetLevelState(RuntimeLevelData.LevelStates.Game);
GetTree().Paused = false;
_player.Play("Hide");
}
}

View file

@ -0,0 +1,24 @@
public class ChoosableHandler : SeedpacketHandler, ISeedpacketPress
{
public ChoosableHandler(Seedpacket owner) : base(owner)
{
}
public void Pressed()
{
_owner.disablePacket = true;
var hotbarSeedpacket = Seedpacket.Prefab.Instantiate<Seedpacket>();
LevelGUIElements.Instance.SeedpacketsHotbar.AddChild(hotbarSeedpacket);
hotbarSeedpacket.SetPlantResource(_owner.GetPlantResource());
var pregameHandler = new HotbarPregameHandler(hotbarSeedpacket);
hotbarSeedpacket.SetHandler(pregameHandler);
pregameHandler.Clicked += OnHotbarClicked;
}
public void OnHotbarClicked()
{
_owner.disablePacket = false;
}
}

View file

@ -0,0 +1,22 @@
using System;
using Godot;
public class HotbarHandler : SeedpacketHandler, ISeedpacketPress, ISeedpacketProcess, ISeedpacketUnfocus
{
public HotbarHandler(Seedpacket owner) : base(owner)
{
}
public void Pressed()
{
PlantField.Instance.SetPlant(_owner,_owner.GetPlantResource());
}
public void Process()
{
_owner.disablePacket = RuntimeLevelData.Instance.SunCount < _owner.GetPlantResource().Cost;
}
public void OnUnfocused()
{
PlantField.Instance.ResetPlant();
}
}

View file

@ -0,0 +1,29 @@
using System;
using Godot;
public class HotbarPregameHandler : SeedpacketHandler, ISeedpacketPress
{
public HotbarPregameHandler(Seedpacket owner) : base(owner)
{
RuntimeLevelData.Instance.OnLevelStateChanged += OnLevelStateChanged;
}
public event Action Clicked;
public void Pressed()
{
if (Clicked != null) Clicked();
_owner.QueueFree();
}
public void OnLevelStateChanged(RuntimeLevelData.LevelStates state)
{
if (state == RuntimeLevelData.LevelStates.Game)
{
_owner.SetHandler(new HotbarHandler(_owner));
}
else if (state != RuntimeLevelData.LevelStates.ChooseYourSeeds)
{
_owner.disablePacket = true;
}
}
}

View file

@ -0,0 +1,4 @@
public interface ISeedpacketPress
{
public void Pressed();
}

View file

@ -0,0 +1,4 @@
public interface ISeedpacketProcess
{
public void Process();
}

View file

@ -0,0 +1,4 @@
public interface ISeedpacketUnfocus
{
public void OnUnfocused();
}

View file

@ -0,0 +1,76 @@
using Godot;
using System;
public partial class Seedpacket : TextureButton
{
private const string PATH_TO_PACKED_SCENE = "res://scenes/gui/seedpacket.tscn";
private PlantResource _resource;
private Label _cost;
private TextureRect _icon;
private Timer _timer;
private SeedpacketHandler _handler;
public bool disablePacket = false;
public static PackedScene Prefab { get; private set; }
// Node overrides
public override void _Ready()
{
if (_resource != null)
UpdateContents();
if (Prefab == null)
{
Prefab = ResourceLoader.Load<PackedScene>(PATH_TO_PACKED_SCENE);
}
_cost = GetNode<Label>("Cost");
_icon = GetNode<TextureRect>("PlantPreviewContainer/Preview");
_timer = GetNode<Timer>("RechargeTimer");
}
public override void _Process(double delta)
{
Disabled = disablePacket || _timer.TimeLeft > 0;
if (_handler is ISeedpacketProcess processHandler) processHandler.Process();
}
public void SetPlantResource( PlantResource resource )
{
_resource = resource;
UpdateContents();
}
public PlantResource GetPlantResource()
{
return _resource;
}
public void SetHandler(SeedpacketHandler newHandler)
{
disablePacket = false;
_handler = newHandler;
}
protected virtual void UpdateContents()
{
_cost.Text = _resource.Cost.ToString();
_icon.Texture = _resource.Preview;
_timer.WaitTime = _resource.ReloadTime;
}
public override void _Pressed()
{
if (_handler is ISeedpacketPress pressHandler)
pressHandler.Pressed();
}
public void Recharge()
{
_timer.Start();
ReleaseFocus();
}
public void OnUnfocused()
{
if (_handler is ISeedpacketUnfocus unfocusHandler) unfocusHandler.OnUnfocused();
}
}

View file

@ -0,0 +1,9 @@
public class SeedpacketHandler
{
protected Seedpacket _owner;
public SeedpacketHandler(Seedpacket owner)
{
_owner = owner;
}
}