1 | extends KinematicBody
|
---|
2 |
|
---|
3 | # Load Config File
|
---|
4 | var player_data = {}
|
---|
5 | var player_config = ConfigFile.new()
|
---|
6 | var player_filepath = "res://config/player.ini"
|
---|
7 |
|
---|
8 | # Popup menus
|
---|
9 | var Pause_Popup_tscn = preload("res://Pause_Popup.tscn")
|
---|
10 | var Dead_Popup_tscn = preload("res://Dead_Popup.tscn")
|
---|
11 |
|
---|
12 | ###################-VARIABLES-####################
|
---|
13 |
|
---|
14 | # Camera
|
---|
15 | export(float) var mouse_sensitivity = 8.0
|
---|
16 | export(NodePath) var head_path = "Head"
|
---|
17 | export(NodePath) var cam_path = "Head/Camera"
|
---|
18 | export(float) var FOV = 80.0
|
---|
19 | var mouse_axis := Vector2()
|
---|
20 | onready var head: Spatial = get_node(head_path)
|
---|
21 | onready var cam: Camera = get_node(cam_path)
|
---|
22 |
|
---|
23 | # Move
|
---|
24 | var velocity := Vector3()
|
---|
25 | var direction := Vector3()
|
---|
26 | var move_axis := Vector2()
|
---|
27 | var snap := Vector3() # Connecting to ground
|
---|
28 | var falling := Vector3()
|
---|
29 |
|
---|
30 | # Damage
|
---|
31 | var max_fall := Vector3() # Total disatance falled down
|
---|
32 | var fall_threshold_damage = 17 # distance down a player has to fall before taking damage
|
---|
33 |
|
---|
34 | # Walk and Sprint
|
---|
35 | const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
|
---|
36 | export(float) var gravity = 20.0
|
---|
37 | export(int) var walk_speed = 10
|
---|
38 | export(int) var sprint_speed = 16
|
---|
39 | export(int) var acceleration = 8
|
---|
40 | export(int) var deacceleration = 10
|
---|
41 | export(float, 0.0, 1.0, 0.05) var air_control = 0.3
|
---|
42 | export(int) var jump_height = 12
|
---|
43 | var _speed: int
|
---|
44 | var _is_sprinting_input := false
|
---|
45 | var _is_jumping_input := false
|
---|
46 | var sprint_enabled := true
|
---|
47 | var hold_walk := false
|
---|
48 | var stamina_delay = 0
|
---|
49 |
|
---|
50 | # Grounded
|
---|
51 | enum GROUNDED_STATE {
|
---|
52 | GROUNDED,
|
---|
53 | MIDAIR,
|
---|
54 | TOUCHDOWN
|
---|
55 | }
|
---|
56 | var player_state = GROUNDED_STATE.GROUNDED
|
---|
57 |
|
---|
58 | # Player Stats
|
---|
59 | var health = 100
|
---|
60 | var stamina = 100
|
---|
61 | var mana = 100
|
---|
62 | var lifeforce = 100
|
---|
63 | var xp = 0
|
---|
64 | var dead = false
|
---|
65 |
|
---|
66 | var exhausted_delay = 30
|
---|
67 | var sprint_energy_consumption = 0.5
|
---|
68 | var stamina_regen_speed = 0.5
|
---|
69 | var mana_needed_to_heal = 30
|
---|
70 | var mana_heal_ammount = 10
|
---|
71 | var mana_regen_speed = 0.01
|
---|
72 | var player = "player"
|
---|
73 |
|
---|
74 | ##################################################
|
---|
75 |
|
---|
76 | # Called when the node enters the scene tree
|
---|
77 | func _ready() -> void:
|
---|
78 | if player_config.load(player_filepath) == OK:
|
---|
79 | mana_needed_to_heal = player_config.get_value(player, "mana_needed_to_heal")
|
---|
80 | mana_heal_ammount = player_config.get_value(player, "mana_heal_ammount")
|
---|
81 | mana_regen_speed = player_config.get_value(player, "mana_regen_speed")
|
---|
82 | fall_threshold_damage = player_config.get_value(player, "fall_threshold_damage")
|
---|
83 | gravity = player_config.get_value(player, "gravity")
|
---|
84 | walk_speed = player_config.get_value(player, "walk_speed")
|
---|
85 | exhausted_delay = player_config.get_value(player, "exhausted_delay")
|
---|
86 | stamina_regen_speed = player_config.get_value(player, "stamina_regen_speed")
|
---|
87 | sprint_speed = player_config.get_value(player, "sprint_speed")
|
---|
88 | sprint_energy_consumption = player_config.get_value(player, "sprint_energy_consumption")
|
---|
89 | jump_height = player_config.get_value(player, "jump_height")
|
---|
90 |
|
---|
91 | else:
|
---|
92 | print("Player Config File Not Found - Using defaults")
|
---|
93 |
|
---|
94 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
---|
95 | cam.fov = FOV
|
---|
96 |
|
---|
97 | # Called every frame. 'delta' is the elapsed time since the previous frame
|
---|
98 | func _process(_delta: float) -> void:
|
---|
99 | move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
|
---|
100 | move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
|
---|
101 |
|
---|
102 | if dead:
|
---|
103 | Globals.player_dead()
|
---|
104 |
|
---|
105 | if Input.is_action_just_pressed("move_jump"):
|
---|
106 | _is_jumping_input = true
|
---|
107 |
|
---|
108 | if Input.is_action_pressed("move_sprint"):
|
---|
109 | _is_sprinting_input = true
|
---|
110 |
|
---|
111 | if Input.is_action_just_pressed("hold_move_forward"):
|
---|
112 | if hold_walk == true:
|
---|
113 | hold_walk = false
|
---|
114 | else:
|
---|
115 | hold_walk = true
|
---|
116 |
|
---|
117 | if Input.is_action_just_pressed("move_forward") || Input.is_action_just_pressed("move_backward"):
|
---|
118 | hold_walk = false
|
---|
119 |
|
---|
120 | if hold_walk == true:
|
---|
121 | move_axis.x = hold_walk
|
---|
122 |
|
---|
123 | # Recapture mouse when resuming
|
---|
124 | if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
|
---|
125 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
---|
126 |
|
---|
127 | # Called every physics tick. 'delta' is constant
|
---|
128 | func _physics_process(delta: float) -> void:
|
---|
129 | walk(delta)
|
---|
130 |
|
---|
131 | # Fall damage calculation
|
---|
132 | match player_state:
|
---|
133 | GROUNDED_STATE.GROUNDED:
|
---|
134 | falling = Vector3.ZERO
|
---|
135 | if not is_on_floor():
|
---|
136 | player_state = GROUNDED_STATE.MIDAIR
|
---|
137 | GROUNDED_STATE.MIDAIR:
|
---|
138 | falling += Vector3.DOWN * gravity * delta
|
---|
139 | if velocity.y <= -fall_threshold_damage:
|
---|
140 | max_fall = velocity
|
---|
141 | if is_on_floor():
|
---|
142 | player_state = GROUNDED_STATE.TOUCHDOWN
|
---|
143 | GROUNDED_STATE.TOUCHDOWN:
|
---|
144 | if falling.length() >= fall_threshold_damage and max_fall.y <= -fall_threshold_damage:
|
---|
145 | health -= round(-(max_fall.y + fall_threshold_damage))
|
---|
146 | stamina -= round(-(max_fall.y + fall_threshold_damage))
|
---|
147 | if round(-(max_fall.y + fall_threshold_damage)) > 5:
|
---|
148 | health -= round(-(max_fall.y + fall_threshold_damage)*9)
|
---|
149 | else:
|
---|
150 | if round(-(max_fall.y + fall_threshold_damage)) > 10:
|
---|
151 | health -= round(-(max_fall.y + fall_threshold_damage)*15)
|
---|
152 | if health <= 0:
|
---|
153 | lifeforce += health * 2
|
---|
154 | if lifeforce <= 0:
|
---|
155 | health = 0
|
---|
156 | lifeforce = 0
|
---|
157 | dead = true
|
---|
158 | else:
|
---|
159 | health = 1
|
---|
160 | mana = 0
|
---|
161 | stamina = 0
|
---|
162 | stamina_delay = exhausted_delay
|
---|
163 | sprint_enabled = false
|
---|
164 | get_node("HUD/Control/LifeforceBar").value = lifeforce
|
---|
165 | get_node("HUD/Control/ManaBar").value = mana
|
---|
166 | get_node("Head/AnimationPlayer").play("FallDamage")
|
---|
167 | get_node("HUD/Control/HealthBar").value = health
|
---|
168 | max_fall = Vector3.ZERO
|
---|
169 | player_state = GROUNDED_STATE.GROUNDED
|
---|
170 |
|
---|
171 |
|
---|
172 | # Called when there is an input event
|
---|
173 | func _input(event: InputEvent) -> void:
|
---|
174 | if event is InputEventMouseMotion:
|
---|
175 | mouse_axis = event.relative
|
---|
176 | camera_rotation()
|
---|
177 |
|
---|
178 | # Heal button
|
---|
179 | if Input.is_action_just_pressed("heal") && mana >= mana_needed_to_heal:
|
---|
180 | if health != 100:
|
---|
181 | mana -= mana_needed_to_heal
|
---|
182 | if 100 < health + mana_heal_ammount:
|
---|
183 | health = 100
|
---|
184 | elif health == 0:
|
---|
185 | health = mana_heal_ammount
|
---|
186 | else:
|
---|
187 | health += mana_heal_ammount
|
---|
188 | get_node("HUD/Control/HealthBar").value = health
|
---|
189 | get_node("HUD/Control/ManaBar").value = mana
|
---|
190 |
|
---|
191 | func walk(delta: float) -> void:
|
---|
192 | direction_input()
|
---|
193 |
|
---|
194 | if is_on_floor():
|
---|
195 | snap = -get_floor_normal() - get_floor_velocity() * delta
|
---|
196 |
|
---|
197 | # Workaround for sliding down after jump on slope
|
---|
198 | if velocity.y < 0:
|
---|
199 | velocity.y = 0
|
---|
200 |
|
---|
201 | jump()
|
---|
202 | else:
|
---|
203 | # Workaround for 'vertical bump' when going off platform
|
---|
204 | if snap != Vector3.ZERO && velocity.y != 0:
|
---|
205 | velocity.y = 0
|
---|
206 |
|
---|
207 | snap = Vector3.ZERO
|
---|
208 |
|
---|
209 | velocity.y -= gravity * delta
|
---|
210 |
|
---|
211 | sprint(delta)
|
---|
212 |
|
---|
213 | accelerate(delta)
|
---|
214 |
|
---|
215 | velocity = move_and_slide_with_snap(velocity, snap, Vector3.UP, true, 4, FLOOR_MAX_ANGLE)
|
---|
216 | _is_jumping_input = false
|
---|
217 | _is_sprinting_input = false
|
---|
218 |
|
---|
219 |
|
---|
220 | func camera_rotation() -> void:
|
---|
221 | if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
|
---|
222 | return
|
---|
223 | if mouse_axis.length() > 0:
|
---|
224 | var horizontal: float = -mouse_axis.x * (mouse_sensitivity / 100)
|
---|
225 | var vertical: float = -mouse_axis.y * (mouse_sensitivity / 100)
|
---|
226 |
|
---|
227 | mouse_axis = Vector2()
|
---|
228 |
|
---|
229 | rotate_y(deg2rad(horizontal))
|
---|
230 | head.rotate_x(deg2rad(vertical))
|
---|
231 |
|
---|
232 | # Clamp mouse rotation
|
---|
233 | var temp_rot: Vector3 = head.rotation_degrees
|
---|
234 | temp_rot.x = clamp(temp_rot.x, -90, 90)
|
---|
235 | head.rotation_degrees = temp_rot
|
---|
236 |
|
---|
237 |
|
---|
238 | func direction_input() -> void:
|
---|
239 | direction = Vector3()
|
---|
240 | var aim: Basis = get_global_transform().basis
|
---|
241 | if move_axis.x >= 0.5:
|
---|
242 | direction -= aim.z
|
---|
243 | if move_axis.x <= -0.5:
|
---|
244 | direction += aim.z
|
---|
245 | if move_axis.y <= -0.5:
|
---|
246 | direction -= aim.x
|
---|
247 | if move_axis.y >= 0.5:
|
---|
248 | direction += aim.x
|
---|
249 | direction.y = 0
|
---|
250 | direction = direction.normalized()
|
---|
251 |
|
---|
252 |
|
---|
253 | func accelerate(delta: float) -> void:
|
---|
254 | # Where would the player go
|
---|
255 | var _temp_vel: Vector3 = velocity
|
---|
256 | var _temp_accel: float
|
---|
257 | var _target: Vector3 = direction * _speed
|
---|
258 |
|
---|
259 | _temp_vel.y = 0
|
---|
260 | if direction.dot(_temp_vel) > 0:
|
---|
261 | _temp_accel = acceleration
|
---|
262 |
|
---|
263 | else:
|
---|
264 | _temp_accel = deacceleration
|
---|
265 |
|
---|
266 | if not is_on_floor():
|
---|
267 | _temp_accel *= air_control
|
---|
268 |
|
---|
269 | # Interpolation
|
---|
270 | _temp_vel = _temp_vel.linear_interpolate(_target, _temp_accel * delta)
|
---|
271 |
|
---|
272 | velocity.x = _temp_vel.x
|
---|
273 | velocity.z = _temp_vel.z
|
---|
274 |
|
---|
275 | # Make too low values zero
|
---|
276 | if direction.dot(velocity) == 0:
|
---|
277 | var _vel_clamp := 0.01
|
---|
278 | if abs(velocity.x) < _vel_clamp:
|
---|
279 | velocity.x = 0
|
---|
280 | if abs(velocity.z) < _vel_clamp:
|
---|
281 | velocity.z = 0
|
---|
282 |
|
---|
283 |
|
---|
284 | func jump() -> void:
|
---|
285 | if _is_jumping_input:
|
---|
286 | velocity.y = jump_height
|
---|
287 | snap = Vector3.ZERO
|
---|
288 |
|
---|
289 |
|
---|
290 | func sprint(delta: float) -> void:
|
---|
291 | if can_sprint():
|
---|
292 | if stamina >= 0 && stamina_delay <= 1:
|
---|
293 | _speed = sprint_speed
|
---|
294 | cam.set_fov(lerp(cam.fov, FOV * 1.25, delta * 8))
|
---|
295 | stamina -= sprint_energy_consumption
|
---|
296 | if stamina <= 0:
|
---|
297 | stamina_delay = exhausted_delay
|
---|
298 | sprint_enabled = false
|
---|
299 | cam.fov = FOV
|
---|
300 | _speed = walk_speed
|
---|
301 | return
|
---|
302 | get_node("HUD/Control/StaminaBar").value = stamina
|
---|
303 | else:
|
---|
304 | _speed = walk_speed
|
---|
305 | cam.set_fov(lerp(cam.fov, FOV, delta * 8))
|
---|
306 | if stamina_delay >= 0:
|
---|
307 | stamina_delay -= 0.1
|
---|
308 | sprint_enabled = true
|
---|
309 | elif stamina <= 100:
|
---|
310 | if velocity.x == 0:
|
---|
311 | stamina += stamina_regen_speed * 2
|
---|
312 | else:
|
---|
313 | stamina += stamina_regen_speed
|
---|
314 | elif stamina >= 100 && mana < 100:
|
---|
315 | if velocity.x == 0:
|
---|
316 | mana += mana_regen_speed * 2
|
---|
317 | else:
|
---|
318 | mana += mana_regen_speed
|
---|
319 | get_node("HUD/Control/ManaBar").value = mana
|
---|
320 | get_node("HUD/Control/StaminaBar").value = stamina
|
---|
321 |
|
---|
322 | func can_sprint() -> bool:
|
---|
323 | return (sprint_enabled and is_on_floor() and _is_sprinting_input and move_axis.x >= 0.5)
|
---|