2024-08-18 15:29:04 +02:00
|
|
|
using Godot;
|
|
|
|
|
|
|
|
namespace Gmtk24 {
|
2024-08-18 17:02:50 +02:00
|
|
|
public partial class Player : CharacterBody3D {
|
2024-08-19 22:27:26 +02:00
|
|
|
[ExportCategory("Movement")]
|
2024-08-18 15:29:04 +02:00
|
|
|
[Export]
|
|
|
|
public float MovementSpeed = 7;
|
|
|
|
[Export]
|
2024-08-18 17:02:50 +02:00
|
|
|
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]
|
2024-08-18 20:37:57 +02:00
|
|
|
public float Gravity = 20;
|
2024-08-19 22:27:26 +02:00
|
|
|
[ExportCategory("Vision")]
|
2024-08-18 20:06:11 +02:00
|
|
|
[Export]
|
2024-08-19 23:33:24 +02:00
|
|
|
public Camera3D Eye;
|
2024-08-19 22:27:26 +02:00
|
|
|
[Export]
|
|
|
|
public RayCast3D LookingAt;
|
2024-08-19 17:19:03 +02:00
|
|
|
[ExportCategory("Noises")]
|
|
|
|
[Export]
|
|
|
|
public AudioStreamPlayer HeightBasedWindPlayer;
|
|
|
|
[Export]
|
|
|
|
public float MinVolumeWindHeight = 0;
|
|
|
|
[Export]
|
|
|
|
public float MaxVolumeWindHeight = 0;
|
|
|
|
[Export]
|
|
|
|
public AudioStreamPlayer3D FootstepPlayer;
|
|
|
|
[Export]
|
|
|
|
public float FootstepNoiseInterval = 0.25f;
|
2024-08-18 15:29:04 +02:00
|
|
|
|
2024-08-18 17:28:11 +02:00
|
|
|
private float CurrentYaw = 0;
|
2024-08-18 17:02:50 +02:00
|
|
|
private float CurrentPitch = 0;
|
2024-08-18 17:28:11 +02:00
|
|
|
private float JumpBufferTime = 0;
|
2024-08-19 17:19:03 +02:00
|
|
|
private float FootstepCooldown = 0;
|
2024-08-18 15:29:04 +02:00
|
|
|
|
2024-08-20 00:51:43 +02:00
|
|
|
private bool NoclipEnabled = false;
|
|
|
|
|
2024-08-18 17:28:11 +02:00
|
|
|
public override void _UnhandledInput(InputEvent @event) {
|
2024-08-18 20:06:11 +02:00
|
|
|
if (@event is InputEventMouseMotion mouseMotion && Input.MouseMode == Input.MouseModeEnum.Captured) {
|
2024-08-19 15:51:51 +02:00
|
|
|
var cameraSensitivity = UserSettings.Singleton.GetCameraSpeedMultipliers();
|
2024-08-18 17:28:11 +02:00
|
|
|
var mouseMultiplier = 0.0003f;
|
2024-08-19 15:51:51 +02:00
|
|
|
CurrentYaw -= mouseMotion.ScreenRelative.X * mouseMultiplier * cameraSensitivity.X;
|
|
|
|
CurrentPitch -= mouseMotion.ScreenRelative.Y * mouseMultiplier * cameraSensitivity.Y;
|
2024-08-18 17:28:11 +02:00
|
|
|
GetViewport().SetInputAsHandled();
|
2024-08-18 17:02:50 +02:00
|
|
|
}
|
2024-08-18 20:06:11 +02:00
|
|
|
|
|
|
|
if (@event.IsActionPressed("jump")) {
|
|
|
|
JumpBufferTime = JumpBufferLengthSeconds;
|
2024-08-20 00:51:43 +02:00
|
|
|
GetViewport().SetInputAsHandled();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (@event.IsActionPressed("toggle_noclip")) {
|
|
|
|
NoclipEnabled = !NoclipEnabled;
|
|
|
|
if (NoclipEnabled) {
|
|
|
|
CollisionLayer = 0;
|
|
|
|
CollisionMask = 0;
|
|
|
|
} else {
|
|
|
|
CollisionLayer = 1;
|
|
|
|
CollisionMask = 1;
|
|
|
|
}
|
|
|
|
GetViewport().SetInputAsHandled();
|
2024-08-18 20:06:11 +02:00
|
|
|
}
|
2024-08-18 17:02:50 +02:00
|
|
|
}
|
2024-08-18 15:29:04 +02:00
|
|
|
|
2024-08-18 17:02:50 +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-19 15:51:51 +02:00
|
|
|
var cameraSensitivity = UserSettings.Singleton.GetCameraSpeedMultipliers();
|
|
|
|
CurrentYaw += yawInput * cameraSensitivity.X * (float)delta;
|
2024-08-18 17:02:50 +02:00
|
|
|
if (Eye != null) {
|
2024-08-19 15:51:51 +02:00
|
|
|
CurrentPitch += pitchInput * cameraSensitivity.Y * (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;
|
|
|
|
}
|
2024-08-19 17:19:03 +02:00
|
|
|
|
|
|
|
float windRange = MaxVolumeWindHeight - MinVolumeWindHeight;
|
|
|
|
float windVol = windRange == 0 ? 1 : Mathf.Clamp((Position.Y - MinVolumeWindHeight) / windRange, 0, 1);
|
|
|
|
if (windVol != 0 && !HeightBasedWindPlayer.Playing) {
|
|
|
|
HeightBasedWindPlayer.Play();
|
|
|
|
}
|
|
|
|
HeightBasedWindPlayer.VolumeDb = Mathf.LinearToDb(windVol);
|
|
|
|
|
|
|
|
if (IsOnFloor() && Velocity.Length() > 1) {
|
|
|
|
FootstepCooldown -= (float)delta * Velocity.Length();
|
|
|
|
if (IsInstanceValid(FootstepPlayer) && FootstepCooldown < 0) {
|
|
|
|
FootstepPlayer.GlobalPosition = GlobalPosition;
|
|
|
|
FootstepPlayer.Play();
|
|
|
|
FootstepCooldown += FootstepNoiseInterval;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
FootstepCooldown = 0;
|
|
|
|
}
|
2024-08-18 17:02:50 +02:00
|
|
|
}
|
2024-08-18 15:29:04 +02:00
|
|
|
|
2024-08-18 17:02:50 +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;
|
2024-08-20 00:51:43 +02:00
|
|
|
|
|
|
|
if (NoclipEnabled) {
|
|
|
|
GlobalPosition += move * MovementSpeed * 10 * (float)delta;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-18 20:06:11 +02:00
|
|
|
move.Y = 0;
|
|
|
|
move = move.Normalized() * Mathf.Min(1, moveInput.Length()) * MovementSpeed;
|
2024-08-18 17:02:50 +02:00
|
|
|
if (Input.IsActionPressed("sprint")) {
|
|
|
|
move *= SprintMultiplier;
|
2024-08-18 15:29:04 +02:00
|
|
|
}
|
2024-08-18 17:28:11 +02:00
|
|
|
|
2024-08-18 17:02:50 +02:00
|
|
|
Vector3 vel = Velocity;
|
|
|
|
vel.X = move.X;
|
|
|
|
vel.Z = move.Z;
|
2024-08-18 20:37:57 +02:00
|
|
|
vel += Vector3.Down * Gravity * (float)delta;
|
|
|
|
bool tooLow = Position.Y < 0;
|
|
|
|
bool askedToJump = (IsOnFloor() || tooLow) && JumpBufferTime > 0;
|
|
|
|
if (askedToJump) {
|
2024-08-18 17:28:11 +02:00
|
|
|
JumpBufferTime = 0;
|
|
|
|
vel += Vector3.Up * JumpVelocity;
|
|
|
|
}
|
2024-08-18 20:37:57 +02:00
|
|
|
if (tooLow && vel.Y < 0) {
|
|
|
|
vel.Y = 0;
|
|
|
|
}
|
2024-08-18 17:02:50 +02:00
|
|
|
Velocity = vel;
|
2024-08-18 17:28:11 +02:00
|
|
|
|
2024-08-18 17:02:50 +02:00
|
|
|
MoveAndSlide();
|
2024-08-18 15:29:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|