You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
47 lines
1.3 KiB
class_name PlayerHealth |
|
extends Node |
|
|
|
const TIME_BEFORE_REGEN := 1.0 |
|
const REGENERATION_TIMER := 1.0 / 3 |
|
const REGENERATION_AMOUNT := 0.025 |
|
const RESPAWN_TIMER := 5.0 |
|
|
|
var current := 1.0 |
|
var previous := 1.0 |
|
var regeneration_delay := 0.0 |
|
var respawn_delay := 0.0 |
|
|
|
var is_alive: bool: |
|
get: return current > 0.0 |
|
|
|
@onready var player: Player = get_parent() |
|
|
|
func _process(delta: float) -> void: |
|
# Damage player when falling into "the void", so they can respawn. |
|
if player.position.y > 9000: current -= 0.01 |
|
|
|
if is_alive and current < 1.0: |
|
regeneration_delay += delta |
|
if regeneration_delay >= (TIME_BEFORE_REGEN + REGENERATION_TIMER): |
|
regeneration_delay -= REGENERATION_TIMER |
|
current = minf(1.0, current + REGENERATION_AMOUNT) |
|
else: |
|
regeneration_delay = 0.0 |
|
|
|
if not is_alive: |
|
respawn_delay += delta |
|
if respawn_delay >= RESPAWN_TIMER: |
|
player.position = Vector2.ZERO |
|
player.velocity = Vector2.ZERO |
|
player.modulate = Color.WHITE |
|
current = 1.0 |
|
respawn_delay = 0.0 |
|
# TODO: Add invulnerability timer? Or some other way to prevent "void" damage |
|
# after server considers player respawned, but it hasn't teleported yet. |
|
|
|
if previous != current: |
|
if not is_alive and previous > 0: |
|
player.modulate = Color(0.35, 0.35, 0.35, 0.8) |
|
if current < previous: |
|
regeneration_delay = 0.0 |
|
previous = current
|
|
|