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.
210 lines
6.7 KiB
210 lines
6.7 KiB
using System;
|
|
using Babushka.scripts.CSharp.Common.CharacterControls;
|
|
using Babushka.scripts.CSharp.Common.Inventory;
|
|
using Babushka.scripts.CSharp.Common.Savegame;
|
|
using Babushka.scripts.CSharp.Low_Code.Events;
|
|
using Babushka.scripts.CSharp.Low_Code.Variables;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Farming;
|
|
|
|
/// <summary>
|
|
/// Defines the behaviour of the field, i.e. interactions, states and effects.
|
|
/// </summary>
|
|
[GlobalClass]
|
|
public partial class FieldBehaviour2D : Sprite2D
|
|
{
|
|
[ExportGroup("Persistence")]
|
|
[Export] public string SaveId = "";
|
|
[Export] private VariableNode _fieldIndex;
|
|
[Export] public VariableResource _sceneKeyProvider;
|
|
[Export] public FieldState FieldState = FieldState.Tilled;
|
|
|
|
[ExportGroup("Field Visuals")]
|
|
[Export] private Sprite2D _fieldSprite;
|
|
[Export] private Sprite2D _maskSprite;
|
|
[Export] private Sprite2D _outlineSprite;
|
|
[Export] private Texture2D[] _maskOutlineTextures;
|
|
[Export] private Texture2D[] _maskTexture;
|
|
[Export] private Texture2D Tilled;
|
|
[Export] private Texture2D Watered;
|
|
|
|
[ExportGroup("Field Interactions")]
|
|
[Export] public InteractionArea2D PlantingInteraction;
|
|
[Export] public InteractionArea2D FieldInteractionArea;
|
|
|
|
[ExportGroup("Configuration")]
|
|
[Export] public Node2D PlantingPlaceholder;
|
|
[Export] public ItemRepository ItemRepository;
|
|
[Export] private CpuParticles2D _wateringParticles;
|
|
[Export] private EventResource _wateringEvent;
|
|
|
|
private bool _seedsActive;
|
|
private bool _wateringCanActive;
|
|
|
|
private bool _canPlant;
|
|
private bool _canWater;
|
|
|
|
private PlantBehaviour2D? _currentPlant;
|
|
|
|
[Signal] public delegate void PlantedEventHandler();
|
|
|
|
private void UpdateInteractionArea()
|
|
{
|
|
// fieldstate == tilled / watered && samen im Inventar
|
|
_canPlant = (FieldState == FieldState.Tilled || FieldState == FieldState.Watered) && _seedsActive;
|
|
// fieldstate == tilled && watering can ausgewählt
|
|
_canWater = (FieldState == FieldState.Tilled || FieldState == FieldState.Planted) && _wateringCanActive;
|
|
FieldInteractionArea.IsActive = _canPlant || _canWater;
|
|
}
|
|
|
|
public void ActivatedSeedInInventory(bool activated)
|
|
{
|
|
_seedsActive = activated;
|
|
UpdateInteractionArea();
|
|
}
|
|
|
|
public void ActivateWateringCanInInventory(bool activated)
|
|
{
|
|
_wateringCanActive = activated;
|
|
UpdateInteractionArea();
|
|
}
|
|
|
|
public override void _Ready()
|
|
{
|
|
|
|
if(PlantingPlaceholder.GetChildCount() > 0)
|
|
_currentPlant = PlantingPlaceholder.GetChild<PlantBehaviour2D>(0);
|
|
UpdateFieldState(FieldState);
|
|
|
|
FieldService.Instance.TryAddEntry(_sceneKeyProvider.Payload.AsString(),_fieldIndex.Payload.AsInt32(), this);
|
|
|
|
int randomIndex = new Random().Next(0, _maskTexture.Length);
|
|
_maskSprite.Texture = _maskTexture[randomIndex];
|
|
_outlineSprite.Texture = _maskOutlineTextures[randomIndex];
|
|
base._Ready();
|
|
}
|
|
|
|
public void UpdateFieldState(FieldState state)
|
|
{
|
|
switch (state)
|
|
{
|
|
case FieldState.Empty:
|
|
FieldState = FieldState.Empty;
|
|
PlantingInteraction.IsActive = false;
|
|
break;
|
|
case FieldState.Tilled:
|
|
FieldState = FieldState.Tilled;
|
|
_fieldSprite.Texture = Tilled;
|
|
PlantingInteraction.IsActive = true;
|
|
break;
|
|
case FieldState.Watered:
|
|
FieldState = FieldState.Watered;
|
|
_fieldSprite.Texture = Watered;
|
|
PlantingInteraction.IsActive = true;
|
|
break;
|
|
case FieldState.Planted:
|
|
FieldState = FieldState.Planted;
|
|
_fieldSprite.Texture = Tilled;
|
|
PlantingInteraction.IsActive = false;
|
|
break;
|
|
default:
|
|
FieldState = FieldState.NotFound;
|
|
break;
|
|
}
|
|
UpdateInteractionArea();
|
|
UpdateSaveData();
|
|
}
|
|
|
|
|
|
public void Water()
|
|
{
|
|
if (WateringCanState.GetFillState() > 0)
|
|
{
|
|
UpdateFieldState(FieldState.Watered);
|
|
_wateringParticles.Emitting = true;
|
|
WateringCanState.Water();
|
|
_wateringEvent.Raise();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when the player enters the field's interaction area and presses <E> or clicks.
|
|
/// </summary>
|
|
public void Farm()
|
|
{
|
|
if (_canPlant && TryPlant())
|
|
{
|
|
EmitSignal(SignalName.Planted);
|
|
UpdateFieldState(FieldState.Planted);
|
|
}
|
|
|
|
if (_canWater)
|
|
{
|
|
Water();
|
|
}
|
|
}
|
|
|
|
private bool TryPlant()
|
|
{
|
|
bool success = false;
|
|
int currentSlotIndex = InventoryManager.Instance.CurrentSelectedSlotIndex;
|
|
ItemInstance? item = InventoryManager.Instance.playerInventory.Slots[currentSlotIndex].itemInstance;
|
|
|
|
if (item == null || PlantingPlaceholder.GetChildCount() > 0 || item.amount == 0)
|
|
return success;
|
|
|
|
string plantPrefabPath = ItemRepository.TryGetPrefabPath(item.blueprint);
|
|
|
|
if (!string.IsNullOrEmpty(plantPrefabPath))
|
|
{
|
|
PackedScene prefab = ResourceLoader.Load<PackedScene>(plantPrefabPath, nameof(PackedScene));
|
|
Node2D plant2d = prefab.Instantiate<Node2D>();
|
|
PlantingPlaceholder.AddChild(plant2d);
|
|
plant2d.GlobalPosition = PlantingPlaceholder.GlobalPosition;
|
|
_currentPlant = plant2d as PlantBehaviour2D;
|
|
|
|
if (_currentPlant != null)
|
|
{
|
|
_currentPlant.Field = this;
|
|
}
|
|
|
|
InventoryManager.Instance.playerInventory.RemoveItem(currentSlotIndex);
|
|
success = true;
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
|
|
public void UpdateSaveData()
|
|
{
|
|
var saveData = new SaveData();
|
|
|
|
saveData.SceneName = _sceneKeyProvider.Payload.AsString();
|
|
saveData.Id = SaveId + _fieldIndex.Payload.AsString();
|
|
var payloadData = new Dictionary<string, Variant>
|
|
{
|
|
{ "field_state", (int)FieldState }
|
|
};
|
|
|
|
if (_currentPlant != null)
|
|
{
|
|
payloadData.Add(
|
|
"plant_data", new Dictionary<string, Variant>()
|
|
{
|
|
{ "prefab_path", _currentPlant.PrefabPath },
|
|
{ "plant_state", (int)_currentPlant.State },
|
|
{ "plant_days_growing", _currentPlant.DaysGrowing }
|
|
}
|
|
);
|
|
}
|
|
|
|
saveData.JsonPayload = Json.Stringify(payloadData, indent: "\t");
|
|
|
|
SavegameService.AppendSave(saveData);
|
|
}
|
|
|
|
}
|