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.
124 lines
2.8 KiB
124 lines
2.8 KiB
using System;
|
|
using System.Linq;
|
|
using Babushka.scripts.CSharp.Common.Services;
|
|
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
|
|
|
public partial class InteractionArea2D : Node2D
|
|
{
|
|
|
|
[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;
|
|
|
|
[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);
|
|
}
|
|
}
|
|
|
|
public void SetSpriteActiveState(bool success, int id) // TODO: remove
|
|
{
|
|
if (!_active)
|
|
return;
|
|
}
|
|
|
|
public void ToggleActive()
|
|
{
|
|
_active = !_active;
|
|
_label.Hide();
|
|
}
|
|
} |