generated from krampus/template-godot4
Peer-to-peer connection logic
This commit is contained in:
parent
5745324fe9
commit
c90146476b
7
assets/fonts/Cherry_Bomb_One/CherryBombOne_italic.tres
Normal file
7
assets/fonts/Cherry_Bomb_One/CherryBombOne_italic.tres
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[gd_resource type="FontVariation" load_steps=2 format=3 uid="uid://cscd67o6vdxph"]
|
||||||
|
|
||||||
|
[ext_resource type="FontFile" uid="uid://b5rg388cars1d" path="res://assets/fonts/Cherry_Bomb_One/CherryBombOne-Regular.ttf" id="1_luvj2"]
|
||||||
|
|
||||||
|
[resource]
|
||||||
|
base_font = ExtResource("1_luvj2")
|
||||||
|
variation_transform = Transform2D(1, 0.25, 0, 1, 0, 0)
|
@ -1 +1,31 @@
|
|||||||
class_name Game extends Control
|
class_name Game extends Control
|
||||||
|
|
||||||
|
@export var player_name: String
|
||||||
|
|
||||||
|
@onready var player_name_label: Label = %PlayerName
|
||||||
|
@onready var opponent_name: Label = %OpponentName
|
||||||
|
|
||||||
|
@onready var connecting_screen: ColorRect = %ConnectingScreen
|
||||||
|
|
||||||
|
|
||||||
|
func _ready() -> void:
|
||||||
|
multiplayer.peer_connected.connect(_on_peer_connected)
|
||||||
|
multiplayer.peer_disconnected.connect(_on_peer_disconnected)
|
||||||
|
|
||||||
|
player_name_label.text = player_name
|
||||||
|
|
||||||
|
|
||||||
|
@rpc("any_peer", "call_remote", "reliable", 0)
|
||||||
|
func set_opponent_name(opp_name: String) -> void:
|
||||||
|
opponent_name.text = opp_name
|
||||||
|
|
||||||
|
|
||||||
|
func _on_peer_connected(id: int) -> void:
|
||||||
|
print("PEER CONNECTED ", id)
|
||||||
|
set_opponent_name.rpc(player_name)
|
||||||
|
connecting_screen.hide()
|
||||||
|
|
||||||
|
|
||||||
|
func _on_peer_disconnected(id: int) -> void:
|
||||||
|
print("PEER DISCONNECTED: ", id)
|
||||||
|
connecting_screen.show()
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
[gd_scene load_steps=4 format=3 uid="uid://d0d6xpa4ma7wd"]
|
[gd_scene load_steps=5 format=3 uid="uid://d0d6xpa4ma7wd"]
|
||||||
|
|
||||||
[ext_resource type="Shader" path="res://src/shaders/balatro_bg.gdshader" id="1_defdw"]
|
[ext_resource type="Shader" path="res://src/shaders/balatro_bg.gdshader" id="1_defdw"]
|
||||||
[ext_resource type="Script" path="res://src/game/game.gd" id="1_kepju"]
|
[ext_resource type="Script" path="res://src/game/game.gd" id="1_kepju"]
|
||||||
|
[ext_resource type="FontVariation" uid="uid://cscd67o6vdxph" path="res://assets/fonts/Cherry_Bomb_One/CherryBombOne_italic.tres" id="3_jqmr8"]
|
||||||
|
|
||||||
[sub_resource type="ShaderMaterial" id="ShaderMaterial_h23fw"]
|
[sub_resource type="ShaderMaterial" id="ShaderMaterial_h23fw"]
|
||||||
shader = ExtResource("1_defdw")
|
shader = ExtResource("1_defdw")
|
||||||
@ -28,7 +29,7 @@ grow_horizontal = 2
|
|||||||
grow_vertical = 2
|
grow_vertical = 2
|
||||||
script = ExtResource("1_kepju")
|
script = ExtResource("1_kepju")
|
||||||
|
|
||||||
[node name="ColorRect" type="ColorRect" parent="."]
|
[node name="Background" type="ColorRect" parent="."]
|
||||||
material = SubResource("ShaderMaterial_h23fw")
|
material = SubResource("ShaderMaterial_h23fw")
|
||||||
layout_mode = 1
|
layout_mode = 1
|
||||||
anchors_preset = 15
|
anchors_preset = 15
|
||||||
@ -36,3 +37,88 @@ anchor_right = 1.0
|
|||||||
anchor_bottom = 1.0
|
anchor_bottom = 1.0
|
||||||
grow_horizontal = 2
|
grow_horizontal = 2
|
||||||
grow_vertical = 2
|
grow_vertical = 2
|
||||||
|
|
||||||
|
[node name="HBoxContainer" type="HBoxContainer" parent="."]
|
||||||
|
layout_mode = 1
|
||||||
|
anchors_preset = 15
|
||||||
|
anchor_right = 1.0
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
grow_vertical = 2
|
||||||
|
|
||||||
|
[node name="LeftColumn" type="MarginContainer" parent="HBoxContainer"]
|
||||||
|
layout_mode = 2
|
||||||
|
size_flags_horizontal = 3
|
||||||
|
theme_override_constants/margin_left = 32
|
||||||
|
theme_override_constants/margin_top = 32
|
||||||
|
theme_override_constants/margin_right = 32
|
||||||
|
theme_override_constants/margin_bottom = 32
|
||||||
|
|
||||||
|
[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer/LeftColumn"]
|
||||||
|
layout_mode = 2
|
||||||
|
|
||||||
|
[node name="Label" type="Label" parent="HBoxContainer/LeftColumn/VBoxContainer"]
|
||||||
|
layout_mode = 2
|
||||||
|
theme_override_colors/font_color = Color(0, 0, 0, 1)
|
||||||
|
text = "(you)"
|
||||||
|
horizontal_alignment = 1
|
||||||
|
|
||||||
|
[node name="PlayerName" type="Label" parent="HBoxContainer/LeftColumn/VBoxContainer"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
layout_mode = 2
|
||||||
|
theme_override_colors/font_color = Color(0, 0, 0, 1)
|
||||||
|
theme_override_font_sizes/font_size = 32
|
||||||
|
text = "Lieutenant Nudisco"
|
||||||
|
horizontal_alignment = 1
|
||||||
|
|
||||||
|
[node name="RightColumn" type="MarginContainer" parent="HBoxContainer"]
|
||||||
|
layout_mode = 2
|
||||||
|
size_flags_horizontal = 3
|
||||||
|
theme_override_constants/margin_left = 32
|
||||||
|
theme_override_constants/margin_top = 32
|
||||||
|
theme_override_constants/margin_right = 32
|
||||||
|
theme_override_constants/margin_bottom = 32
|
||||||
|
|
||||||
|
[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer/RightColumn"]
|
||||||
|
layout_mode = 2
|
||||||
|
|
||||||
|
[node name="Label" type="Label" parent="HBoxContainer/RightColumn/VBoxContainer"]
|
||||||
|
layout_mode = 2
|
||||||
|
theme_override_colors/font_color = Color(0, 0, 0, 1)
|
||||||
|
text = "(the other guy)"
|
||||||
|
horizontal_alignment = 1
|
||||||
|
|
||||||
|
[node name="OpponentName" type="Label" parent="HBoxContainer/RightColumn/VBoxContainer"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
layout_mode = 2
|
||||||
|
theme_override_colors/font_color = Color(0, 0, 0, 1)
|
||||||
|
theme_override_font_sizes/font_size = 32
|
||||||
|
text = "Waiting for Connection..."
|
||||||
|
horizontal_alignment = 1
|
||||||
|
|
||||||
|
[node name="ConnectingScreen" type="ColorRect" parent="."]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
layout_mode = 1
|
||||||
|
anchors_preset = 15
|
||||||
|
anchor_right = 1.0
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
grow_vertical = 2
|
||||||
|
color = Color(0, 0, 0, 0.52549)
|
||||||
|
|
||||||
|
[node name="Label" type="Label" parent="ConnectingScreen"]
|
||||||
|
layout_mode = 1
|
||||||
|
anchors_preset = 8
|
||||||
|
anchor_left = 0.5
|
||||||
|
anchor_top = 0.5
|
||||||
|
anchor_right = 0.5
|
||||||
|
anchor_bottom = 0.5
|
||||||
|
offset_left = -20.0
|
||||||
|
offset_top = -12.0
|
||||||
|
offset_right = 20.0
|
||||||
|
offset_bottom = 12.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
grow_vertical = 2
|
||||||
|
theme_override_fonts/font = ExtResource("3_jqmr8")
|
||||||
|
theme_override_font_sizes/font_size = 64
|
||||||
|
text = "Waiting for Connection..."
|
||||||
|
@ -1,6 +1,31 @@
|
|||||||
extends Control
|
extends Control
|
||||||
|
|
||||||
const MAX_CLIENTS = 2
|
const MAX_CLIENTS = 2
|
||||||
|
const DEFAULT_PORT = 8383
|
||||||
|
|
||||||
|
const RANDOM_NAMES = [
|
||||||
|
"Guy Likely",
|
||||||
|
"Roy G. Biv",
|
||||||
|
"Narbacular Strunk",
|
||||||
|
"Eco Sourcepower",
|
||||||
|
"Monty Carlo",
|
||||||
|
"Banana Joe",
|
||||||
|
"Chunk Busko",
|
||||||
|
"Flip Moran",
|
||||||
|
"Motrin Onesie",
|
||||||
|
"Kobe Sparrow",
|
||||||
|
"Jambo Wallende",
|
||||||
|
"Corbyn Kingston",
|
||||||
|
"Andrej Wojnicz",
|
||||||
|
"Ouija Kinkade",
|
||||||
|
"Angel Gullhammer",
|
||||||
|
"Skeleton Collector",
|
||||||
|
"Bosko Stanislav",
|
||||||
|
"Lieutenant Nudisco",
|
||||||
|
"Trace Slamvector",
|
||||||
|
"Barrison Hegemon",
|
||||||
|
"Bux Amillion",
|
||||||
|
]
|
||||||
|
|
||||||
@export var game_scene: PackedScene
|
@export var game_scene: PackedScene
|
||||||
|
|
||||||
@ -8,11 +33,18 @@ const MAX_CLIENTS = 2
|
|||||||
@onready var host_menu: VBoxContainer = %HostMenu
|
@onready var host_menu: VBoxContainer = %HostMenu
|
||||||
@onready var join_menu: VBoxContainer = %JoinMenu
|
@onready var join_menu: VBoxContainer = %JoinMenu
|
||||||
|
|
||||||
|
@onready var name_input: LineEdit = %NameInput
|
||||||
@onready var host_port_input: SpinBox = %HostPortInput
|
@onready var host_port_input: SpinBox = %HostPortInput
|
||||||
@onready var hostname_input: LineEdit = %HostnameInput
|
@onready var hostname_input: LineEdit = %HostnameInput
|
||||||
@onready var join_port_input: SpinBox = %JoinPortInput
|
@onready var join_port_input: SpinBox = %JoinPortInput
|
||||||
|
|
||||||
|
|
||||||
|
func _ready() -> void:
|
||||||
|
name_input.text = RANDOM_NAMES.pick_random()
|
||||||
|
host_port_input.value = DEFAULT_PORT
|
||||||
|
join_port_input.value = DEFAULT_PORT
|
||||||
|
|
||||||
|
|
||||||
func quit() -> void:
|
func quit() -> void:
|
||||||
get_tree().quit()
|
get_tree().quit()
|
||||||
|
|
||||||
@ -35,8 +67,14 @@ func show_main_menu() -> void:
|
|||||||
main_menu.show()
|
main_menu.show()
|
||||||
|
|
||||||
|
|
||||||
func start_host_lobby() -> void:
|
func _build_game() -> Game:
|
||||||
var game: Game = game_scene.instantiate()
|
var game: Game = game_scene.instantiate()
|
||||||
|
game.player_name = name_input.text
|
||||||
|
return game
|
||||||
|
|
||||||
|
|
||||||
|
func start_host_lobby() -> void:
|
||||||
|
var game: Game = _build_game()
|
||||||
var peer := ENetMultiplayerPeer.new()
|
var peer := ENetMultiplayerPeer.new()
|
||||||
peer.create_server(host_port_input.value, MAX_CLIENTS)
|
peer.create_server(host_port_input.value, MAX_CLIENTS)
|
||||||
add_sibling(game)
|
add_sibling(game)
|
||||||
@ -45,7 +83,7 @@ func start_host_lobby() -> void:
|
|||||||
|
|
||||||
|
|
||||||
func join_lobby() -> void:
|
func join_lobby() -> void:
|
||||||
var game: Game = game_scene.instantiate()
|
var game: Game = _build_game()
|
||||||
var peer := ENetMultiplayerPeer.new()
|
var peer := ENetMultiplayerPeer.new()
|
||||||
peer.create_client(hostname_input.text, join_port_input.value)
|
peer.create_client(hostname_input.text, join_port_input.value)
|
||||||
add_sibling(game)
|
add_sibling(game)
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
[gd_scene load_steps=5 format=3 uid="uid://n7a1ch8ck0p4"]
|
[gd_scene load_steps=6 format=3 uid="uid://n7a1ch8ck0p4"]
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://src/title_screen/title_screen.gd" id="1_yksqe"]
|
[ext_resource type="Script" path="res://src/title_screen/title_screen.gd" id="1_yksqe"]
|
||||||
[ext_resource type="PackedScene" uid="uid://d0d6xpa4ma7wd" path="res://src/game/game.tscn" id="2_2uwxn"]
|
[ext_resource type="PackedScene" uid="uid://d0d6xpa4ma7wd" path="res://src/game/game.tscn" id="2_2uwxn"]
|
||||||
[ext_resource type="Shader" path="res://src/shaders/balatro_bg.gdshader" id="2_hejxs"]
|
[ext_resource type="Shader" path="res://src/shaders/balatro_bg.gdshader" id="2_hejxs"]
|
||||||
|
[ext_resource type="FontVariation" uid="uid://cscd67o6vdxph" path="res://assets/fonts/Cherry_Bomb_One/CherryBombOne_italic.tres" id="4_c3wh6"]
|
||||||
|
|
||||||
[sub_resource type="ShaderMaterial" id="ShaderMaterial_7xgym"]
|
[sub_resource type="ShaderMaterial" id="ShaderMaterial_7xgym"]
|
||||||
shader = ExtResource("2_hejxs")
|
shader = ExtResource("2_hejxs")
|
||||||
@ -56,8 +57,9 @@ theme_override_colors/font_color = Color(0, 0, 0, 1)
|
|||||||
theme_override_font_sizes/font_size = 64
|
theme_override_font_sizes/font_size = 64
|
||||||
text = "Network Rock-Paper-Scissors"
|
text = "Network Rock-Paper-Scissors"
|
||||||
|
|
||||||
[node name="Menu" type="Control" parent="."]
|
[node name="Menu" type="VBoxContainer" parent="."]
|
||||||
layout_mode = 1
|
layout_mode = 1
|
||||||
|
anchors_preset = -1
|
||||||
anchor_left = 0.5
|
anchor_left = 0.5
|
||||||
anchor_top = 0.6
|
anchor_top = 0.6
|
||||||
anchor_right = 0.5
|
anchor_right = 0.5
|
||||||
@ -65,20 +67,16 @@ anchor_bottom = 0.6
|
|||||||
grow_horizontal = 2
|
grow_horizontal = 2
|
||||||
grow_vertical = 2
|
grow_vertical = 2
|
||||||
|
|
||||||
|
[node name="NameInput" type="LineEdit" parent="Menu"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
layout_mode = 2
|
||||||
|
theme_override_fonts/font = ExtResource("4_c3wh6")
|
||||||
|
placeholder_text = "Enter a name..."
|
||||||
|
alignment = 1
|
||||||
|
|
||||||
[node name="MainMenu" type="VBoxContainer" parent="Menu"]
|
[node name="MainMenu" type="VBoxContainer" parent="Menu"]
|
||||||
unique_name_in_owner = true
|
unique_name_in_owner = true
|
||||||
layout_mode = 1
|
layout_mode = 2
|
||||||
anchors_preset = 8
|
|
||||||
anchor_left = 0.5
|
|
||||||
anchor_top = 0.5
|
|
||||||
anchor_right = 0.5
|
|
||||||
anchor_bottom = 0.5
|
|
||||||
offset_left = -75.0
|
|
||||||
offset_top = -88.0
|
|
||||||
offset_right = 75.0
|
|
||||||
offset_bottom = 88.0
|
|
||||||
grow_horizontal = 2
|
|
||||||
grow_vertical = 2
|
|
||||||
size_flags_horizontal = 4
|
size_flags_horizontal = 4
|
||||||
|
|
||||||
[node name="Host" type="Button" parent="Menu/MainMenu"]
|
[node name="Host" type="Button" parent="Menu/MainMenu"]
|
||||||
@ -96,18 +94,7 @@ text = "Quit"
|
|||||||
[node name="HostMenu" type="VBoxContainer" parent="Menu"]
|
[node name="HostMenu" type="VBoxContainer" parent="Menu"]
|
||||||
unique_name_in_owner = true
|
unique_name_in_owner = true
|
||||||
visible = false
|
visible = false
|
||||||
layout_mode = 1
|
layout_mode = 2
|
||||||
anchors_preset = 8
|
|
||||||
anchor_left = 0.5
|
|
||||||
anchor_top = 0.5
|
|
||||||
anchor_right = 0.5
|
|
||||||
anchor_bottom = 0.5
|
|
||||||
offset_left = -77.5
|
|
||||||
offset_top = -58.0
|
|
||||||
offset_right = 77.5
|
|
||||||
offset_bottom = 58.0
|
|
||||||
grow_horizontal = 2
|
|
||||||
grow_vertical = 2
|
|
||||||
size_flags_horizontal = 4
|
size_flags_horizontal = 4
|
||||||
|
|
||||||
[node name="HBoxContainer" type="HBoxContainer" parent="Menu/HostMenu"]
|
[node name="HBoxContainer" type="HBoxContainer" parent="Menu/HostMenu"]
|
||||||
@ -139,23 +126,13 @@ text = "Cancel
|
|||||||
[node name="JoinMenu" type="VBoxContainer" parent="Menu"]
|
[node name="JoinMenu" type="VBoxContainer" parent="Menu"]
|
||||||
unique_name_in_owner = true
|
unique_name_in_owner = true
|
||||||
visible = false
|
visible = false
|
||||||
layout_mode = 1
|
layout_mode = 2
|
||||||
anchors_preset = 8
|
|
||||||
anchor_left = 0.5
|
|
||||||
anchor_top = 0.5
|
|
||||||
anchor_right = 0.5
|
|
||||||
anchor_bottom = 0.5
|
|
||||||
offset_left = -62.0
|
|
||||||
offset_top = -58.0
|
|
||||||
offset_right = 62.0
|
|
||||||
offset_bottom = 58.0
|
|
||||||
grow_horizontal = 2
|
|
||||||
grow_vertical = 2
|
|
||||||
size_flags_horizontal = 4
|
size_flags_horizontal = 4
|
||||||
|
|
||||||
[node name="HostnameInput" type="LineEdit" parent="Menu/JoinMenu"]
|
[node name="HostnameInput" type="LineEdit" parent="Menu/JoinMenu"]
|
||||||
unique_name_in_owner = true
|
unique_name_in_owner = true
|
||||||
layout_mode = 2
|
layout_mode = 2
|
||||||
|
theme_override_fonts/font = ExtResource("4_c3wh6")
|
||||||
text = "localhost"
|
text = "localhost"
|
||||||
placeholder_text = "hostname"
|
placeholder_text = "hostname"
|
||||||
alignment = 2
|
alignment = 2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user