add storage for player info
This commit is contained in:
parent
dd429beb5b
commit
bab7bea373
@ -1,13 +1,17 @@
|
||||
extends Node
|
||||
|
||||
signal main_loaded
|
||||
signal player_connected(peer_id: int, player_info: Dictionary)
|
||||
signal network_error(message: String)
|
||||
|
||||
const DEFAULT_PORT: int = 10567
|
||||
const MAX_PEERS: int = 12
|
||||
const MAX_PEERS: int = 20
|
||||
|
||||
var _peer: ENetMultiplayerPeer
|
||||
|
||||
var _players := {}
|
||||
var _local_player_info := {"name": "ass"}
|
||||
|
||||
var _title_scene := preload("res://scenes/title.tscn")
|
||||
var _main_scene := preload("res://scenes/main.tscn")
|
||||
var _player_scene := preload("res://scenes/player.tscn")
|
||||
@ -16,10 +20,15 @@ var _player_scene := preload("res://scenes/player.tscn")
|
||||
func _ready() -> void:
|
||||
multiplayer.peer_connected.connect(_on_multiplayer_peer_connected)
|
||||
multiplayer.peer_disconnected.connect(_on_multiplayer_peer_disconnected)
|
||||
multiplayer.connected_to_server.connect(_on_multiplayer_connected_to_server)
|
||||
multiplayer.connection_failed.connect(_on_multiplayer_connection_failed)
|
||||
multiplayer.server_disconnected.connect(_on_multiplayer_server_disconnected)
|
||||
|
||||
|
||||
func set_local_player_info(username: String) -> void:
|
||||
_local_player_info["name"] = username
|
||||
|
||||
|
||||
func host_game() -> void:
|
||||
_peer = ENetMultiplayerPeer.new()
|
||||
var error := _peer.create_server(DEFAULT_PORT, MAX_PEERS)
|
||||
@ -34,6 +43,9 @@ func host_game() -> void:
|
||||
|
||||
multiplayer.set_multiplayer_peer(_peer)
|
||||
|
||||
_players[1] = _local_player_info
|
||||
player_connected.emit(1, _local_player_info)
|
||||
|
||||
_load_main()
|
||||
_add_player(multiplayer.get_unique_id())
|
||||
|
||||
@ -89,16 +101,31 @@ func _remove_player(peer_id: int) -> void:
|
||||
player.queue_free()
|
||||
|
||||
|
||||
@rpc("any_peer", "reliable")
|
||||
func _register_player(player_info: Dictionary) -> void:
|
||||
var player_id := multiplayer.get_remote_sender_id()
|
||||
_players[player_id] = player_info
|
||||
player_connected.emit(player_id, player_info)
|
||||
|
||||
|
||||
func _on_multiplayer_peer_connected(peer_id: int) -> void:
|
||||
print("Peer connected ", peer_id)
|
||||
_add_player(peer_id)
|
||||
_register_player.rpc_id(peer_id, _local_player_info)
|
||||
|
||||
|
||||
func _on_multiplayer_peer_disconnected(peer_id: int) -> void:
|
||||
print("Peer disconnected ", peer_id)
|
||||
_players.erase(peer_id)
|
||||
_remove_player(peer_id)
|
||||
|
||||
|
||||
func _on_multiplayer_connected_to_server() -> void:
|
||||
var peer_id := multiplayer.get_unique_id()
|
||||
_players[peer_id] = _local_player_info
|
||||
player_connected.emit(peer_id, _local_player_info)
|
||||
|
||||
|
||||
func _on_multiplayer_connection_failed() -> void:
|
||||
printerr("Connection failed")
|
||||
multiplayer.set_multiplayer_peer(null)
|
||||
@ -107,5 +134,6 @@ func _on_multiplayer_connection_failed() -> void:
|
||||
|
||||
func _on_multiplayer_server_disconnected() -> void:
|
||||
printerr("Server disconnected")
|
||||
_players.clear()
|
||||
_switch_scene($/root/Main, _title_scene)
|
||||
network_error.emit("Server disconnected")
|
||||
|
||||
@ -20,6 +20,7 @@ func _display_error(message: String) -> void:
|
||||
|
||||
|
||||
func _on_host_button_pressed() -> void:
|
||||
Networker.set_local_player_info(_name_edit.text)
|
||||
Networker.host_game()
|
||||
|
||||
|
||||
@ -32,6 +33,7 @@ func _on_join_button_pressed() -> void:
|
||||
_join_button.disabled = true
|
||||
_host_button.disabled = true
|
||||
|
||||
Networker.set_local_player_info(_name_edit.text)
|
||||
Networker.join_game(_ip_edit.text)
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user