130 lines
3.4 KiB
GDScript
130 lines
3.4 KiB
GDScript
extends Control
|
|
class_name HUD
|
|
## 游戏内 HUD
|
|
## 显示在线玩家数量、网络状态等信息
|
|
|
|
# UI 元素
|
|
var online_players_label: Label
|
|
var network_status_indicator: ColorRect
|
|
var network_status_label: Label
|
|
var interaction_hint: Label
|
|
var container: VBoxContainer
|
|
|
|
# 状态
|
|
var online_player_count: int = 0
|
|
var network_connected: bool = false
|
|
|
|
func _ready():
|
|
"""初始化 HUD"""
|
|
_create_ui()
|
|
update_layout()
|
|
|
|
## 创建 UI 元素
|
|
func _create_ui():
|
|
"""创建 HUD 的所有 UI 元素"""
|
|
# 设置为全屏但不阻挡输入
|
|
anchor_right = 1.0
|
|
anchor_bottom = 1.0
|
|
mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
|
|
# 创建顶部容器
|
|
var top_container = HBoxContainer.new()
|
|
top_container.name = "TopContainer"
|
|
top_container.position = Vector2(10, 10)
|
|
add_child(top_container)
|
|
|
|
# 网络状态指示器
|
|
network_status_indicator = ColorRect.new()
|
|
network_status_indicator.custom_minimum_size = Vector2(16, 16)
|
|
network_status_indicator.color = Color(1, 0, 0, 0.8) # 红色 - 断开
|
|
top_container.add_child(network_status_indicator)
|
|
|
|
# 间距
|
|
top_container.add_child(_create_spacer(10))
|
|
|
|
# 网络状态标签
|
|
network_status_label = Label.new()
|
|
network_status_label.text = "未连接"
|
|
top_container.add_child(network_status_label)
|
|
|
|
# 间距
|
|
top_container.add_child(_create_spacer(20))
|
|
|
|
# 在线玩家数量标签
|
|
online_players_label = Label.new()
|
|
online_players_label.text = "在线玩家: 0"
|
|
top_container.add_child(online_players_label)
|
|
|
|
# 创建底部容器(交互提示)
|
|
var bottom_container = VBoxContainer.new()
|
|
bottom_container.name = "BottomContainer"
|
|
bottom_container.anchor_top = 1.0
|
|
bottom_container.anchor_bottom = 1.0
|
|
bottom_container.offset_top = -100
|
|
bottom_container.offset_left = 10
|
|
add_child(bottom_container)
|
|
|
|
# 交互提示
|
|
interaction_hint = Label.new()
|
|
interaction_hint.text = ""
|
|
interaction_hint.add_theme_font_size_override("font_size", 18)
|
|
interaction_hint.add_theme_color_override("font_color", Color(1, 1, 0.5))
|
|
bottom_container.add_child(interaction_hint)
|
|
|
|
## 创建间距
|
|
func _create_spacer(width: float) -> Control:
|
|
"""创建水平间距"""
|
|
var spacer = Control.new()
|
|
spacer.custom_minimum_size = Vector2(width, 0)
|
|
return spacer
|
|
|
|
## 更新布局
|
|
func update_layout():
|
|
"""更新布局以适应窗口大小"""
|
|
# HUD 元素使用锚点,会自动适应
|
|
pass
|
|
|
|
## 更新在线玩家数量
|
|
func update_online_players(count: int):
|
|
"""
|
|
更新在线玩家数量显示
|
|
@param count: 在线玩家数量
|
|
"""
|
|
online_player_count = count
|
|
if online_players_label:
|
|
online_players_label.text = "在线玩家: %d" % count
|
|
|
|
## 更新网络状态
|
|
func update_network_status(connected: bool):
|
|
"""
|
|
更新网络连接状态
|
|
@param connected: 是否已连接
|
|
"""
|
|
network_connected = connected
|
|
|
|
if network_status_indicator:
|
|
if connected:
|
|
network_status_indicator.color = Color(0.2, 1, 0.2, 0.8) # 绿色 - 已连接
|
|
else:
|
|
network_status_indicator.color = Color(1, 0, 0, 0.8) # 红色 - 断开
|
|
|
|
if network_status_label:
|
|
network_status_label.text = "已连接" if connected else "未连接"
|
|
|
|
## 显示交互提示
|
|
func show_interaction_hint(hint_text: String):
|
|
"""
|
|
显示交互提示
|
|
@param hint_text: 提示文本
|
|
"""
|
|
if interaction_hint:
|
|
interaction_hint.text = hint_text
|
|
interaction_hint.show()
|
|
|
|
## 隐藏交互提示
|
|
func hide_interaction_hint():
|
|
"""隐藏交互提示"""
|
|
if interaction_hint:
|
|
interaction_hint.text = ""
|
|
interaction_hint.hide()
|