-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Demo.cs
86 lines (67 loc) · 2.97 KB
/
Demo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Godot;
public partial class Demo : CharacterBody3D {
public const float Speed = 15.0f;
public const float JumpVelocity = 10f;
public const float MouseSensitivity = 0.2f;
public float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
[Export] private Camera3D Camera { get;set; }
[Export] public TerraBrush.TerraBrush Terrain { get;set; }
[Export] public Label DebugLabel { get;set; }
public override void _Ready(){
base._Ready();
Terrain.Connect(TerraBrush.StringNames.TerrainLoaded, Callable.From(() => GD.Print("TerrainLoaded")));
}
public override void _Input(InputEvent e) {
if (e.IsPressed() && !e.IsEcho() && Input.IsKeyPressed(Key.Escape)) {
Input.MouseMode = Input.MouseMode == Input.MouseModeEnum.Captured ? Input.MouseModeEnum.Visible : Input.MouseModeEnum.Captured;
}
if (e is InputEventMouseMotion) {
var mouseMotion = (InputEventMouseMotion) e;
RotateY(Mathf.DegToRad(-mouseMotion.Relative.X * MouseSensitivity));
Camera.RotateX(Mathf.DegToRad(-mouseMotion.Relative.Y * MouseSensitivity));
var resultRotation = Camera.Rotation;
resultRotation.X = Mathf.Clamp(resultRotation.X, Mathf.DegToRad(-85), Mathf.DegToRad(85));
Camera.Rotation = resultRotation;
}
}
public override void _PhysicsProcess(double delta) {
Vector3 velocity = Velocity;
if (!IsOnFloor()) {
velocity.Y -= gravity * (float)delta;
}
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor()) {
velocity.Y = JumpVelocity;
}
Vector2 inputDir = Input.GetVector("left", "right", "forward", "backward");
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();
var playerX = GlobalPosition.X;
var playerZ = GlobalPosition.Z;
var debugText = $"[Press ESC to grab/release mouse] FPS: {Engine.GetFramesPerSecond()}";
if (IsOnFloor() && GetLastSlideCollision() != null) {
var collision = GetLastSlideCollision();
if (collision?.GetCollider() == Terrain?.Terrain?.TerrainCollider) {
var result = Terrain.GetPositionInformation(playerX, playerZ);
if (result != null) {
debugText = $"{debugText} | Current collision : Water {result.WaterFactor}, Deep : {result.WaterDeepness} | Snow {result.SnowFactor}, Height : {result.SnowHeight} | Main Texture {(result.Textures?.Length > 0 ? result.Textures?[0].Factor : "")} - {(result.Textures?.Length > 0 ? result.Textures?[0].Name : "" )}";
} else {
debugText = $"{debugText} : No zone";
}
} else {
debugText = $"{debugText} : Not colliding with terrain";
}
}
if (IsOnFloor()) {
Terrain.AddInteractionPoint(playerX, playerZ);
}
DebugLabel.Text = debugText;
}
}