You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Babushka/scripts/CSharp/Common/Farming/PlantBehaviour2D.cs

86 lines
2.7 KiB

using System;
using Babushka.scripts.CSharp.Common.CharacterControls;
using Babushka.scripts.CSharp.Common.Inventory;
using Godot;
namespace Babushka.scripts.CSharp.Common.Farming;
public enum PlantState
{
None = 0,
Planted = 1,
SmallPlant = 2,
BigPlant = 3,
Ready = 4
}
/// <summary>
/// Determines the behaviour of a plant in Babushka.
/// </summary>
public partial class PlantBehaviour2D : Node2D
{
[Export] private Sprite2D[] _seeds;
[Export] private Sprite2D[] _smallPlants;
[Export] private Sprite2D[] _bigPlants;
[Export] private Sprite2D[] _readyPlants;
[Export] private PlantState _state = PlantState.None;
[Export] private FieldBehaviour2D _field;
[Export] private ItemOnGround2D _harvestablePlant;
private Sprite2D _currentPlantSprite = null;
/// <summary>
/// Transitions the plant to its next growth stage.
/// </summary>
public void Grow()
{
if (_field.FieldState != FieldState.Watered)
return;
GetTree().CallGroup("PlantGrowing", Player2D.MethodName.PlayFarmingAnimation);
switch (_state)
{
case PlantState.None:
_state = PlantState.Planted;
_currentPlantSprite = GetRandomSprite(_seeds);
_currentPlantSprite.Visible = true;
break;
case PlantState.Planted:
_state = PlantState.SmallPlant;
_currentPlantSprite.Visible = false;
_currentPlantSprite = GetRandomSprite(_smallPlants);
_currentPlantSprite.Visible = true;
break;
case PlantState.SmallPlant:
_state = PlantState.BigPlant;
_currentPlantSprite.Visible = false;
_currentPlantSprite = GetRandomSprite(_bigPlants);
_currentPlantSprite.Visible = true;
break;
case PlantState.BigPlant:
_state = PlantState.Ready;
_currentPlantSprite.Visible = false;
_currentPlantSprite = GetRandomSprite(_readyPlants);
_harvestablePlant.IsActive = true;
_currentPlantSprite.Visible = true;
break;
case PlantState.Ready:
_state = PlantState.None;
_currentPlantSprite.Visible = false;
_currentPlantSprite = null;
break;
default:
break;
}
_field.UpdateFieldState(FieldState.Tilled);
}
private Sprite2D GetRandomSprite(Sprite2D[] sprites)
{
Random rand = new Random();
return sprites[rand.Next(sprites.Length)];
}
}