source: Studio/Player.gd@ 49e3bfa

sandbox
Last change on this file since 49e3bfa was ef2ca13, checked in by Kevin Chapman <victory2b@…>, 3 years ago

Enabled the death Popup when player reaches 0 heath.

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