52 lines
No EOL
1.6 KiB
C#
52 lines
No EOL
1.6 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
namespace MinImm;
|
|
|
|
public partial class Player : CharacterBody3D
|
|
{
|
|
[Export] public Camera3D Camera { get; set; }
|
|
[Export] public float Speed { get; set; } = 5f;
|
|
[Export] public float Gravity { get; set; } = 20f;
|
|
|
|
public override void _Ready()
|
|
{
|
|
Input.SetMouseMode(Input.MouseModeEnum.Captured);
|
|
}
|
|
|
|
public override void _UnhandledInput(InputEvent @event)
|
|
{
|
|
if (@event is InputEventMouseMotion mouseMotion)
|
|
{
|
|
RotationDegrees = RotationDegrees with { Y = RotationDegrees.Y - mouseMotion.ScreenRelative.X * 0.5f };
|
|
Camera.RotationDegrees = Camera.RotationDegrees with
|
|
{
|
|
X = Math.Clamp(Camera.RotationDegrees.X - mouseMotion.ScreenRelative.Y * 0.2f, -60, 60)
|
|
};
|
|
}
|
|
else if (@event.IsActionPressed("ui_cancel"))
|
|
{
|
|
Input.SetMouseMode(Input.MouseModeEnum.Visible);
|
|
}
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
var inputDirection = Input.GetVector("move_left", "move_right", "move_forward", "move_backward");
|
|
var input3D = new Vector3(inputDirection.X, 0, inputDirection.Y);
|
|
var direction = Transform.Basis * input3D;
|
|
|
|
var velY = (float)(Velocity.Y - Gravity * delta);
|
|
if (Input.IsActionJustPressed("jump") && IsOnFloor())
|
|
{
|
|
velY = 10f;
|
|
}
|
|
else if (Input.IsActionJustReleased("jump") && velY > 0)
|
|
{
|
|
velY = 0f;
|
|
}
|
|
|
|
Velocity = Velocity with { X = direction.X * Speed, Y = velY, Z = direction.Z * Speed };
|
|
MoveAndSlide();
|
|
}
|
|
} |