first iteration with authorative movement
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
extends Node
|
||||
|
||||
const PORT = 1280
|
||||
const IP_ADDRESS = "127.0.0.1"
|
||||
const MAX_CLIENTS = 5
|
||||
|
||||
var in_game_scene = preload("res://scenes/in_game.tscn")
|
||||
var main_menu_scene = preload("res://scenes/main_menu.tscn")
|
||||
|
||||
func _ready() -> void:
|
||||
set_process(false)
|
||||
set_physics_process(false)
|
||||
|
||||
if OS.has_feature("dedicated_server"):
|
||||
get_tree().change_scene_to_packed.call_deferred(in_game_scene)
|
||||
|
||||
var peer = ENetMultiplayerPeer.new()
|
||||
peer.create_server(PORT, MAX_CLIENTS)
|
||||
multiplayer.multiplayer_peer = peer
|
||||
|
||||
multiplayer.peer_connected.connect(_on_connect)
|
||||
multiplayer.peer_disconnected.connect(_on_disconnect)
|
||||
else:
|
||||
get_tree().change_scene_to_packed.call_deferred(main_menu_scene)
|
||||
|
||||
func connect_to_ip(ip: String = "") -> void:
|
||||
if ip.is_empty():
|
||||
ip = IP_ADDRESS
|
||||
|
||||
print("Connecting to: %s" % ip)
|
||||
|
||||
var peer = ENetMultiplayerPeer.new()
|
||||
peer.create_client(ip, PORT)
|
||||
multiplayer.multiplayer_peer = peer
|
||||
|
||||
multiplayer.connected_to_server.connect(_on_connect_client)
|
||||
multiplayer.connection_failed.connect(_on_disconnect_client)
|
||||
multiplayer.server_disconnected.connect(_on_server_closed_client)
|
||||
|
||||
func _on_connect(id: int) -> void:
|
||||
print("Client ID #%s connected" % id)
|
||||
get_tree().get_current_scene().spawn_player(id)
|
||||
|
||||
func _on_disconnect(id: int) -> void:
|
||||
print("Client ID #%s disconnected" % id)
|
||||
pass
|
||||
|
||||
func _on_connect_client() -> void:
|
||||
print("[%s] Connected to server" % multiplayer.get_unique_id())
|
||||
pass
|
||||
|
||||
func _on_disconnect_client() -> void:
|
||||
print("[%s] Disconnected" % multiplayer.get_unique_id())
|
||||
pass
|
||||
|
||||
func _on_server_closed_client() -> void:
|
||||
print("[%s] Server closed" % multiplayer.get_unique_id())
|
||||
pass
|
||||
Reference in New Issue
Block a user