[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 |
|
---|
[f9161ba] | 8 | # Popup menu
|
---|
[f7b09c8] | 9 | var Pause_Popup_tscn = preload("res://Pause_Popup.tscn")
|
---|
[aa1db83] | 10 | var Dead_Popup_tscn = preload("res://Dead_Popup.tscn")
|
---|
[f7b09c8] | 11 | var Pause_Popup
|
---|
[aa1db83] | 12 | var Dead_Popup
|
---|
[f7b09c8] | 13 |
|
---|
| 14 | ###################-VARIABLES-####################
|
---|
| 15 |
|
---|
| 16 | # Camera
|
---|
| 17 | export(float) var mouse_sensitivity = 8.0
|
---|
| 18 | export(NodePath) var head_path = "Head"
|
---|
| 19 | export(NodePath) var cam_path = "Head/Camera"
|
---|
| 20 | export(float) var FOV = 80.0
|
---|
| 21 | var mouse_axis := Vector2()
|
---|
| 22 | onready var head: Spatial = get_node(head_path)
|
---|
| 23 | onready var cam: Camera = get_node(cam_path)
|
---|
[aa1db83] | 24 |
|
---|
[f7b09c8] | 25 | # Move
|
---|
| 26 | var velocity := Vector3()
|
---|
| 27 | var direction := Vector3()
|
---|
| 28 | var move_axis := Vector2()
|
---|
[70fb6d4] | 29 | var snap := Vector3() # Connecting to ground
|
---|
| 30 | var falling := Vector3()
|
---|
| 31 |
|
---|
| 32 | # Damage
|
---|
| 33 | var max_fall := Vector3() # Total disatance falled down
|
---|
| 34 | var fall_threshold_damage = 17 # distance down a player has to fall before taking damage
|
---|
| 35 |
|
---|
[aa1db83] | 36 | # Walk and Sprint
|
---|
[f7b09c8] | 37 | const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
|
---|
[70fb6d4] | 38 | export(float) var gravity = 20.0
|
---|
[f7b09c8] | 39 | export(int) var walk_speed = 10
|
---|
| 40 | export(int) var sprint_speed = 16
|
---|
| 41 | export(int) var acceleration = 8
|
---|
| 42 | export(int) var deacceleration = 10
|
---|
| 43 | export(float, 0.0, 1.0, 0.05) var air_control = 0.3
|
---|
| 44 | export(int) var jump_height = 12
|
---|
| 45 | var _speed: int
|
---|
| 46 | var _is_sprinting_input := false
|
---|
| 47 | var _is_jumping_input := false
|
---|
[aa1db83] | 48 | var sprint_enabled := true
|
---|
| 49 | var hold_walk := false
|
---|
[1b6ba32] | 50 | var stamina_delay = 0
|
---|
| 51 |
|
---|
[aa1db83] | 52 | # Grounded
|
---|
[70fb6d4] | 53 | enum GROUNDED_STATE {
|
---|
| 54 | GROUNDED,
|
---|
| 55 | MIDAIR,
|
---|
| 56 | TOUCHDOWN
|
---|
| 57 | }
|
---|
| 58 | var player_state = GROUNDED_STATE.GROUNDED
|
---|
[aa1db83] | 59 |
|
---|
[70fb6d4] | 60 | # Player Stats
|
---|
| 61 | var health = 100
|
---|
| 62 | var stamina = 100
|
---|
[aa1db83] | 63 | var mana = 100
|
---|
| 64 |
|
---|
[1b6ba32] | 65 | var exhausted_delay = 30
|
---|
| 66 | var sprint_energy_consumption = 0.5
|
---|
[f9161ba] | 67 | var stamina_regen_speed = 0.5
|
---|
[aa1db83] | 68 | var mana_needed_to_heal = 30
|
---|
| 69 | var mana_heal_ammount = 10
|
---|
[f9161ba] | 70 | var mana_regen_speed = 0.01
|
---|
| 71 | var player = "player"
|
---|
[f7b09c8] | 72 |
|
---|
| 73 | ##################################################
|
---|
| 74 |
|
---|
| 75 | # Called when the node enters the scene tree
|
---|
| 76 | func _ready() -> void:
|
---|
[f9161ba] | 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")
|
---|
[1b6ba32] | 85 | exhausted_delay = player_config.get_value(player, "exhausted_delay")
|
---|
| 86 | stamina_regen_speed = player_config.get_value(player, "stamina_regen_speed")
|
---|
[f9161ba] | 87 | sprint_speed = player_config.get_value(player, "sprint_speed")
|
---|
[1b6ba32] | 88 | sprint_energy_consumption = player_config.get_value(player, "sprint_energy_consumption")
|
---|
[f9161ba] | 89 | jump_height = player_config.get_value(player, "jump_height")
|
---|
| 90 |
|
---|
| 91 | else:
|
---|
| 92 | print("Player Config File Not Found - Using defaults")
|
---|
| 93 |
|
---|
[f7b09c8] | 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
|
---|
| 98 | func _process(_delta: float) -> void:
|
---|
[aa1db83] | 99 | move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
|
---|
[f7b09c8] | 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 |
|
---|
[aa1db83] | 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 |
|
---|
[f7b09c8] | 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
|
---|
| 125 | func _physics_process(delta: float) -> void:
|
---|
| 126 | walk(delta)
|
---|
| 127 |
|
---|
[70fb6d4] | 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:
|
---|
[f9161ba] | 141 | if falling.length() >= fall_threshold_damage and max_fall.y <= -fall_threshold_damage:
|
---|
[70fb6d4] | 142 | health -= round(-(max_fall.y + fall_threshold_damage))
|
---|
[aa1db83] | 143 | stamina -= round(-(max_fall.y + fall_threshold_damage))
|
---|
[70fb6d4] | 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 |
|
---|
[f7b09c8] | 155 | # Called when there is an input event
|
---|
| 156 | func _input(event: InputEvent) -> void:
|
---|
| 157 | if event is InputEventMouseMotion:
|
---|
| 158 | mouse_axis = event.relative
|
---|
| 159 | camera_rotation()
|
---|
| 160 |
|
---|
[1b6ba32] | 161 | # Heal button
|
---|
[aa1db83] | 162 | if Input.is_action_just_pressed("heal") && mana >= mana_needed_to_heal:
|
---|
[1b6ba32] | 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
|
---|
[f7b09c8] | 171 |
|
---|
| 172 | func 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 |
|
---|
| 201 | func 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 |
|
---|
| 219 | func 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 |
|
---|
| 234 | func 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 |
|
---|
| 265 | func jump() -> void:
|
---|
| 266 | if _is_jumping_input:
|
---|
| 267 | velocity.y = jump_height
|
---|
| 268 | snap = Vector3.ZERO
|
---|
| 269 |
|
---|
| 270 |
|
---|
| 271 | func sprint(delta: float) -> void:
|
---|
| 272 | if can_sprint():
|
---|
[aa1db83] | 273 | if stamina >= 0 && stamina_delay <= 1:
|
---|
[70fb6d4] | 274 | _speed = sprint_speed
|
---|
| 275 | cam.set_fov(lerp(cam.fov, FOV * 1.25, delta * 8))
|
---|
[1b6ba32] | 276 | stamina -= sprint_energy_consumption
|
---|
[aa1db83] | 277 | if stamina <= 0:
|
---|
[1b6ba32] | 278 | stamina_delay = exhausted_delay
|
---|
[aa1db83] | 279 | sprint_enabled = false
|
---|
| 280 | cam.fov = FOV
|
---|
| 281 | _speed = walk_speed
|
---|
| 282 | return
|
---|
[70fb6d4] | 283 | get_node("HUD/Control/StaminaBar").value = stamina
|
---|
[f7b09c8] | 284 | else:
|
---|
| 285 | _speed = walk_speed
|
---|
| 286 | cam.set_fov(lerp(cam.fov, FOV, delta * 8))
|
---|
[aa1db83] | 287 | if stamina_delay >= 0:
|
---|
[70fb6d4] | 288 | stamina_delay -= 0.1
|
---|
[aa1db83] | 289 | sprint_enabled = true
|
---|
| 290 | elif stamina <= 100:
|
---|
[1b6ba32] | 291 | if velocity.x == 0:
|
---|
| 292 | stamina += stamina_regen_speed * 2
|
---|
| 293 | else:
|
---|
| 294 | stamina += stamina_regen_speed
|
---|
[aa1db83] | 295 | elif stamina >= 100 && mana < 100:
|
---|
[f9161ba] | 296 | if velocity.x == 0:
|
---|
| 297 | mana += mana_regen_speed * 2
|
---|
| 298 | else:
|
---|
| 299 | mana += mana_regen_speed
|
---|
[aa1db83] | 300 | get_node("HUD/Control/ManaBar").value = mana
|
---|
| 301 | get_node("HUD/Control/StaminaBar").value = stamina
|
---|
[f7b09c8] | 302 |
|
---|
| 303 | func can_sprint() -> bool:
|
---|
| 304 | return (sprint_enabled and is_on_floor() and _is_sprinting_input and move_axis.x >= 0.5)
|
---|