Files
CosmicClash/Game/scripts/hud_gauge.gd
T
Josh Creek 04691aaa48 chore(multiplayer): Phase 0 refactors + graphics/perf settings groundwork
Lands the non-networked Phase 0 tasks from multiplayer-todo.md (ship/camera/
arena refactors, sim constants, background FPS handling) plus a first pass
at exposing graphics/performance settings (presets, resolution scaling,
vsync, FPS cap, perf overlay) and a GPU profiling harness for the
real-hardware follow-up in task 0.15b.
2026-08-19 22:37:17 +01:00

70 lines
2.7 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)
var new_value := lerpf(_value, _target, t)
# Redraw threshold scales with the gauge's own range so it stays
# imperceptible whether this is a 0-12 altitude gauge or a 0-100 thrust
# bar, while skipping the redraw once the lerp has visually settled.
var changed := absf(new_value - _value) > max_value * 0.001
_value = new_value
if changed:
_throttled_redraw(delta)
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)