Godot-SpaceShooter/Scripts/PlayerShip.cs

77 lines
1.7 KiB
C#

using Godot;
public partial class PlayerShip : Node3D {
[Export] private float moveVelocity = 10f;
[Export] private Node3D jet;
private const float jetTimerMax = 0.5f;
private float jetTimer = jetTimerMax;
private bool jetActive;
private bool JetActive {
get => jetActive;
set {
jetTimer = jetTimerMax;
jetActive = value;
jet.Visible = value;
GD.Print($"JetActive is {value}");
}
}
public override void _Ready() => Position = new(0, 0, 5);
public override void _Process(double delta) {
HandleMovement(delta);
if (JetActive) {
jetTimer -= (float)delta;
// GD.Print($"{jetTimer}/{jetTimerMax}");
}
if (jetTimer < 0) JetActive = false;
}
private void HandleMovement(double delta) {
Vector3 moveDir = new();
Vector3 rotDir = new(0,180,0);
if (Input.IsKeyPressed(Key.A) || Input.IsKeyPressed(Key.D) || Input.IsKeyPressed(Key.W) || Input.IsKeyPressed(Key.S)) JetActive = true;
if (Input.IsKeyPressed(Key.A)) {
moveDir.X -= moveVelocity * (float)delta;
rotDir.Z = -30f;
}
if (Input.IsKeyPressed(Key.D)) {
moveDir.X += moveVelocity * (float)delta;
rotDir.Z = 30f;
}
if (Input.IsKeyPressed(Key.W)) {
moveDir.Z -= moveVelocity * (float)delta;
rotDir.X = -15f;
}
if (Input.IsKeyPressed(Key.S)) {
moveDir.Z += moveVelocity * (float)delta;
rotDir.X = 15f;
}
Position += moveDir;
RotationDegrees = rotDir;
CheckBoundaries();
}
private void CheckBoundaries() {
Vector3 correctedPos = Position;
correctedPos.X = Position.X switch {
< -3.3f => -3.3f,
> 3.3f => 3.3f,
_ => correctedPos.X
};
correctedPos.Z = Position.Z switch {
< -5.2f => -5.2f,
> 5.2f => 5.2f,
_ => correctedPos.Z
};
Position = correctedPos;
}
}