starfighter/MB_FYP/script/player.cs

54 lines
1.9 KiB
C#

using Godot;
using System;
public partial class player : CharacterBody2D
{
[Export]
public Vector2 ScreenSize;
[Export]
public int MaxSpeed { get; set;} = 300;
[Export]
public int MainSpeed { get; set; } = 10;
[Export]
public int StrafeSpeed { get; set; } = 4;
[Export]
public float RotationSpeed { get; set; } = 2f;
private int _rotationDirection;
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.Y * Input.GetAxis("strafe_left", "strafe_right") * StrafeSpeed) + (Transform.X * Input.GetAxis("down", "up") * 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 override void _Ready(){ // on init
GD.Print(GetViewportRect().Size);
ScreenSize = GetViewportRect().Size;
}
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);}
}
}