using Cirno.Scripts.Components.Actors; using Cirno.Scripts.Components.Actors._3D; using Cirno.Scripts.Utils; using Godot; using GodotPlugins.Game; namespace Cirno.Scripts.Components.FSM._3DPlayer; public partial class IsoMovementModule : ModuleBase { [Export] public IsoPlayerStorageModule PlayerStorage { get; private set; } [Export] private InputProvider _inputProvider; [Export] public PlayerHitboxSpriteProvider3D HitboxSpriteProvider { get; private set; } [Export] public int Speed { get; set; } = 45; [Export] public int StrafeSpeed { get; set; } = 35; [Export] public float Acceleration = 8f; [Export] public float Deceleration = 8f; [Export] public float Gravity = -9.8f; [Export] public float FallSpeed = 20f; private bool _isStrafing; private float _accelerationPerSecond; private float _decelerationPerSecond; public int MovementSpeed => _isStrafing ? StrafeSpeed : Speed; private IStateMachine _stateMachine; private CharacterBody3D MainObject => _stateMachine.MainObject; public override void EnterState(PlayerState state) { _accelerationPerSecond = Speed / Acceleration; _decelerationPerSecond = Speed / Deceleration; } public override void ExitState(PlayerState state) { } public override void Init(IStateMachine machine) { _stateMachine = machine; } public override void Process(double delta) { var movementInput = _inputProvider.GetMovementInput(); _isStrafing = _inputProvider.GetStrafePressed(); var rightStickInput = _inputProvider.GetAimInput().Normalized(); // Update Facing Direction // if (!_isStrafing) // { if (rightStickInput.Length() > 0.1f) // If the right stick is moved { PlayerStorage.FacingDirection = rightStickInput; PlayerStorage.AimingDirection = rightStickInput; } else if (movementInput != Vector2.Zero) // Fall back to movement direction { PlayerStorage.FacingDirection = movementInput; PlayerStorage.AimingDirection = rightStickInput; } // } var rotatedMovementDirection = movementInput.Rotated(Mathf.DegToRad(-45f)); //PlayerStorage.MovementDirection = new Vector3(rotatedMovementDirection.X, 0, rotatedMovementDirection.Y); PlayerStorage.MovementDirection = rotatedMovementDirection; HitboxSpriteProvider.SetVisibility(_isStrafing); } public override void PhysicsProcess(double delta) { var frameVelocity = MainObject.Velocity.ToVector2(); var frameVelocityY = MainObject.Velocity.Y; if (_isStrafing) { // Instant movement at strafe speed frameVelocity = PlayerStorage.MovementDirection * StrafeSpeed; } else { Vector2 targetVelocity = PlayerStorage.MovementDirection * Speed; if (PlayerStorage.MovementDirection != Vector2.Zero) { frameVelocity = frameVelocity.MoveToward(targetVelocity, Acceleration * (float)delta); } else { frameVelocity = frameVelocity.MoveToward(Vector2.Zero, Deceleration * (float)delta); } } //MainObject.Velocity += _movementDirection * MovementSpeed; var velocityY = Mathf.Clamp(frameVelocityY + Gravity * (float)delta, -FallSpeed, FallSpeed); //frameVelocity.Y = velocityY; // Debug moonjump if (Input.IsKeyLabelPressed(Key.Z)) { //velocityY -= (float)(100 * delta); velocityY = 10; } MainObject.Velocity = frameVelocity.ToVector3(velocityY); MainObject.MoveAndSlide(); } }