102 lines
3.8 KiB
C#
102 lines
3.8 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class asteroid : Area2D
|
|
{
|
|
[Signal]
|
|
public delegate void ExplodedEventHandler(Vector2 pos, int size);
|
|
|
|
public enum AsteroidSize {LARGE, MEDIUM, SMALL};
|
|
|
|
private string[] LargeAsteroidTexture = new string[] {"res://assets/PNG/Meteors/meteorBrown_big1.png",
|
|
"res://assets/PNG/Meteors/meteorBrown_big4.png",
|
|
"res://assets/PNG/Meteors/meteorGrey_big4.png"};
|
|
|
|
private string[] MediumAsteroidTexture = new string[] {"res://assets/PNG/Meteors/meteorGrey_med1.png",
|
|
"res://assets/PNG/Meteors/meteorBrown_med3.png",
|
|
"res://assets/PNG/Meteors/meteorBrown_med1.png"};
|
|
|
|
private string[] SmallAsteroidTexture = new string[] {"res://assets/PNG/Meteors/meteorBrown_small1.png",
|
|
"res://assets/PNG/Meteors/meteorBrown_small2.png",
|
|
"res://assets/PNG/Meteors/meteorGrey_small1.png"};
|
|
|
|
|
|
[Export]
|
|
public AsteroidSize size;
|
|
public int Speed = 200;
|
|
|
|
public Vector2 MovementVector;
|
|
|
|
public Sprite2D Sprite = new Sprite2D();
|
|
|
|
public CollisionShape2D ColShape = null;
|
|
|
|
public override void _Ready()
|
|
{
|
|
Sprite = GetNode<Sprite2D>("Sprite2D");
|
|
ColShape = GetNode<CollisionShape2D>("CollisionShape2D");
|
|
var rand = new Random();
|
|
Rotation = (float)rand.NextDouble();
|
|
Rotation = (float)((Rotation * 2) * 3.14);
|
|
MovementVector = new Vector2(0, -1);
|
|
|
|
|
|
CircleShape2D ColShapeL;
|
|
switch (size)
|
|
{
|
|
case AsteroidSize.LARGE:
|
|
Speed = rand.Next(50, 100);
|
|
Sprite.Texture = GD.Load<Texture2D>(LargeAsteroidTexture[rand.Next(3)]);
|
|
ColShapeL = GD.Load<CircleShape2D>("res://assets/CollisionShapes/asteroid_cshape_big.tres");
|
|
ColShape.SetDeferred("shape", ColShapeL);
|
|
break;
|
|
|
|
case AsteroidSize.MEDIUM:
|
|
Speed = rand.Next(100, 150);
|
|
Sprite.Texture = GD.Load<Texture2D>(MediumAsteroidTexture[rand.Next(3)]);
|
|
ColShapeL = GD.Load<CircleShape2D>("res://assets/CollisionShapes/asteroid_cshape_medcircle.tres");
|
|
ColShape.SetDeferred("shape", ColShapeL);
|
|
break;
|
|
|
|
case AsteroidSize.SMALL:
|
|
Speed = rand.Next(150, 200);
|
|
Sprite.Texture = GD.Load<Texture2D>(SmallAsteroidTexture[rand.Next(3)]);
|
|
ColShapeL = GD.Load<CircleShape2D>("res://assets/CollisionShapes/asteroid_cshape_smallcircle.tres");
|
|
ColShape.SetDeferred("shape", ColShapeL);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void Explode()
|
|
{
|
|
EmitSignal(SignalName.Exploded, GlobalPosition, (int)size);
|
|
QueueFree();
|
|
}
|
|
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
GlobalPosition += MovementVector.Rotated(Rotation) * Speed * (float)delta;
|
|
CircleShape2D circleshape = (CircleShape2D)ColShape.Shape;
|
|
var radius = circleshape.Radius;
|
|
var ScreenSize = GetViewportRect().Size;
|
|
|
|
if (GlobalPosition.Y + radius < 0)
|
|
{
|
|
GlobalPosition = new Vector2(GlobalPosition.X, ScreenSize.Y + radius);
|
|
}
|
|
else if (GlobalPosition.Y - radius > ScreenSize.Y)
|
|
{
|
|
GlobalPosition = new Vector2(GlobalPosition.X, -radius);
|
|
}
|
|
if (GlobalPosition.X + radius < 0)
|
|
{
|
|
GlobalPosition = new Vector2(ScreenSize.X + radius, GlobalPosition.Y);
|
|
}
|
|
else if (GlobalPosition.X - radius > ScreenSize.X)
|
|
{
|
|
GlobalPosition = new Vector2(-radius, GlobalPosition.Y);
|
|
}
|
|
}
|
|
}
|