Files
CosmicClash/Game/scripts/hud_gauge.gd
T
Josh Creek 08a0f74391 refactor(*): DRY up arena scenes, game modes, and HUD instruments
Arenas inherit from a new arena_base.tscn instead of restating ~40 shared
lines each; only sky/ambient/glow/tint/decoration vary, exposed via new
Arena exports since nested Environment properties aren't overridable
through scene inheritance. Bot construction and score-keeping move onto
GameMode, shared by match and spectate modes while preserving their
differing GameSettings-override behavior and the HUD's score-row
duck-typing. HUD instruments share a HudInstrument base for the
smoothing-weight calc and angle-lerp helper. Also dedupes
MAIN_MENU_SCENE_PATH into ScenePaths and documents why DIFFICULTIES
tiers share one checkpoint.
2026-08-04 15:07:33 +01:00

64 lines
2.3 KiB
GDScript

class_name HudGauge
extends HudInstrument
# Generic bar gauge (vertical or horizontal) with a short label and numeric
# readout — used for speed, altitude, and thrust. A dumb display fed by
# HUDController via set_value.
const BG_COLOR := Color(0.05, 0.07, 0.11, 0.55)
const FRAME_COLOR := Color(0.5, 0.85, 1.0, 0.7)
const TEXT_COLOR := Color(0.85, 0.92, 1.0, 0.9)
@export var label := "SPD"
@export var max_value := 100.0
@export var horizontal := false
@export var fill_color := Color(0.5, 0.85, 1.0, 0.85)
# Printf pattern for the numeric readout.
@export var value_format := "%.0f"
var _target := 0.0
var _value := 0.0
func set_value(value: float) -> void:
_target = value
func _process(delta: float) -> void:
var t := _smoothing_weight(delta)
_value = lerpf(_value, _target, t)
queue_redraw()
func _draw() -> void:
var font := ThemeDB.fallback_font
var fraction := clampf(_value / max_value, 0.0, 1.0)
var reading := value_format % _value
if horizontal:
# [label] [======bar======] [value]
var hbar := Rect2(34, 2, size.x - 74, size.y - 4)
draw_string(font, Vector2(0, size.y - 4), label, HORIZONTAL_ALIGNMENT_LEFT, -1, 11, TEXT_COLOR)
draw_rect(hbar, BG_COLOR)
draw_rect(Rect2(hbar.position, Vector2(hbar.size.x * fraction, hbar.size.y)), fill_color)
draw_rect(hbar, FRAME_COLOR, false, 1.0)
draw_string(font, Vector2(hbar.end.x + 6, size.y - 4), reading, HORIZONTAL_ALIGNMENT_LEFT, -1, 11, TEXT_COLOR)
return
# label / vertical bar filling upward / value
var bar := Rect2(size.x / 2.0 - 9, 16, 18, size.y - 34)
var label_size := font.get_string_size(label, HORIZONTAL_ALIGNMENT_CENTER, -1, 11)
draw_string(font, Vector2(size.x / 2.0 - label_size.x / 2.0, 12), label,
HORIZONTAL_ALIGNMENT_CENTER, -1, 11, TEXT_COLOR)
draw_rect(bar, BG_COLOR)
var fill_h := bar.size.y * fraction
draw_rect(Rect2(Vector2(bar.position.x, bar.end.y - fill_h), Vector2(bar.size.x, fill_h)), fill_color)
draw_rect(bar, FRAME_COLOR, false, 1.0)
# Quarter tick marks up the side of the bar.
for i in range(1, 4):
var y := bar.end.y - bar.size.y * i / 4.0
draw_line(Vector2(bar.position.x - 3, y), Vector2(bar.position.x, y), FRAME_COLOR, 1.0)
var reading_size := font.get_string_size(reading, HORIZONTAL_ALIGNMENT_CENTER, -1, 11)
draw_string(font, Vector2(size.x / 2.0 - reading_size.x / 2.0, size.y - 2), reading,
HORIZONTAL_ALIGNMENT_CENTER, -1, 11, TEXT_COLOR)