[f7b09c8] | 1 | extends KinematicBody
|
---|
| 2 |
|
---|
[70fb6d4] | 3 | # Popup menu
|
---|
| 4 |
|
---|
[f7b09c8] | 5 | var Pause_Popup_tscn = preload("res://Pause_Popup.tscn")
|
---|
[aa1db83] | 6 | var Dead_Popup_tscn = preload("res://Dead_Popup.tscn")
|
---|
[f7b09c8] | 7 | var Pause_Popup
|
---|
[aa1db83] | 8 | var Dead_Popup
|
---|
[f7b09c8] | 9 |
|
---|
| 10 | ###################-VARIABLES-####################
|
---|
| 11 |
|
---|
| 12 | # Camera
|
---|
| 13 | export(float) var mouse_sensitivity = 8.0
|
---|
| 14 | export(NodePath) var head_path = "Head"
|
---|
| 15 | export(NodePath) var cam_path = "Head/Camera"
|
---|
| 16 | export(float) var FOV = 80.0
|
---|
| 17 | var mouse_axis := Vector2()
|
---|
| 18 | onready var head: Spatial = get_node(head_path)
|
---|
| 19 | onready var cam: Camera = get_node(cam_path)
|
---|
[aa1db83] | 20 |
|
---|
[f7b09c8] | 21 | # Move
|
---|
| 22 | var velocity := Vector3()
|
---|
| 23 | var direction := Vector3()
|
---|
| 24 | var move_axis := Vector2()
|
---|
[70fb6d4] | 25 | var snap := Vector3() # Connecting to ground
|
---|
| 26 | var falling := Vector3()
|
---|
| 27 |
|
---|
| 28 | # Damage
|
---|
| 29 | var max_fall := Vector3() # Total disatance falled down
|
---|
| 30 | var fall_threshold_damage = 17 # distance down a player has to fall before taking damage
|
---|
| 31 |
|
---|
[aa1db83] | 32 | # Walk and Sprint
|
---|
[f7b09c8] | 33 | const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
|
---|
[70fb6d4] | 34 | export(float) var gravity = 20.0
|
---|
[f7b09c8] | 35 | export(int) var walk_speed = 10
|
---|
| 36 | export(int) var sprint_speed = 16
|
---|
| 37 | export(int) var acceleration = 8
|
---|
| 38 | export(int) var deacceleration = 10
|
---|
| 39 | export(float, 0.0, 1.0, 0.05) var air_control = 0.3
|
---|
| 40 | export(int) var jump_height = 12
|
---|
| 41 | var _speed: int
|
---|
| 42 | var _is_sprinting_input := false
|
---|
| 43 | var _is_jumping_input := false
|
---|
[aa1db83] | 44 | var sprint_enabled := true
|
---|
| 45 | var hold_walk := false
|
---|
| 46 |
|
---|
| 47 | # Grounded
|
---|
[70fb6d4] | 48 | enum GROUNDED_STATE {
|
---|
| 49 | GROUNDED,
|
---|
| 50 | MIDAIR,
|
---|
| 51 | TOUCHDOWN
|
---|
| 52 | }
|
---|
| 53 | var player_state = GROUNDED_STATE.GROUNDED
|
---|
[aa1db83] | 54 |
|
---|
[70fb6d4] | 55 | # Player Stats
|
---|
| 56 | var health = 100
|
---|
| 57 | var stamina = 100
|
---|
[aa1db83] | 58 | var stamina_delay = 0
|
---|
| 59 | var mana = 100
|
---|
| 60 |
|
---|
| 61 | var mana_needed_to_heal = 30
|
---|
| 62 | var mana_heal_ammount = 10
|
---|
[f7b09c8] | 63 |
|
---|
| 64 | ##################################################
|
---|
| 65 |
|
---|
| 66 | # Called when the node enters the scene tree
|
---|
| 67 | func _ready() -> void:
|
---|
| 68 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
---|
| 69 | cam.fov = FOV
|
---|
| 70 |
|
---|
| 71 | # Called every frame. 'delta' is the elapsed time since the previous frame
|
---|
| 72 | func _process(_delta: float) -> void:
|
---|
[aa1db83] | 73 | move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
|
---|
[f7b09c8] | 74 | move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
|
---|
| 75 |
|
---|
| 76 | if Input.is_action_just_pressed("move_jump"):
|
---|
| 77 | _is_jumping_input = true
|
---|
| 78 |
|
---|
| 79 | if Input.is_action_pressed("move_sprint"):
|
---|
| 80 | _is_sprinting_input = true
|
---|
| 81 |
|
---|
[aa1db83] | 82 | if Input.is_action_just_pressed("hold_move_forward"):
|
---|
| 83 | if hold_walk == true:
|
---|
| 84 | hold_walk = false
|
---|
| 85 | else:
|
---|
| 86 | hold_walk = true
|
---|
| 87 |
|
---|
| 88 | if Input.is_action_just_pressed("move_forward") || Input.is_action_just_pressed("move_backward"):
|
---|
| 89 | hold_walk = false
|
---|
| 90 |
|
---|
| 91 | if hold_walk == true:
|
---|
| 92 | move_axis.x = hold_walk
|
---|
| 93 |
|
---|
[f7b09c8] | 94 | # Recapture mouse when resuming
|
---|
| 95 | if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
|
---|
| 96 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
---|
| 97 |
|
---|
| 98 | # Called every physics tick. 'delta' is constant
|
---|
| 99 | func _physics_process(delta: float) -> void:
|
---|
| 100 | walk(delta)
|
---|
| 101 |
|
---|
[70fb6d4] | 102 | # Fall damage calculation
|
---|
| 103 | match player_state:
|
---|
| 104 | GROUNDED_STATE.GROUNDED:
|
---|
| 105 | falling = Vector3.ZERO
|
---|
| 106 | if not is_on_floor():
|
---|
| 107 | player_state = GROUNDED_STATE.MIDAIR
|
---|
| 108 | GROUNDED_STATE.MIDAIR:
|
---|
| 109 | falling += Vector3.DOWN * gravity * delta
|
---|
| 110 | if velocity.y <= -fall_threshold_damage:
|
---|
| 111 | max_fall = velocity
|
---|
| 112 | if is_on_floor():
|
---|
| 113 | player_state = GROUNDED_STATE.TOUCHDOWN
|
---|
| 114 | GROUNDED_STATE.TOUCHDOWN:
|
---|
| 115 | if falling.length() >= 17 and max_fall.y <= -fall_threshold_damage:
|
---|
| 116 | health -= round(-(max_fall.y + fall_threshold_damage))
|
---|
[aa1db83] | 117 | stamina -= round(-(max_fall.y + fall_threshold_damage))
|
---|
[70fb6d4] | 118 | if round(-(max_fall.y + fall_threshold_damage)) > 5:
|
---|
| 119 | health -= round(-(max_fall.y + fall_threshold_damage)*9)
|
---|
| 120 | else:
|
---|
| 121 | if round(-(max_fall.y + fall_threshold_damage)) > 10:
|
---|
| 122 | health -= round(-(max_fall.y + fall_threshold_damage)*15)
|
---|
| 123 | get_node("Head/AnimationPlayer").play("FallDamage")
|
---|
| 124 | get_node("HUD/Control/HealthBar").value = health
|
---|
| 125 | max_fall = Vector3.ZERO
|
---|
| 126 | player_state = GROUNDED_STATE.GROUNDED
|
---|
| 127 |
|
---|
| 128 |
|
---|
[f7b09c8] | 129 | # Called when there is an input event
|
---|
| 130 | func _input(event: InputEvent) -> void:
|
---|
| 131 | if event is InputEventMouseMotion:
|
---|
| 132 | mouse_axis = event.relative
|
---|
| 133 | camera_rotation()
|
---|
| 134 |
|
---|
[70fb6d4] | 135 | # Testing tool to instent regen health
|
---|
[aa1db83] | 136 | if Input.is_action_just_pressed("heal") && mana >= mana_needed_to_heal:
|
---|
| 137 | mana -= mana_needed_to_heal
|
---|
| 138 | if 100 <= health + mana_heal_ammount:
|
---|
| 139 | health = 100
|
---|
| 140 | else :
|
---|
| 141 | health += mana_heal_ammount
|
---|
[70fb6d4] | 142 | get_node("HUD/Control/HealthBar").value = health
|
---|
[aa1db83] | 143 | get_node("HUD/Control/ManaBar").value = mana
|
---|
[f7b09c8] | 144 |
|
---|
| 145 | func walk(delta: float) -> void:
|
---|
| 146 | direction_input()
|
---|
| 147 |
|
---|
| 148 | if is_on_floor():
|
---|
| 149 | snap = -get_floor_normal() - get_floor_velocity() * delta
|
---|
| 150 |
|
---|
| 151 | # Workaround for sliding down after jump on slope
|
---|
| 152 | if velocity.y < 0:
|
---|
| 153 | velocity.y = 0
|
---|
| 154 |
|
---|
| 155 | jump()
|
---|
| 156 | else:
|
---|
| 157 | # Workaround for 'vertical bump' when going off platform
|
---|
| 158 | if snap != Vector3.ZERO && velocity.y != 0:
|
---|
| 159 | velocity.y = 0
|
---|
| 160 |
|
---|
| 161 | snap = Vector3.ZERO
|
---|
| 162 |
|
---|
| 163 | velocity.y -= gravity * delta
|
---|
| 164 |
|
---|
| 165 | sprint(delta)
|
---|
| 166 |
|
---|
| 167 | accelerate(delta)
|
---|
| 168 |
|
---|
| 169 | velocity = move_and_slide_with_snap(velocity, snap, Vector3.UP, true, 4, FLOOR_MAX_ANGLE)
|
---|
| 170 | _is_jumping_input = false
|
---|
| 171 | _is_sprinting_input = false
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 | func camera_rotation() -> void:
|
---|
| 175 | if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
|
---|
| 176 | return
|
---|
| 177 | if mouse_axis.length() > 0:
|
---|
| 178 | var horizontal: float = -mouse_axis.x * (mouse_sensitivity / 100)
|
---|
| 179 | var vertical: float = -mouse_axis.y * (mouse_sensitivity / 100)
|
---|
| 180 |
|
---|
| 181 | mouse_axis = Vector2()
|
---|
| 182 |
|
---|
| 183 | rotate_y(deg2rad(horizontal))
|
---|
| 184 | head.rotate_x(deg2rad(vertical))
|
---|
| 185 |
|
---|
| 186 | # Clamp mouse rotation
|
---|
| 187 | var temp_rot: Vector3 = head.rotation_degrees
|
---|
| 188 | temp_rot.x = clamp(temp_rot.x, -90, 90)
|
---|
| 189 | head.rotation_degrees = temp_rot
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | func direction_input() -> void:
|
---|
| 193 | direction = Vector3()
|
---|
| 194 | var aim: Basis = get_global_transform().basis
|
---|
| 195 | if move_axis.x >= 0.5:
|
---|
| 196 | direction -= aim.z
|
---|
| 197 | if move_axis.x <= -0.5:
|
---|
| 198 | direction += aim.z
|
---|
| 199 | if move_axis.y <= -0.5:
|
---|
| 200 | direction -= aim.x
|
---|
| 201 | if move_axis.y >= 0.5:
|
---|
| 202 | direction += aim.x
|
---|
| 203 | direction.y = 0
|
---|
| 204 | direction = direction.normalized()
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 | func accelerate(delta: float) -> void:
|
---|
| 208 | # Where would the player go
|
---|
| 209 | var _temp_vel: Vector3 = velocity
|
---|
| 210 | var _temp_accel: float
|
---|
| 211 | var _target: Vector3 = direction * _speed
|
---|
| 212 |
|
---|
| 213 | _temp_vel.y = 0
|
---|
| 214 | if direction.dot(_temp_vel) > 0:
|
---|
| 215 | _temp_accel = acceleration
|
---|
| 216 |
|
---|
| 217 | else:
|
---|
| 218 | _temp_accel = deacceleration
|
---|
| 219 |
|
---|
| 220 | if not is_on_floor():
|
---|
| 221 | _temp_accel *= air_control
|
---|
| 222 |
|
---|
| 223 | # Interpolation
|
---|
| 224 | _temp_vel = _temp_vel.linear_interpolate(_target, _temp_accel * delta)
|
---|
| 225 |
|
---|
| 226 | velocity.x = _temp_vel.x
|
---|
| 227 | velocity.z = _temp_vel.z
|
---|
| 228 |
|
---|
| 229 | # Make too low values zero
|
---|
| 230 | if direction.dot(velocity) == 0:
|
---|
| 231 | var _vel_clamp := 0.01
|
---|
| 232 | if abs(velocity.x) < _vel_clamp:
|
---|
| 233 | velocity.x = 0
|
---|
| 234 | if abs(velocity.z) < _vel_clamp:
|
---|
| 235 | velocity.z = 0
|
---|
| 236 |
|
---|
| 237 |
|
---|
| 238 | func jump() -> void:
|
---|
| 239 | if _is_jumping_input:
|
---|
| 240 | velocity.y = jump_height
|
---|
| 241 | snap = Vector3.ZERO
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 | func sprint(delta: float) -> void:
|
---|
| 245 | if can_sprint():
|
---|
[aa1db83] | 246 | if stamina >= 0 && stamina_delay <= 1:
|
---|
[70fb6d4] | 247 | _speed = sprint_speed
|
---|
| 248 | cam.set_fov(lerp(cam.fov, FOV * 1.25, delta * 8))
|
---|
| 249 | stamina -= 0.5
|
---|
[aa1db83] | 250 | if stamina <= 0:
|
---|
| 251 | stamina_delay = 30
|
---|
| 252 | sprint_enabled = false
|
---|
| 253 | cam.fov = FOV
|
---|
| 254 | _speed = walk_speed
|
---|
| 255 | return
|
---|
[70fb6d4] | 256 | get_node("HUD/Control/StaminaBar").value = stamina
|
---|
[f7b09c8] | 257 | else:
|
---|
| 258 | _speed = walk_speed
|
---|
| 259 | cam.set_fov(lerp(cam.fov, FOV, delta * 8))
|
---|
[aa1db83] | 260 | if stamina_delay >= 0:
|
---|
[70fb6d4] | 261 | stamina_delay -= 0.1
|
---|
[aa1db83] | 262 | sprint_enabled = true
|
---|
| 263 | elif stamina <= 100:
|
---|
[70fb6d4] | 264 | stamina += 0.1
|
---|
[aa1db83] | 265 | elif stamina >= 100 && mana < 100:
|
---|
| 266 | mana += 0.01
|
---|
| 267 | get_node("HUD/Control/ManaBar").value = mana
|
---|
| 268 | get_node("HUD/Control/StaminaBar").value = stamina
|
---|
[f7b09c8] | 269 |
|
---|
| 270 | func can_sprint() -> bool:
|
---|
| 271 | return (sprint_enabled and is_on_floor() and _is_sprinting_input and move_axis.x >= 0.5)
|
---|