83 lines
2.7 KiB
C#
83 lines
2.7 KiB
C#
using Godot;
|
|
|
|
namespace GodotspaceShooter.Scripts;
|
|
|
|
public partial class PlayerShip : Node3D {
|
|
private const float jetTimerMax = 0.5f;
|
|
private const float laserTimerMax = 1f;
|
|
|
|
[Export] private CpuParticles3D cpulaser;
|
|
[Export] private PackedScene explosion;
|
|
[Export] private GpuParticles3D gpulaser;
|
|
[Export] private Node3D jet;
|
|
private float jetTimer = jetTimerMax;
|
|
[Export] private PackedScene laser;
|
|
private float laserTimer = laserTimerMax;
|
|
|
|
[Export] private float moveVelocity = 10f;
|
|
public float MoveVelocity => moveVelocity;
|
|
[Export] private float rotationVelocity = 15f;
|
|
public float RotationVelocity => rotationVelocity;
|
|
|
|
[Export] public RigidBody3D PlayerRb;
|
|
[Export] private Node3D shots;
|
|
|
|
public Vector3 MoveDirection { get; set; }
|
|
public Vector3 RotationDirection { get; set; }
|
|
public bool Shooting { get; set; }
|
|
|
|
public static PlayerShip Instance { get; private set; }
|
|
|
|
public override void _Ready() {
|
|
Position = new(0, 0, 5);
|
|
Instance = this;
|
|
}
|
|
|
|
public override void _Process(double delta) {
|
|
if (Shooting)
|
|
if (laser.Instantiate() is Node3D shot) {
|
|
shot.Position = PlayerRb.Position + new Vector3(0f, 0f, 0.74f);
|
|
shots.AddChild(shot);
|
|
SoundManager.Instance.Play(SoundManager.Sound.Laser, PlayerRb.Position);
|
|
Shooting = false;
|
|
}
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta) {
|
|
//Movement
|
|
if (MoveDirection != Vector3.Zero) {
|
|
jet.Visible = true;
|
|
KinematicCollision3D collision = PlayerRb.MoveAndCollide(MoveDirection * (float)delta);
|
|
if (collision?.GetCollider() is Node3D collider) {
|
|
Node3D parent = collider.GetParent<Node3D>();
|
|
Vector3 collisionPosition = collision.GetPosition();
|
|
switch (parent) {
|
|
case GameArea:
|
|
GD.Print($"PlayerShip hits GameArea");
|
|
break;
|
|
case Asteroid asteroid:
|
|
Explode(collisionPosition);
|
|
asteroid.Explode(collisionPosition);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
jet.Visible = false;
|
|
}
|
|
|
|
//Rotation
|
|
PlayerRb.RotationDegrees = RotationDirection;
|
|
}
|
|
|
|
public void Explode(Vector3 collisionPosition) {
|
|
if (explosion?.Instantiate() is GpuParticles3D ex) {
|
|
GetParent().AddChild(ex);
|
|
ex.Position = collisionPosition;
|
|
ex.Emitting = true;
|
|
}
|
|
|
|
SoundManager.Instance.Play(SoundManager.Sound.PlayerExplosion, collisionPosition);
|
|
GameManager.Instance.Lives--;
|
|
}
|
|
} |