forked from OverfortGames/LandscapeDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_controller.gd
121 lines (93 loc) · 3.38 KB
/
camera_controller.gd
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
extends CharacterBody3D
const INPUT_SPRINT : String = "sprint";
const INPUT_CROUCH : String = "crouch";
const INPUT_LEFT : String = "left";
const INPUT_RIGHT : String = "right";
const INPUT_UP : String = "up";
const INPUT_DOWN : String = "down";
const INPUT_TOGGLE_GRAVITY : String = "toggle_gravity";
const INPUT_TOGGLE_CURSOR : String = "toggle_cursor";
const GRAVITY : float = 9.81;
@export_group("Editor References")
@export
var camera : Camera3D;
@export_group("Settings")
@export
var normalSpeed : float = 20;
@export
var sprintSpeed : float = 60;
@export
var crouchSpeed : float = 10;
@export
var decelerationLerpSpeed : float = 5.0;
@export
var gravityMultiplier = 1;
@export
var cameraSensitivity : float = 10;
@export
var cameraClamp : Vector2 = Vector2(-80,80);
var inputSprintPressed : bool;
var inputCrouchPressed : bool;
var inputDirection : Vector2;
var inputToggleGravityPressedThisFrame: bool;
var inputToggleCursorPressedThisFrame: bool;
var currentVelocity : Vector3;
var newCameraRotation : Vector3;
var cursorEnabled : bool;
var gravityEnabled : bool;
func _ready() -> void:
pass;
func _unhandled_input(event: InputEvent) -> void:
if(inputToggleCursorPressedThisFrame == true):
return;
if event is InputEventMouseMotion:
newCameraRotation.y -= event.relative.x * cameraSensitivity;
newCameraRotation.x -= event.relative.y * cameraSensitivity;
newCameraRotation.x = clamp(newCameraRotation.x, deg_to_rad(cameraClamp.x), deg_to_rad(cameraClamp.y));
func _process(delta):
gather_input();
if(inputToggleGravityPressedThisFrame):
gravityEnabled = !gravityEnabled
if(inputToggleCursorPressedThisFrame):
cursorEnabled = !cursorEnabled
if(cursorEnabled):
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE;
else:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED;
if(inputToggleCursorPressedThisFrame == false):
camera.rotation = newCameraRotation;
calculate_current_velocity(delta);
velocity = currentVelocity;
move_and_slide();
pass;
func gather_input() -> void:
inputSprintPressed = Input.is_action_pressed(INPUT_SPRINT);
inputCrouchPressed = Input.is_action_pressed(INPUT_CROUCH);
inputDirection = Input.get_vector(INPUT_LEFT, INPUT_RIGHT, INPUT_UP, INPUT_DOWN);
inputToggleGravityPressedThisFrame = Input.is_action_just_pressed(INPUT_TOGGLE_GRAVITY);
inputToggleCursorPressedThisFrame =Input.is_action_just_pressed(INPUT_TOGGLE_CURSOR);
pass;
func calculate_current_velocity(deltaFloat: float) -> void:
var speed = normalSpeed;
if(inputSprintPressed):
speed = sprintSpeed;
if(inputCrouchPressed):
speed = crouchSpeed;
# Get the input direction and handle the movement/deceleration.
var direction = (camera.basis * Vector3(inputDirection.x, 0, inputDirection.y)).normalized();
if direction.length_squared() > 0.5:
currentVelocity.x = direction.x * speed;
currentVelocity.z = direction.z * speed;
if(gravityEnabled == false):
currentVelocity.y = direction.y * speed;
else:
currentVelocity.x = lerp(currentVelocity.x, direction.x * speed, deltaFloat * decelerationLerpSpeed);
if(gravityEnabled == false):
currentVelocity.y = 0;
currentVelocity.z = lerp(currentVelocity.z, direction.z * speed, deltaFloat * decelerationLerpSpeed);
add_gravity(deltaFloat);
func add_gravity(deltaFloat: float) -> void:
if(gravityEnabled == false):
return;
if(is_on_floor() == false):
currentVelocity.y -= GRAVITY * gravityMultiplier * deltaFloat;