source: Studio/Player.gd@ c8c895d

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

Updated the INI file to hold the basic player stats like health. Made the slider for the mouse sensitivity work

  • Property mode set to 100644
File size: 9.5 KB
Line 
1extends KinematicBody
2
3# Load Config File
4var player_data = {}
5var player_config = ConfigFile.new()
6var player_filepath = "res://config/player.ini"
7
8# Popup menus
9var Pause_Popup_tscn = preload("res://Pause_Popup.tscn")
10var Dead_Popup_tscn = preload("res://Dead_Popup.tscn")
11
12###################-VARIABLES-####################
13
14# Camera
15export(NodePath) var head_path = "Head"
16export(NodePath) var cam_path = "Head/Camera"
17export(float) var FOV = 80.0
18var mouse_axis := Vector2()
19onready var head: Spatial = get_node(head_path)
20onready var cam: Camera = get_node(cam_path)
21
22# Move
23var velocity := Vector3()
24var direction := Vector3()
25var move_axis := Vector2()
26var snap := Vector3() # Connecting to ground
27var falling := Vector3()
28
29# Damage
30var max_fall := Vector3() # Total disatance falled down
31var fall_threshold_damage = 17 # distance down a player has to fall before taking damage
32
33# Walk and Sprint
34const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
35export(float) var gravity = 20.0
36export(int) var walk_speed = 10
37export(int) var sprint_speed = 16
38export(int) var acceleration = 8
39export(int) var deacceleration = 10
40export(float, 0.0, 1.0, 0.05) var air_control = 0.3
41export(int) var jump_height = 12
42var _speed: int
43var _is_sprinting_input := false
44var _is_jumping_input := false
45var sprint_enabled := true
46var hold_walk := false
47var stamina_delay = 0
48
49# Grounded
50enum GROUNDED_STATE {
51 GROUNDED,
52 MIDAIR,
53 TOUCHDOWN
54}
55var player_state = GROUNDED_STATE.GROUNDED
56
57var player = "player"
58var exhausted_delay = 30
59var sprint_energy_consumption = 0.5
60var stamina_regen_speed = 0.5
61var mana_needed_to_heal = 30
62var mana_heal_ammount = 10
63var mana_regen_speed = 0.01
64var health = 100
65var stamina = 100
66var mana = 100
67var lifeforce = 100
68var xp = 0
69var dead = false
70
71##################################################
72
73# Called when the node enters the scene tree
74func _ready() -> void:
75 if player_config.load(player_filepath) == OK:
76 mana_needed_to_heal = player_config.get_value(player, "mana_needed_to_heal")
77 mana_heal_ammount = player_config.get_value(player, "mana_heal_ammount")
78 mana_regen_speed = player_config.get_value(player, "mana_regen_speed")
79 fall_threshold_damage = player_config.get_value(player, "fall_threshold_damage")
80 gravity = player_config.get_value(player, "gravity")
81 walk_speed = player_config.get_value(player, "walk_speed")
82 exhausted_delay = player_config.get_value(player, "exhausted_delay")
83 stamina_regen_speed = player_config.get_value(player, "stamina_regen_speed")
84 sprint_speed = player_config.get_value(player, "sprint_speed")
85 sprint_energy_consumption = player_config.get_value(player, "sprint_energy_consumption")
86 jump_height = player_config.get_value(player, "jump_height")
87
88 else:
89 print("Player Config File Not Found - Using defaults")
90
91
92 # Remove this later, this is just a temp line until future code is avalable for each player to have its own stats
93 Globals.dead = false
94
95 Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
96 cam.fov = FOV
97
98# Called every frame. 'delta' is the elapsed time since the previous frame
99func _process(_delta: float) -> void:
100 move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
101 move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
102
103 if Globals.dead:
104 Globals.player_dead()
105
106 if Globals.food_task:
107 get_node("HUD/Control/LifeforceBar").value = Globals.lifeforce
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
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
127# Recapture mouse when resuming
128 if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE && !Globals.dead:
129 Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
130
131# Called every physics tick. 'delta' is constant
132func _physics_process(delta: float) -> void:
133 walk(delta)
134
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:
148 if falling.length() >= fall_threshold_damage and max_fall.y <= -fall_threshold_damage:
149 Globals.health -= round(-(max_fall.y + fall_threshold_damage))
150 Globals.stamina -= round(-(max_fall.y + fall_threshold_damage))
151 if round(-(max_fall.y + fall_threshold_damage)) > 5:
152 Globals.health -= round(-(max_fall.y + fall_threshold_damage)*9)
153 else:
154 if round(-(max_fall.y + fall_threshold_damage)) > 10:
155 Globals.health -= round(-(max_fall.y + fall_threshold_damage)*15)
156 if Globals.health <= 0:
157 Globals.lifeforce += Globals.health * 2
158 if Globals.lifeforce <= 0:
159 Globals.health = 0
160 Globals.lifeforce = 0
161 Globals.dead = true
162 else:
163 Globals.health = 1
164 Globals.mana = 0
165 Globals.stamina = 0
166 stamina_delay = exhausted_delay
167 sprint_enabled = false
168 get_node("HUD/Control/LifeforceBar").value = Globals.lifeforce
169 get_node("HUD/Control/ManaBar").value = Globals.mana
170 get_node("Head/AnimationPlayer").play("FallDamage")
171 get_node("HUD/Control/HealthBar").value = Globals.health
172 max_fall = Vector3.ZERO
173 player_state = GROUNDED_STATE.GROUNDED
174
175
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
182# Heal button
183 if Input.is_action_just_pressed("heal") && Globals.mana >= mana_needed_to_heal:
184 if Globals.health != 100:
185 Globals.mana -= mana_needed_to_heal
186 if 100 < Globals.health + mana_heal_ammount:
187 Globals.health = 100
188 elif Globals.health == 0:
189 Globals.health = mana_heal_ammount
190 else:
191 Globals.health += mana_heal_ammount
192 get_node("HUD/Control/HealthBar").value = Globals.health
193 get_node("HUD/Control/ManaBar").value = Globals.mana
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 * (Globals.mouse_sensitivity / 100)
229 var vertical: float = -mouse_axis.y * (Globals.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():
296 if Globals.stamina >= 0 && stamina_delay <= 1:
297 _speed = sprint_speed
298 cam.set_fov(lerp(cam.fov, FOV * 1.25, delta * 8))
299 Globals.stamina -= sprint_energy_consumption
300 if Globals.stamina <= 0:
301 stamina_delay = exhausted_delay
302 sprint_enabled = false
303 cam.fov = FOV
304 _speed = walk_speed
305 return
306 get_node("HUD/Control/StaminaBar").value = Globals.stamina
307 else:
308 _speed = walk_speed
309 cam.set_fov(lerp(cam.fov, FOV, delta * 8))
310 if stamina_delay >= 0:
311 stamina_delay -= 0.1
312 sprint_enabled = true
313 elif Globals.stamina <= 100:
314 if velocity.x == 0:
315 Globals.stamina += stamina_regen_speed * 2
316 else:
317 Globals.stamina += stamina_regen_speed
318 elif Globals.stamina >= 100 && Globals.mana < 100:
319 if velocity.x == 0:
320 Globals.mana += mana_regen_speed * 2
321 else:
322 Globals.mana += mana_regen_speed
323 get_node("HUD/Control/ManaBar").value = Globals.mana
324 get_node("HUD/Control/StaminaBar").value = Globals.stamina
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.