[f7b09c8] | 1 | extends KinematicBody
|
---|
| 2 |
|
---|
[f9161ba] | 3 | # Load Config File
|
---|
| 4 | var player_data = {}
|
---|
| 5 | var player_config = ConfigFile.new()
|
---|
| 6 | var player_filepath = "res://config/player.ini"
|
---|
[70fb6d4] | 7 |
|
---|
[994b0da] | 8 | # Popup menus
|
---|
[f7b09c8] | 9 | var Pause_Popup_tscn = preload("res://Pause_Popup.tscn")
|
---|
[aa1db83] | 10 | var Dead_Popup_tscn = preload("res://Dead_Popup.tscn")
|
---|
[f7b09c8] | 11 |
|
---|
| 12 | ###################-VARIABLES-####################
|
---|
| 13 |
|
---|
| 14 | # Camera
|
---|
| 15 | export(NodePath) var head_path = "Head"
|
---|
| 16 | export(NodePath) var cam_path = "Head/Camera"
|
---|
| 17 | export(float) var FOV = 80.0
|
---|
| 18 | var mouse_axis := Vector2()
|
---|
| 19 | onready var head: Spatial = get_node(head_path)
|
---|
| 20 | onready var cam: Camera = get_node(cam_path)
|
---|
[aa1db83] | 21 |
|
---|
[f7b09c8] | 22 | # Move
|
---|
| 23 | var velocity := Vector3()
|
---|
| 24 | var direction := Vector3()
|
---|
| 25 | var move_axis := Vector2()
|
---|
[70fb6d4] | 26 | var snap := Vector3() # Connecting to ground
|
---|
| 27 | var falling := Vector3()
|
---|
[cc65f28] | 28 | var player_loc := Vector3()
|
---|
[70fb6d4] | 29 |
|
---|
[aa1db83] | 30 | # Walk and Sprint
|
---|
[f7b09c8] | 31 | const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
|
---|
[70fb6d4] | 32 | export(float) var gravity = 20.0
|
---|
[f7b09c8] | 33 | export(int) var walk_speed = 10
|
---|
| 34 | export(int) var sprint_speed = 16
|
---|
| 35 | export(int) var acceleration = 8
|
---|
| 36 | export(int) var deacceleration = 10
|
---|
| 37 | export(float, 0.0, 1.0, 0.05) var air_control = 0.3
|
---|
| 38 | export(int) var jump_height = 12
|
---|
| 39 | var _speed: int
|
---|
| 40 | var _is_sprinting_input := false
|
---|
| 41 | var _is_jumping_input := false
|
---|
[aa1db83] | 42 | var sprint_enabled := true
|
---|
| 43 | var hold_walk := false
|
---|
[1b6ba32] | 44 | var stamina_delay = 0
|
---|
| 45 |
|
---|
[5a9a237] | 46 | # Climb stairs
|
---|
[cc65f28] | 47 | var foot_direction = 0.0
|
---|
| 48 | var _is_step_up = false
|
---|
| 49 | var hold_jump_height
|
---|
[5a9a237] | 50 |
|
---|
[aa1db83] | 51 | # Grounded
|
---|
[70fb6d4] | 52 | enum GROUNDED_STATE {
|
---|
| 53 | GROUNDED,
|
---|
| 54 | MIDAIR,
|
---|
| 55 | TOUCHDOWN
|
---|
| 56 | }
|
---|
| 57 | var player_state = GROUNDED_STATE.GROUNDED
|
---|
[aa1db83] | 58 |
|
---|
[5a9a237] | 59 | # Player defaults if not found in INI file
|
---|
[c8c895d] | 60 | var player = "player"
|
---|
[1b6ba32] | 61 | var exhausted_delay = 30
|
---|
| 62 | var sprint_energy_consumption = 0.5
|
---|
[f9161ba] | 63 | var stamina_regen_speed = 0.5
|
---|
[aa1db83] | 64 | var mana_needed_to_heal = 30
|
---|
| 65 | var mana_heal_ammount = 10
|
---|
[f9161ba] | 66 | var mana_regen_speed = 0.01
|
---|
[c8c895d] | 67 | var health = 100
|
---|
| 68 | var stamina = 100
|
---|
| 69 | var mana = 100
|
---|
| 70 | var lifeforce = 100
|
---|
| 71 | var xp = 0
|
---|
| 72 | var dead = false
|
---|
[f7b09c8] | 73 |
|
---|
[5a9a237] | 74 |
|
---|
| 75 | # Damage
|
---|
| 76 | var max_fall := Vector3() # Total disatance falled down
|
---|
| 77 | var fall_threshold_damage = 17 # distance down a player has to fall before taking damage
|
---|
| 78 |
|
---|
| 79 |
|
---|
[f7b09c8] | 80 | ##################################################
|
---|
| 81 |
|
---|
| 82 | # Called when the node enters the scene tree
|
---|
| 83 | func _ready() -> void:
|
---|
[f9161ba] | 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")
|
---|
[1b6ba32] | 91 | exhausted_delay = player_config.get_value(player, "exhausted_delay")
|
---|
| 92 | stamina_regen_speed = player_config.get_value(player, "stamina_regen_speed")
|
---|
[f9161ba] | 93 | sprint_speed = player_config.get_value(player, "sprint_speed")
|
---|
[1b6ba32] | 94 | sprint_energy_consumption = player_config.get_value(player, "sprint_energy_consumption")
|
---|
[f9161ba] | 95 | jump_height = player_config.get_value(player, "jump_height")
|
---|
| 96 |
|
---|
| 97 | else:
|
---|
| 98 | print("Player Config File Not Found - Using defaults")
|
---|
| 99 |
|
---|
[ef2ca13] | 100 |
|
---|
| 101 | # Remove this later, this is just a temp line until future code is avalable for each player to have its own stats
|
---|
[25e7b67] | 102 | Globals.dead = false
|
---|
[ef2ca13] | 103 |
|
---|
[f7b09c8] | 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
|
---|
| 108 | func _process(_delta: float) -> void:
|
---|
[aa1db83] | 109 | move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
|
---|
[f7b09c8] | 110 | move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
|
---|
[cc65f28] | 111 | check_foot(_delta)
|
---|
| 112 |
|
---|
[25e7b67] | 113 | if Globals.dead:
|
---|
[994b0da] | 114 | Globals.player_dead()
|
---|
[f7b09c8] | 115 |
|
---|
[25e7b67] | 116 | if Globals.food_task:
|
---|
| 117 | get_node("HUD/Control/LifeforceBar").value = Globals.lifeforce
|
---|
| 118 |
|
---|
[f7b09c8] | 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 |
|
---|
[aa1db83] | 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 |
|
---|
[f7b09c8] | 137 | # Recapture mouse when resuming
|
---|
[25e7b67] | 138 | if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE && !Globals.dead:
|
---|
[ef2ca13] | 139 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
---|
[f7b09c8] | 140 |
|
---|
| 141 | # Called every physics tick. 'delta' is constant
|
---|
| 142 | func _physics_process(delta: float) -> void:
|
---|
| 143 | walk(delta)
|
---|
[cc65f28] | 144 | # get_foot_direction()
|
---|
[f7b09c8] | 145 |
|
---|
[70fb6d4] | 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:
|
---|
[f9161ba] | 159 | if falling.length() >= fall_threshold_damage and max_fall.y <= -fall_threshold_damage:
|
---|
[25e7b67] | 160 | Globals.health -= round(-(max_fall.y + fall_threshold_damage))
|
---|
| 161 | Globals.stamina -= round(-(max_fall.y + fall_threshold_damage))
|
---|
[70fb6d4] | 162 | if round(-(max_fall.y + fall_threshold_damage)) > 5:
|
---|
[25e7b67] | 163 | Globals.health -= round(-(max_fall.y + fall_threshold_damage)*9)
|
---|
[70fb6d4] | 164 | else:
|
---|
| 165 | if round(-(max_fall.y + fall_threshold_damage)) > 10:
|
---|
[25e7b67] | 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
|
---|
[3c8ca52] | 173 | else:
|
---|
[25e7b67] | 174 | Globals.health = 1
|
---|
| 175 | Globals.mana = 0
|
---|
| 176 | Globals.stamina = 0
|
---|
[3c8ca52] | 177 | stamina_delay = exhausted_delay
|
---|
| 178 | sprint_enabled = false
|
---|
[25e7b67] | 179 | get_node("HUD/Control/LifeforceBar").value = Globals.lifeforce
|
---|
| 180 | get_node("HUD/Control/ManaBar").value = Globals.mana
|
---|
[70fb6d4] | 181 | get_node("Head/AnimationPlayer").play("FallDamage")
|
---|
[25e7b67] | 182 | get_node("HUD/Control/HealthBar").value = Globals.health
|
---|
[70fb6d4] | 183 | max_fall = Vector3.ZERO
|
---|
| 184 | player_state = GROUNDED_STATE.GROUNDED
|
---|
| 185 |
|
---|
[f7b09c8] | 186 | # Called when there is an input event
|
---|
| 187 | func _input(event: InputEvent) -> void:
|
---|
| 188 | if event is InputEventMouseMotion:
|
---|
| 189 | mouse_axis = event.relative
|
---|
| 190 | camera_rotation()
|
---|
| 191 |
|
---|
[1b6ba32] | 192 | # Heal button
|
---|
[25e7b67] | 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
|
---|
[3c8ca52] | 200 | else:
|
---|
[25e7b67] | 201 | Globals.health += mana_heal_ammount
|
---|
| 202 | get_node("HUD/Control/HealthBar").value = Globals.health
|
---|
| 203 | get_node("HUD/Control/ManaBar").value = Globals.mana
|
---|
[f7b09c8] | 204 |
|
---|
| 205 | func 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
|
---|
[cc65f28] | 228 | _is_step_up = false
|
---|
[f7b09c8] | 229 |
|
---|
| 230 | func camera_rotation() -> void:
|
---|
| 231 | if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
|
---|
| 232 | return
|
---|
| 233 | if mouse_axis.length() > 0:
|
---|
[25e7b67] | 234 | var horizontal: float = -mouse_axis.x * (Globals.mouse_sensitivity / 100)
|
---|
| 235 | var vertical: float = -mouse_axis.y * (Globals.mouse_sensitivity / 100)
|
---|
[f7b09c8] | 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 |
|
---|
| 248 | func 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
|
---|
[cc65f28] | 253 | foot_direction = 270
|
---|
[f7b09c8] | 254 | if move_axis.x <= -0.5:
|
---|
| 255 | direction += aim.z
|
---|
[cc65f28] | 256 | foot_direction = 90
|
---|
[f7b09c8] | 257 | if move_axis.y <= -0.5:
|
---|
| 258 | direction -= aim.x
|
---|
[cc65f28] | 259 | foot_direction = 0
|
---|
[f7b09c8] | 260 | if move_axis.y >= 0.5:
|
---|
| 261 | direction += aim.x
|
---|
[cc65f28] | 262 | foot_direction = 180
|
---|
[f7b09c8] | 263 | direction.y = 0
|
---|
| 264 | direction = direction.normalized()
|
---|
| 265 |
|
---|
| 266 |
|
---|
| 267 | func 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
|
---|
[cc65f28] | 274 |
|
---|
[f7b09c8] | 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 |
|
---|
| 299 | func jump() -> void:
|
---|
| 300 | if _is_jumping_input:
|
---|
[cc65f28] | 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
|
---|
[f7b09c8] | 308 | snap = Vector3.ZERO
|
---|
| 309 |
|
---|
| 310 | func sprint(delta: float) -> void:
|
---|
| 311 | if can_sprint():
|
---|
[25e7b67] | 312 | if Globals.stamina >= 0 && stamina_delay <= 1:
|
---|
[70fb6d4] | 313 | _speed = sprint_speed
|
---|
| 314 | cam.set_fov(lerp(cam.fov, FOV * 1.25, delta * 8))
|
---|
[25e7b67] | 315 | Globals.stamina -= sprint_energy_consumption
|
---|
| 316 | if Globals.stamina <= 0:
|
---|
[1b6ba32] | 317 | stamina_delay = exhausted_delay
|
---|
[aa1db83] | 318 | sprint_enabled = false
|
---|
| 319 | cam.fov = FOV
|
---|
| 320 | _speed = walk_speed
|
---|
| 321 | return
|
---|
[25e7b67] | 322 | get_node("HUD/Control/StaminaBar").value = Globals.stamina
|
---|
[f7b09c8] | 323 | else:
|
---|
| 324 | _speed = walk_speed
|
---|
| 325 | cam.set_fov(lerp(cam.fov, FOV, delta * 8))
|
---|
[aa1db83] | 326 | if stamina_delay >= 0:
|
---|
[70fb6d4] | 327 | stamina_delay -= 0.1
|
---|
[aa1db83] | 328 | sprint_enabled = true
|
---|
[25e7b67] | 329 | elif Globals.stamina <= 100:
|
---|
[1b6ba32] | 330 | if velocity.x == 0:
|
---|
[25e7b67] | 331 | Globals.stamina += stamina_regen_speed * 2
|
---|
[1b6ba32] | 332 | else:
|
---|
[25e7b67] | 333 | Globals.stamina += stamina_regen_speed
|
---|
| 334 | elif Globals.stamina >= 100 && Globals.mana < 100:
|
---|
[f9161ba] | 335 | if velocity.x == 0:
|
---|
[25e7b67] | 336 | Globals.mana += mana_regen_speed * 2
|
---|
[f9161ba] | 337 | else:
|
---|
[25e7b67] | 338 | Globals.mana += mana_regen_speed
|
---|
| 339 | get_node("HUD/Control/ManaBar").value = Globals.mana
|
---|
| 340 | get_node("HUD/Control/StaminaBar").value = Globals.stamina
|
---|
[f7b09c8] | 341 |
|
---|
| 342 | func can_sprint() -> bool:
|
---|
| 343 | return (sprint_enabled and is_on_floor() and _is_sprinting_input and move_axis.x >= 0.5)
|
---|
[5a9a237] | 344 |
|
---|
[cc65f28] | 345 | # Checks to see if footRay is connecting with a step or sort object it needs to be able to jump over
|
---|
| 346 | func 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
|
---|
[5a9a237] | 354 | snap = Vector3.ZERO
|
---|
[cc65f28] | 355 | accelerate(delta)
|
---|