feat: 完成设置部分和游戏内房主房客身份及IP地址的识别

This commit is contained in:
2026-01-06 08:12:57 +08:00
parent 7d75cd5660
commit 10e6d2d645
16 changed files with 159 additions and 5 deletions
+2
View File
@@ -18,6 +18,8 @@ config/icon="res://icon.svg"
[autoload]
MultiGame="*res://scripts/autoload/MultiGame.gd"
SceneManager="*res://scripts/autoload/SceneManager.gd"
GameManager="*res://scripts/autoload/GameManager.gd"
[rendering]
+30 -1
View File
@@ -1,3 +1,32 @@
[gd_scene format=3 uid="uid://3gquhwcjfis7"]
[gd_scene load_steps=3 format=3 uid="uid://3gquhwcjfis7"]
[ext_resource type="FontFile" uid="uid://c2bipf4tpdjj8" path="res://assets/fonts/AlibabaPuHuiTi-3-65-Medium.ttf" id="1_uwrxv"]
[ext_resource type="Script" uid="uid://78lmrp321x85" path="res://scripts/game/game.gd" id="1_yqjtg"]
[node name="Game" type="Node2D"]
script = ExtResource("1_yqjtg")
[node name="Background" type="ColorRect" parent="."]
offset_left = -34.0
offset_top = -27.0
offset_right = 1186.0
offset_bottom = 694.0
color = Color(0.7058824, 0.6862745, 0, 1)
[node name="IsServerLabel" type="Label" parent="."]
offset_left = 30.0
offset_top = 11.0
offset_right = 227.0
offset_bottom = 59.0
theme_override_fonts/font = ExtResource("1_uwrxv")
theme_override_font_sizes/font_size = 20
text = "啊啊啊啊啊:啊啊啊啊"
[node name="IPLabel" type="Label" parent="."]
offset_left = 948.0
offset_top = 11.0
offset_right = 1052.0
offset_bottom = 46.0
theme_override_fonts/font = ExtResource("1_uwrxv")
theme_override_font_sizes/font_size = 24
text = "127.0.0.2"
+14 -4
View File
@@ -9,10 +9,10 @@
script = ExtResource("1_ekxnf")
[node name="Background" type="ColorRect" parent="."]
offset_left = -21.0
offset_top = -15.0
offset_right = 1186.0
offset_bottom = 654.0
offset_left = -24.0
offset_top = -22.0
offset_right = 1183.0
offset_bottom = 647.0
color = Color(0.7058824, 0.6862745, 0, 1)
[node name="Title" type="Label" parent="."]
@@ -138,8 +138,18 @@ offset_bottom = 126.0
theme_override_fonts/font = ExtResource("1_l6cm7")
text = "请输入服务器IP无需加端口"
[node name="SettingButton" type="Button" parent="."]
offset_left = 1029.0
offset_top = 14.0
offset_right = 1137.0
offset_bottom = 84.0
theme_override_fonts/font = ExtResource("1_l6cm7")
theme_override_font_sizes/font_size = 30
text = "设置"
[connection signal="pressed" from="StartGame" to="." method="_on_start_game_pressed"]
[connection signal="pressed" from="JoinGame" to="." method="_on_join_game_pressed"]
[connection signal="pressed" from="CreateGameUI/CreateGameButton" to="CreateGameUI" method="_on_create_game_button_pressed"]
[connection signal="pressed" from="CreateGameUI/Warn/WarnButton" to="CreateGameUI" method="_on_warn_button_pressed"]
[connection signal="pressed" from="JoinGameUI/JoinGameButton" to="JoinGameUI" method="_on_join_game_button_pressed"]
[connection signal="pressed" from="SettingButton" to="." method="_on_setting_button_pressed"]
+54
View File
@@ -0,0 +1,54 @@
[gd_scene load_steps=3 format=3 uid="uid://bxbycggrcmo3q"]
[ext_resource type="Script" uid="uid://deltvf06kqovt" path="res://scripts/settings/settings.gd" id="1_r6d6q"]
[ext_resource type="FontFile" uid="uid://c2bipf4tpdjj8" path="res://assets/fonts/AlibabaPuHuiTi-3-65-Medium.ttf" id="2_6wm04"]
[node name="Settings" type="Node2D"]
script = ExtResource("1_r6d6q")
[node name="Background" type="ColorRect" parent="."]
offset_left = -34.0
offset_top = -27.0
offset_right = 1186.0
offset_bottom = 694.0
color = Color(0.7058824, 0.6862745, 0, 1)
[node name="DataSetting" type="Node2D" parent="."]
[node name="DataSetLabel" type="Label" parent="DataSetting"]
offset_left = 45.0
offset_top = 28.0
offset_right = 193.0
offset_bottom = 90.0
theme_override_fonts/font = ExtResource("2_6wm04")
theme_override_font_sizes/font_size = 30
text = "数据源设置"
[node name="LineEdit" type="LineEdit" parent="DataSetting"]
offset_left = 217.0
offset_top = 18.0
offset_right = 874.0
offset_bottom = 81.0
theme_override_fonts/font = ExtResource("2_6wm04")
theme_override_font_sizes/font_size = 24
[node name="SaveButton" type="Button" parent="."]
offset_left = 56.0
offset_top = 520.0
offset_right = 220.0
offset_bottom = 597.0
theme_override_fonts/font = ExtResource("2_6wm04")
theme_override_font_sizes/font_size = 30
text = "保存设置"
[node name="CancelButton" type="Button" parent="."]
offset_left = 243.0
offset_top = 514.0
offset_right = 419.0
offset_bottom = 603.0
theme_override_fonts/font = ExtResource("2_6wm04")
theme_override_font_sizes/font_size = 30
text = "取消"
[connection signal="pressed" from="SaveButton" to="." method="_on_save_button_pressed"]
[connection signal="pressed" from="CancelButton" to="." method="_on_cancel_button_pressed"]
+3
View File
@@ -0,0 +1,3 @@
extends Node
var data_origin: String = ""
+1
View File
@@ -0,0 +1 @@
uid://c11tksjyetdby
+6
View File
@@ -1,11 +1,17 @@
extends Node
var isServer: bool
# 基于低级 ENet 多人游戏
func create_server(player_num: int) -> void:
isServer = true
var peer = ENetMultiplayerPeer.new()
peer.create_server(8989, player_num)
multiplayer.multiplayer_peer = peer
func create_client(ip: String) -> void:
isServer = false
var peer = ENetMultiplayerPeer.new()
peer.create_client(ip, 8989)
multiplayer.multiplayer_peer = peer
+11
View File
@@ -0,0 +1,11 @@
extends Node
var current_scene = null
func _ready():
var root = get_tree().root
current_scene = root.get_child(root.get_child_count() - 1)
func goto_scene(path: String):
current_scene = path
get_tree().change_scene_to_file("res://scenes/%s.tscn" % [path])
+1
View File
@@ -0,0 +1 @@
uid://dxogo0f6xslqs
+17
View File
@@ -0,0 +1,17 @@
extends Node2D
func _ready() -> void:
init()
func init() -> void:
if MultiGame.isServer:
$IsServerLabel.text = "房间运行中:您是房主"
else:
$IsServerLabel.text = "房间运行中:您是房客"
var addresses: PackedStringArray = IP.get_local_addresses()
var ipaddress: String = ""
for address in addresses:
if address.substr(0, 8) == "192.168.":
ipaddress = address
$IPLabel.text = ipaddress
+1
View File
@@ -0,0 +1 @@
uid://78lmrp321x85
+1
View File
@@ -6,6 +6,7 @@ func _on_create_game_button_pressed() -> void:
if 2 <= player_num and player_num <= 4:
MultiGame.create_server(int(player_num))
$".".hide()
SceneManager.goto_scene("game")
else:
$Warn.show()
+1
View File
@@ -5,3 +5,4 @@ func _on_join_game_button_pressed() -> void:
var ip: String = $JoinGameEdit.text
MultiGame.create_client(ip)
$".".hide()
SceneManager.goto_scene("game")
+4
View File
@@ -6,3 +6,7 @@ func _on_join_game_pressed() -> void:
func _on_start_game_pressed() -> void:
$CreateGameUI.show()
func _on_setting_button_pressed() -> void:
SceneManager.goto_scene("settings")
+12
View File
@@ -0,0 +1,12 @@
extends Node2D
func _ready() -> void:
$DataSetting/LineEdit.text = GameManager.data_origin
func _on_save_button_pressed() -> void:
GameManager.data_origin = $DataSetting/LineEdit.text
SceneManager.goto_scene("main_menu")
func _on_cancel_button_pressed() -> void:
SceneManager.goto_scene("main_menu")
+1
View File
@@ -0,0 +1 @@
uid://deltvf06kqovt