mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
Moving FSM Elevator
This commit is contained in:
parent
eef9bc71c1
commit
8b378abef3
22 changed files with 349 additions and 16 deletions
12
Scripts/Activables/Elevator.cs
Normal file
12
Scripts/Activables/Elevator.cs
Normal 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;
|
||||
|
||||
}
|
||||
1
Scripts/Activables/Elevator.cs.uid
Normal file
1
Scripts/Activables/Elevator.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bpey64n21hyhu
|
||||
47
Scripts/Components/FSM/Elevator/Ascending.cs
Normal file
47
Scripts/Components/FSM/Elevator/Ascending.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/Elevator/Ascending.cs.uid
Normal file
1
Scripts/Components/FSM/Elevator/Ascending.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bgmehqn7g7fvj
|
||||
47
Scripts/Components/FSM/Elevator/Bottom.cs
Normal file
47
Scripts/Components/FSM/Elevator/Bottom.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/Elevator/Bottom.cs.uid
Normal file
1
Scripts/Components/FSM/Elevator/Bottom.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://ju87yydywmxi
|
||||
47
Scripts/Components/FSM/Elevator/Descending.cs
Normal file
47
Scripts/Components/FSM/Elevator/Descending.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/Elevator/Descending.cs.uid
Normal file
1
Scripts/Components/FSM/Elevator/Descending.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://72sfdklqrc6d
|
||||
34
Scripts/Components/FSM/Elevator/ElevatorProxy.cs
Normal file
34
Scripts/Components/FSM/Elevator/ElevatorProxy.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/Elevator/ElevatorProxy.cs.uid
Normal file
1
Scripts/Components/FSM/Elevator/ElevatorProxy.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://b6jp6fhmwmobv
|
||||
20
Scripts/Components/FSM/Elevator/Init.cs
Normal file
20
Scripts/Components/FSM/Elevator/Init.cs
Normal 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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/Elevator/Init.cs.uid
Normal file
1
Scripts/Components/FSM/Elevator/Init.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cuxk8rrhvpe2c
|
||||
46
Scripts/Components/FSM/Elevator/Top.cs
Normal file
46
Scripts/Components/FSM/Elevator/Top.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/Elevator/Top.cs.uid
Normal file
1
Scripts/Components/FSM/Elevator/Top.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cxxu5ed50wiev
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
11
Scripts/Enums/ElevatorState.cs
Normal file
11
Scripts/Enums/ElevatorState.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
namespace Cirno.Scripts.Enums;
|
||||
|
||||
public enum ElevatorState
|
||||
{
|
||||
Init,
|
||||
Bottom,
|
||||
Top,
|
||||
Ascending,
|
||||
Descending,
|
||||
Disabled,
|
||||
}
|
||||
1
Scripts/Enums/ElevatorState.cs.uid
Normal file
1
Scripts/Enums/ElevatorState.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dio7irf4in3us
|
||||
Loading…
Add table
Add a link
Reference in a new issue