1 | extends KinematicBody
|
---|
2 |
|
---|
3 | var Pause_Popup_tscn = preload("res://Pause_Popup.tscn")
|
---|
4 | var Pause_Popup
|
---|
5 |
|
---|
6 | ###################-VARIABLES-####################
|
---|
7 |
|
---|
8 | # Camera
|
---|
9 | export(float) var mouse_sensitivity = 8.0
|
---|
10 | export(NodePath) var head_path = "Head"
|
---|
11 | export(NodePath) var cam_path = "Head/Camera"
|
---|
12 | export(float) var FOV = 80.0
|
---|
13 | var mouse_axis := Vector2()
|
---|
14 | onready var head: Spatial = get_node(head_path)
|
---|
15 | onready var cam: Camera = get_node(cam_path)
|
---|
16 | # Move
|
---|
17 | var velocity := Vector3()
|
---|
18 | var direction := Vector3()
|
---|
19 | var move_axis := Vector2()
|
---|
20 | var snap := Vector3()
|
---|
21 | var sprint_enabled := true
|
---|
22 | var sprinting := false
|
---|
23 | # Walk
|
---|
24 | const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
|
---|
25 | export(float) var gravity = 30.0
|
---|
26 | export(int) var walk_speed = 10
|
---|
27 | export(int) var sprint_speed = 16
|
---|
28 | export(int) var acceleration = 8
|
---|
29 | export(int) var deacceleration = 10
|
---|
30 | export(float, 0.0, 1.0, 0.05) var air_control = 0.3
|
---|
31 | export(int) var jump_height = 12
|
---|
32 | var _speed: int
|
---|
33 | var _is_sprinting_input := false
|
---|
34 | var _is_jumping_input := false
|
---|
35 |
|
---|
36 | ##################################################
|
---|
37 |
|
---|
38 | # Called when the node enters the scene tree
|
---|
39 | func _ready() -> void:
|
---|
40 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
---|
41 | cam.fov = FOV
|
---|
42 |
|
---|
43 | # Called every frame. 'delta' is the elapsed time since the previous frame
|
---|
44 | func _process(_delta: float) -> void:
|
---|
45 | move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
|
---|
46 | move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
|
---|
47 |
|
---|
48 | if Input.is_action_just_pressed("move_jump"):
|
---|
49 | _is_jumping_input = true
|
---|
50 |
|
---|
51 | if Input.is_action_pressed("move_sprint"):
|
---|
52 | _is_sprinting_input = true
|
---|
53 |
|
---|
54 | # Recapture mouse when resuming
|
---|
55 | if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
|
---|
56 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
---|
57 |
|
---|
58 |
|
---|
59 | # Called every physics tick. 'delta' is constant
|
---|
60 | func _physics_process(delta: float) -> void:
|
---|
61 | walk(delta)
|
---|
62 |
|
---|
63 |
|
---|
64 | # Called when there is an input event
|
---|
65 | func _input(event: InputEvent) -> void:
|
---|
66 | if event is InputEventMouseMotion:
|
---|
67 | mouse_axis = event.relative
|
---|
68 | camera_rotation()
|
---|
69 |
|
---|
70 |
|
---|
71 | func walk(delta: float) -> void:
|
---|
72 | direction_input()
|
---|
73 |
|
---|
74 | if is_on_floor():
|
---|
75 | snap = -get_floor_normal() - get_floor_velocity() * delta
|
---|
76 |
|
---|
77 | # Workaround for sliding down after jump on slope
|
---|
78 | if velocity.y < 0:
|
---|
79 | velocity.y = 0
|
---|
80 |
|
---|
81 | jump()
|
---|
82 | else:
|
---|
83 | # Workaround for 'vertical bump' when going off platform
|
---|
84 | if snap != Vector3.ZERO && velocity.y != 0:
|
---|
85 | velocity.y = 0
|
---|
86 |
|
---|
87 | snap = Vector3.ZERO
|
---|
88 |
|
---|
89 | velocity.y -= gravity * delta
|
---|
90 |
|
---|
91 | sprint(delta)
|
---|
92 |
|
---|
93 | accelerate(delta)
|
---|
94 |
|
---|
95 | velocity = move_and_slide_with_snap(velocity, snap, Vector3.UP, true, 4, FLOOR_MAX_ANGLE)
|
---|
96 | _is_jumping_input = false
|
---|
97 | _is_sprinting_input = false
|
---|
98 |
|
---|
99 |
|
---|
100 | func camera_rotation() -> void:
|
---|
101 | if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
|
---|
102 | return
|
---|
103 | if mouse_axis.length() > 0:
|
---|
104 | var horizontal: float = -mouse_axis.x * (mouse_sensitivity / 100)
|
---|
105 | var vertical: float = -mouse_axis.y * (mouse_sensitivity / 100)
|
---|
106 |
|
---|
107 | mouse_axis = Vector2()
|
---|
108 |
|
---|
109 | rotate_y(deg2rad(horizontal))
|
---|
110 | head.rotate_x(deg2rad(vertical))
|
---|
111 |
|
---|
112 | # Clamp mouse rotation
|
---|
113 | var temp_rot: Vector3 = head.rotation_degrees
|
---|
114 | temp_rot.x = clamp(temp_rot.x, -90, 90)
|
---|
115 | head.rotation_degrees = temp_rot
|
---|
116 |
|
---|
117 |
|
---|
118 | func direction_input() -> void:
|
---|
119 | direction = Vector3()
|
---|
120 | var aim: Basis = get_global_transform().basis
|
---|
121 | if move_axis.x >= 0.5:
|
---|
122 | direction -= aim.z
|
---|
123 | if move_axis.x <= -0.5:
|
---|
124 | direction += aim.z
|
---|
125 | if move_axis.y <= -0.5:
|
---|
126 | direction -= aim.x
|
---|
127 | if move_axis.y >= 0.5:
|
---|
128 | direction += aim.x
|
---|
129 | direction.y = 0
|
---|
130 | direction = direction.normalized()
|
---|
131 |
|
---|
132 |
|
---|
133 | func accelerate(delta: float) -> void:
|
---|
134 | # Where would the player go
|
---|
135 | var _temp_vel: Vector3 = velocity
|
---|
136 | var _temp_accel: float
|
---|
137 | var _target: Vector3 = direction * _speed
|
---|
138 |
|
---|
139 | _temp_vel.y = 0
|
---|
140 | if direction.dot(_temp_vel) > 0:
|
---|
141 | _temp_accel = acceleration
|
---|
142 |
|
---|
143 | else:
|
---|
144 | _temp_accel = deacceleration
|
---|
145 |
|
---|
146 | if not is_on_floor():
|
---|
147 | _temp_accel *= air_control
|
---|
148 |
|
---|
149 | # Interpolation
|
---|
150 | _temp_vel = _temp_vel.linear_interpolate(_target, _temp_accel * delta)
|
---|
151 |
|
---|
152 | velocity.x = _temp_vel.x
|
---|
153 | velocity.z = _temp_vel.z
|
---|
154 |
|
---|
155 | # Make too low values zero
|
---|
156 | if direction.dot(velocity) == 0:
|
---|
157 | var _vel_clamp := 0.01
|
---|
158 | if abs(velocity.x) < _vel_clamp:
|
---|
159 | velocity.x = 0
|
---|
160 | if abs(velocity.z) < _vel_clamp:
|
---|
161 | velocity.z = 0
|
---|
162 |
|
---|
163 |
|
---|
164 | func jump() -> void:
|
---|
165 | if _is_jumping_input:
|
---|
166 | velocity.y = jump_height
|
---|
167 | snap = Vector3.ZERO
|
---|
168 |
|
---|
169 |
|
---|
170 | func sprint(delta: float) -> void:
|
---|
171 | if can_sprint():
|
---|
172 | _speed = sprint_speed
|
---|
173 | cam.set_fov(lerp(cam.fov, FOV * 1.25, delta * 8))
|
---|
174 | sprinting = true
|
---|
175 |
|
---|
176 | else:
|
---|
177 | _speed = walk_speed
|
---|
178 | cam.set_fov(lerp(cam.fov, FOV, delta * 8))
|
---|
179 | sprinting = false
|
---|
180 |
|
---|
181 |
|
---|
182 | func can_sprint() -> bool:
|
---|
183 | return (sprint_enabled and is_on_floor() and _is_sprinting_input and move_axis.x >= 0.5)
|
---|