carotrauma/scenes/sandbox/player/SandboxPlayer.cs
Nick Guy de59b2486a
All checks were successful
/ build (push) Successful in 1m41s
Commit of C# project
2024-05-27 19:42:11 +01:00

70 lines
1.9 KiB
C#

using Godot;
using System;
public partial class SandboxPlayer : CharacterBody3D
{
[Export]
public float Speed = 5.0f;
[Export]
public float JumpVelocity = 4.5f;
// Get the gravity from the project settings to be synced with RigidBody nodes.
public float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
private float camera_anglev = 0;
private Camera3D cam;
public override void _Ready() {
cam = GetChild<Camera3D>(3, true);
}
public static float ToRadians (float degrees) {
// Angle in 10th of a degree
return (float)((degrees * Math.PI)/180.0f);
}
public override void _Input(InputEvent @event) {
if (@event is InputEventMouseMotion) {
var M = (InputEventMouseMotion)@event;
cam.RotateY(ToRadians(-M.Relative.X * 0.3f));
var changeV = -M.Relative.Y * 0.3f;
if (camera_anglev + changeV > -50 && camera_anglev + changeV < 50) {
camera_anglev += changeV;
cam.RotateX(ToRadians(changeV));
}
GetViewport().SetInputAsHandled();
}
}
public override void _PhysicsProcess(double delta) {
Vector3 velocity = Velocity;
// Add the gravity.
if (!IsOnFloor())
velocity.Y -= gravity * (float)delta;
// Handle Jump.
if (Input.IsActionJustPressed("jump") && IsOnFloor())
velocity.Y = JumpVelocity;
// Get the input direction and handle the movement/deceleration.
// As good practice, you should replace UI actions with custom gameplay actions.
Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_back");;
Vector3 direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.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();
}
}