Elevators move players

This commit is contained in:
Marco 2025-03-06 11:34:45 +01:00
commit 2e0ad40f33
8 changed files with 156 additions and 93 deletions

View file

@ -10,15 +10,18 @@
[ext_resource type="Script" uid="uid://72sfdklqrc6d" path="res://Scripts/Components/FSM/Elevator/Descending.cs" id="8_flmvm"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_3lpp4"]
size = Vector2(32, 32)
size = Vector2(20, 19)
[node name="Elevator" type="Area2D"]
collision_layer = 0
collision_mask = 2
script = ExtResource("1_xv5vg")
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("1_0xq5m")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(0, -0.5)
shape = SubResource("RectangleShape2D_3lpp4")
[node name="StateMachine" type="Node2D" parent="."]
@ -40,3 +43,6 @@ script = ExtResource("7_r4wj7")
script = ExtResource("8_flmvm")
[node name="Disabled" type="Node2D" parent="StateMachine"]
[connection signal="area_entered" from="." to="." method="_on_area_entered"]
[connection signal="area_exited" from="." to="." method="_on_area_exited"]

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -8,40 +8,10 @@ using GTweensGodot.Extensions;
namespace Cirno.Scripts.Components.FSM.Elevator;
public partial class Ascending : BaseState<ElevatorState, ElevatorProxy>
public partial class Ascending : ElevatorMovementState
{
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);
}
protected override Vector2 StartingPosition => MainObject.Bottom;
protected override Vector2 EndingPosition => MainObject.Top;
protected override ElevatorState EndState => ElevatorState.Top;
}

View file

@ -8,40 +8,11 @@ using GTweensGodot.Extensions;
namespace Cirno.Scripts.Components.FSM.Elevator;
public partial class Descending : BaseState<ElevatorState, ElevatorProxy>
public partial class Descending : ElevatorMovementState
{
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);
}
protected override Vector2 StartingPosition => MainObject.Top;
protected override Vector2 EndingPosition => MainObject.Bottom;
protected override ElevatorState EndState => ElevatorState.Bottom;
}

View file

@ -0,0 +1,72 @@
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 abstract partial class ElevatorMovementState : BaseState<ElevatorState, ElevatorProxy>
{
protected GTween Tween;
protected Node2D OldPlayerParent;
protected abstract Vector2 StartingPosition { get; }
protected abstract Vector2 EndingPosition { get; }
protected abstract ElevatorState EndState { get; }
protected CharacterBody2D PlayerBody => MainObject.CachedPlayer?.StateMachine.MainObject;
public override void EnterState()
{
Tween?.Kill();
MainObject.SetPosition(StartingPosition);
Move();
}
public override void ExitState()
{
Tween?.Kill();
RestorePlayerParent();
}
private void RestorePlayerParent()
{
if (PlayerBody is null) return;
PlayerBody.Reparent(OldPlayerParent);
OldPlayerParent = null;
MainObject.CachedPlayer?.StateMachine.SetState(PlayerState.Active);
}
private void CatchPlayer()
{
if (PlayerBody is null) return;
OldPlayerParent = PlayerBody.GetParent<Node2D>();
MainObject.CachedPlayer.StateMachine.SetState(PlayerState.Cutscene);
PlayerBody.Reparent(MainObject);
}
private void Move()
{
// Grab player if in range
CatchPlayer();
//Ascend
_ = PlayAnimation();
}
private async Task PlayAnimation()
{
Tween = GTweenSequenceBuilder.New()
.Append(MainObject.TweenPosition(EndingPosition, MainObject.MovementTime))
.Build();
await Tween.PlayAsync(CancellationToken.None);
StateMachine.SetState(EndState);
}
}

View file

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

View file

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Cirno.Scripts.Enums;
using Godot;
@ -26,9 +27,27 @@ public partial class ElevatorProxy : Area2D, IActivable
// {
//
// }
public InteractionController CachedPlayer { get; private set; }
public void Activate(ActivationType activationType = ActivationType.Toggle)
{
EmitSignal(SignalName.Activated, (int)activationType);
}
private void _on_area_entered(Area2D area)
{
if (area is InteractionController player)
{
CachedPlayer = player;
}
}
private void _on_area_exited(Area2D area)
{
if (area is InteractionController player)
{
CachedPlayer = null;
}
}
}