Update other networking demos, with style fixes

This commit is contained in:
Aaron Franke
2020-02-19 17:48:36 -05:00
parent 984a731502
commit b310877037
26 changed files with 275 additions and 266 deletions

View File

@@ -8,13 +8,7 @@ ground_bottom_color = Color( 0.101961, 0.145098, 0.188235, 1 )
ground_horizon_color = Color( 0.482353, 0.788235, 0.952941, 1 )
ground_curve = 0.01
sun_energy = 16.0
__meta__ = {
}
[resource]
background_mode = 2
background_sky = SubResource( 1 )
__meta__ = {
}

View File

@@ -16,34 +16,22 @@ __meta__ = {
[node name="HBoxContainer" type="HBoxContainer" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
__meta__ = {
}
[node name="RichTextLabel" type="RichTextLabel" parent="HBoxContainer"]
margin_right = 510.0
margin_bottom = 600.0
size_flags_horizontal = 3
__meta__ = {
}
[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer"]
margin_left = 514.0
margin_right = 1024.0
margin_bottom = 600.0
size_flags_horizontal = 3
__meta__ = {
}
[node name="Label" type="Label" parent="HBoxContainer/VBoxContainer"]
margin_right = 510.0
margin_bottom = 14.0
text = "Players:"
__meta__ = {
}
[node name="ItemList" type="ItemList" parent="HBoxContainer/VBoxContainer"]
margin_top = 18.0
@@ -52,9 +40,6 @@ margin_bottom = 576.0
size_flags_horizontal = 3
size_flags_vertical = 3
same_column_width = true
__meta__ = {
}
[node name="Action" type="Button" parent="HBoxContainer/VBoxContainer"]
margin_top = 580.0
@@ -62,7 +47,4 @@ margin_right = 510.0
margin_bottom = 600.0
disabled = true
text = "Do Action!"
__meta__ = {
}
[connection signal="pressed" from="HBoxContainer/VBoxContainer/Action" to="." method="_on_Action_pressed"]

View File

@@ -3,20 +3,17 @@
[ext_resource path="res://script/main.gd" type="Script" id=1]
[ext_resource path="res://scene/game.tscn" type="PackedScene" id=2]
[node name="Control" type="Control"]
[node name="Main" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Panel" type="Panel" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
__meta__ = {
}
[node name="VBoxContainer" type="VBoxContainer" parent="Panel"]
anchor_right = 1.0
@@ -25,16 +22,10 @@ margin_left = 20.0
margin_top = 20.0
margin_right = -20.0
margin_bottom = -20.0
__meta__ = {
}
[node name="HBoxContainer" type="HBoxContainer" parent="Panel/VBoxContainer"]
margin_right = 984.0
margin_bottom = 24.0
__meta__ = {
}
[node name="Label" type="Label" parent="Panel/VBoxContainer/HBoxContainer"]
margin_top = 5.0
@@ -42,9 +33,6 @@ margin_right = 326.0
margin_bottom = 19.0
size_flags_horizontal = 3
text = "Name"
__meta__ = {
}
[node name="NameEdit" type="LineEdit" parent="Panel/VBoxContainer/HBoxContainer"]
margin_left = 330.0
@@ -53,51 +41,33 @@ margin_bottom = 24.0
size_flags_horizontal = 3
size_flags_stretch_ratio = 2.0
text = "A Godot User"
__meta__ = {
}
[node name="HBoxContainer2" type="HBoxContainer" parent="Panel/VBoxContainer"]
margin_top = 28.0
margin_right = 984.0
margin_bottom = 52.0
__meta__ = {
}
[node name="HBoxContainer" type="HBoxContainer" parent="Panel/VBoxContainer/HBoxContainer2"]
margin_right = 326.0
margin_bottom = 24.0
size_flags_horizontal = 3
__meta__ = {
}
[node name="Host" type="Button" parent="Panel/VBoxContainer/HBoxContainer2/HBoxContainer"]
margin_right = 42.0
margin_bottom = 24.0
text = "Host"
__meta__ = {
}
[node name="Control" type="Control" parent="Panel/VBoxContainer/HBoxContainer2/HBoxContainer"]
margin_left = 46.0
margin_right = 241.0
margin_bottom = 24.0
size_flags_horizontal = 3
__meta__ = {
}
[node name="Connect" type="Button" parent="Panel/VBoxContainer/HBoxContainer2/HBoxContainer"]
margin_left = 245.0
margin_right = 326.0
margin_bottom = 24.0
text = "Connect to"
__meta__ = {
}
[node name="Disconnect" type="Button" parent="Panel/VBoxContainer/HBoxContainer2/HBoxContainer"]
visible = false
@@ -105,9 +75,6 @@ margin_left = 68.0
margin_right = 152.0
margin_bottom = 24.0
text = "Disconnect"
__meta__ = {
}
[node name="Hostname" type="LineEdit" parent="Panel/VBoxContainer/HBoxContainer2"]
margin_left = 330.0
@@ -117,18 +84,12 @@ size_flags_horizontal = 3
size_flags_stretch_ratio = 2.0
text = "localhost"
placeholder_text = "localhost"
__meta__ = {
}
[node name="Control" type="Control" parent="Panel/VBoxContainer"]
margin_top = 56.0
margin_right = 984.0
margin_bottom = 76.0
rect_min_size = Vector2( 0, 20 )
__meta__ = {
}
[node name="Game" parent="Panel/VBoxContainer" instance=ExtResource( 2 )]
anchor_right = 0.0
@@ -147,9 +108,6 @@ margin_top = -100.0
margin_right = 200.0
margin_bottom = 100.0
dialog_text = "Connection closed"
__meta__ = {
}
[connection signal="pressed" from="Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Host" to="." method="_on_Host_pressed"]
[connection signal="pressed" from="Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Connect" to="." method="_on_Connect_pressed"]
[connection signal="pressed" from="Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Disconnect" to="." method="_on_Disconnect_pressed"]

View File

@@ -12,11 +12,13 @@ master func set_player_name(name):
var sender = get_tree().get_rpc_sender_id()
rpc("update_player_name", sender, name)
sync func update_player_name(player, name):
var pos = _players.find(player)
if pos != -1:
_list.set_item_text(pos, name)
master func request_action(action):
var sender = get_tree().get_rpc_sender_id()
if _players[_turn] != get_tree().get_rpc_sender_id():
@@ -25,11 +27,13 @@ master func request_action(action):
do_action(action)
next_turn()
sync func do_action(action):
var name = _list.get_item_text(_turn)
var val = randi() % 100
rpc("_log", "%s: %ss %d" % [name, action, val])
sync func set_turn(turn):
_turn = turn
if turn >= _players.size():
@@ -41,6 +45,7 @@ sync func set_turn(turn):
_list.set_item_icon(i, null)
_action.disabled = _players[turn] != get_tree().get_network_unique_id()
sync func del_player(id):
var pos = _players.find(id)
if pos == -1:
@@ -52,6 +57,7 @@ sync func del_player(id):
if get_tree().is_network_server():
rpc("set_turn", _turn)
sync func add_player(id, name=""):
_players.append(id)
if name == "":
@@ -59,27 +65,32 @@ sync func add_player(id, name=""):
else:
_list.add_item(name, null, false)
func get_player_name(pos):
if pos < _list.get_item_count():
return _list.get_item_text(pos)
else:
return "Error!"
func next_turn():
_turn += 1
if _turn >= _players.size():
_turn = 0
rpc("set_turn", _turn)
func start():
set_turn(0)
func stop():
_players.clear()
_list.clear()
_turn = 0
_action.disabled = true
func on_peer_add(id):
if not get_tree().is_network_server():
return
@@ -88,14 +99,17 @@ func on_peer_add(id):
rpc("add_player", id)
rpc_id(id, "set_turn", _turn)
func on_peer_del(id):
if not get_tree().is_network_server():
return
rpc("del_player", id)
sync func _log(what):
$HBoxContainer/RichTextLabel.add_text(what + "\n")
func _on_Action_pressed():
if get_tree().is_network_server():
do_action("roll")

View File

@@ -11,10 +11,18 @@ onready var _host_edit = $Panel/VBoxContainer/HBoxContainer2/Hostname
onready var _game = $Panel/VBoxContainer/Game
func _ready():
#warning-ignore-all:return_value_discarded
get_tree().connect("network_peer_disconnected", self, "_peer_disconnected")
get_tree().connect("network_peer_connected", self, "_peer_connected")
$AcceptDialog.get_label().align = Label.ALIGN_CENTER
$AcceptDialog.get_label().valign = Label.VALIGN_CENTER
# Set the player name according to the system username. Fallback to the path.
if OS.has_environment("USERNAME"):
_name_edit.text = OS.get_environment("USERNAME")
else:
var desktop_path = OS.get_system_dir(0).replace("\\", "/").split("/")
_name_edit.text = desktop_path[desktop_path.size() - 2]
func start_game():
_host_btn.disabled = true
@@ -24,6 +32,7 @@ func start_game():
_disconnect_btn.show()
_game.start()
func stop_game():
_host_btn.disabled = false
_name_edit.editable = true
@@ -32,6 +41,7 @@ func stop_game():
_connect_btn.show()
_game.stop()
func _close_network():
if get_tree().is_connected("server_disconnected", self, "_close_network"):
get_tree().disconnect("server_disconnected", self, "_close_network")
@@ -44,15 +54,19 @@ func _close_network():
$AcceptDialog.get_close_button().grab_focus()
get_tree().set_network_peer(null)
func _connected():
_game.rpc("set_player_name", _name_edit.text)
func _peer_connected(id):
_game.on_peer_add(id)
func _peer_disconnected(id):
_game.on_peer_del(id)
func _on_Host_pressed():
var host = WebSocketServer.new()
host.listen(DEF_PORT, PoolStringArray(["ludus"]), true)
@@ -61,9 +75,11 @@ func _on_Host_pressed():
_game.add_player(1, _name_edit.text)
start_game()
func _on_Disconnect_pressed():
_close_network()
func _on_Connect_pressed():
var host = WebSocketClient.new()
host.connect_to_url("ws://" + _host_edit.text + ":" + str(DEF_PORT), PoolStringArray([PROTO_NAME]), true)