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.
93 lines
2.7 KiB
93 lines
2.7 KiB
using Babushka.scripts.CSharp.Common.CharacterControls;
|
|
using Babushka.scripts.CSharp.Common.Savegame;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Farming;
|
|
|
|
/// <summary>
|
|
/// Enables a preset field in the scene sothat it can be used for farming.
|
|
/// </summary>
|
|
public partial class FieldActivator : Node, ISaveable
|
|
{
|
|
[Export] private FieldBehaviour2D _field;
|
|
[Export] private InteractionArea2D _activatorArea;
|
|
[Export] private Node _saveIdHolder;
|
|
|
|
private bool _used = false;
|
|
private bool _rakeInHand;
|
|
|
|
[Signal] public delegate void FieldCreatedEventHandler();
|
|
|
|
public override void _Ready()
|
|
{
|
|
LoadFromSaveData();
|
|
ToggleInteractionArea();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Activates the fieldbehaviour node and sets it to the tilled state.
|
|
/// </summary>
|
|
public void ActivateField()
|
|
{
|
|
if (!_used && _rakeInHand)
|
|
{
|
|
_field.Visible = true;
|
|
_field.UpdateFieldState(FieldState.Tilled);
|
|
EmitSignal(SignalName.FieldCreated, _field);
|
|
_used = true;
|
|
ToggleInteractionArea();
|
|
UpdateSaveData();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reacts to changes in the inventory.
|
|
/// If setup correctly, the field activator interactable should only trigger when using the rake.
|
|
/// </summary>
|
|
/// <param name="activated"></param>
|
|
public void RakeActivated(bool activated)
|
|
{
|
|
if (_used || ProcessMode == ProcessModeEnum.Disabled)
|
|
return;
|
|
_rakeInHand = activated;
|
|
ToggleInteractionArea();
|
|
}
|
|
|
|
private void ToggleInteractionArea()
|
|
{
|
|
_activatorArea.IsActive = !_used && _rakeInHand;
|
|
}
|
|
|
|
public void UpdateSaveData()
|
|
{
|
|
var payloadData = new Dictionary<string, Variant>
|
|
{
|
|
{ "field_activator_used", _used }
|
|
};
|
|
|
|
// Building a unique id from the top node's save id and a qualifier to make it new.
|
|
string parent_id = _saveIdHolder.GetMeta("SaveID").AsString();
|
|
string id = $"{parent_id}_field_activator";
|
|
SavegameService.AppendDataToSave( id, payloadData);
|
|
}
|
|
|
|
public void LoadFromSaveData()
|
|
{
|
|
string parent_id = _saveIdHolder.GetMeta("SaveID").AsString();
|
|
string id = $"{parent_id}_field_activator";
|
|
|
|
Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
|
|
if (save.Count > 0)
|
|
{
|
|
if (save.TryGetValue("field_activator_used", out Variant usedVar))
|
|
{
|
|
_used = usedVar.AsBool();
|
|
}
|
|
else
|
|
{
|
|
_used = false;
|
|
}
|
|
}
|
|
}
|
|
} |