Randoloot/scripts/game/ship_energy_indicator.gd
2026-10-10 23:07:57 +02:00

53 lines
2.0 KiB
GDScript

extends Node2D
const FONT := preload("res://assets/fonts/font2.ttf")
var player: CharacterBody2D
var _display_ratio := 1.0
var _background: StyleBoxFlat
func _ready() -> void:
# World rendering interpolates this position together with the ship and camera.
top_level = true
process_physics_priority = 1
global_position = player.global_position
reset_physics_interpolation()
z_index = 24
_display_ratio = _energy_ratio()
_background = StyleBoxFlat.new()
_background.bg_color = Color("101925")
_background.border_color = Color("456277")
_background.set_border_width_all(1)
func _physics_process(_delta: float) -> void:
if is_instance_valid(player):
var teleported := global_position.distance_to(player.global_position) > 600.0
global_position = player.global_position
if teleported:
reset_physics_interpolation()
func _process(delta: float) -> void:
if not is_instance_valid(player):
return
visible = not bool(player.get("_is_destroyed"))
var target := _energy_ratio()
_display_ratio = lerpf(_display_ratio, target, 1.0 - exp(-20.0 * delta))
if absf(_display_ratio - target) < 0.001:
_display_ratio = target
queue_redraw()
func _energy_ratio() -> float:
return clampf(float(player.get("energy")) / maxf(1.0, float(player.get("max_energy"))), 0.0, 1.0)
func _draw() -> void:
if not is_instance_valid(player):
return
var color := Color("71e3ff") if _energy_ratio() >= 0.25 else Color("ffbd66")
var bar_position := Vector2(-44, 48)
draw_style_box(_background, Rect2(bar_position - Vector2(2, 2), Vector2(92, 10)))
draw_rect(Rect2(bar_position, Vector2(88 * _display_ratio, 6)), color)
var charging := float(player.call("get_solar_regeneration")) > 0.0 and _energy_ratio() < 1.0
var caption := "ENERGIA %d%%" % roundi(_energy_ratio() * 100)
draw_string(FONT, bar_position + Vector2(0, 22), caption, HORIZONTAL_ALIGNMENT_CENTER, 88, 10, color)
if charging:
draw_string(FONT, bar_position + Vector2(-18, 36), "ŁADOWANIE SOLARNE", HORIZONTAL_ALIGNMENT_CENTER, 124, 9, Color("71e3ff"))