source: Studio/Player.gd@ 3c8ca52

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

(WIP) About to remove pause menu for testing.

  • Property mode set to 100644
File size: 9.1 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 Dead_Popup_thingy = preload("res://Dead_Popup.gd")
12var Dead_Popup = Dead_Popup_thingy.new()
13
14
15###################-VARIABLES-####################
16
17# Camera
18export(float) var mouse_sensitivity = 8.0
19export(NodePath) var head_path = "Head"
20export(NodePath) var cam_path = "Head/Camera"
21export(float) var FOV = 80.0
22var mouse_axis := Vector2()
23onready var head: Spatial = get_node(head_path)
24onready var cam: Camera = get_node(cam_path)
25
26# Move
27var velocity := Vector3()
28var direction := Vector3()
29var move_axis := Vector2()
30var snap := Vector3() # Connecting to ground
31var falling := Vector3()
32
33# Damage
34var max_fall := Vector3() # Total disatance falled down
35var fall_threshold_damage = 17 # distance down a player has to fall before taking damage
36
37# Walk and Sprint
38const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
39export(float) var gravity = 20.0
40export(int) var walk_speed = 10
41export(int) var sprint_speed = 16
42export(int) var acceleration = 8
43export(int) var deacceleration = 10
44export(float, 0.0, 1.0, 0.05) var air_control = 0.3
45export(int) var jump_height = 12
46var _speed: int
47var _is_sprinting_input := false
48var _is_jumping_input := false
49var sprint_enabled := true
50var hold_walk := false
51var stamina_delay = 0
52
53# Grounded
54enum GROUNDED_STATE {
55 GROUNDED,
56 MIDAIR,
57 TOUCHDOWN
58}
59var player_state = GROUNDED_STATE.GROUNDED
60
61# Player Stats
62var health = 100
63var stamina = 100
64var mana = 100
65var lifeforce = 100
66var xp = 0
67var dead = false
68
69var exhausted_delay = 30
70var sprint_energy_consumption = 0.5
71var stamina_regen_speed = 0.5
72var mana_needed_to_heal = 30
73var mana_heal_ammount = 10
74var mana_regen_speed = 0.01
75var player = "player"
76
77##################################################
78
79# Called when the node enters the scene tree
80func _ready() -> void:
81 if player_config.load(player_filepath) == OK:
82 mana_needed_to_heal = player_config.get_value(player, "mana_needed_to_heal")
83 mana_heal_ammount = player_config.get_value(player, "mana_heal_ammount")
84 mana_regen_speed = player_config.get_value(player, "mana_regen_speed")
85 fall_threshold_damage = player_config.get_value(player, "fall_threshold_damage")
86 gravity = player_config.get_value(player, "gravity")
87 walk_speed = player_config.get_value(player, "walk_speed")
88 exhausted_delay = player_config.get_value(player, "exhausted_delay")
89 stamina_regen_speed = player_config.get_value(player, "stamina_regen_speed")
90 sprint_speed = player_config.get_value(player, "sprint_speed")
91 sprint_energy_consumption = player_config.get_value(player, "sprint_energy_consumption")
92 jump_height = player_config.get_value(player, "jump_height")
93
94 else:
95 print("Player Config File Not Found - Using defaults")
96
97 Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
98 cam.fov = FOV
99
100# Called every frame. 'delta' is the elapsed time since the previous frame
101func _process(_delta: float) -> void:
102 move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
103 move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
104
105 if dead:
106 Dead_Popup._on_dead()
107# get_node(Dead_Popup_tscn).popup()
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:
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 health -= round(-(max_fall.y + fall_threshold_damage))
150 stamina -= round(-(max_fall.y + fall_threshold_damage))
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)
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
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
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") && mana >= mana_needed_to_heal:
184 if health != 100:
185 mana -= mana_needed_to_heal
186 if 100 < health + mana_heal_ammount:
187 health = 100
188 elif health == 0:
189 health = mana_heal_ammount
190 else:
191 health += mana_heal_ammount
192 get_node("HUD/Control/HealthBar").value = health
193 get_node("HUD/Control/ManaBar").value = 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 * (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():
296 if stamina >= 0 && stamina_delay <= 1:
297 _speed = sprint_speed
298 cam.set_fov(lerp(cam.fov, FOV * 1.25, delta * 8))
299 stamina -= sprint_energy_consumption
300 if 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 = 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 stamina <= 100:
314 if velocity.x == 0:
315 stamina += stamina_regen_speed * 2
316 else:
317 stamina += stamina_regen_speed
318 elif stamina >= 100 && mana < 100:
319 if velocity.x == 0:
320 mana += mana_regen_speed * 2
321 else:
322 mana += mana_regen_speed
323 get_node("HUD/Control/ManaBar").value = mana
324 get_node("HUD/Control/StaminaBar").value = 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.