Added start game functionality
This commit is contained in:
parent
c3dd861e61
commit
27cce98d46
@ -3,7 +3,7 @@
|
||||
[ext_resource type="Script" path="res://assets/core/networking/scripts/lobby.gd" id="1_o4fbq"]
|
||||
[ext_resource type="PackedScene" uid="uid://biryul3n6thlw" path="res://assets/core/networking/scenes/user_box_prefab.tscn" id="2_dpthk"]
|
||||
|
||||
[node name="Lobby" type="Control" node_paths=PackedStringArray("host_button", "join_button", "leave_button", "lobby_id_line_edit", "user_list_box")]
|
||||
[node name="Lobby" type="Control" node_paths=PackedStringArray("host_button", "leave_button", "start_button", "user_list_box")]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
@ -12,9 +12,8 @@ grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_o4fbq")
|
||||
host_button = NodePath("Panel/HBoxContainer/Buttons/Host")
|
||||
join_button = NodePath("Panel/HBoxContainer/Buttons/HBoxContainer/Join")
|
||||
leave_button = NodePath("Panel/HBoxContainer/Buttons/Leave")
|
||||
lobby_id_line_edit = NodePath("Panel/HBoxContainer/Buttons/HBoxContainer/LineEdit")
|
||||
start_button = NodePath("Panel/HBoxContainer/Buttons/Start")
|
||||
user_list_box = NodePath("Panel/HBoxContainer/PlayerList")
|
||||
user_box_prefab = ExtResource("2_dpthk")
|
||||
|
||||
@ -45,23 +44,16 @@ layout_mode = 2
|
||||
size_flags_vertical = 4
|
||||
text = "Host Lobby"
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="Panel/HBoxContainer/Buttons"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="LineEdit" type="LineEdit" parent="Panel/HBoxContainer/Buttons/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
|
||||
[node name="Join" type="Button" parent="Panel/HBoxContainer/Buttons/HBoxContainer"]
|
||||
custom_minimum_size = Vector2(90, 0)
|
||||
layout_mode = 2
|
||||
text = "Join"
|
||||
|
||||
[node name="Leave" type="Button" parent="Panel/HBoxContainer/Buttons"]
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Leave Lobby"
|
||||
|
||||
[node name="Start" type="Button" parent="Panel/HBoxContainer/Buttons"]
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Start"
|
||||
|
||||
[node name="PlayerList" type="VBoxContainer" parent="Panel/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
|
@ -4,6 +4,9 @@ const STEAM_APP_ID: int = 480
|
||||
|
||||
const PACKET_READ_LIMIT: int = 32
|
||||
|
||||
var level_scene: PackedScene
|
||||
var player_scene: PackedScene
|
||||
|
||||
var lobby_data
|
||||
var lobby_id: int = 0
|
||||
var lobby_members: Array = []
|
||||
@ -22,7 +25,10 @@ signal user_left_lobby(user_id: int)
|
||||
signal host_left_lobby()
|
||||
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
player_scene = load("res://assets/core/player-controller/scenes/player.tscn") as PackedScene
|
||||
level_scene = load("res://assets/core/enviroment/dev-level/dev-level.tscn") as PackedScene
|
||||
_init_steam()
|
||||
|
||||
|
||||
@ -250,6 +256,10 @@ func send_p2p_packet(target: int, packet_data: Dictionary) -> void:
|
||||
for this_member in lobby_members:
|
||||
if this_member['steam_id'] != steam_id:
|
||||
Steam.sendP2PPacket(this_member['steam_id'], this_data, send_type, channel)
|
||||
print("Sent packet to %s." % this_member['steam_name'])
|
||||
# Else send it to yourself
|
||||
else:
|
||||
Steam.sendP2PPacket(steam_id, this_data, send_type, channel)
|
||||
|
||||
# Else send it to someone specific
|
||||
else:
|
||||
@ -284,6 +294,19 @@ func read_p2p_packet() -> void:
|
||||
# Decompress the array before turning it into a useable dictionary
|
||||
var readable_data: Dictionary = bytes_to_var(packet_code.decompress_dynamic(-1, FileAccess.COMPRESSION_GZIP))
|
||||
|
||||
# Handshake packet
|
||||
if "message" in readable_data and readable_data["message"] == "handshake":
|
||||
if packet_sender != steam_id:
|
||||
print("Received handshake packet from %s." % Steam.getFriendPersonaName(packet_sender))
|
||||
|
||||
# Start game packet
|
||||
if "message" in readable_data and readable_data["message"] == "start_game":
|
||||
if packet_sender == host_id:
|
||||
print("Received start game packet from host user.")
|
||||
_on_game_started()
|
||||
else:
|
||||
printerr("WARNING: Received start game packet from non-host user.")
|
||||
|
||||
# Print the packet to output
|
||||
print("Packet: %s" % readable_data)
|
||||
|
||||
@ -317,6 +340,18 @@ func get_player_avatar(user_id: int) -> ImageTexture:
|
||||
return avatar_texture_cache[user_id]
|
||||
|
||||
|
||||
func start_game():
|
||||
print("Starting game...")
|
||||
var packet_data = {"message":"start_game"}
|
||||
send_p2p_packet(0, packet_data) # Send to all lobby members
|
||||
|
||||
|
||||
func _on_game_started():
|
||||
print("Game started.")
|
||||
var level: Node = level_scene.instantiate()
|
||||
get_tree().root.add_child(level)
|
||||
|
||||
for member in lobby_members:
|
||||
var player: Node = player_scene.instantiate()
|
||||
level.add_child(player)
|
||||
player.name = "Player_" + str(member["steam_id"])
|
||||
|
@ -1,9 +1,8 @@
|
||||
extends Control
|
||||
|
||||
@export var host_button: Button
|
||||
@export var join_button: Button
|
||||
@export var leave_button: Button
|
||||
@export var lobby_id_line_edit: LineEdit
|
||||
@export var start_button: Button
|
||||
@export var user_list_box: VBoxContainer
|
||||
@export var user_box_prefab: PackedScene
|
||||
|
||||
@ -12,8 +11,8 @@ var added_users = []
|
||||
|
||||
func _ready() -> void:
|
||||
host_button.pressed.connect(_on_host)
|
||||
join_button.pressed.connect(_on_join)
|
||||
leave_button.pressed.connect(_on_leave)
|
||||
start_button.pressed.connect(_on_start)
|
||||
|
||||
NetworkManager.lobby_created.connect(_on_lobby_created)
|
||||
NetworkManager.lobby_joined.connect(_on_lobby_joined)
|
||||
@ -27,10 +26,6 @@ func _on_host():
|
||||
NetworkManager.host_lobby()
|
||||
|
||||
|
||||
func _on_join():
|
||||
NetworkManager.join_lobby(int(lobby_id_line_edit.text))
|
||||
|
||||
|
||||
func _on_leave():
|
||||
NetworkManager.leave_lobby()
|
||||
added_users.clear()
|
||||
@ -40,18 +35,14 @@ func _on_leave():
|
||||
|
||||
func _on_lobby_created(lobby_id: int):
|
||||
print("Lobby created")
|
||||
lobby_id_line_edit.text = str(NetworkManager.lobby_id)
|
||||
host_button.disabled = true
|
||||
join_button.disabled = true
|
||||
leave_button.disabled = false
|
||||
lobby_id_line_edit.editable = false
|
||||
start_button.disabled = false
|
||||
|
||||
|
||||
func _on_lobby_joined(lobby_id: int):
|
||||
host_button.disabled = true
|
||||
join_button.disabled = true
|
||||
leave_button.disabled = false
|
||||
lobby_id_line_edit.editable = false
|
||||
|
||||
|
||||
func _on_user_joined_lobby(user_id: int):
|
||||
@ -67,6 +58,15 @@ func _on_user_left_lobby(user_id: int):
|
||||
update()
|
||||
|
||||
|
||||
func _on_start():
|
||||
NetworkManager.start_game()
|
||||
visible = false
|
||||
|
||||
|
||||
func _on_game_started():
|
||||
visible = false
|
||||
|
||||
|
||||
func update() -> void:
|
||||
for child in user_list_box.get_children():
|
||||
child.queue_free()
|
||||
|
Loading…
Reference in New Issue
Block a user