[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()
|
---|
| 28 |
|
---|
| 29 | # Damage
|
---|
| 30 | var max_fall := Vector3() # Total disatance falled down
|
---|
| 31 | var fall_threshold_damage = 17 # distance down a player has to fall before taking damage
|
---|
| 32 |
|
---|
[aa1db83] | 33 | # Walk and Sprint
|
---|
[f7b09c8] | 34 | const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
|
---|
[70fb6d4] | 35 | export(float) var gravity = 20.0
|
---|
[f7b09c8] | 36 | export(int) var walk_speed = 10
|
---|
| 37 | export(int) var sprint_speed = 16
|
---|
| 38 | export(int) var acceleration = 8
|
---|
| 39 | export(int) var deacceleration = 10
|
---|
| 40 | export(float, 0.0, 1.0, 0.05) var air_control = 0.3
|
---|
| 41 | export(int) var jump_height = 12
|
---|
| 42 | var _speed: int
|
---|
| 43 | var _is_sprinting_input := false
|
---|
| 44 | var _is_jumping_input := false
|
---|
[aa1db83] | 45 | var sprint_enabled := true
|
---|
| 46 | var hold_walk := false
|
---|
[1b6ba32] | 47 | var stamina_delay = 0
|
---|
| 48 |
|
---|
[aa1db83] | 49 | # Grounded
|
---|
[70fb6d4] | 50 | enum GROUNDED_STATE {
|
---|
| 51 | GROUNDED,
|
---|
| 52 | MIDAIR,
|
---|
| 53 | TOUCHDOWN
|
---|
| 54 | }
|
---|
| 55 | var player_state = GROUNDED_STATE.GROUNDED
|
---|
[aa1db83] | 56 |
|
---|
[c8c895d] | 57 | var player = "player"
|
---|
[1b6ba32] | 58 | var exhausted_delay = 30
|
---|
| 59 | var sprint_energy_consumption = 0.5
|
---|
[f9161ba] | 60 | var stamina_regen_speed = 0.5
|
---|
[aa1db83] | 61 | var mana_needed_to_heal = 30
|
---|
| 62 | var mana_heal_ammount = 10
|
---|
[f9161ba] | 63 | var mana_regen_speed = 0.01
|
---|
[c8c895d] | 64 | var health = 100
|
---|
| 65 | var stamina = 100
|
---|
| 66 | var mana = 100
|
---|
| 67 | var lifeforce = 100
|
---|
| 68 | var xp = 0
|
---|
| 69 | var dead = false
|
---|
[f7b09c8] | 70 |
|
---|
| 71 | ##################################################
|
---|
| 72 |
|
---|
| 73 | # Called when the node enters the scene tree
|
---|
| 74 | func _ready() -> void:
|
---|
[f9161ba] | 75 | if player_config.load(player_filepath) == OK:
|
---|
| 76 | mana_needed_to_heal = player_config.get_value(player, "mana_needed_to_heal")
|
---|
| 77 | mana_heal_ammount = player_config.get_value(player, "mana_heal_ammount")
|
---|
| 78 | mana_regen_speed = player_config.get_value(player, "mana_regen_speed")
|
---|
| 79 | fall_threshold_damage = player_config.get_value(player, "fall_threshold_damage")
|
---|
| 80 | gravity = player_config.get_value(player, "gravity")
|
---|
| 81 | walk_speed = player_config.get_value(player, "walk_speed")
|
---|
[1b6ba32] | 82 | exhausted_delay = player_config.get_value(player, "exhausted_delay")
|
---|
| 83 | stamina_regen_speed = player_config.get_value(player, "stamina_regen_speed")
|
---|
[f9161ba] | 84 | sprint_speed = player_config.get_value(player, "sprint_speed")
|
---|
[1b6ba32] | 85 | sprint_energy_consumption = player_config.get_value(player, "sprint_energy_consumption")
|
---|
[f9161ba] | 86 | jump_height = player_config.get_value(player, "jump_height")
|
---|
| 87 |
|
---|
| 88 | else:
|
---|
| 89 | print("Player Config File Not Found - Using defaults")
|
---|
| 90 |
|
---|
[ef2ca13] | 91 |
|
---|
| 92 | # Remove this later, this is just a temp line until future code is avalable for each player to have its own stats
|
---|
[25e7b67] | 93 | Globals.dead = false
|
---|
[ef2ca13] | 94 |
|
---|
[f7b09c8] | 95 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
---|
| 96 | cam.fov = FOV
|
---|
| 97 |
|
---|
| 98 | # Called every frame. 'delta' is the elapsed time since the previous frame
|
---|
| 99 | func _process(_delta: float) -> void:
|
---|
[aa1db83] | 100 | move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
|
---|
[f7b09c8] | 101 | move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
|
---|
[3c8ca52] | 102 |
|
---|
[25e7b67] | 103 | if Globals.dead:
|
---|
[994b0da] | 104 | Globals.player_dead()
|
---|
[f7b09c8] | 105 |
|
---|
[25e7b67] | 106 | if Globals.food_task:
|
---|
| 107 | get_node("HUD/Control/LifeforceBar").value = Globals.lifeforce
|
---|
| 108 |
|
---|
[f7b09c8] | 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 |
|
---|
[aa1db83] | 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 |
|
---|
[f7b09c8] | 127 | # Recapture mouse when resuming
|
---|
[25e7b67] | 128 | if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE && !Globals.dead:
|
---|
[ef2ca13] | 129 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
---|
[f7b09c8] | 130 |
|
---|
| 131 | # Called every physics tick. 'delta' is constant
|
---|
| 132 | func _physics_process(delta: float) -> void:
|
---|
| 133 | walk(delta)
|
---|
| 134 |
|
---|
[70fb6d4] | 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:
|
---|
[f9161ba] | 148 | if falling.length() >= fall_threshold_damage and max_fall.y <= -fall_threshold_damage:
|
---|
[25e7b67] | 149 | Globals.health -= round(-(max_fall.y + fall_threshold_damage))
|
---|
| 150 | Globals.stamina -= round(-(max_fall.y + fall_threshold_damage))
|
---|
[70fb6d4] | 151 | if round(-(max_fall.y + fall_threshold_damage)) > 5:
|
---|
[25e7b67] | 152 | Globals.health -= round(-(max_fall.y + fall_threshold_damage)*9)
|
---|
[70fb6d4] | 153 | else:
|
---|
| 154 | if round(-(max_fall.y + fall_threshold_damage)) > 10:
|
---|
[25e7b67] | 155 | Globals.health -= round(-(max_fall.y + fall_threshold_damage)*15)
|
---|
| 156 | if Globals.health <= 0:
|
---|
| 157 | Globals.lifeforce += Globals.health * 2
|
---|
| 158 | if Globals.lifeforce <= 0:
|
---|
| 159 | Globals.health = 0
|
---|
| 160 | Globals.lifeforce = 0
|
---|
| 161 | Globals.dead = true
|
---|
[3c8ca52] | 162 | else:
|
---|
[25e7b67] | 163 | Globals.health = 1
|
---|
| 164 | Globals.mana = 0
|
---|
| 165 | Globals.stamina = 0
|
---|
[3c8ca52] | 166 | stamina_delay = exhausted_delay
|
---|
| 167 | sprint_enabled = false
|
---|
[25e7b67] | 168 | get_node("HUD/Control/LifeforceBar").value = Globals.lifeforce
|
---|
| 169 | get_node("HUD/Control/ManaBar").value = Globals.mana
|
---|
[70fb6d4] | 170 | get_node("Head/AnimationPlayer").play("FallDamage")
|
---|
[25e7b67] | 171 | get_node("HUD/Control/HealthBar").value = Globals.health
|
---|
[70fb6d4] | 172 | max_fall = Vector3.ZERO
|
---|
| 173 | player_state = GROUNDED_STATE.GROUNDED
|
---|
| 174 |
|
---|
| 175 |
|
---|
[f7b09c8] | 176 | # Called when there is an input event
|
---|
| 177 | func _input(event: InputEvent) -> void:
|
---|
| 178 | if event is InputEventMouseMotion:
|
---|
| 179 | mouse_axis = event.relative
|
---|
| 180 | camera_rotation()
|
---|
| 181 |
|
---|
[1b6ba32] | 182 | # Heal button
|
---|
[25e7b67] | 183 | if Input.is_action_just_pressed("heal") && Globals.mana >= mana_needed_to_heal:
|
---|
| 184 | if Globals.health != 100:
|
---|
| 185 | Globals.mana -= mana_needed_to_heal
|
---|
| 186 | if 100 < Globals.health + mana_heal_ammount:
|
---|
| 187 | Globals.health = 100
|
---|
| 188 | elif Globals.health == 0:
|
---|
| 189 | Globals.health = mana_heal_ammount
|
---|
[3c8ca52] | 190 | else:
|
---|
[25e7b67] | 191 | Globals.health += mana_heal_ammount
|
---|
| 192 | get_node("HUD/Control/HealthBar").value = Globals.health
|
---|
| 193 | get_node("HUD/Control/ManaBar").value = Globals.mana
|
---|
[f7b09c8] | 194 |
|
---|
| 195 | func 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 |
|
---|
| 224 | func camera_rotation() -> void:
|
---|
| 225 | if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
|
---|
| 226 | return
|
---|
| 227 | if mouse_axis.length() > 0:
|
---|
[25e7b67] | 228 | var horizontal: float = -mouse_axis.x * (Globals.mouse_sensitivity / 100)
|
---|
| 229 | var vertical: float = -mouse_axis.y * (Globals.mouse_sensitivity / 100)
|
---|
[f7b09c8] | 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 |
|
---|
| 242 | func 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 |
|
---|
| 257 | func 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 |
|
---|
| 288 | func jump() -> void:
|
---|
| 289 | if _is_jumping_input:
|
---|
| 290 | velocity.y = jump_height
|
---|
| 291 | snap = Vector3.ZERO
|
---|
| 292 |
|
---|
| 293 |
|
---|
| 294 | func sprint(delta: float) -> void:
|
---|
| 295 | if can_sprint():
|
---|
[25e7b67] | 296 | if Globals.stamina >= 0 && stamina_delay <= 1:
|
---|
[70fb6d4] | 297 | _speed = sprint_speed
|
---|
| 298 | cam.set_fov(lerp(cam.fov, FOV * 1.25, delta * 8))
|
---|
[25e7b67] | 299 | Globals.stamina -= sprint_energy_consumption
|
---|
| 300 | if Globals.stamina <= 0:
|
---|
[1b6ba32] | 301 | stamina_delay = exhausted_delay
|
---|
[aa1db83] | 302 | sprint_enabled = false
|
---|
| 303 | cam.fov = FOV
|
---|
| 304 | _speed = walk_speed
|
---|
| 305 | return
|
---|
[25e7b67] | 306 | get_node("HUD/Control/StaminaBar").value = Globals.stamina
|
---|
[f7b09c8] | 307 | else:
|
---|
| 308 | _speed = walk_speed
|
---|
| 309 | cam.set_fov(lerp(cam.fov, FOV, delta * 8))
|
---|
[aa1db83] | 310 | if stamina_delay >= 0:
|
---|
[70fb6d4] | 311 | stamina_delay -= 0.1
|
---|
[aa1db83] | 312 | sprint_enabled = true
|
---|
[25e7b67] | 313 | elif Globals.stamina <= 100:
|
---|
[1b6ba32] | 314 | if velocity.x == 0:
|
---|
[25e7b67] | 315 | Globals.stamina += stamina_regen_speed * 2
|
---|
[1b6ba32] | 316 | else:
|
---|
[25e7b67] | 317 | Globals.stamina += stamina_regen_speed
|
---|
| 318 | elif Globals.stamina >= 100 && Globals.mana < 100:
|
---|
[f9161ba] | 319 | if velocity.x == 0:
|
---|
[25e7b67] | 320 | Globals.mana += mana_regen_speed * 2
|
---|
[f9161ba] | 321 | else:
|
---|
[25e7b67] | 322 | Globals.mana += mana_regen_speed
|
---|
| 323 | get_node("HUD/Control/ManaBar").value = Globals.mana
|
---|
| 324 | get_node("HUD/Control/StaminaBar").value = Globals.stamina
|
---|
[f7b09c8] | 325 |
|
---|
| 326 | func can_sprint() -> bool:
|
---|
| 327 | return (sprint_enabled and is_on_floor() and _is_sprinting_input and move_axis.x >= 0.5)
|
---|