Godot-SpaceShooter/Scripts/InputManager.cs

45 lines
2.1 KiB
C#

using Godot;
namespace GodotspaceShooter.Scripts;
public partial class InputManager : Node
{
private const string GAME_PAUSE = "Game_Pause";
private const string PLAYER_MOVE_FORWARD = "Player_Move_Forward";
private const string PLAYER_MOVE_BACKWARDS = "Player_Move_Backwards";
private const string PLAYER_MOVE_LEFT = "Player_Move_Left";
private const string PLAYER_MOVE_RIGHT = "Player_Move_Right";
private const string PLAYER_FIRE = "Player_Fire";
private Vector3 moveDirection;
private Vector3 rotationDirection;
public override void _UnhandledInput(InputEvent @event) {
//Pause
if (Input.IsActionJustReleased(GAME_PAUSE)) GameManager.Instance.GamePaused = GameManager.Instance.GamePaused switch { true => false, false => true };
//Movement
float moveVelocity = PlayerShip.Instance.MoveVelocity;
float rotationVelocity = PlayerShip.Instance.RotationVelocity;
moveDirection = Vector3.Zero;
rotationDirection = Vector3.Zero;
if (Input.IsActionPressed(PLAYER_MOVE_FORWARD)) moveDirection.Z = -moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_FORWARD)) rotationDirection.X = -rotationVelocity;
if (Input.IsActionPressed(PLAYER_MOVE_BACKWARDS)) moveDirection.Z = +moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_BACKWARDS)) rotationDirection.X = +rotationVelocity;
if (Input.IsActionPressed(PLAYER_MOVE_LEFT)) moveDirection.X = -moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_LEFT)) rotationDirection.Z = -rotationVelocity * 2;
if (Input.IsActionPressed(PLAYER_MOVE_RIGHT)) moveDirection.X = moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_RIGHT)) rotationDirection.Z = +rotationVelocity * 2;
if (Input.IsActionJustReleased(PLAYER_MOVE_LEFT) || Input.IsActionJustReleased(PLAYER_MOVE_RIGHT)) rotationDirection = Vector3.Zero;
PlayerShip.Instance.MoveDirection = moveDirection;
PlayerShip.Instance.RotationDirection = rotationDirection;
//Shooting
if (Input.IsActionJustPressed(PLAYER_FIRE)) PlayerShip.Instance.Shooting = true;
// if (Input.IsActionJustReleased(PLAYER_FIRE)) PlayerShip.Instance.Shooting = false;
}
}