cirnogodot/Scripts/Interactables/Switch3D.cs

111 lines
3.3 KiB
C#
Raw Normal View History

2025-06-30 10:02:37 +02:00
using System;
using System.Linq;
using Cirno.Scripts.Utils;
2025-06-13 17:46:44 +02:00
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Interactables;
2025-06-18 12:18:45 +02:00
[Tool]
2025-06-13 17:46:44 +02:00
public partial class Switch3D : Interactable3D
{
2025-07-09 12:00:23 +02:00
[Export] public string TargetGroup { get; protected set; }
2025-06-13 17:46:44 +02:00
[Export] public Node Target { get; set; }
[Export] public Array<Node> Targets { get; private set; } = [];
[Export] public ActivationType ActivationType { get; set; } = ActivationType.Toggle;
2025-07-08 14:31:12 +02:00
2025-06-13 17:46:44 +02:00
[Signal]
public delegate void OnActivatedEventHandler(ActivationType activationType);
private AudioStreamPlayer _activationSound;
2025-07-08 14:31:12 +02:00
private AudioStreamPlayer _denySound;
2025-06-13 17:46:44 +02:00
private readonly string _activationSoundName = "ActivationSound";
2025-07-08 14:31:12 +02:00
private readonly string _denySoundName = "ActivationSound";
2025-06-13 17:46:44 +02:00
public override void _Ready()
{
2025-06-18 12:18:45 +02:00
if (Engine.IsEditorHint()) return;
2025-07-08 14:31:12 +02:00
2025-06-13 17:46:44 +02:00
_activationSound = GetNodeOrNull<AudioStreamPlayer>(_activationSoundName);
2025-07-08 14:31:12 +02:00
_denySound = GetNodeOrNull<AudioStreamPlayer>(_denySoundName);
2025-06-13 17:46:44 +02:00
}
2025-07-08 14:31:12 +02:00
2025-07-09 12:00:23 +02:00
public virtual void _func_godot_apply_properties(Dictionary<string, Variant> props)
2025-06-18 12:18:45 +02:00
{
2025-07-09 12:00:23 +02:00
TargetGroup = props["target"].AsString();
2025-06-25 16:52:31 +02:00
if (props.TryGetValue("key", out var prop))
{
2025-07-09 12:00:23 +02:00
RequirementKeys = [prop.AsString()];
2025-06-25 16:52:31 +02:00
}
2025-07-08 14:31:12 +02:00
2025-06-30 10:02:37 +02:00
if (props.TryGetValue("activationtype", out var type))
{
2025-07-09 12:00:23 +02:00
var t = Enum.TryParse(type.AsString(), true, out ActivationType activationType);
2025-06-30 10:02:37 +02:00
if (t)
{
ActivationType = activationType;
}
}
2025-06-18 12:18:45 +02:00
//TargetFunc = props["targetfunc"];
//TargetName = props["targetname"];
}
2025-07-08 14:31:12 +02:00
2025-06-13 17:46:44 +02:00
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
{
var activationTypeToUse = activationType is ActivationType.Use ? ActivationType : activationType;
2025-07-08 14:31:12 +02:00
if (!MeetsRequirements())
{
_denySound?.Play();
return false;
}
2025-06-13 17:46:44 +02:00
EmitSignal(SignalName.OnActivated, (int)activationTypeToUse);
2025-07-08 14:31:12 +02:00
2025-06-13 17:46:44 +02:00
// Compatibility for old single system
bool success = ActivateTarget(Target, activationTypeToUse);
2025-06-18 12:18:45 +02:00
if (!string.IsNullOrWhiteSpace(TargetGroup))
{
2025-07-09 12:00:23 +02:00
success |= ActivationHelper.UseTargets(this, TargetGroup, activationType);
2025-06-18 12:18:45 +02:00
}
2025-07-08 14:31:12 +02:00
var result = Targets.Aggregate(success,
2025-07-09 12:00:23 +02:00
(current, target) => current | ActivateTarget(target, activationTypeToUse));
2025-07-08 14:31:12 +02:00
if (result)
{
_activationSound?.Play();
}
else
{
_denySound?.Play();
}
return result;
2025-06-13 17:46:44 +02:00
}
private bool ActivateTarget(Node target, ActivationType activationType = ActivationType.Toggle)
{
if (target is not IActivable activable) return false;
activable?.Activate(activationType);
return true;
}
2025-07-08 14:31:12 +02:00
2025-06-30 10:02:37 +02:00
// private void UseTargets(Node activator, string target)
// {
// GD.Print($"Trying to use targets called: {target}");
// var targetList = activator.GetTree().GetNodesInGroup(target);
// foreach (var t in targetList)
// {
// //string f;
// GD.Print($"Trying to use {t.Name}");
// if (t is IActivable activable)
// {
// GD.Print($"Activating {t.Name}");
// activable.Toggle();
// }
// }
// }
2025-06-13 17:46:44 +02:00
}