Added visibility component
This commit is contained in:
@ -8,23 +8,23 @@ const DEATH_THRESHOLD = 0.01
|
||||
|
||||
@export var max_value: float
|
||||
var value: float
|
||||
var is_dead: bool
|
||||
var is_alive: bool
|
||||
|
||||
func _ready():
|
||||
restore()
|
||||
|
||||
func restore():
|
||||
is_dead = false
|
||||
is_alive = true
|
||||
value = max_value
|
||||
value_changed.emit()
|
||||
|
||||
func take_damage(attack: DamageInstance):
|
||||
if is_dead:
|
||||
if !is_alive:
|
||||
return
|
||||
|
||||
value = clampf(value - attack.damage, 0, max_value)
|
||||
value_changed.emit()
|
||||
|
||||
if value < DEATH_THRESHOLD:
|
||||
is_dead = true
|
||||
is_alive = false
|
||||
death.emit()
|
||||
|
@ -11,4 +11,4 @@ func _ready():
|
||||
owner.name_changed.connect(update_name)
|
||||
|
||||
func update_name(new_name: String):
|
||||
%Label.text = new_name
|
||||
%Label.text = new_name
|
34
client/player/visibility/VisibilityComponent.gd
Normal file
34
client/player/visibility/VisibilityComponent.gd
Normal file
@ -0,0 +1,34 @@
|
||||
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)
|
10
client/player/visibility/VisibilityComponent.tscn
Normal file
10
client/player/visibility/VisibilityComponent.tscn
Normal file
@ -0,0 +1,10 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://c8lifxuvpchu3"]
|
||||
|
||||
[ext_resource type="Script" path="res://player/visibility/VisibilityComponent.gd" id="1_lhvjf"]
|
||||
|
||||
[node name="Visibility" type="Timer"]
|
||||
wait_time = 0.5
|
||||
autostart = true
|
||||
script = ExtResource("1_lhvjf")
|
||||
|
||||
[connection signal="timeout" from="." to="." method="update_visibility"]
|
Reference in New Issue
Block a user