90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using System;
|
|
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 float speed;
|
|
private Vector3 moveDirection = Vector3.Zero;
|
|
private Vector3 rotationDirection = Vector3.Zero;
|
|
|
|
public event EventHandler StartCountdown;
|
|
|
|
public static InputManager Instance { get; private set; }
|
|
|
|
public override void _Ready() => Instance = this;
|
|
|
|
public override void _Input(InputEvent @event) {
|
|
float acceleration = PlayerShip.Instance.Acceleration;
|
|
|
|
//Countdown
|
|
if (GameManager.Instance.Countdown && Input.IsAnythingPressed()) StartCountdown?.Invoke(this,EventArgs.Empty);
|
|
|
|
//Pause
|
|
if (Input.IsActionJustReleased(GAME_PAUSE)) GameManager.Instance.GamePaused = GameManager.Instance.GamePaused switch { true => false, false => true };
|
|
if (GameManager.Instance.GamePaused || GameManager.Instance.Countdown || GameManager.Instance.GameOver) return;
|
|
|
|
//Movement
|
|
if (Input.IsActionPressed(PLAYER_MOVE_FORWARD)) {
|
|
moveDirection.Z = -1;
|
|
speed += acceleration;
|
|
rotationDirection.X = -1;
|
|
}
|
|
if (Input.IsActionJustReleased(PLAYER_MOVE_FORWARD)) {
|
|
moveDirection.Z = 0f;
|
|
rotationDirection.X = 0f;
|
|
}
|
|
|
|
if (Input.IsActionPressed(PLAYER_MOVE_BACKWARDS)) {
|
|
moveDirection.Z = 1;
|
|
speed += acceleration;
|
|
rotationDirection.X = 1;
|
|
}
|
|
if (Input.IsActionJustReleased(PLAYER_MOVE_BACKWARDS)) {
|
|
moveDirection.Z = 0f;
|
|
rotationDirection.X = 0f;
|
|
}
|
|
|
|
if (Input.IsActionPressed(PLAYER_MOVE_LEFT)) {
|
|
moveDirection.X = -1;
|
|
speed += acceleration;
|
|
rotationDirection.Z = -1;
|
|
}
|
|
if (Input.IsActionJustReleased(PLAYER_MOVE_LEFT)) {
|
|
moveDirection.X = 0f;
|
|
rotationDirection.Z = 0f;
|
|
}
|
|
|
|
if (Input.IsActionPressed(PLAYER_MOVE_RIGHT)) {
|
|
moveDirection.X = 1;
|
|
speed += acceleration;
|
|
rotationDirection.Z = 1;
|
|
}
|
|
if (Input.IsActionJustReleased(PLAYER_MOVE_RIGHT)) {
|
|
moveDirection.X = 0f;
|
|
rotationDirection.Z = 0f;
|
|
}
|
|
|
|
// if (Input.IsActionJustReleased(PLAYER_MOVE_FORWARD) || Input.IsActionJustReleased(PLAYER_MOVE_BACKWARDS) || Input.IsActionJustReleased(PLAYER_MOVE_LEFT) || Input.IsActionJustReleased(PLAYER_MOVE_RIGHT)) {
|
|
// moveDirection = Vector3.Zero;
|
|
// rotationDirection = Vector3.Zero;
|
|
// speed = 0f;
|
|
// }
|
|
|
|
PlayerShip.Instance.MoveDirection = moveDirection.Normalized();
|
|
PlayerShip.Instance.RotationDirection = rotationDirection.Normalized();
|
|
PlayerShip.Instance.Speed = speed;
|
|
|
|
//Shooting
|
|
if (Input.IsActionJustPressed(PLAYER_FIRE)) PlayerShip.Instance.Shooting = true;
|
|
// if (Input.IsActionJustReleased(PLAYER_FIRE)) PlayerShip.Instance.Shooting = false;
|
|
}
|
|
} |