60 lines
1.7 KiB
C#
60 lines
1.7 KiB
C#
using Godot;
|
|
|
|
namespace Scripts;
|
|
[GlobalClass]
|
|
public partial class Asteroid : Node3D {
|
|
[Export] private RigidBody3D asteroidRb;
|
|
[Export] private MeshInstance3D asteroidMesh;
|
|
[Export] private PackedScene explosion;
|
|
|
|
private float speed;
|
|
private float rotSpeed = 100f;
|
|
private float size;
|
|
private Vector3 rotation;
|
|
|
|
public override void _Ready() {
|
|
size = GD.Randf();
|
|
asteroidRb.Mass = size;
|
|
speed = GD.Randf() * 5f + 1f;
|
|
|
|
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);
|
|
}
|
|
|
|
public override void _Process(double delta) {
|
|
// Position += new Vector3(0, 0, speed * (float)delta);
|
|
// RotationDegrees += rotation * (float)delta;
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta) {
|
|
asteroidRb.Rotate(rotation.Normalized(), (float)delta);
|
|
|
|
KinematicCollision3D collision = asteroidRb.MoveAndCollide(new(0, 0, speed * (float)delta));
|
|
if (collision?.GetCollider() is Node3D collider) {
|
|
GD.Print($"{Name} collides with {collider.GetParentNode3D().Name}");
|
|
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) {
|
|
// GD.Print($"Asteroid {Name} below screen!");
|
|
// Explode(collisionPosition);
|
|
// }
|
|
}
|
|
|
|
private void Explode(Vector3 collisionPosition) {
|
|
if (explosion.Instantiate() is GpuParticles3D ex) {
|
|
GetParent().AddChild(ex);
|
|
ex.Position = collisionPosition;
|
|
ex.Emitting = true;
|
|
}
|
|
|
|
QueueFree();
|
|
}
|
|
}
|