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; } private Vector2 _facingDirection { get; set; } [Export] public Sprite2D HitboxSprite { get; set; } [ExportGroup("Action Names")] [Export] private string _shootActionName = "shoot"; [Export] private string _useActionName = "Use"; [Export] private string _strafeActionName = "strafe"; [Export] private string _nextWeaponActionName = "next_weapon"; [Export] private string _previousWeaponActionName = "previous_weapon"; private PlayerWeaponProvider _weaponProvider; private bool _isStrafing { get; set; } [Export] public int Speed { get; set; } = 45; [Export] public int StrafeSpeed { get; set; } = 35; public int MovementSpeed => _isStrafing ? StrafeSpeed : Speed; public override void Init(ActorStateMachine stateMachine) { base.Init(stateMachine); _weaponProvider = stateMachine.GetNode("WeaponProvider"); _weaponProvider.Init(stateMachine); } public override void EnterState() { // enable sprite // enable crosshair GD.Print(this.State.ToString()); } public override void ExitState() { } public override void PhysicsProcessState(double delta) { _stateMachine.Velocity = _movementDirection * MovementSpeed; _stateMachine.MoveAndSlide(); } public override void ProcessState(double delta) { SetAnimation(); _movementDirection = GetInput(); _isStrafing = Input.IsActionPressed(_strafeActionName); // Toggle visibility of the hitbox sprite based on strafing if (HitboxSprite != null) { HitboxSprite.Visible = _isStrafing; } var rightStickInput = GetRightStickInput(); // 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; } } HandleShoot(); // FindInteractable(); // if (Input.IsActionJustPressed(_nextWeaponActionName)) // { // NextWeapon(); // } // if (Input.IsActionJustPressed(_previousWeaponActionName)) // { // PreviousWeapon(); // } // _crosshair.Position = CalculateCrosshairPosition(); } private void SetAnimation() { // if (Velocity.X == 0 && Velocity.Y == 0) // { // _animatedSprite.SpeedScale = 0; // } // else // { // _animatedSprite.SpeedScale = 1; // } // if (Velocity.X > 0) // { // _animatedSprite.Play("walk_right"); // } // else if (Velocity.X < 0) // { // _animatedSprite.Play("walk_left"); // } // else if (Velocity.Y > 0) // { // _animatedSprite.Play("walk_down"); // } // else if (Velocity.Y < 0) // { // _animatedSprite.Play("walk_up"); // } } private void HandleShoot() { if (!Input.IsActionPressed(_shootActionName)) return; _weaponProvider.Shoot(this._facingDirection); } public Vector2 GetInput() { return Input.GetVector("left", "right", "up", "down"); } private Vector2 GetRightStickInput() { return new Vector2( Input.GetAxis("aim_left", "aim_right"), Input.GetAxis("aim_up", "aim_down") ); } }