1
1
mirror of https://github.com/Rundll86/Dog-Lynx-And-HCN.git synced 2026-05-27 22:41:56 +08:00

feat(多人游戏): 添加连接状态检查和断开连接功能

- 在MultiplayerState中添加isConnected方法检查连接状态
- 修改launchServer和connectClient方法以更新连接状态
- 在Starter面板中添加断开连接按钮并实现状态同步
- 更新UI显示连接状态和颜色
This commit is contained in:
2025-11-09 15:24:24 +08:00
parent 00c56484f4
commit c28d725d3e
3 changed files with 19 additions and 2 deletions
@@ -88,9 +88,10 @@ label_settings = SubResource("LabelSettings_lfxcn")
[node name="connectionState" type="Label" parent="content/wrapper/starter/multiplayer/main" index="1"]
unique_name_in_owner = true
modulate = Color(1, 0, 0, 1)
layout_mode = 2
size_flags_horizontal = 4
text = "状态:"
text = "状态:未连接到服务器。"
label_settings = SubResource("LabelSettings_kl3ko")
[node name="host" type="HBoxContainer" parent="content/wrapper/starter/multiplayer/main" index="2"]
@@ -145,6 +146,13 @@ layout_mode = 2
size_flags_horizontal = 4
text = "连接服务器"
[node name="disconnectBtn" type="Button" parent="content/wrapper/starter/multiplayer/main/operation" index="2"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 4
disabled = true
text = "断开连接"
[node name="serverConfig" type="VBoxContainer" parent="content/wrapper/starter/multiplayer" index="1"]
layout_mode = 2
alignment = 1
+2
View File
@@ -10,6 +10,7 @@ extends FullscreenPanelBase
@onready var connectBtn: Button = $"%connectBtn"
@onready var maxPlayerInput: LineEdit = $"%maxPlayerInput"
@onready var connectionState: Label = $"%connectionState"
@onready var disconnectBtn: Button = $"%disconnectBtn"
func _ready():
diffEdit.min_value = GameRule.difficultyRange.x
@@ -36,3 +37,4 @@ func setState(state: MultiplayerState.ConnectionState):
MultiplayerState.state = state
connectionState.text = "状态:%s" % MultiplayerState.stateTextMap[state]
connectionState.modulate = MultiplayerState.stateColorMap[state]
disconnectBtn.disabled = not MultiplayerState.isConnected()
+8 -1
View File
@@ -25,8 +25,15 @@ static var isMultiplayer: bool = false
static var maxPlayer: int = 10
static func isConnected():
return [ConnectionState.CONNECTED_HOST, ConnectionState.CONNECTED_CLIENT].has(state)
static func launchServer(port: int):
isMultiplayer = true
var peer = ENetMultiplayerPeer.new()
peer.create_server(port, maxPlayer)
state = ConnectionState.CONNECTED_HOST
return peer
static func connectClient(host: String, port: int):
var peer = ENetMultiplayerPeer.new()
peer.create_client(host, port)
state = ConnectionState.CONNECTED_CLIENT
return peer