1
1
mirror of https://github.com/Rundll86/Dog-Lynx-And-HCN.git synced 2026-05-31 08:21:54 +08:00

feat(多人游戏): 添加多人游戏状态管理和服务器配置功能

- 新增 MultiplayerState 类管理连接状态和服务器操作
- 在 Starter 面板中添加服务器配置和状态显示
- 实现服务器启动和最大玩家数设置功能
This commit is contained in:
2025-11-09 15:19:21 +08:00
parent 92c3e89879
commit d4e5a11e3d
5 changed files with 112 additions and 18 deletions
+20
View File
@@ -4,6 +4,12 @@ extends FullscreenPanelBase
@onready var diffEdit: HSlider = $"%diffEdit"
@onready var startBtn: Button = $"%startBtn"
@onready var levelShow: Label = $"%levelShow"
@onready var hostInput: LineEdit = $"%hostInput"
@onready var portInput: LineEdit = $"%portInput"
@onready var launchBtn: Button = $"%launchBtn"
@onready var connectBtn: Button = $"%connectBtn"
@onready var maxPlayerInput: LineEdit = $"%maxPlayerInput"
@onready var connectionState: Label = $"%connectionState"
func _ready():
diffEdit.min_value = GameRule.difficultyRange.x
@@ -13,6 +19,20 @@ func _ready():
Wave.next()
UIState.closeCurrentPanel()
)
maxPlayerInput.text_changed.connect(
func(text):
MultiplayerState.maxPlayer = int(text)
)
launchBtn.pressed.connect(
func():
MultiplayerState.launchServer(int(portInput.text))
)
setState(MultiplayerState.ConnectionState.DISCONNECTED)
func _physics_process(_delta):
levelShow.text = "%d ∈ [%d, %d]" % [diffEdit.value, diffEdit.min_value, diffEdit.max_value]
GameRule.difficulty = diffEdit.value
func setState(state: MultiplayerState.ConnectionState):
MultiplayerState.state = state
connectionState.text = MultiplayerState.stateTextMap[state]
connectionState.modulate = MultiplayerState.stateColorMap[state]
@@ -15,9 +15,6 @@ func showPanel(args: Array = []):
animator.play("show")
await animator.animation_finished
func _ready():
visible = false
# 钩子
func beforeOpen(_args: Array = []):
pass
@@ -0,0 +1,31 @@
class_name MultiplayerState
enum ConnectionState {
DISCONNECTED,
CONNECTING,
CONNECTED_HOST,
CONNECTED_CLIENT,
}
static var stateTextMap = {
ConnectionState.DISCONNECTED: "未连接到服务器。",
ConnectionState.CONNECTING: "连接中...",
ConnectionState.CONNECTED_HOST: "服务器启动成功!",
ConnectionState.CONNECTED_CLIENT: "已连接到服务器!",
}
static var stateColorMap = {
ConnectionState.DISCONNECTED: Color.RED,
ConnectionState.CONNECTING: Color.YELLOW,
ConnectionState.CONNECTED_HOST: Color.GREEN,
ConnectionState.CONNECTED_CLIENT: Color.GREEN,
}
static var state: ConnectionState = ConnectionState.DISCONNECTED
static var isMultiplayer: bool = false
static var maxPlayer: int = 10
static func launchServer(port: int):
isMultiplayer = true
var peer = ENetMultiplayerPeer.new()
peer.create_server(port, maxPlayer)
return peer
@@ -0,0 +1 @@
uid://ec6qkbh82cf