177 lines
4.6 KiB
C#
177 lines
4.6 KiB
C#
using System.Collections;
|
|
using Godot;
|
|
|
|
public partial class ship : CharacterBody2D
|
|
{
|
|
public enum ShipType { FIGHTER, INTERCEPTOR, GUARDIAN }
|
|
|
|
public enum ShipFaction {PLAYER, FRIENDLY, ENEMY, ACE}
|
|
|
|
[Signal]
|
|
public delegate void LaserShotEventHandler(Area2D Laser);
|
|
|
|
//[Export]
|
|
//public Vector2 ScreenSize;
|
|
[Export]
|
|
public int Health { get; set; } = 100;
|
|
[Export]
|
|
public int MaxHealth { get; set; } = 100;
|
|
[Export]
|
|
public int MaxSpeed { get; set;} = 300;
|
|
[Export]
|
|
public int MainSpeed { get; set; } = 20;
|
|
[Export]
|
|
public int StrafeSpeed { get; set; } = 5;
|
|
[Export]
|
|
public float RotationSpeed { get; set; } = 2f;
|
|
[Export]
|
|
public ShipType type;
|
|
[Export]
|
|
public ShipFaction faction;
|
|
|
|
public Sprite2D Sprite = new Sprite2D();
|
|
|
|
public Node2D LaserSpawn = null;
|
|
|
|
protected string spritePath = "";
|
|
|
|
protected int _rotationDirection;
|
|
protected readonly PackedScene LaserScene = GD.Load<PackedScene>("res://scenes/laser.tscn");
|
|
|
|
|
|
public virtual void ShootLaser()
|
|
{
|
|
Node2D Laser = LaserScene.Instantiate<Node2D>();
|
|
Laser.Position = LaserSpawn.GlobalPosition;
|
|
Laser.Rotation = Rotation;
|
|
EmitSignal(SignalName.LaserShot, Laser);
|
|
}
|
|
|
|
public void SetupVisual()
|
|
{
|
|
Sprite = GetNode<Sprite2D>("ShipSprite");
|
|
spritePath = ""; // Have to initialise as "" because of switch statements
|
|
|
|
if (faction == ShipFaction.PLAYER)
|
|
{
|
|
switch (type)
|
|
{
|
|
case ShipType.FIGHTER:
|
|
spritePath = "res://assets/Ships/Fighters/Player/Fighter/";
|
|
break;
|
|
|
|
case ShipType.INTERCEPTOR:
|
|
spritePath = "res://assets/Ships/Fighters/Player/Interceptor/";
|
|
break;
|
|
|
|
case ShipType.GUARDIAN:
|
|
spritePath = "res://assets/Ships/Fighters/Player/Guardian/";
|
|
break;
|
|
}
|
|
}
|
|
else if (faction == ShipFaction.FRIENDLY)
|
|
{
|
|
switch (type)
|
|
{
|
|
case ShipType.FIGHTER:
|
|
spritePath = "res://assets/Ships/Fighters/Friendly/friendlyFighter.png/";
|
|
break;
|
|
|
|
case ShipType.INTERCEPTOR:
|
|
spritePath = "res://assets/Ships/Fighters/Friendly/friendlyInterceptor.png/";
|
|
break;
|
|
|
|
case ShipType.GUARDIAN:
|
|
spritePath = "res://assets/Ships/Fighters/Friendly/friendlyGuardian.png/";
|
|
break;
|
|
}
|
|
}
|
|
|
|
else if (faction == ShipFaction.ENEMY)
|
|
{
|
|
switch (type)
|
|
{
|
|
case ShipType.FIGHTER:
|
|
spritePath = "res://assets/Ships/Fighters/Enemy/enemyFighter.png/";
|
|
break;
|
|
|
|
case ShipType.INTERCEPTOR:
|
|
spritePath = "res://assets/Ships/Fighters/Enemy/enemyInterceptor.png/";
|
|
break;
|
|
|
|
case ShipType.GUARDIAN:
|
|
spritePath = "res://assets/Ships/Fighters/Enemy/enemyGuardian.png/";
|
|
break;
|
|
}
|
|
}
|
|
else if (faction == ShipFaction.ACE)
|
|
{
|
|
switch (type)
|
|
{
|
|
case ShipType.FIGHTER:
|
|
spritePath = "res://assets/Ships/Fighters/Ace/aceFighter.png/";
|
|
break;
|
|
|
|
case ShipType.INTERCEPTOR:
|
|
spritePath = "res://assets/Ships/Fighters/Ace/aceInterceptor.png/";
|
|
break;
|
|
|
|
case ShipType.GUARDIAN:
|
|
spritePath = "res://assets/Ships/Fighters/Ace/aceGuardian.png/";
|
|
break;
|
|
}
|
|
}
|
|
if (faction == ShipFaction.ENEMY || faction ==ShipFaction.FRIENDLY)
|
|
{
|
|
Sprite.RotationDegrees = 180;
|
|
}
|
|
else
|
|
{
|
|
Sprite.RotationDegrees = 0;
|
|
}
|
|
}
|
|
|
|
public void SetShipStats()
|
|
{
|
|
switch (type)
|
|
{
|
|
case ShipType.INTERCEPTOR:
|
|
MaxSpeed = 450;
|
|
MainSpeed = 35;
|
|
StrafeSpeed = 10;
|
|
RotationSpeed = 4f;
|
|
MaxHealth = 75;
|
|
break;
|
|
|
|
case ShipType.GUARDIAN:
|
|
MaxSpeed = 200;
|
|
MainSpeed = 10;
|
|
StrafeSpeed = 3;
|
|
RotationSpeed = 1f;
|
|
MaxHealth = 250;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public virtual void ShipDamage(int damage)
|
|
{
|
|
Health -= damage;
|
|
if (Health < 0){ Health = 0;}
|
|
}
|
|
|
|
public override void _Ready()
|
|
{
|
|
//ScreenSize = GetViewportRect().Size;
|
|
LaserSpawn = GetNode<Node2D>("LaserSpawn");
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
// Common movement logic for all ships
|
|
Rotation += _rotationDirection * RotationSpeed * (float)delta;
|
|
Velocity.LimitLength(MaxSpeed);
|
|
|
|
MoveAndSlide();
|
|
}
|
|
}
|