starfighter/MB_FYP/script/player.cs

135 lines
3.2 KiB
C#
Raw Normal View History

using Godot;
using System;
public partial class player : ship // Inherits from base ship class
{
public enum ShipColor{RED, GREEN, BLUE}
[Signal]
public delegate void HealthUpdateEventHandler(int health);
[Signal]
public delegate void PlayerDeathEventHandler();
[Export]
public ShipColor Color;
public Sprite2D Sprite = new Sprite2D();
[Export]
public float FlightAssistValue { get; set; } = 2.5f;
public void GetInput()
{
/*LookAt(GetGlobalMousePosition()); //used for mouse-based rotation and movement
Velocity = Transform.X * Input.GetAxis("down", "up") * Speed;
*/
// Movement, could probably move into its own methods instead of GetInput()
_rotationDirection = (int)Input.GetAxis("left", "right");
Velocity += (Transform.X * Input.GetAxis("strafe_left", "strafe_right") * StrafeSpeed) + (Transform.Y * Input.GetAxis("up", "down") * MainSpeed);
Velocity = Velocity.LimitLength(MaxSpeed);
//move into selection statement for toggling between fa off and on
if(Input.GetAxis("strafe_left", "strafe_right") == 0){
Velocity = Velocity.MoveToward(Vector2.Zero, FlightAssistValue);
}
else if(Input.GetAxis("down", "up") == 0){
Velocity = Velocity.MoveToward(Vector2.Zero, FlightAssistValue);
}
//FA toggle
if (Input.IsActionJustPressed("toggle_fa"))
{
ToggleFlightAssist();
}
}
public override void ShipDamage(int damage)
{
base.ShipDamage(damage);
EmitSignal(SignalName.HealthUpdate, Health);
}
public void ToggleFlightAssist()
{
if (FlightAssistValue == 0f){FlightAssistValue = 2.5f;}
else {FlightAssistValue = 0;}
}
public override void _Ready()
{
Sprite = GetNode<Sprite2D>("ShipSprite");
string spritePath = ""; // Have to initialise as "" because of switch statements
switch (type)
{
case ShipType.FIGHTER:
spritePath = "res://assets/Player/Fighter/";
break;
case ShipType.INTERCEPTOR:
spritePath = "res://assets/Player/Interceptor/";
break;
case ShipType.GUARDIAN:
spritePath = "res://assets/Player/Guardian/";
break;
}
switch(Color)
{
case ShipColor.RED:
spritePath = spritePath + "ShipRed.png";
break;
case ShipColor.GREEN:
spritePath = spritePath + "ShipGreen.png";
break;
case ShipColor.BLUE:
spritePath = spritePath + "ShipBlue.png";
break;
}
GD.Print(spritePath);
Sprite.Texture = GD.Load<Texture2D>(spritePath);
LaserSpawn = GetNode<Node2D>("LaserSpawn");
//Connect("body_entered" +=)
SetShipStats();
Health = MaxHealth;
EmitSignal(SignalName.HealthUpdate, Health);
}
public override void _Process(double delta)
{
if(Input.IsActionJustPressed("shoot"))
{
ShootLaser();
}
if (Health <= 0)
{
EmitSignal(SignalName.PlayerDeath);
}
}
public override void _PhysicsProcess(double delta)
{ // every frame
GetInput();
Rotation += _rotationDirection * RotationSpeed * (float)delta;
Velocity.LimitLength(MaxSpeed);
MoveAndSlide();
/*
if (Position.Y < 0) {Position = new Vector2(Position.X, ScreenSize.Y);}
if (Position.Y > ScreenSize.Y) {Position = new Vector2(Position.X, 0);}
if (Position.X < 0) { Position = new Vector2(ScreenSize.X, Position.Y);}
if (Position.X > ScreenSize.X) { Position = new Vector2(0, Position.Y);}
*/
}
}