starfighter/MB_FYP/script/player.cs

86 lines
2.8 KiB
C#
Raw Normal View History

using Godot;
using System;
public partial class player : CharacterBody2D
{
[Signal]
public delegate void LaserShotEventHandler(Area2D Laser);
[Export]
public Vector2 ScreenSize;
[Export]
public int MaxSpeed { get; set;} = 300;
[Export]
public int MainSpeed { get; set; } = 10;
[Export]
public int StrafeSpeed { get; set; } = 5;
[Export]
public float RotationSpeed { get; set; } = 2f;
public Node2D LaserSpawn = null;
private int _rotationDirection;
private readonly PackedScene LaserScene = GD.Load<PackedScene>("res://scenes/laser.tscn");
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);
if(Input.GetAxis("strafe_left", "strafe_right") == 0){
Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f);
}
else if(Input.GetAxis("down", "up") == 0){
Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f);
}
}
public void ShootLaser()
{
Node2D Laser = LaserScene.Instantiate<Node2D>();
Laser.Position = LaserSpawn.GlobalPosition;
Laser.Rotation = Rotation;
GD.Print("shoooot");
var l = new Laser
{
Position = new Vector2(LaserSpawn.Position.X, LaserSpawn.Position.Y),
Rotation = Rotation
};
EmitSignal(SignalName.LaserShot, Laser);
//GetParent().AddChild(Laser);
//AddChild(Laser);
}
public override void _Ready()
{
GD.Print(GetViewportRect().Size);
ScreenSize = GetViewportRect().Size;
LaserSpawn = GetNode<Node2D>("LaserSpawn");
}
public override void _Process(double delta)
{
if(Input.IsActionJustPressed("shoot"))
{
ShootLaser();
}
}
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);}
}
}