2025-11-04 10:06:01 +01:00
|
|
|
using System.Linq;
|
2025-08-19 23:02:14 +02:00
|
|
|
using Babushka.scripts.CSharp.Common.Services;
|
2025-05-12 00:16:13 +02:00
|
|
|
using Godot;
|
|
|
|
|
|
|
|
|
|
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
|
|
|
|
|
2025-11-25 13:33:53 +01:00
|
|
|
public partial class InteractionArea2D : Node2D
|
2025-05-12 00:16:13 +02:00
|
|
|
{
|
2025-11-23 21:02:40 +01:00
|
|
|
[ExportGroup("Settings")]
|
2025-05-12 00:16:13 +02:00
|
|
|
[Export] private Area2D _area;
|
|
|
|
|
[Export] private Label _label;
|
2025-07-31 20:36:17 +02:00
|
|
|
[Export] private bool _active = true;
|
2025-07-27 13:17:34 +02:00
|
|
|
[Export] private bool _useOutline = true;
|
|
|
|
|
[Export] private ShaderMaterial _outlineMaterial;
|
2025-11-04 17:20:15 +01:00
|
|
|
[Export] private CanvasItem[] _spritesToOutline = [];
|
2025-05-12 00:16:13 +02:00
|
|
|
[Export] private bool _showLabel = true;
|
2025-05-24 21:47:25 +02:00
|
|
|
[Export] private int _id = -1; // TODO: remove
|
2025-05-12 00:16:13 +02:00
|
|
|
|
2025-11-04 10:06:01 +01:00
|
|
|
private Material[] _backupMaterials;
|
|
|
|
|
|
2025-05-20 20:42:32 +02:00
|
|
|
[Signal] public delegate void InteractedToolEventHandler(int id); // TODO: remove
|
2025-11-04 10:06:01 +01:00
|
|
|
|
2025-05-18 11:44:33 +02:00
|
|
|
[Signal] public delegate void InteractedEventHandler();
|
2025-05-12 00:16:13 +02:00
|
|
|
|
2025-07-31 20:36:17 +02:00
|
|
|
public bool IsActive
|
|
|
|
|
{
|
|
|
|
|
get => _active;
|
2025-11-04 10:06:01 +01:00
|
|
|
set => _active = value;
|
2025-07-31 20:36:17 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-20 00:48:12 +02:00
|
|
|
public void SetActiveInverse(bool active)
|
|
|
|
|
{
|
|
|
|
|
IsActive = !active;
|
|
|
|
|
}
|
2025-07-09 23:50:43 +02:00
|
|
|
|
2025-07-27 13:17:34 +02:00
|
|
|
public override void _Ready()
|
|
|
|
|
{
|
2025-11-04 10:06:01 +01:00
|
|
|
if (_useOutline)
|
2025-07-31 20:36:17 +02:00
|
|
|
{
|
2025-11-04 17:20:15 +01:00
|
|
|
_backupMaterials = _spritesToOutline.Select(s => s.Material).ToArray();
|
2025-07-31 20:36:17 +02:00
|
|
|
}
|
2025-07-27 13:17:34 +02:00
|
|
|
}
|
2025-11-04 10:06:01 +01:00
|
|
|
|
2025-05-12 00:16:13 +02:00
|
|
|
public void OnPlayerEntered(Node2D player)
|
|
|
|
|
{
|
2025-08-19 23:02:14 +02:00
|
|
|
if (!_active || !InputService.Instance.InputEnabled)
|
2025-07-09 23:50:43 +02:00
|
|
|
return;
|
2025-11-04 10:06:01 +01:00
|
|
|
|
|
|
|
|
if (_showLabel)
|
2025-05-12 00:16:13 +02:00
|
|
|
_label.Show();
|
2025-11-04 10:06:01 +01:00
|
|
|
|
|
|
|
|
if (!_useOutline)
|
2025-07-27 13:17:34 +02:00
|
|
|
return;
|
2025-11-04 10:06:01 +01:00
|
|
|
|
|
|
|
|
foreach (var sprite in _spritesToOutline)
|
|
|
|
|
{
|
|
|
|
|
sprite.Material = _outlineMaterial;
|
|
|
|
|
}
|
2025-05-12 00:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnPlayerExited(Node2D player)
|
|
|
|
|
{
|
|
|
|
|
_label.Hide();
|
2025-07-27 13:17:34 +02:00
|
|
|
|
2025-11-04 10:06:01 +01:00
|
|
|
if (!_useOutline)
|
2025-07-27 13:17:34 +02:00
|
|
|
return;
|
2025-11-04 10:06:01 +01:00
|
|
|
|
|
|
|
|
for (var i = 0; i < _spritesToOutline.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var sprite = _spritesToOutline[i];
|
|
|
|
|
sprite.Material = _backupMaterials[i];
|
|
|
|
|
}
|
2025-05-12 00:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void _Input(InputEvent @event)
|
|
|
|
|
{
|
2025-11-04 10:06:01 +01:00
|
|
|
if (!_active || !InputService.Instance.InputEnabled)
|
2025-07-09 23:50:43 +02:00
|
|
|
return;
|
2025-11-04 10:06:01 +01:00
|
|
|
|
2025-07-31 20:36:17 +02:00
|
|
|
if (@event.IsAction("interact") && @event.IsPressed())
|
2025-05-12 00:16:13 +02:00
|
|
|
{
|
2025-08-17 22:50:28 +02:00
|
|
|
TryInteract();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (@event.IsActionPressed("click") && @event.IsPressed())
|
|
|
|
|
{
|
|
|
|
|
TryInteract();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TryInteract()
|
|
|
|
|
{
|
2025-10-24 15:13:35 +02:00
|
|
|
if (_area.HasOverlappingAreas())
|
2025-08-17 22:50:28 +02:00
|
|
|
{
|
|
|
|
|
_label.Hide();
|
2025-11-04 10:06:01 +01:00
|
|
|
|
|
|
|
|
if (_useOutline)
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < _spritesToOutline.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var sprite = _spritesToOutline[i];
|
|
|
|
|
sprite.Material = _backupMaterials[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-23 22:25:59 +01:00
|
|
|
Interact();
|
2025-05-17 16:38:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 22:25:59 +01:00
|
|
|
private void Interact()
|
|
|
|
|
{
|
|
|
|
|
EmitSignal(SignalName.InteractedTool, _id);
|
|
|
|
|
EmitSignal(SignalName.Interacted);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-20 20:42:32 +02:00
|
|
|
public void SetSpriteActiveState(bool success, int id) // TODO: remove
|
2025-05-17 16:38:32 +02:00
|
|
|
{
|
2025-11-04 10:06:01 +01:00
|
|
|
if (!_active)
|
2025-07-09 23:50:43 +02:00
|
|
|
return;
|
2025-05-12 00:16:13 +02:00
|
|
|
}
|
2025-07-09 23:50:43 +02:00
|
|
|
|
|
|
|
|
public void ToggleActive()
|
|
|
|
|
{
|
2025-07-31 20:36:17 +02:00
|
|
|
_active = !_active;
|
2025-07-09 23:50:43 +02:00
|
|
|
_label.Hide();
|
|
|
|
|
}
|
2025-11-23 21:02:40 +01:00
|
|
|
|
2025-05-12 00:16:13 +02:00
|
|
|
}
|