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.
155 lines
3.8 KiB
155 lines
3.8 KiB
using System.Linq;
|
|
using Babushka.scripts.CSharp.Common.Savegame;
|
|
using Babushka.scripts.CSharp.Common.Services;
|
|
using Babushka.scripts.CSharp.Low_Code.Variables;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
|
|
|
public partial class InteractionArea2D : Node2D, ISaveable
|
|
{
|
|
[ExportGroup("Persistence")]
|
|
[Export] public string SaveId = "";
|
|
[Export] private VariableNode _sceneID;
|
|
[Export] public VariableResource _sceneKeyProvider;
|
|
|
|
[ExportGroup("Settings")]
|
|
[Export] private Area2D _area;
|
|
[Export] private Label _label;
|
|
[Export] private bool _active = true;
|
|
[Export] private bool _useOutline = true;
|
|
[Export] private ShaderMaterial _outlineMaterial;
|
|
[Export] private CanvasItem[] _spritesToOutline = [];
|
|
[Export] private bool _showLabel = true;
|
|
[Export] private int _id = -1; // TODO: remove
|
|
|
|
private Material[] _backupMaterials;
|
|
private int _interactionCounter;
|
|
|
|
[Signal] public delegate void InteractedToolEventHandler(int id); // TODO: remove
|
|
|
|
[Signal] public delegate void InteractedEventHandler();
|
|
|
|
public bool IsActive
|
|
{
|
|
get => _active;
|
|
set => _active = value;
|
|
}
|
|
|
|
public void SetActiveInverse(bool active)
|
|
{
|
|
IsActive = !active;
|
|
}
|
|
|
|
public override void _Ready()
|
|
{
|
|
if (_useOutline)
|
|
{
|
|
_backupMaterials = _spritesToOutline.Select(s => s.Material).ToArray();
|
|
}
|
|
}
|
|
|
|
|
|
public void OnPlayerEntered(Node2D player)
|
|
{
|
|
if (!_active || !InputService.Instance.InputEnabled)
|
|
return;
|
|
|
|
if (_showLabel)
|
|
_label.Show();
|
|
|
|
if (!_useOutline)
|
|
return;
|
|
|
|
foreach (var sprite in _spritesToOutline)
|
|
{
|
|
sprite.Material = _outlineMaterial;
|
|
}
|
|
}
|
|
|
|
public void OnPlayerExited(Node2D player)
|
|
{
|
|
_label.Hide();
|
|
|
|
if (!_useOutline)
|
|
return;
|
|
|
|
for (var i = 0; i < _spritesToOutline.Length; i++)
|
|
{
|
|
var sprite = _spritesToOutline[i];
|
|
sprite.Material = _backupMaterials[i];
|
|
}
|
|
}
|
|
|
|
public override void _Input(InputEvent @event)
|
|
{
|
|
if (!_active || !InputService.Instance.InputEnabled)
|
|
return;
|
|
|
|
if (@event.IsAction("interact") && @event.IsPressed())
|
|
{
|
|
TryInteract();
|
|
}
|
|
|
|
if (@event.IsActionPressed("click") && @event.IsPressed())
|
|
{
|
|
TryInteract();
|
|
}
|
|
}
|
|
|
|
private void TryInteract()
|
|
{
|
|
if (_area.HasOverlappingAreas())
|
|
{
|
|
_label.Hide();
|
|
|
|
if (_useOutline)
|
|
{
|
|
for (var i = 0; i < _spritesToOutline.Length; i++)
|
|
{
|
|
var sprite = _spritesToOutline[i];
|
|
sprite.Material = _backupMaterials[i];
|
|
}
|
|
}
|
|
|
|
EmitSignal(SignalName.InteractedTool, _id);
|
|
EmitSignal(SignalName.Interacted);
|
|
_interactionCounter++;
|
|
}
|
|
}
|
|
|
|
public void SetSpriteActiveState(bool success, int id) // TODO: remove
|
|
{
|
|
if (!_active)
|
|
return;
|
|
}
|
|
|
|
public void ToggleActive()
|
|
{
|
|
_active = !_active;
|
|
_label.Hide();
|
|
}
|
|
|
|
#region SAVE AND LOAD
|
|
|
|
public void UpdateSaveData()
|
|
{
|
|
var payloadData = new Dictionary<string, Variant>
|
|
{
|
|
{ "interaction counter", _interactionCounter }
|
|
};
|
|
|
|
SavegameService.AppendDataToSave(_sceneKeyProvider.Payload.AsString(), SaveId + _sceneID.Payload.AsString(), payloadData);
|
|
}
|
|
|
|
public void LoadFromSaveData()
|
|
{
|
|
var sceneName = _sceneKeyProvider.Payload.AsString();
|
|
var id = SaveId + _sceneID.Payload.AsString();
|
|
|
|
Dictionary<string, Variant> save = SavegameService.GetSaveData(sceneName, id);
|
|
}
|
|
|
|
#endregion
|
|
} |