53 lines
1.9 KiB
C#
53 lines
1.9 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class FlightController : Node3D {
|
|
Camera3D PlayerCam;
|
|
Vector3 AngularVelocity;
|
|
|
|
|
|
public override void _Ready() {
|
|
this.PlayerCam = this.GetNode<Camera3D>("MainCamera");
|
|
this.PlayerCam.Current = true;
|
|
Input.MouseMode = Input.MouseModeEnum.Hidden;
|
|
}
|
|
|
|
|
|
public override void _Process(double deltaSeconds) {
|
|
Vector2 currentMousePos = GetViewport().GetMousePosition();
|
|
Vector2I center = GetViewport().GetWindow().Size / 2;
|
|
Vector2 centerFloat = new(center.X, center.Y);
|
|
Vector2 delta = (currentMousePos - centerFloat) * (float)deltaSeconds;
|
|
Input.WarpMouse(center);
|
|
|
|
if (delta != Vector2.Zero) {
|
|
Quaternion quat = new Quaternion(this.GlobalTransform.Basis);
|
|
Vector3 LocalAccel = new Vector3(delta.Y, delta.X, 0f) * 3f * (float)deltaSeconds;
|
|
if (quat.GetAxis().LengthSquared() != 0f) {
|
|
this.AngularVelocity -= LocalAccel.Rotated(quat.GetAxis().Normalized(), quat.GetAngle());
|
|
}
|
|
else {
|
|
this.AngularVelocity -= LocalAccel;
|
|
}
|
|
}
|
|
this.AngularVelocity *= (float)(1.0 - deltaSeconds * 2.0);
|
|
if (this.AngularVelocity.Length() > 0f) {
|
|
Quaternion rotationThisFrame = Quaternion.FromEuler(this.AngularVelocity * (float)deltaSeconds);
|
|
if (rotationThisFrame.GetAxis().Normalized().LengthSquared() != 0f) {
|
|
this.GlobalRotate(rotationThisFrame.GetAxis().Normalized(), rotationThisFrame.GetAngle());
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public override void _Input(InputEvent @event) {
|
|
base._Input(@event);
|
|
if (@event is InputEventKey keyEvent) {
|
|
if (keyEvent.Keycode == Key.Escape) {
|
|
GetTree().Root.PropagateNotification((int)NotificationWMCloseRequest);
|
|
GetTree().Quit();
|
|
}
|
|
}
|
|
}
|
|
}
|