using Godot; namespace Gmtk24 { public partial class Player : CharacterBody3D { [Export] public float MovementSpeed = 7; [Export] public float SprintMultiplier = 1.5f; [Export] public float JumpVelocity = 10; [Export] public float JumpBufferLengthSeconds = 0.2f; [Export] public Node3D Eye; private float CurrentYaw = 0; private float CurrentPitch = 0; private float JumpBufferTime = 0; public override void _Ready() { Input.MouseMode = Input.MouseModeEnum.Captured; } public override void _UnhandledInput(InputEvent @event) { if (@event is InputEventMouseMotion ev && Input.MouseMode == Input.MouseModeEnum.Captured) { 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(); } } 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"); 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; 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; } } 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; } Vector3 vel = Velocity; vel.X = move.X; vel.Z = move.Z; vel += GetGravity() * (float)delta; if (IsOnFloor() && JumpBufferTime > 0) { JumpBufferTime = 0; vel += Vector3.Up * JumpVelocity; } Velocity = vel; MoveAndSlide(); } } }