source: Studio/Player.gd@ 25e7b67

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

Got Food item to increase player stats by 10 when walking into the food space.

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