62 lines
2.0 KiB
C#
62 lines
2.0 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class player : CharacterBody3D
|
|
{
|
|
[Export] public const float Speed = 5.0f;
|
|
[Export] public const float JumpVelocity = 5f;
|
|
[Export] public const float gravity = 14f;
|
|
[Export(PropertyHint.Range, "0.1,1.0")] float mouseSensitivity = 0.3f;
|
|
[Export(PropertyHint.Range, "-90,0,1")] float minMousePitch = -90f;
|
|
[Export(PropertyHint.Range, "0,90,1")] float maxMousePitch = 50f;
|
|
public float allDelta;
|
|
public Marker3D camCenter;
|
|
|
|
public override void _Ready()
|
|
{
|
|
camCenter = GetNode<Marker3D>("camera_center");
|
|
}
|
|
public override void _Process(double delta)
|
|
{
|
|
allDelta = (float)delta;
|
|
if (Input.IsActionJustPressed("uncapture_mouse")) Input.MouseMode = Input.MouseModeEnum.Visible;
|
|
if (Input.IsMouseButtonPressed(MouseButton.Left)) Input.MouseMode = Input.MouseModeEnum.Captured;
|
|
}
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
Vector3 velocity = Velocity;
|
|
|
|
if (!IsOnFloor())
|
|
velocity.y -= gravity * allDelta;
|
|
|
|
if (Input.IsActionJustPressed("move_jump") && IsOnFloor() && Input.MouseMode == Input.MouseModeEnum.Captured)
|
|
velocity.y = JumpVelocity;
|
|
|
|
Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_backward");
|
|
Vector3 direction = new Vector3(inputDir.x, 0, inputDir.y).Rotated(Vector3.Up, camCenter.Rotation.y).Normalized();
|
|
if (direction != Vector3.Zero)
|
|
{
|
|
velocity.x = direction.x * Speed;
|
|
velocity.z = direction.z * Speed;
|
|
}
|
|
else
|
|
{
|
|
velocity.x = Mathf.MoveToward(Velocity.x, 0, Speed);
|
|
velocity.z = Mathf.MoveToward(Velocity.z, 0, Speed);
|
|
}
|
|
Velocity = velocity;
|
|
MoveAndSlide();
|
|
}
|
|
public override void _Input(InputEvent @event)
|
|
{
|
|
if (@event is InputEventMouseMotion mouseMotion && Input.MouseMode == Input.MouseModeEnum.Captured)
|
|
{
|
|
Vector3 camRot = camCenter.RotationDegrees;
|
|
camRot.y -= mouseMotion.Relative.x * mouseSensitivity;
|
|
camRot.x -= mouseMotion.Relative.y * mouseSensitivity;
|
|
camRot.x = Mathf.Clamp(camRot.x, minMousePitch, maxMousePitch);
|
|
camCenter.RotationDegrees = camRot;
|
|
}
|
|
}
|
|
}
|