mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-08 06:35:55 +00:00
Boxswitch with state
This commit is contained in:
parent
036a36a80f
commit
87645f2617
14 changed files with 278 additions and 37 deletions
46
Scripts/Interactables/Modules/SwitchSpriteChanger.cs
Normal file
46
Scripts/Interactables/Modules/SwitchSpriteChanger.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
1
Scripts/Interactables/Modules/SwitchSpriteChanger.cs.uid
Normal file
1
Scripts/Interactables/Modules/SwitchSpriteChanger.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://b5sanpv5j7wyd
|
||||
62
Scripts/Interactables/StateSwitch.cs
Normal file
62
Scripts/Interactables/StateSwitch.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
1
Scripts/Interactables/StateSwitch.cs.uid
Normal file
1
Scripts/Interactables/StateSwitch.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://booisvod4sc6s
|
||||
|
|
@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue