Preliminary multiplayer capability (origin stuff disabled for now)
This commit is contained in:
parent
73baafbd6c
commit
f0c4263675
18 changed files with 704 additions and 99 deletions
117
demo/menu.gd
Normal file
117
demo/menu.gd
Normal file
|
@ -0,0 +1,117 @@
|
|||
extends Node
|
||||
|
||||
const PORT := 28002
|
||||
const MAX_CLIENTS := 2
|
||||
|
||||
@export
|
||||
var multiplayer_scene: PackedScene
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
PlayerData.player1_index = 1
|
||||
($Main/PlayButton as Button).grab_focus.call_deferred()
|
||||
|
||||
|
||||
func _on_play_button_pressed() -> void:
|
||||
multiplayer.multiplayer_peer = OfflineMultiplayerPeer.new()
|
||||
get_tree().change_scene_to_file("res://demo/demo.tscn")
|
||||
|
||||
|
||||
func _on_host_button_pressed() -> void:
|
||||
($Main as Control).hide()
|
||||
($Host as Control).show()
|
||||
var peer := ENetMultiplayerPeer.new()
|
||||
multiplayer.peer_connected.connect(_on_host_joined)
|
||||
multiplayer.peer_disconnected.connect(_on_host_left)
|
||||
peer.create_server(PORT, MAX_CLIENTS)
|
||||
multiplayer.multiplayer_peer = peer
|
||||
|
||||
|
||||
func _on_join_button_pressed() -> void:
|
||||
($Main as Control).hide()
|
||||
($Join as Control).show()
|
||||
|
||||
|
||||
func _on_exit_button_pressed() -> void:
|
||||
get_tree().quit()
|
||||
|
||||
|
||||
func _on_back_from_host_button_pressed() -> void:
|
||||
($Host as Control).hide()
|
||||
($Main as Control).show()
|
||||
PlayerData.player2_index = 0
|
||||
multiplayer.peer_connected.disconnect(_on_host_joined)
|
||||
multiplayer.peer_disconnected.disconnect(_on_host_left)
|
||||
_on_host_left(0)
|
||||
multiplayer.multiplayer_peer = OfflineMultiplayerPeer.new()
|
||||
|
||||
|
||||
func _on_back_from_join_button_pressed() -> void:
|
||||
($Join as Control).hide()
|
||||
($Main as Control).show()
|
||||
($Join/PlayerJoinedLabel as Label).text = ""
|
||||
PlayerData.player2_index = 0
|
||||
multiplayer.connected_to_server.disconnect(_on_client_joined)
|
||||
multiplayer.server_disconnected.disconnect(_on_client_left)
|
||||
multiplayer.connection_failed.disconnect(_on_client_failed_to_connect)
|
||||
multiplayer.multiplayer_peer = OfflineMultiplayerPeer.new()
|
||||
|
||||
|
||||
func _on_join_room_button_pressed() -> void:
|
||||
var peer := ENetMultiplayerPeer.new()
|
||||
var address := ($Join/AddressBar as LineEdit).text
|
||||
if (address == ""):
|
||||
address = "127.0.0.1"
|
||||
multiplayer.connected_to_server.connect(_on_client_joined)
|
||||
multiplayer.server_disconnected.connect(_on_client_left)
|
||||
multiplayer.connection_failed.connect(_on_client_failed_to_connect)
|
||||
peer.create_client(address, PORT)
|
||||
multiplayer.multiplayer_peer = peer
|
||||
|
||||
|
||||
func _on_host_joined(id: int) -> void:
|
||||
PlayerData.player2_index = id
|
||||
($Host/PlayerHostedLabel as Label).text = "Player has joined."
|
||||
($Host/PlayMultiplayerButton as Button).disabled = false
|
||||
|
||||
|
||||
func _on_host_left(_id: int) -> void:
|
||||
($Host/PlayerHostedLabel as Label).text = "Waiting for player to join..."
|
||||
($Host/PlayMultiplayerButton as Button).disabled = true
|
||||
|
||||
|
||||
func _on_client_joined() -> void:
|
||||
PlayerData.player2_index = multiplayer.get_unique_id()
|
||||
($Join/PlayerJoinedLabel as Label).text = "Player has joined."
|
||||
|
||||
|
||||
func _on_client_left() -> void:
|
||||
($Join/PlayerJoinedLabel as Label).text = "Server has closed."
|
||||
|
||||
|
||||
func _on_client_failed_to_connect() -> void:
|
||||
($Join/PlayerJoinedLabel as Label).text = "Connection failed."
|
||||
|
||||
|
||||
func _on_play_multiplayer_button_pressed() -> void:
|
||||
if multiplayer.is_server():
|
||||
_hide_menu.rpc()
|
||||
_play_multiplayer.call_deferred()
|
||||
|
||||
|
||||
func _play_multiplayer() -> void:
|
||||
var level := $Level
|
||||
for c in level.get_children():
|
||||
level.remove_child(c)
|
||||
c.queue_free()
|
||||
var mp := multiplayer_scene.instantiate()
|
||||
level.add_child(mp)
|
||||
|
||||
|
||||
@rpc("authority", "call_local", "reliable")
|
||||
func _hide_menu() -> void:
|
||||
($Join as Control).hide()
|
||||
($Main as Control).hide()
|
||||
($Host as Control).hide()
|
||||
($Background as Control).hide()
|
||||
($Title as Control).hide()
|
Loading…
Add table
Add a link
Reference in a new issue