gmtk24/scripts/Player.cs

80 lines
3.2 KiB
C#
Raw Normal View History

2024-08-18 15:29:04 +02:00
using Godot;
namespace Gmtk24 {
public partial class Player : CharacterBody3D {
2024-08-18 15:29:04 +02:00
[Export]
public float MovementSpeed = 7;
[Export]
public float SprintMultiplier = 1.5f;
[Export]
2024-08-18 17:28:11 +02:00
public float JumpVelocity = 10;
[Export]
public float JumpBufferLengthSeconds = 0.2f;
[Export]
public Node3D Eye;
2024-08-18 15:29:04 +02:00
2024-08-18 17:28:11 +02:00
private float CurrentYaw = 0;
private float CurrentPitch = 0;
2024-08-18 17:28:11 +02:00
private float JumpBufferTime = 0;
2024-08-18 15:29:04 +02:00
public override void _Ready() {
2024-08-18 17:28:11 +02:00
Input.MouseMode = Input.MouseModeEnum.Captured;
}
2024-08-18 15:29:04 +02:00
2024-08-18 17:28:11 +02:00
public override void _UnhandledInput(InputEvent @event) {
if (@event is InputEventMouseMotion ev && Input.MouseMode == Input.MouseModeEnum.Captured) {
2024-08-18 17:28:11 +02:00
var cameraSensitivityX = 2f; // FIXME: use camera sensitivity and invert settings
var cameraSensitivityY = 2f; // FIXME: use camera sensitivity and invert settings
var mouseMultiplier = 0.0003f;
CurrentYaw -= ev.Relative.X * mouseMultiplier * cameraSensitivityX;
CurrentPitch -= ev.Relative.Y * mouseMultiplier * cameraSensitivityY;
GetViewport().SetInputAsHandled();
}
}
2024-08-18 15:29:04 +02:00
public override void _Process(double delta) {
var yawInput = Input.GetActionStrength("look_left") - Input.GetActionStrength("look_right");
var pitchInput = Input.GetActionStrength("look_up") - Input.GetActionStrength("look_down");
2024-08-18 17:28:11 +02:00
var cameraSensitivityX = 2f; // FIXME: use camera sensitivity and invert settings
var cameraSensitivityY = 2f; // FIXME: use camera sensitivity and invert settings
CurrentYaw += yawInput * cameraSensitivityX * (float)delta;
if (Eye != null) {
CurrentPitch += pitchInput * cameraSensitivityY * (float)delta;
2024-08-18 17:28:11 +02:00
CurrentPitch = Mathf.Clamp(CurrentPitch, -Mathf.Pi * 0.49f, Mathf.Pi * 0.49f);
}
Eye.Quaternion = new Quaternion(Vector3.Up, CurrentYaw) * new Quaternion(Vector3.Right, CurrentPitch);
if (JumpBufferTime > 0) {
JumpBufferTime -= (float)delta;
}
if (Input.IsActionJustPressed("jump")) {
JumpBufferTime = JumpBufferLengthSeconds;
}
}
2024-08-18 15:29:04 +02:00
public override void _PhysicsProcess(double delta) {
Vector3 moveInput = new Vector3(
Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left"), 0,
Input.GetActionStrength("move_backward") - Input.GetActionStrength("move_forward")
);
Vector3 move = Eye.Basis * moveInput;
move = move.Normalized() * Mathf.Min(1, move.Length()) * MovementSpeed;
if (Input.IsActionPressed("sprint")) {
move *= SprintMultiplier;
2024-08-18 15:29:04 +02:00
}
2024-08-18 17:28:11 +02:00
Vector3 vel = Velocity;
vel.X = move.X;
vel.Z = move.Z;
vel += GetGravity() * (float)delta;
2024-08-18 17:28:11 +02:00
if (IsOnFloor() && JumpBufferTime > 0) {
JumpBufferTime = 0;
vel += Vector3.Up * JumpVelocity;
}
Velocity = vel;
2024-08-18 17:28:11 +02:00
MoveAndSlide();
2024-08-18 15:29:04 +02:00
}
}
}