starfighter/MB_FYP/script/player.cs

119 lines
3 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;
[Export]
public float FlightAssistValue { get; set; } = 2.5f;
private 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);
if (Health <= 0)
{
EmitSignal(SignalName.PlayerDeath);
}
}
private void ToggleFlightAssist()
{
if (FlightAssistValue == 0f){FlightAssistValue = 2.5f;}
else {FlightAssistValue = 0;}
}
public override void _Ready()
{
SetupVisual();
GD.Print(Faction);
this.spritePath = Color switch
{
ShipColor.RED => this.spritePath + "ShipRed.png",
ShipColor.GREEN => this.spritePath + "ShipGreen.png",
ShipColor.BLUE => this.spritePath + "ShipBlue.png",
_ => this.spritePath
};
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)
{
fireTimer -= (float)delta;
if(Input.IsActionPressed("shoot"))
{
if (fireTimer > 0f) return;
ShootLaser();
fireTimer = FireCooldown;
}
}
public override void _PhysicsProcess(double delta)
{ // every frame
GetInput();
Rotation += RotationDirection * RotationSpeed * (float)delta;
Velocity.LimitLength(MaxSpeed);
2025-04-18 01:07:51 +00:00
//GD.Print(MainSpeed);
//GD.Print("v ",Velocity, "v");
//GD.Print(Name, ": MainSpeed = ", MainSpeed, " Velocity = ", Velocity.Length());
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);}
*/
}
}