mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:45:33 +00:00
143 lines
3.9 KiB
C#
143 lines
3.9 KiB
C#
using System;
|
|
using Cirno.Scripts.Components.Actors;
|
|
using Godot;
|
|
|
|
namespace Cirno.Scripts.Components.FSM.Player;
|
|
|
|
public partial class Active : PlayerFSMState
|
|
{
|
|
private Vector2 _movementDirection { get; set; }
|
|
public Vector2 FacingDirection { get; private set; } = Vector2.Down;
|
|
|
|
[Export]
|
|
public int Speed { get; set; } = 45;
|
|
|
|
[Export]
|
|
public int StrafeSpeed { get; set; } = 35;
|
|
|
|
[ExportCategory("Providers")]
|
|
[Export]
|
|
private PlayerWeaponProvider _weaponProvider;
|
|
[Export]
|
|
private PlayerAnimationProvider _animationProvider;
|
|
[Export]
|
|
private PlayerCrosshairProvider _crosshairProvider;
|
|
[Export] private PlayerHitboxSpriteProvider _hitboxSpriteProvider;
|
|
|
|
[Export]
|
|
private InputProvider _inputProvider;
|
|
|
|
[Export] private PlayerDamageReceiver _damageReceiver;
|
|
[Export] private ActivationProvider _activationProvider;
|
|
|
|
private bool _isStrafing;
|
|
|
|
public int MovementSpeed => _isStrafing ? StrafeSpeed : Speed;
|
|
|
|
private PlayerStateMachine _player;
|
|
|
|
public override void Init(ActorStateMachine stateMachine)
|
|
{
|
|
base.Init(stateMachine);
|
|
|
|
_player = (PlayerStateMachine)stateMachine;
|
|
|
|
_damageReceiver.Death += () =>
|
|
{
|
|
ChangeState(PlayerState.Dead);
|
|
};
|
|
|
|
_damageReceiver.HealthChanged += (value, maxValue) =>
|
|
{
|
|
_animationProvider.Blink();
|
|
};
|
|
|
|
_damageReceiver.ShieldChanged += (value, maxValue) =>
|
|
{
|
|
_animationProvider.PlayShieldAnimation();
|
|
};
|
|
|
|
_damageReceiver.Init();
|
|
|
|
_activationProvider.Init();
|
|
|
|
//_weaponProvider = stateMachine.GetNode<PlayerWeaponProvider>("WeaponProvider");
|
|
//_animationProvider = stateMachine.GetNode<PlayerAnimationProvider>("AnimationProvider");
|
|
|
|
_weaponProvider.Init(stateMachine);
|
|
}
|
|
|
|
public override void EnterState()
|
|
{
|
|
// enable sprite
|
|
// enable crosshair
|
|
_crosshairProvider.Show();
|
|
|
|
_damageReceiver.Enabled = true;
|
|
_activationProvider.Enabled = true;
|
|
}
|
|
|
|
public override void ExitState()
|
|
{
|
|
_animationProvider.SetAnimation(Vector2.Zero);
|
|
_crosshairProvider.Hide();
|
|
_hitboxSpriteProvider.Hide();
|
|
|
|
_damageReceiver.Enabled = false;
|
|
_activationProvider.Enabled = false;
|
|
}
|
|
|
|
public override void PhysicsProcessState(double delta)
|
|
{
|
|
_stateMachine.Velocity = _movementDirection * MovementSpeed;
|
|
|
|
_stateMachine.MoveAndSlide();
|
|
}
|
|
|
|
public override void ProcessState(double delta)
|
|
{
|
|
_movementDirection = _inputProvider.GetMovementInput().Normalized();
|
|
_isStrafing = _inputProvider.GetStrafePressed();
|
|
|
|
// Toggle visibility of the hitbox sprite based on strafing
|
|
_hitboxSpriteProvider.SetVisibility(_isStrafing);
|
|
|
|
var rightStickInput = _inputProvider.GetAimInput().Normalized();
|
|
|
|
// Update Facing Direction
|
|
if (!_isStrafing)
|
|
{
|
|
if (rightStickInput.Length() > 0.1f) // If the right stick is moved
|
|
{
|
|
FacingDirection = rightStickInput.Normalized();
|
|
}
|
|
else if (_movementDirection != Vector2.Zero) // Fall back to movement direction
|
|
{
|
|
FacingDirection = _movementDirection;
|
|
}
|
|
}
|
|
|
|
_animationProvider.SetAnimation(_stateMachine.Velocity);
|
|
|
|
_crosshairProvider.UpdatePosition(FacingDirection);
|
|
|
|
HandleShoot();
|
|
|
|
HandleInteraction();
|
|
// FindInteractable();
|
|
|
|
// _crosshair.Position = CalculateCrosshairPosition();
|
|
|
|
}
|
|
|
|
private void HandleShoot()
|
|
{
|
|
if (!_inputProvider.GetShootPressed()) return;
|
|
_weaponProvider.Shoot(this.FacingDirection);
|
|
}
|
|
|
|
private void HandleInteraction()
|
|
{
|
|
_activationProvider.HandleInteraction();
|
|
}
|
|
}
|