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.
96 lines
2.4 KiB
96 lines
2.4 KiB
using Babushka.scripts.CSharp.Common.Savegame;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Temp;
|
|
|
|
/// <summary>
|
|
/// Temporary Duck behaviour to make sure we can use them in the first showcase
|
|
/// </summary>
|
|
public partial class MVPDuck : Node2D, ISaveable
|
|
{
|
|
[ExportGroup("Animation")]
|
|
[Export] private Node2D _penTarget;
|
|
[Export] private int _transferDelayMs;
|
|
[Export] private AnimationPlayer _animationPlayer;
|
|
[Export] private string _flapAnimationName = "flapFlap";
|
|
|
|
private bool _collected;
|
|
|
|
[Signal] public delegate void DuckCollectedEventHandler();
|
|
|
|
public override void _Ready()
|
|
{
|
|
LoadFromSaveData();
|
|
}
|
|
|
|
public void TransferToTargetAfterDelay()
|
|
{
|
|
if (!_collected)
|
|
{
|
|
MoveAfterDelay();
|
|
PlayAnimation();
|
|
_collected = true;
|
|
}
|
|
}
|
|
|
|
private void PlayAnimation()
|
|
{
|
|
_animationPlayer.CurrentAnimation = _flapAnimationName;
|
|
_animationPlayer.Play();
|
|
}
|
|
|
|
public async void MoveAfterDelay()
|
|
{
|
|
await ToSignal(GetTree().CreateTimer(1.0f), "timeout");
|
|
if(!_penTarget.Equals(null))
|
|
Position = _penTarget.GlobalPosition;
|
|
EmitSignal(SignalName.DuckCollected);
|
|
UpdateSaveData();
|
|
}
|
|
|
|
#region SAVE AND LOAD
|
|
|
|
/// <summary>
|
|
// Saves duck position.
|
|
/// </summary>
|
|
public void UpdateSaveData()
|
|
{
|
|
var payloadData = new Dictionary<string, Variant>
|
|
{
|
|
{ "globalPositionX", GlobalPosition.X },
|
|
{ "globalPositionY", GlobalPosition.Y },
|
|
};
|
|
|
|
string id = GetMeta("SaveID").AsString();
|
|
SavegameService.AppendDataToSave( id, payloadData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads duck position.
|
|
/// </summary>
|
|
public void LoadFromSaveData()
|
|
{
|
|
string id = GetMeta("SaveID").AsString();
|
|
|
|
Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
|
|
if (save.Count > 0)
|
|
{
|
|
float xPos = 0;
|
|
float yPos = 0;
|
|
if (save.TryGetValue("globalPositionX", out Variant xPosVar))
|
|
{
|
|
xPos = xPosVar.AsSingle();
|
|
}
|
|
|
|
if (save.TryGetValue("globalPositionY", out Variant yPosVar))
|
|
{
|
|
yPos = yPosVar.AsSingle();
|
|
}
|
|
|
|
GlobalPosition = new Vector2(xPos, yPos);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
} |