refactor: movement system - MovementPreset, decouple abilities, fix timing
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
@@ -10,15 +9,7 @@ namespace Mr.BrickAdventures.scripts.components;
|
||||
public partial class PlayerController : CharacterBody2D
|
||||
{
|
||||
[Export] private Node MovementAbilitiesContainer { get; set; }
|
||||
|
||||
[ExportGroup("Movement Ability Scenes")]
|
||||
[Export] public PackedScene GroundMovementScene { get; set; }
|
||||
[Export] public PackedScene JumpMovementScene { get; set; }
|
||||
[Export] public PackedScene GravityScene { get; set; }
|
||||
[Export] public PackedScene OneWayPlatformScene { get; set; }
|
||||
[Export] public PackedScene SpaceshipMovementScene { get; set; }
|
||||
[Export] public PackedScene WallJumpScene { get; set; }
|
||||
[Export] public PackedScene GridMovementScene { get; set; }
|
||||
[Export] public MovementPreset DefaultPreset { get; set; }
|
||||
|
||||
[Signal] public delegate void JumpInitiatedEventHandler();
|
||||
[Signal] public delegate void MovementAbilitiesChangedEventHandler();
|
||||
@@ -48,16 +39,10 @@ public partial class PlayerController : CharacterBody2D
|
||||
}
|
||||
|
||||
_inputHandler = GetNode<PlayerInputHandler>("PlayerInputHandler");
|
||||
foreach (var child in MovementAbilitiesContainer.GetChildren())
|
||||
{
|
||||
if (child is MovementAbility ability)
|
||||
{
|
||||
ability.Initialize(this);
|
||||
_abilities.Add(ability);
|
||||
}
|
||||
}
|
||||
|
||||
_ = ConnectJumpAndGravityAbilities();
|
||||
if (DefaultPreset != null)
|
||||
ApplyPreset(DefaultPreset);
|
||||
|
||||
EmitSignalMovementAbilitiesChanged();
|
||||
EventBus.EmitPlayerSpawned(this);
|
||||
}
|
||||
@@ -81,6 +66,19 @@ public partial class PlayerController : CharacterBody2D
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
public void ApplyPreset(MovementPreset preset)
|
||||
{
|
||||
if (preset == null) return;
|
||||
ClearMovementAbilities();
|
||||
Velocity = Vector2.Zero;
|
||||
foreach (var scene in preset.Abilities)
|
||||
{
|
||||
if (scene != null)
|
||||
AddAbility(scene.Instantiate<MovementAbility>());
|
||||
}
|
||||
EmitSignalMovementAbilitiesChanged();
|
||||
}
|
||||
|
||||
public void AddAbility(MovementAbility ability)
|
||||
{
|
||||
MovementAbilitiesContainer.AddChild(ability);
|
||||
@@ -110,46 +108,4 @@ public partial class PlayerController : CharacterBody2D
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetPlatformMovement()
|
||||
{
|
||||
ClearMovementAbilities();
|
||||
|
||||
if (GroundMovementScene != null) AddAbility(GroundMovementScene.Instantiate<MovementAbility>());
|
||||
if (JumpMovementScene != null) AddAbility(JumpMovementScene.Instantiate<MovementAbility>());
|
||||
if (GravityScene != null) AddAbility(GravityScene.Instantiate<MovementAbility>());
|
||||
if (OneWayPlatformScene != null) AddAbility(OneWayPlatformScene.Instantiate<MovementAbility>());
|
||||
|
||||
_ = ConnectJumpAndGravityAbilities();
|
||||
EmitSignalMovementAbilitiesChanged();
|
||||
}
|
||||
|
||||
public void SetSpaceshipMovement()
|
||||
{
|
||||
ClearMovementAbilities();
|
||||
|
||||
if (SpaceshipMovementScene != null) AddAbility(SpaceshipMovementScene.Instantiate<MovementAbility>());
|
||||
EmitSignalMovementAbilitiesChanged();
|
||||
}
|
||||
|
||||
public void SetGridMovement()
|
||||
{
|
||||
ClearMovementAbilities();
|
||||
if (GridMovementScene != null) AddAbility(GridMovementScene.Instantiate<MovementAbility>());
|
||||
EmitSignalMovementAbilitiesChanged();
|
||||
}
|
||||
|
||||
private async Task ConnectJumpAndGravityAbilities()
|
||||
{
|
||||
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
|
||||
|
||||
var jumpAbility = _abilities.OfType<VariableJumpAbility>().FirstOrDefault();
|
||||
var gravityAbility = _abilities.OfType<GravityAbility>().FirstOrDefault();
|
||||
|
||||
if (jumpAbility != null && gravityAbility != null)
|
||||
{
|
||||
gravityAbility.AscendGravity = jumpAbility.AscendGravity;
|
||||
gravityAbility.DescendGravity = jumpAbility.DescendGravity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user