From 1c158760bd518aaea5644669516614031bca27ee Mon Sep 17 00:00:00 2001 From: Sascha Date: Fri, 2 Feb 2024 11:23:44 +0100 Subject: [PATCH] added acceleration and complete refactoring of movement --- Scripts/InputManager.cs | 78 +++++++++++++++++++++++++++++------------ Scripts/PlayerShip.cs | 19 +++++++--- 2 files changed, 71 insertions(+), 26 deletions(-) diff --git a/Scripts/InputManager.cs b/Scripts/InputManager.cs index b75d4fc..8abe0d5 100644 --- a/Scripts/InputManager.cs +++ b/Scripts/InputManager.cs @@ -12,42 +12,76 @@ public partial class InputManager : Node private const string PLAYER_MOVE_RIGHT = "Player_Move_Right"; private const string PLAYER_FIRE = "Player_Fire"; - private Vector3 moveDirection; - private Vector3 rotationDirection; - + 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 _UnhandledInput(InputEvent @event) { + 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 - float moveVelocity = PlayerShip.Instance.MoveVelocity; - float rotationVelocity = PlayerShip.Instance.RotationVelocity; + 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; + } - 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; + 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; diff --git a/Scripts/PlayerShip.cs b/Scripts/PlayerShip.cs index f4241f6..a018d65 100644 --- a/Scripts/PlayerShip.cs +++ b/Scripts/PlayerShip.cs @@ -13,9 +13,17 @@ public partial class PlayerShip : Node3D { private const float laserTimerMax = 1f; private float laserTimer = laserTimerMax; private float jetTimer = jetTimerMax; + private float maxSpeed = 5f; + private float speed; + public float Acceleration { get; private set; } = .5f; - public float MoveVelocity { get; private set; } = 10f; - public float RotationVelocity { get; private set; } = 15f; + public float Speed { + get => speed; + set { + speed = value; + if (speed > maxSpeed) speed = maxSpeed; + } + } public Vector3 MoveDirection { get; set; } public Vector3 RotationDirection { get; set; } @@ -40,9 +48,12 @@ public partial class PlayerShip : Node3D { public override void _PhysicsProcess(double delta) { //Movement - if (MoveDirection != Vector3.Zero) { + GD.Print($"Move {MoveDirection.X}, {MoveDirection.Z}, Speed {Speed}, Rot {RotationDirection.X}, {RotationDirection.Z}"); + if (Speed > 0f) Speed -= Acceleration * (float)delta * 10f; + + if (MoveDirection.Length() > 0) { jet.Visible = true; - KinematicCollision3D collision = rbPlayer.MoveAndCollide(MoveDirection * (float)delta); + KinematicCollision3D collision = rbPlayer.MoveAndCollide(MoveDirection * Speed * (float)delta); if (collision?.GetCollider() is Node3D collider) { Node3D parent = collider.GetParent(); Vector3 collisionPosition = collision.GetPosition();