cirnogodot/Scripts/Interactables/Switch.cs

39 lines
No EOL
1.1 KiB
C#

using System.Linq;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Interactables;
public partial class Switch : Interactable
{
[Export] public Node2D Target { get; set; }
[Export] public Array<Node2D> Targets { get; private set; } = [];
[Export] public ActivationType ActivationType { get; set; } = ActivationType.Toggle;
private AudioStreamPlayer2D _activationSound;
private readonly string _activationSoundName = "ActivationSound";
public override void _Ready()
{
_activationSound = GetNodeOrNull<AudioStreamPlayer2D>(_activationSoundName);
}
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
{
if (!MeetsRequirements()) return false;
_activationSound?.Play();
// Compatibility for old single system
bool success = ActivateTarget(Target);
return Targets.Aggregate(success, (current, target) => ActivateTarget(target) | success);
}
private bool ActivateTarget(Node2D target)
{
if (target is not IActivable activable) return false;
activable?.Activate(ActivationType);
return true;
}
}