added multiplayer connection. TODO: Sync Inventory values. Disable moving of other color tiles
This commit is contained in:
parent
e8cd148bc7
commit
3de181134d
13 changed files with 360 additions and 67 deletions
69
Multiplayer/LobbyInfo.gd
Normal file
69
Multiplayer/LobbyInfo.gd
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
extends CanvasLayer
|
||||
|
||||
@onready var insect_icons: HBoxContainer = $PanelContainer/VBoxContainer/InsectIcons
|
||||
|
||||
@onready var lobby_code: Label = $PanelContainer/VBoxContainer/LobbyCode
|
||||
const INSECT_BUTTON = preload("res://UI/insect_button.tscn")
|
||||
|
||||
const insect_resources = [
|
||||
preload("res://Tile/Prefabs/Ant.tres"),
|
||||
preload("res://Tile/Prefabs/Bee.tres"),
|
||||
preload("res://Tile/Prefabs/Beetle.tres"),
|
||||
preload("res://Tile/Prefabs/Grasshopper.tres"),
|
||||
preload("res://Tile/Prefabs/Ladybug.tres"),
|
||||
preload("res://Tile/Prefabs/Mosquito.tres"),
|
||||
preload("res://Tile/Prefabs/Pillbug.tres"),
|
||||
preload("res://Tile/Prefabs/Spider.tres")
|
||||
]
|
||||
|
||||
var insect_data = {
|
||||
|
||||
}
|
||||
|
||||
func prepare_insect_data() -> void:
|
||||
var c: int = 65
|
||||
|
||||
for i in insect_resources:
|
||||
for k in 2:
|
||||
insect_data[String.chr(c)] = {"resource": i, "is_black": k%2 != 0}
|
||||
c = c + 1
|
||||
|
||||
|
||||
@onready var start_game_button = $PanelContainer/VBoxContainer/StartGameButton
|
||||
|
||||
func _on_connect() -> void:
|
||||
lobby_code.text = "Waiting for the host to start the game..."
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
multiplayer.connected_to_server.connect(_on_connect)
|
||||
|
||||
if not multiplayer.is_server():
|
||||
return
|
||||
|
||||
insect_icons.visible = true
|
||||
start_game_button.visible = true
|
||||
|
||||
lobby_code.text = GameData.lobby_code
|
||||
|
||||
prepare_insect_data()
|
||||
|
||||
for c in GameData.lobby_code:
|
||||
var b = INSECT_BUTTON.instantiate()
|
||||
b.deactivated = true
|
||||
b.disabled = true
|
||||
b.disable_amount_display = true
|
||||
var data = insect_data[c]
|
||||
b.is_black = data.is_black
|
||||
b.insect_resource = data.resource
|
||||
insect_icons.add_child(b)
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
pass
|
||||
|
||||
|
||||
func _on_button_pressed():
|
||||
WSClient.stop()
|
||||
get_tree().change_scene_to_file("res://main_menu.tscn")
|
||||
60
Multiplayer/Multiplayer.gd
Normal file
60
Multiplayer/Multiplayer.gd
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
extends Node
|
||||
|
||||
var has_opponent: bool = false
|
||||
@onready var start_game_button = $LobbyInfo/PanelContainer/VBoxContainer/StartGameButton
|
||||
@onready var game = $Game
|
||||
|
||||
func _ready():
|
||||
multiplayer.connected_to_server.connect(_mp_server_connected)
|
||||
multiplayer.connection_failed.connect(_mp_server_disconnect)
|
||||
multiplayer.server_disconnected.connect(_mp_server_disconnect)
|
||||
multiplayer.peer_connected.connect(_mp_peer_connected)
|
||||
multiplayer.peer_disconnected.connect(_mp_peer_disconnected)
|
||||
|
||||
func _mp_peer_disconnected(id: int) -> void:
|
||||
has_opponent = false
|
||||
start_game_button.disabled = true
|
||||
|
||||
if id == 1:
|
||||
# 1 is always the server
|
||||
# So we better get going back to the main menu?
|
||||
WSClient.close()
|
||||
get_tree().change_scene_to_file("res://main_menu.tscn")
|
||||
# Maybe just display a "server close
|
||||
return
|
||||
|
||||
func _log(msg):
|
||||
print(msg)
|
||||
|
||||
func _mp_server_connected():
|
||||
_log("[Multiplayer] Server connected (I am %d)" % WSClient.rtc_mp.get_unique_id())
|
||||
#pass_initial_playerdata.rpc_id(1, multiplayer.get_unique_id(), GameState.player_name)
|
||||
|
||||
func _mp_server_disconnect():
|
||||
_log("[Multiplayer] Server disconnected (I am %d)" % WSClient.rtc_mp.get_unique_id())
|
||||
get_tree().change_scene_to_file("res://main_menu.tscn")
|
||||
|
||||
func _mp_peer_connected(id: int):
|
||||
_log("[Multiplayer] Peer %d connected" % id)
|
||||
has_opponent = true
|
||||
start_game_button.disabled = false
|
||||
|
||||
@onready var lobby_info = $LobbyInfo
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
pass
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func load_game() -> void:
|
||||
# Hide menu
|
||||
# load game scene
|
||||
lobby_info.visible = false
|
||||
var game_scene = preload("res://Game.tscn").instantiate()
|
||||
game.add_child(game_scene)
|
||||
|
||||
func _on_start_game_button_pressed():
|
||||
# tell other peer to now load the world via rpc
|
||||
load_game.rpc()
|
||||
pass # Replace with function body.
|
||||
47
Multiplayer/Multiplayer.tscn
Normal file
47
Multiplayer/Multiplayer.tscn
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://bh31hq1uwqdts"]
|
||||
|
||||
[ext_resource type="Script" path="res://Multiplayer/Multiplayer.gd" id="1_0n82x"]
|
||||
[ext_resource type="Script" path="res://Multiplayer/LobbyInfo.gd" id="2_wf1xe"]
|
||||
|
||||
[node name="Multiplayer" type="Node"]
|
||||
script = ExtResource("1_0n82x")
|
||||
|
||||
[node name="Game" type="Node" parent="."]
|
||||
|
||||
[node name="LobbyInfo" type="CanvasLayer" parent="."]
|
||||
script = ExtResource("2_wf1xe")
|
||||
|
||||
[node name="PanelContainer" type="PanelContainer" parent="LobbyInfo"]
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="LobbyInfo/PanelContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 4
|
||||
alignment = 1
|
||||
|
||||
[node name="LobbyCode" type="Label" parent="LobbyInfo/PanelContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="InsectIcons" type="HBoxContainer" parent="LobbyInfo/PanelContainer/VBoxContainer"]
|
||||
visible = false
|
||||
layout_mode = 2
|
||||
|
||||
[node name="StartGameButton" type="Button" parent="LobbyInfo/PanelContainer/VBoxContainer"]
|
||||
visible = false
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Start Game"
|
||||
|
||||
[node name="Button" type="Button" parent="LobbyInfo/PanelContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Back to menu"
|
||||
|
||||
[node name="PauseMenu" type="CanvasLayer" parent="."]
|
||||
|
||||
[connection signal="pressed" from="LobbyInfo/PanelContainer/VBoxContainer/StartGameButton" to="." method="_on_start_game_button_pressed"]
|
||||
[connection signal="pressed" from="LobbyInfo/PanelContainer/VBoxContainer/Button" to="LobbyInfo" method="_on_button_pressed"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue