Boxswitch with state

This commit is contained in:
MaddoScientisto 2025-03-13 21:11:53 +01:00
commit 87645f2617
14 changed files with 278 additions and 37 deletions

View file

@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using Godot;
using Godot.Collections;
@ -9,6 +10,9 @@ 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;
[Signal]
public delegate void OnActivatedEventHandler(ActivationType activationType);
private AudioStreamPlayer2D _activationSound;
@ -23,17 +27,19 @@ public partial class Switch : Interactable
{
if (!MeetsRequirements()) return false;
_activationSound?.Play();
EmitSignal(SignalName.OnActivated, (int)activationType);
// Compatibility for old single system
bool success = ActivateTarget(Target);
bool success = ActivateTarget(Target, activationType);
return Targets.Aggregate(success, (current, target) => ActivateTarget(target) | success);
return Targets.Aggregate(success, (current, target) => ActivateTarget(target, activationType) | success);
}
private bool ActivateTarget(Node2D target)
private bool ActivateTarget(Node2D target, ActivationType activationType = ActivationType.Toggle)
{
if (target is not IActivable activable) return false;
activable?.Activate(ActivationType);
activable?.Activate(activationType);
return true;
}
}