Files
Babushka/scripts/CSharp/Common/Farming/FieldBehaviour2D.cs
T

109 lines
3.3 KiB
C#
Raw Normal View History

2025-07-06 23:48:01 +02:00
using System;
using Babushka.scripts.CSharp.Common.CharacterControls;
using Babushka.scripts.CSharp.Common.Inventory;
2025-05-12 00:16:13 +02:00
using Godot;
namespace Babushka.scripts.CSharp.Common.Farming;
[GlobalClass]
public partial class FieldBehaviour2D : Sprite2D
{
2025-07-06 23:48:01 +02:00
[Export] private Sprite2D _fieldSprite;
[Export] private Sprite2D _maskSprite;
[Export] private Texture2D[] _maskTexture;
2025-05-12 00:16:13 +02:00
[Export] private Texture2D Tilled;
[Export] private Texture2D Watered;
2025-05-18 12:32:56 +02:00
[Export] public FieldState FieldState = FieldState.Tilled;
[Export] public InteractionArea2D PlantingInteraction;
2025-08-17 21:57:57 +02:00
[Export] public Node2D PlantingPlaceholder;
2025-09-09 23:57:55 +02:00
[Export] public ItemRepository ItemRepository;
2025-05-12 00:16:13 +02:00
public Vector2 FieldPosition;
public override void _Ready()
{
2025-05-18 12:32:56 +02:00
UpdateFieldState(FieldState);
2025-07-06 23:48:01 +02:00
int randomIndex = new Random().Next(0, _maskTexture.Length);
_maskSprite.Texture = _maskTexture[randomIndex];
2025-05-12 00:16:13 +02:00
base._Ready();
}
2025-05-18 12:32:56 +02:00
public void UpdateFieldState(FieldState state)
{
switch (state)
{
case FieldState.Empty:
FieldState = FieldState.Empty;
PlantingInteraction.IsActive = false;
2025-05-18 12:32:56 +02:00
break;
case FieldState.Tilled:
FieldState = FieldState.Tilled;
2025-07-06 23:48:01 +02:00
_fieldSprite.Texture = Tilled;
PlantingInteraction.IsActive = true;
2025-05-18 12:32:56 +02:00
break;
case FieldState.Watered:
FieldState = FieldState.Watered;
2025-07-06 23:48:01 +02:00
_fieldSprite.Texture = Watered;
PlantingInteraction.IsActive = true;
2025-05-18 12:32:56 +02:00
break;
case FieldState.Planted:
FieldState = FieldState.Planted;
2025-08-17 21:57:57 +02:00
_fieldSprite.Texture = Tilled;
PlantingInteraction.IsActive = false;
2025-05-18 12:32:56 +02:00
break;
default:
FieldState = FieldState.NotFound;
break;
}
}
2025-05-12 00:16:13 +02:00
public void Water()
{
2025-07-11 03:55:36 +02:00
UpdateFieldState(FieldState.Watered);
2025-05-12 00:16:13 +02:00
}
/// <summary>
2025-05-18 12:32:56 +02:00
/// Called when the player enters the field's interaction area and presses <E>.
2025-05-12 00:16:13 +02:00
/// </summary>
public void Farm()
{
if (TryPlant())
2025-05-12 00:16:13 +02:00
{
UpdateFieldState(FieldState.Planted);
}
}
private bool TryPlant()
{
bool success = false;
int currentSlotIndex = InventoryManager.Instance.CurrentSelectedSlotIndex;
ItemInstance? item = InventoryManager.Instance.playerInventory.Slots[currentSlotIndex].itemInstance;
2025-09-17 15:07:17 +02:00
if (item == null || PlantingPlaceholder.GetChildCount() > 0 || item.amount == 0)
return success;
2025-09-10 15:06:49 +02:00
2025-09-09 23:57:55 +02:00
string prefabPath = ItemRepository.TryGetPrefabPath(item.blueprint);
2025-09-09 23:57:55 +02:00
if (prefabPath != null)
{
2025-09-09 23:57:55 +02:00
PackedScene prefab = ResourceLoader.Load<PackedScene>(prefabPath, nameof(PackedScene));
Node2D plant2d = prefab.Instantiate<Node2D>();
PlantingPlaceholder.AddChild(plant2d);
plant2d.GlobalPosition = PlantingPlaceholder.GlobalPosition;
PlantBehaviour2D? plantBehaviour = plant2d as PlantBehaviour2D;
if (plantBehaviour != null)
{
plantBehaviour.Field = this;
}
2025-09-17 15:07:17 +02:00
InventoryManager.Instance.playerInventory.RemoveItem(currentSlotIndex);
2025-09-09 23:57:55 +02:00
success = true;
2025-05-12 00:16:13 +02:00
}
return success;
2025-05-12 00:16:13 +02:00
}
2025-05-12 00:16:13 +02:00
}