mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-07-12 18:43:49 +00:00
refactor(*): Make hud more performant and maintainable
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
# HUD Controller - handles all display logic and label updates
|
||||
# Receives ship data via signals and updates UI accordingly
|
||||
# Simplified version focusing on labels only
|
||||
extends CanvasLayer
|
||||
class_name HUDController
|
||||
|
||||
# UI Label References
|
||||
@onready var timer_label = get_node("Control/TimerLabel")
|
||||
@onready var speed_label = get_node("Control/InfoPanel/SpeedLabel")
|
||||
@onready var altitude_label = get_node("Control/InfoPanel/AltitudeLabel")
|
||||
@onready var angular_vel_label = get_node("Control/InfoPanel/AngularVelLabel")
|
||||
@onready var camera_mode_label = get_node("Control/InfoPanel/CameraModeLabel")
|
||||
@onready var attitude_label = get_node("Control/InfoPanel/AttitudeLabel")
|
||||
@onready var heading_label = get_node("Control/InfoPanel/HeadingLabel")
|
||||
@onready var thrust_label = get_node("Control/InfoPanel/ThrustLabel")
|
||||
|
||||
var ship: Node
|
||||
|
||||
func _ready():
|
||||
# Wait one frame to ensure ship is ready and added to group
|
||||
await get_tree().process_frame
|
||||
_initialize_hud()
|
||||
|
||||
func _initialize_hud():
|
||||
# Find the ship
|
||||
ship = get_tree().get_first_node_in_group("ship")
|
||||
if not ship:
|
||||
# Try to find it as our parent (ship contains this HUD)
|
||||
var parent_node = get_parent()
|
||||
if parent_node and parent_node.is_in_group("ship"):
|
||||
ship = parent_node
|
||||
else:
|
||||
push_error("HUDController: No ship found in 'ship' group")
|
||||
return
|
||||
|
||||
print("HUDController: Found ship: ", ship.name)
|
||||
_connect_ship_signals()
|
||||
|
||||
# Connect to game manager's timer signal
|
||||
var game_manager = get_tree().get_first_node_in_group("game")
|
||||
if game_manager and game_manager.has_signal("timer_updated"):
|
||||
game_manager.timer_updated.connect(_on_timer_updated)
|
||||
print("HUDController: Connected to game timer")
|
||||
|
||||
func _connect_ship_signals():
|
||||
# Connect ship signals to label update methods
|
||||
if ship.has_signal("speed_changed"):
|
||||
ship.speed_changed.connect(_on_ship_speed_changed)
|
||||
print("HUDController: Connected to speed_changed signal")
|
||||
if ship.has_signal("altitude_changed"):
|
||||
ship.altitude_changed.connect(_on_ship_altitude_changed)
|
||||
if ship.has_signal("angular_velocity_changed"):
|
||||
ship.angular_velocity_changed.connect(_on_ship_angular_velocity_changed)
|
||||
if ship.has_signal("camera_mode_changed"):
|
||||
ship.camera_mode_changed.connect(_on_ship_camera_mode_changed)
|
||||
if ship.has_signal("attitude_changed"):
|
||||
ship.attitude_changed.connect(_on_ship_attitude_changed)
|
||||
if ship.has_signal("heading_changed"):
|
||||
ship.heading_changed.connect(_on_ship_heading_changed)
|
||||
if ship.has_signal("thrust_changed"):
|
||||
ship.thrust_changed.connect(_on_ship_thrust_changed)
|
||||
|
||||
# Signal handlers for ship data - these update the legacy text labels
|
||||
# All handlers include null checks for robustness
|
||||
func _on_ship_speed_changed(speed: float):
|
||||
if speed_label and is_instance_valid(speed_label):
|
||||
speed_label.text = "Speed: %.1f m/s" % speed
|
||||
|
||||
func _on_ship_altitude_changed(altitude: float):
|
||||
if altitude_label and is_instance_valid(altitude_label):
|
||||
altitude_label.text = "Altitude: %.1f m" % altitude
|
||||
|
||||
func _on_ship_angular_velocity_changed(angular_speed: float):
|
||||
if angular_vel_label and is_instance_valid(angular_vel_label):
|
||||
angular_vel_label.text = "Angular Vel: %.2f rad/s" % angular_speed
|
||||
|
||||
func _on_ship_camera_mode_changed(is_ball_cam: bool):
|
||||
if camera_mode_label and is_instance_valid(camera_mode_label):
|
||||
var camera_mode = "Ball Cam" if is_ball_cam else "Ship Cam"
|
||||
camera_mode_label.text = "Camera: %s" % camera_mode
|
||||
|
||||
func _on_ship_attitude_changed(pitch: float, roll: float, yaw: float):
|
||||
if attitude_label and is_instance_valid(attitude_label):
|
||||
attitude_label.text = "Pitch: %.0f° Roll: %.0f°" % [pitch, roll]
|
||||
|
||||
func _on_ship_heading_changed(heading_degrees: float):
|
||||
if heading_label and is_instance_valid(heading_label):
|
||||
heading_label.text = "Heading: %.0f°" % heading_degrees
|
||||
|
||||
func _on_ship_thrust_changed(thrust_percent: float):
|
||||
if thrust_label and is_instance_valid(thrust_label):
|
||||
thrust_label.text = "Thrust: %.0f%%" % thrust_percent
|
||||
|
||||
# Timer signal handler from game manager (restored original functionality)
|
||||
func _on_timer_updated(minutes: int, seconds: int):
|
||||
if timer_label and is_instance_valid(timer_label):
|
||||
timer_label.text = "%02d:%02d" % [minutes, seconds]
|
||||
|
||||
# Simple timer update method (kept for compatibility)
|
||||
func update_timer(time_text: String):
|
||||
if timer_label and is_instance_valid(timer_label):
|
||||
timer_label.text = time_text
|
||||
Reference in New Issue
Block a user