35 lines
929 B
GDScript

class_name VisibilityComponent
extends Node
@export var health: HealthComponent
@export var hud: HUDComponent
@export var hud_distance: float
@export var physics_distance: float
@export var render_distance: float
var parent: Node3D
var main_player_distance_sq: float
func _ready():
parent = owner
hud_distance *= hud_distance
physics_distance *= physics_distance
render_distance *= render_distance
update_visibility()
func _process(_delta):
if hud.visible:
hud.transparency = 1.0 - (hud_distance - main_player_distance_sq) * 0.001
func update_visibility():
if !Global.player:
return
if !health.is_alive:
return
main_player_distance_sq = parent.global_position.distance_squared_to(Global.player.global_position)
parent.visible = main_player_distance_sq < render_distance
hud.visible = main_player_distance_sq < hud_distance
DeathComponent.set_physics(parent, main_player_distance_sq < physics_distance)