source: Studio/Player.gd@ 1b6ba32

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

Converting more hard coded values into ones stored int the config ini file.

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