|
|
|
|
@ -5,6 +5,8 @@ namespace Scripts;
|
|
|
|
|
public partial class Asteroid : Node3D {
|
|
|
|
|
[Export] private RigidBody3D asteroidRb;
|
|
|
|
|
[Export] private MeshInstance3D asteroidMesh;
|
|
|
|
|
[Export] private CollisionShape3D asteroidCollisionShape;
|
|
|
|
|
[Export] private PackedScene explosion;
|
|
|
|
|
|
|
|
|
|
private float speed;
|
|
|
|
|
private float rotSpeed = 100f;
|
|
|
|
|
@ -12,20 +14,16 @@ public partial class Asteroid : Node3D {
|
|
|
|
|
private Vector3 rotation;
|
|
|
|
|
|
|
|
|
|
public override void _Ready() {
|
|
|
|
|
size = GD.Randf();
|
|
|
|
|
asteroidRb.Mass = size;
|
|
|
|
|
speed = GD.Randf() * 5f + 1f;
|
|
|
|
|
asteroidRb.Position = new((float)GD.RandRange(-3.7, 3.7), 0, -6.4f);
|
|
|
|
|
|
|
|
|
|
asteroidRb.Scale = new(size, size,size);
|
|
|
|
|
asteroidMesh.Scale = new(size, size,size);
|
|
|
|
|
|
|
|
|
|
rotation = new((float)GD.RandRange(-rotSpeed, rotSpeed), (float)GD.RandRange(-rotSpeed, rotSpeed), (float)GD.RandRange(-rotSpeed, rotSpeed));
|
|
|
|
|
Position = new((float)GD.RandRange(-3.7, 3.7), 0, -6.4f);
|
|
|
|
|
}
|
|
|
|
|
size = (float)GD.RandRange(0.25f, 0.75f);
|
|
|
|
|
Vector3 scale = new(size, size,size);
|
|
|
|
|
asteroidMesh.Scale = scale;
|
|
|
|
|
asteroidRb.Scale = scale;
|
|
|
|
|
asteroidCollisionShape.Scale = scale;
|
|
|
|
|
|
|
|
|
|
public override void _Process(double delta) {
|
|
|
|
|
// Position += new Vector3(0, 0, speed * (float)delta);
|
|
|
|
|
// RotationDegrees += rotation * (float)delta;
|
|
|
|
|
speed = GD.Randf() * 5f + 1f;
|
|
|
|
|
rotation = new((float)GD.RandRange(-rotSpeed, rotSpeed), (float)GD.RandRange(-rotSpeed, rotSpeed), (float)GD.RandRange(-rotSpeed, rotSpeed));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void _PhysicsProcess(double delta) {
|
|
|
|
|
@ -34,18 +32,25 @@ public partial class Asteroid : Node3D {
|
|
|
|
|
KinematicCollision3D collision = asteroidRb.MoveAndCollide(new(0, 0, speed * (float)delta));
|
|
|
|
|
if (collision?.GetCollider() is Node3D collider) {
|
|
|
|
|
GD.Print($"{Name} collides with {collider.GetParentNode3D().Name}");
|
|
|
|
|
if (collider is Asteroid asteroid) asteroid.Explode();
|
|
|
|
|
Explode();
|
|
|
|
|
Vector3 collisionPosition = collision.GetPosition();
|
|
|
|
|
// if (collider is Asteroid asteroid) asteroid.Explode(collisionPosition);
|
|
|
|
|
Explode(collisionPosition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: Change into collision with outer box
|
|
|
|
|
if (Position.Z >= 6.4f) {
|
|
|
|
|
if (asteroidRb.Position.Z >= 8f) {
|
|
|
|
|
GD.Print($"Asteroid {Name} below screen!");
|
|
|
|
|
Explode();
|
|
|
|
|
Explode(asteroidRb.Position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Explode() {
|
|
|
|
|
private void Explode(Vector3 collisionPosition) {
|
|
|
|
|
if (explosion.Instantiate() is GpuParticles3D ex) {
|
|
|
|
|
GetParent().AddChild(ex);
|
|
|
|
|
ex.Position = collisionPosition;
|
|
|
|
|
ex.Emitting = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GameManager.Instance.AsteroidNumber--;
|
|
|
|
|
QueueFree();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|