source: Studio/Player.gd@ cc65f28

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

Made it so player will auto walk up short stairs

  • Property mode set to 100644
File size: 10.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()
28var player_loc := Vector3()
29
30# Walk and Sprint
31const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
32export(float) var gravity = 20.0
33export(int) var walk_speed = 10
34export(int) var sprint_speed = 16
35export(int) var acceleration = 8
36export(int) var deacceleration = 10
37export(float, 0.0, 1.0, 0.05) var air_control = 0.3
38export(int) var jump_height = 12
39var _speed: int
40var _is_sprinting_input := false
41var _is_jumping_input := false
42var sprint_enabled := true
43var hold_walk := false
44var stamina_delay = 0
45
46# Climb stairs
47var foot_direction = 0.0
48var _is_step_up = false
49var hold_jump_height
50
51# Grounded
52enum GROUNDED_STATE {
53 GROUNDED,
54 MIDAIR,
55 TOUCHDOWN
56}
57var player_state = GROUNDED_STATE.GROUNDED
58
59# Player defaults if not found in INI file
60var player = "player"
61var exhausted_delay = 30
62var sprint_energy_consumption = 0.5
63var stamina_regen_speed = 0.5
64var mana_needed_to_heal = 30
65var mana_heal_ammount = 10
66var mana_regen_speed = 0.01
67var health = 100
68var stamina = 100
69var mana = 100
70var lifeforce = 100
71var xp = 0
72var dead = false
73
74
75# Damage
76var max_fall := Vector3() # Total disatance falled down
77var fall_threshold_damage = 17 # distance down a player has to fall before taking damage
78
79
80##################################################
81
82# Called when the node enters the scene tree
83func _ready() -> void:
84 if player_config.load(player_filepath) == OK:
85 mana_needed_to_heal = player_config.get_value(player, "mana_needed_to_heal")
86 mana_heal_ammount = player_config.get_value(player, "mana_heal_ammount")
87 mana_regen_speed = player_config.get_value(player, "mana_regen_speed")
88 fall_threshold_damage = player_config.get_value(player, "fall_threshold_damage")
89 gravity = player_config.get_value(player, "gravity")
90 walk_speed = player_config.get_value(player, "walk_speed")
91 exhausted_delay = player_config.get_value(player, "exhausted_delay")
92 stamina_regen_speed = player_config.get_value(player, "stamina_regen_speed")
93 sprint_speed = player_config.get_value(player, "sprint_speed")
94 sprint_energy_consumption = player_config.get_value(player, "sprint_energy_consumption")
95 jump_height = player_config.get_value(player, "jump_height")
96
97 else:
98 print("Player Config File Not Found - Using defaults")
99
100
101 # Remove this later, this is just a temp line until future code is avalable for each player to have its own stats
102 Globals.dead = false
103
104 Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
105 cam.fov = FOV
106
107# Called every frame. 'delta' is the elapsed time since the previous frame
108func _process(_delta: float) -> void:
109 move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
110 move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
111 check_foot(_delta)
112
113 if Globals.dead:
114 Globals.player_dead()
115
116 if Globals.food_task:
117 get_node("HUD/Control/LifeforceBar").value = Globals.lifeforce
118
119 if Input.is_action_just_pressed("move_jump"):
120 _is_jumping_input = true
121
122 if Input.is_action_pressed("move_sprint"):
123 _is_sprinting_input = true
124
125 if Input.is_action_just_pressed("hold_move_forward"):
126 if hold_walk == true:
127 hold_walk = false
128 else:
129 hold_walk = true
130
131 if Input.is_action_just_pressed("move_forward") || Input.is_action_just_pressed("move_backward"):
132 hold_walk = false
133
134 if hold_walk == true:
135 move_axis.x = hold_walk
136
137# Recapture mouse when resuming
138 if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE && !Globals.dead:
139 Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
140
141# Called every physics tick. 'delta' is constant
142func _physics_process(delta: float) -> void:
143 walk(delta)
144# get_foot_direction()
145
146# Fall damage calculation
147 match player_state:
148 GROUNDED_STATE.GROUNDED:
149 falling = Vector3.ZERO
150 if not is_on_floor():
151 player_state = GROUNDED_STATE.MIDAIR
152 GROUNDED_STATE.MIDAIR:
153 falling += Vector3.DOWN * gravity * delta
154 if velocity.y <= -fall_threshold_damage:
155 max_fall = velocity
156 if is_on_floor():
157 player_state = GROUNDED_STATE.TOUCHDOWN
158 GROUNDED_STATE.TOUCHDOWN:
159 if falling.length() >= fall_threshold_damage and max_fall.y <= -fall_threshold_damage:
160 Globals.health -= round(-(max_fall.y + fall_threshold_damage))
161 Globals.stamina -= round(-(max_fall.y + fall_threshold_damage))
162 if round(-(max_fall.y + fall_threshold_damage)) > 5:
163 Globals.health -= round(-(max_fall.y + fall_threshold_damage)*9)
164 else:
165 if round(-(max_fall.y + fall_threshold_damage)) > 10:
166 Globals.health -= round(-(max_fall.y + fall_threshold_damage)*15)
167 if Globals.health <= 0:
168 Globals.lifeforce += Globals.health * 2
169 if Globals.lifeforce <= 0:
170 Globals.health = 0
171 Globals.lifeforce = 0
172 Globals.dead = true
173 else:
174 Globals.health = 1
175 Globals.mana = 0
176 Globals.stamina = 0
177 stamina_delay = exhausted_delay
178 sprint_enabled = false
179 get_node("HUD/Control/LifeforceBar").value = Globals.lifeforce
180 get_node("HUD/Control/ManaBar").value = Globals.mana
181 get_node("Head/AnimationPlayer").play("FallDamage")
182 get_node("HUD/Control/HealthBar").value = Globals.health
183 max_fall = Vector3.ZERO
184 player_state = GROUNDED_STATE.GROUNDED
185
186# Called when there is an input event
187func _input(event: InputEvent) -> void:
188 if event is InputEventMouseMotion:
189 mouse_axis = event.relative
190 camera_rotation()
191
192# Heal button
193 if Input.is_action_just_pressed("heal") && Globals.mana >= mana_needed_to_heal:
194 if Globals.health != 100:
195 Globals.mana -= mana_needed_to_heal
196 if 100 < Globals.health + mana_heal_ammount:
197 Globals.health = 100
198 elif Globals.health == 0:
199 Globals.health = mana_heal_ammount
200 else:
201 Globals.health += mana_heal_ammount
202 get_node("HUD/Control/HealthBar").value = Globals.health
203 get_node("HUD/Control/ManaBar").value = Globals.mana
204
205func walk(delta: float) -> void:
206 direction_input()
207
208 if is_on_floor():
209 snap = -get_floor_normal() - get_floor_velocity() * delta
210 # Workaround for sliding down after jump on slope
211 if velocity.y < 0:
212 velocity.y = 0
213 jump()
214 else:
215 # Workaround for 'vertical bump' when going off platform
216 if snap != Vector3.ZERO && velocity.y != 0:
217 velocity.y = 0
218 snap = Vector3.ZERO
219 velocity.y -= gravity * delta
220
221 sprint(delta)
222
223 accelerate(delta)
224
225 velocity = move_and_slide_with_snap(velocity, snap, Vector3.UP, true, 4, FLOOR_MAX_ANGLE)
226 _is_jumping_input = false
227 _is_sprinting_input = false
228 _is_step_up = false
229
230func camera_rotation() -> void:
231 if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
232 return
233 if mouse_axis.length() > 0:
234 var horizontal: float = -mouse_axis.x * (Globals.mouse_sensitivity / 100)
235 var vertical: float = -mouse_axis.y * (Globals.mouse_sensitivity / 100)
236
237 mouse_axis = Vector2()
238
239 rotate_y(deg2rad(horizontal))
240 head.rotate_x(deg2rad(vertical))
241
242 # Clamp mouse rotation
243 var temp_rot: Vector3 = head.rotation_degrees
244 temp_rot.x = clamp(temp_rot.x, -90, 90)
245 head.rotation_degrees = temp_rot
246
247
248func direction_input() -> void:
249 direction = Vector3()
250 var aim: Basis = get_global_transform().basis
251 if move_axis.x >= 0.5:
252 direction -= aim.z
253 foot_direction = 270
254 if move_axis.x <= -0.5:
255 direction += aim.z
256 foot_direction = 90
257 if move_axis.y <= -0.5:
258 direction -= aim.x
259 foot_direction = 0
260 if move_axis.y >= 0.5:
261 direction += aim.x
262 foot_direction = 180
263 direction.y = 0
264 direction = direction.normalized()
265
266
267func accelerate(delta: float) -> void:
268 # Where would the player go
269 var _temp_vel: Vector3 = velocity
270 var _temp_accel: float
271 var _target: Vector3 = direction * _speed
272
273 _temp_vel.y = 0
274
275 if direction.dot(_temp_vel) > 0:
276 _temp_accel = acceleration
277
278 else:
279 _temp_accel = deacceleration
280
281 if not is_on_floor():
282 _temp_accel *= air_control
283
284 # Interpolation
285 _temp_vel = _temp_vel.linear_interpolate(_target, _temp_accel * delta)
286
287 velocity.x = _temp_vel.x
288 velocity.z = _temp_vel.z
289
290 # Make too low values zero
291 if direction.dot(velocity) == 0:
292 var _vel_clamp := 0.01
293 if abs(velocity.x) < _vel_clamp:
294 velocity.x = 0
295 if abs(velocity.z) < _vel_clamp:
296 velocity.z = 0
297
298
299func jump() -> void:
300 if _is_jumping_input:
301 # Checked to see if this is triggerd by the stepray or by jump key and applies correct action
302 if _is_step_up == true:
303 # Prevents jumping infront of steps when standing still
304 if velocity.x > 0.1 || velocity.y > 0.1 || velocity.x < -0.1 || velocity.y < -0.1:
305 velocity.y = 4
306 else:
307 velocity.y = jump_height
308 snap = Vector3.ZERO
309
310func sprint(delta: float) -> void:
311 if can_sprint():
312 if Globals.stamina >= 0 && stamina_delay <= 1:
313 _speed = sprint_speed
314 cam.set_fov(lerp(cam.fov, FOV * 1.25, delta * 8))
315 Globals.stamina -= sprint_energy_consumption
316 if Globals.stamina <= 0:
317 stamina_delay = exhausted_delay
318 sprint_enabled = false
319 cam.fov = FOV
320 _speed = walk_speed
321 return
322 get_node("HUD/Control/StaminaBar").value = Globals.stamina
323 else:
324 _speed = walk_speed
325 cam.set_fov(lerp(cam.fov, FOV, delta * 8))
326 if stamina_delay >= 0:
327 stamina_delay -= 0.1
328 sprint_enabled = true
329 elif Globals.stamina <= 100:
330 if velocity.x == 0:
331 Globals.stamina += stamina_regen_speed * 2
332 else:
333 Globals.stamina += stamina_regen_speed
334 elif Globals.stamina >= 100 && Globals.mana < 100:
335 if velocity.x == 0:
336 Globals.mana += mana_regen_speed * 2
337 else:
338 Globals.mana += mana_regen_speed
339 get_node("HUD/Control/ManaBar").value = Globals.mana
340 get_node("HUD/Control/StaminaBar").value = Globals.stamina
341
342func can_sprint() -> bool:
343 return (sprint_enabled and is_on_floor() and _is_sprinting_input and move_axis.x >= 0.5)
344
345# Checks to see if footRay is connecting with a step or sort object it needs to be able to jump over
346func check_foot(delta: float) -> void:
347 if is_on_floor():
348 var player_foot = $Leg/Foot/FootRay
349 for footray in $Leg/Foot.get_children():
350 footray.rotation.x = (foot_direction)/180.0*PI
351 if player_foot.is_colliding():
352 _is_step_up = true
353 _is_jumping_input = true
354 snap = Vector3.ZERO
355 accelerate(delta)
Note: See TracBrowser for help on using the repository browser.