Conveyor belts

This commit is contained in:
Marco 2025-03-14 15:13:00 +01:00
commit 362ea29852
16 changed files with 371 additions and 15 deletions

View file

@ -0,0 +1,79 @@
using Cirno.Scripts.Components.FSM;
using Godot;
namespace Cirno.Scripts.Components.Actors;
public partial class ConveyorBeltMover : PlayerArea2DModule
{
public bool Enabled { get; private set; } = false;
private Vector2 _velocity = Vector2.Zero;
private IStateMachine<PlayerState, CharacterBody2D> _machine;
public override void Init(IStateMachine<PlayerState, CharacterBody2D> machine)
{
base.Init(machine);
_machine = machine;
}
private void OnBodyEntered(Node2D body)
{
if (!Enabled) return;
if (body is not TileMapLayer tileMap) return;
GD.Print($"Entered {body.Name}");
// How do I get the actual coords of the cell at the collision point??
var localTilemapCoords = tileMap.ToLocal(this.GlobalPosition);
GD.Print($"Local tilemap coords: {localTilemapCoords}");
var coords = tileMap.LocalToMap(localTilemapCoords);
GD.Print($"Tilemap coords: {coords}");
var td = tileMap.GetCellTileData(coords);
if (td is null) return;
//tileMap.TileSet.physics
var vel = td.GetConstantLinearVelocity(2);
GD.Print($"velocity: {vel}");
var layerData = td.GetCustomDataByLayerId(0).As<Vector2>();
// I can do whatever I want with this
GD.Print($"Layer Data: {layerData}");
_velocity = vel;
}
private void OnBodyExited(Node2D body)
{
if (body is not TileMapLayer tileMap) return;
GD.Print($"Exited {body.Name}");
_velocity = Vector2.Zero;
}
public override void EnterState(PlayerState state)
{
Enabled = true;
GD.Print("Enabled conveyor");
}
public override void ExitState(PlayerState state)
{
Enabled = false;
GD.Print("Disabled conveyor");
}
public override void Process(double delta)
{
}
public override void PhysicsProcess(double delta)
{
if (Enabled && _velocity.Length() != 0)
{
CharacterBody.Velocity += _velocity;
//GD.Print($"Applying velocity {_velocity} {CharacterBody.Velocity} ");
}
}
}

View file

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

View file

@ -56,11 +56,19 @@ public abstract partial class BaseState<TKey, TType> : Node2D, IState<TKey, TTyp
public virtual void ProcessState(double delta)
{
_modules.ForEach(module => module.Process(delta));
foreach (var module in _modules)
{
module.Process(delta);
}
//_modules.ForEach(module => module.Process(delta));
}
public virtual void PhysicsProcessState(double delta)
{
_modules.ForEach(module => module.PhysicsProcess(delta));
foreach (var module in _modules)
{
module.PhysicsProcess(delta);
}
//_modules.ForEach(module => module.PhysicsProcess(delta));
}
}

View file

@ -102,14 +102,24 @@ public partial class Active : PlayerStateBase
}
public override void PhysicsProcessState(double delta)
{
MainObject.Velocity = _movementDirection * MovementSpeed;
{
// Reset at start of frame
MainObject.Velocity = Vector2.Zero;
// Process modules
base.PhysicsProcessState(delta);
MainObject.Velocity += _movementDirection * MovementSpeed;
MainObject.MoveAndSlide();
}
public override void ProcessState(double delta)
{
base.ProcessState(delta);
_movementDirection = _inputProvider.GetMovementInput().Normalized();
_isStrafing = _inputProvider.GetStrafePressed();
@ -152,6 +162,8 @@ public partial class Active : PlayerStateBase
//CallDeferred(MethodName.PauseDeferred);
PauseDeferred();
}
}
private void PauseDeferred()