41 lines
1.2 KiB
GDScript3
Raw Normal View History

2024-02-27 20:05:55 +00:00
class_name PerformanceComponent
extends VisibleOnScreenNotifier3D
@export var animation: AnimationComponent
var camera_distance_squared: float
var on_screen: bool
2024-02-28 11:15:58 +00:00
var delay_visible: float
2024-02-27 20:05:55 +00:00
func _ready():
assert(animation)
screen_entered.connect(on_screen_entered)
screen_exited.connect(on_screen_exited)
func on_screen_entered():
on_screen = true
2024-02-28 11:15:58 +00:00
animation.delay = delay_visible
2024-02-27 20:05:55 +00:00
func on_screen_exited():
on_screen = false
2024-02-28 11:15:58 +00:00
animation.delay = AnimationPerformance.DELAY_INVISIBLE
2024-02-27 20:05:55 +00:00
2024-02-28 11:15:58 +00:00
static func get_visible_players_sorted_by_distance() -> Array[Player]:
2024-02-27 20:05:55 +00:00
var visible_players: Array[Player] = []
for player in Global.players.id_to_player.values():
player.performance.camera_distance_squared = player.global_position.distance_squared_to(Global.camera.global_position)
if player.performance.on_screen:
visible_players.append(player)
else:
2024-02-28 11:15:58 +00:00
player.performance.animation.delay = AnimationPerformance.DELAY_INVISIBLE
2024-02-27 20:05:55 +00:00
if Global.player:
Global.player.performance.camera_distance_squared = 0
visible_players.sort_custom(PerformanceComponent.distance_sort)
2024-02-28 11:15:58 +00:00
return visible_players
2024-02-27 20:05:55 +00:00
static func distance_sort(a: Player, b: Player) -> bool:
2024-02-28 11:15:58 +00:00
return a.performance.camera_distance_squared < b.performance.camera_distance_squared