cirnogodot/Scripts/Interactables/Switch.cs

55 lines
1.7 KiB
C#
Raw Permalink Normal View History

2025-03-13 21:11:53 +01:00
using System;
using System.Linq;
2025-02-24 10:24:12 +01:00
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Interactables;
public partial class Switch : Interactable
{
2025-02-15 14:33:44 +01:00
[Export] public Node2D Target { get; set; }
2025-03-09 21:58:25 +01:00
[Export] public Array<Node2D> Targets { get; private set; } = [];
2025-02-15 14:33:44 +01:00
[Export] public ActivationType ActivationType { get; set; } = ActivationType.Toggle;
2025-03-13 21:11:53 +01:00
[Signal]
public delegate void OnActivatedEventHandler(ActivationType activationType);
2025-03-01 23:46:25 +01:00
private AudioStreamPlayer2D _activationSound;
private readonly string _activationSoundName = "ActivationSound";
public override void _Ready()
{
_activationSound = GetNodeOrNull<AudioStreamPlayer2D>(_activationSoundName);
}
2025-03-09 21:58:25 +01:00
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
{
2025-04-24 16:40:51 +02:00
ActivationType activationTypeToUse;
if (activationType is ActivationType.Use)
{
activationTypeToUse = ActivationType;
}
else
{
activationTypeToUse = activationType;
}
2025-02-24 10:24:12 +01:00
if (!MeetsRequirements()) return false;
2025-03-01 23:46:25 +01:00
_activationSound?.Play();
2025-03-13 21:11:53 +01:00
2025-04-24 16:40:51 +02:00
EmitSignal(SignalName.OnActivated, (int)activationTypeToUse);
2025-03-13 21:11:53 +01:00
2025-02-24 10:24:12 +01:00
// Compatibility for old single system
2025-04-24 16:40:51 +02:00
bool success = ActivateTarget(Target, activationTypeToUse);
2025-02-24 10:24:12 +01:00
2025-04-24 16:40:51 +02:00
return Targets.Aggregate(success, (current, target) => ActivateTarget(target, activationTypeToUse) | success);
2025-02-24 10:24:12 +01:00
}
2025-03-13 21:11:53 +01:00
private bool ActivateTarget(Node2D target, ActivationType activationType = ActivationType.Toggle)
2025-02-24 10:24:12 +01:00
{
if (target is not IActivable activable) return false;
2025-03-13 21:11:53 +01:00
activable?.Activate(activationType);
2025-02-24 10:24:12 +01:00
return true;
}
}