Moving FSM Elevator

This commit is contained in:
Marco 2025-03-06 10:27:06 +01:00
commit 8b378abef3
22 changed files with 349 additions and 16 deletions

View file

@ -0,0 +1,12 @@
using Cirno.Scripts.Components.FSM;
using Cirno.Scripts.Components.FSM.Elevator;
using Cirno.Scripts.Enums;
using Godot;
namespace Cirno.Scripts.Activables;
public partial class Elevator : StateMachineBase<ElevatorState, ElevatorProxy>
{
[Export] public override ElevatorState InitialState { get; protected set; } = ElevatorState.Init;
}

View file

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

View file

@ -0,0 +1,47 @@
using System.Threading;
using System.Threading.Tasks;
using Cirno.Scripts.Enums;
using Godot;
using GTweens.Builders;
using GTweens.Tweens;
using GTweensGodot.Extensions;
namespace Cirno.Scripts.Components.FSM.Elevator;
public partial class Ascending : BaseState<ElevatorState, ElevatorProxy>
{
public override ElevatorState StateId => ElevatorState.Ascending;
private GTween _tween;
public override void EnterState()
{
_tween?.Kill();
MainObject.SetPosition(MainObject.Bottom);
Ascend();
}
public override void ExitState()
{
_tween?.Kill();
}
private void Ascend()
{
// Grab player if in range
//Ascend
_ = RisingAnimation();
}
private async Task RisingAnimation()
{
_tween = GTweenSequenceBuilder.New()
.Append(MainObject.TweenPosition(MainObject.Top, MainObject.MovementTime))
.Build();
await _tween.PlayAsync(CancellationToken.None);
StateMachine.SetState(ElevatorState.Top);
}
}

View file

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

View file

@ -0,0 +1,47 @@
using System;
using Cirno.Scripts.Enums;
using Godot;
namespace Cirno.Scripts.Components.FSM.Elevator;
public partial class Bottom : BaseState<ElevatorState, ElevatorProxy>
{
public override ElevatorState StateId => ElevatorState.Bottom;
public override void EnterState()
{
MainObject.SetPosition(MainObject.Bottom);
MainObject.Activated += ElevatorActivated;
}
private void ElevatorActivated(ActivationType type)
{
switch (type)
{
case ActivationType.Use:
case ActivationType.Toggle:
MoveToTop();
break;
case ActivationType.Enable:
break;
case ActivationType.Disable:
break;
case ActivationType.Destroy:
break;
case ActivationType.Open:
break;
case ActivationType.Close:
break;
}
}
private void MoveToTop()
{
StateMachine.SetState(ElevatorState.Ascending);
}
public override void ExitState()
{
MainObject.Activated -= ElevatorActivated;
}
}

View file

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

View file

@ -0,0 +1,47 @@
using System.Threading;
using System.Threading.Tasks;
using Cirno.Scripts.Enums;
using Godot;
using GTweens.Builders;
using GTweens.Tweens;
using GTweensGodot.Extensions;
namespace Cirno.Scripts.Components.FSM.Elevator;
public partial class Descending : BaseState<ElevatorState, ElevatorProxy>
{
public override ElevatorState StateId => ElevatorState.Descending;
private GTween _tween;
public override void EnterState()
{
_tween?.Kill();
MainObject.SetPosition(MainObject.Top);
Descend();
}
public override void ExitState()
{
_tween?.Kill();
}
private void Descend()
{
// Grab player if in range
//Ascend
_ = DescendingAnimation();
}
private async Task DescendingAnimation()
{
_tween = GTweenSequenceBuilder.New()
.Append(MainObject.TweenPosition(MainObject.Bottom, MainObject.MovementTime))
.Build();
await _tween.PlayAsync(CancellationToken.None);
StateMachine.SetState(ElevatorState.Bottom);
}
}

View file

@ -0,0 +1 @@
uid://72sfdklqrc6d

View file

@ -0,0 +1,34 @@
using System;
using Cirno.Scripts.Enums;
using Godot;
namespace Cirno.Scripts.Components.FSM.Elevator;
public partial class ElevatorProxy : Area2D, IActivable
{
[Export]
public ElevatorState StartingState { get; protected set; } = ElevatorState.Bottom;
[Export]
public float MovementTime { get; protected set; } = 1.0f;
[Export]
public Path2D ElevatorPath { get; protected set; }
[Signal] public delegate void ActivatedEventHandler(ActivationType type);
public Vector2 Top => ElevatorPath.Curve.GetPointPosition(0);
public Vector2 Bottom => ElevatorPath.Curve.GetPointPosition(1);
public IStateMachine<ElevatorState,ElevatorProxy> StateMachine { get; set; }
// public void SetPosition(Vector2 position)
// {
//
// }
public void Activate(ActivationType activationType = ActivationType.Toggle)
{
EmitSignal(SignalName.Activated, (int)activationType);
}
}

View file

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

View file

@ -0,0 +1,20 @@
using Cirno.Scripts.Enums;
using Godot;
namespace Cirno.Scripts.Components.FSM.Elevator;
public partial class Init : BaseState<ElevatorState, ElevatorProxy>
{
public override ElevatorState StateId => ElevatorState.Init;
public override void EnterState()
{
MainObject.StateMachine = StateMachine;
StateMachine.SetState(MainObject.StartingState);
}
public override void ExitState()
{
}
}

View file

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

View file

@ -0,0 +1,46 @@
using Cirno.Scripts.Enums;
using Godot;
namespace Cirno.Scripts.Components.FSM.Elevator;
public partial class Top : BaseState<ElevatorState, ElevatorProxy>
{
public override ElevatorState StateId => ElevatorState.Top;
public override void EnterState()
{
MainObject.SetPosition(MainObject.ElevatorPath.Curve.GetPointPosition(0));
MainObject.Activated += ElevatorActivated;
}
private void ElevatorActivated(ActivationType type)
{
switch (type)
{
case ActivationType.Use:
case ActivationType.Toggle:
MoveToBottom();
break;
case ActivationType.Enable:
break;
case ActivationType.Disable:
break;
case ActivationType.Destroy:
break;
case ActivationType.Open:
break;
case ActivationType.Close:
break;
}
}
private void MoveToBottom()
{
StateMachine.SetState(ElevatorState.Descending);
}
public override void ExitState()
{
MainObject.Activated -= ElevatorActivated;
}
}

View file

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

View file

@ -28,7 +28,6 @@ public abstract partial class StateMachineBase<TKey, TType> : Node2D, IStateMach
state.Init(this);
}
}
GD.Print("FSM Ready");
SetState(InitialState);
}
@ -41,9 +40,7 @@ public abstract partial class StateMachineBase<TKey, TType> : Node2D, IStateMach
CurrentStateIndex = stateId;
CurrentState.EnterState();
}
public override void _Process(double delta)
{
if (CurrentStateIndex is null) return;

View file

@ -0,0 +1,11 @@
namespace Cirno.Scripts.Enums;
public enum ElevatorState
{
Init,
Bottom,
Top,
Ascending,
Descending,
Disabled,
}

View file

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