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

@ -0,0 +1,46 @@
using System;
using Cirno.Scripts.Enums;
using Godot;
namespace Cirno.Scripts.Interactables.Modules;
public partial class SwitchSpriteChanger : AnimatedSprite2D
{
[Export]
public StateSwitch Switch { get; private set; }
[Export] public StringName OnAnimationName { get; private set; } = "On";
[Export] public StringName OffAnimationName { get; private set; } = "Off";
[Export] public StringName DestroyedAnimationName { get; private set; } = "Destroyed";
[Export] public StringName DisabledAnimationName { get; private set; } = "Disabled";
public override void _Ready()
{
Switch.OnActivated += SwitchOnActivated;
UpdateSprite();
}
private void SwitchOnActivated(ActivationType activationType)
{
UpdateSprite();
}
private void UpdateSprite()
{
switch (Switch.CurrentState)
{
case SwitchState.On:
this.Play(OnAnimationName);
break;
case SwitchState.Off:
this.Play(OffAnimationName);
break;
case SwitchState.Destroyed:
this.Play(DestroyedAnimationName);
break;
case SwitchState.Disabled:
this.Play(DisabledAnimationName);
break;
}
}
}

View file

@ -0,0 +1 @@
uid://b5sanpv5j7wyd

View file

@ -0,0 +1,62 @@
using System;
using Cirno.Scripts.Enums;
using Godot;
namespace Cirno.Scripts.Interactables;
public partial class StateSwitch : Switch
{
[Export]
public SwitchState StartingState { get; private set; } = SwitchState.Off;
public SwitchState CurrentState { get; private set; }
public override void _Ready()
{
base._Ready();
CurrentState = StartingState;
}
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
{
switch (activationType)
{
case ActivationType.Use:
case ActivationType.Toggle:
switch (CurrentState)
{
case SwitchState.On:
return TriggerDisable();
case SwitchState.Off:
return TriggerEnable();
}
break;
case ActivationType.Close:
case ActivationType.Enable:
return TriggerEnable();
case ActivationType.Open:
case ActivationType.Disable:
return TriggerDisable();
case ActivationType.Destroy:
this.CurrentState = SwitchState.Destroyed;
break;
}
return false;
}
protected bool TriggerDisable()
{
CurrentState = SwitchState.Off;
//EmitSignal(Switch.SignalName.OnActivated, (int)ActivationType.Disable);
return base.Activate(ActivationType.Disable);
}
protected bool TriggerEnable()
{
CurrentState = SwitchState.On;
//EmitSignal(Switch.SignalName.OnActivated, (int)ActivationType.Enable);
return base.Activate(ActivationType.Enable);
}
}

View file

@ -0,0 +1 @@
uid://booisvod4sc6s

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;
}
}