Multiple players now works

This commit is contained in:
Jan 2020-06-15 15:24:26 +02:00
parent 8204bf91ba
commit afbaac7647
4 changed files with 26 additions and 12 deletions

18
Game.gd
View file

@ -16,9 +16,6 @@ func _ready():
debug_connection_status()
func debug_connection_status():
#if (get_tree().network_peer.get_connection_status() == NetworkedMultiplayerPeer.CONNECTION_CONNECTED):
# print("We have connected succesfully")
if (get_tree().network_peer.get_connection_status() == NetworkedMultiplayerPeer.CONNECTION_CONNECTING):
print("We are trying to connect")
@ -38,7 +35,7 @@ func initialize_server():
peer.create_server(SERVER_PORT, MAX_PLAYERS)
get_tree().connect("network_peer_connected", self, "on_peer_connected")
get_tree().connect("network_peer_disconnected", self, "on_peer_disconnected")
add_player(get_tree().get_network_unique_id())
add_player(1, false)
return peer
func initialize_client():
@ -57,7 +54,7 @@ func get_player_names():
return player_names
remote func check_players(player_names):
sync func check_players(player_names):
for player_name in player_names:
if not $Players.has_node(player_name):
var player = player_scene.instance()
@ -66,7 +63,10 @@ remote func check_players(player_names):
$Players.add_child(player)
player.translation += Vector3(0.0, 3.0, 0.0)
func add_player(id):
if player_name == str(get_tree().get_network_unique_id()):
player.camera.current = true
func add_player(id, check=true):
var player = player_scene.instance()
$Players.add_child(player)
@ -75,12 +75,14 @@ func add_player(id):
player.translation += Vector3(0.0, 3.0, 0.0)
var player_names = get_player_names()
if check:
rpc("check_players", player_names)
remote func remove_player(id):
sync func remove_player(id):
for player in $Players.get_children():
if player.name == str(id):
remove_child(player)
$Players.remove_child(player)
func on_peer_connected(id):
print("Peer connected with id ", id)

View file

@ -6,6 +6,7 @@
[node name="Game" type="Spatial"]
script = ExtResource( 2 )
is_server = false
SERVER_IP = "127.0.0.1"
[node name="Level" parent="." instance=ExtResource( 3 )]

View file

@ -19,11 +19,16 @@ var velocity = Vector3.ZERO
var walkDirection = Vector2.ZERO
var walkDirInt = Vector2.ZERO
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
func gravity():
#print("--------")
#print(get_tree().get_network_unique_id())
#print(name, " ", self.velocity.y)
#print(name, " ", self.translation.y)
if not is_on_floor():
self.velocity.y -= GRAVITY
@ -67,6 +72,9 @@ func motion(delta):
self.move_and_slide(velocity * delta, Vector3.UP, true)
func _physics_process(delta):
if str(get_tree().get_network_unique_id()) != name:
return
gravity()
rpc("walk", walkDirection)
@ -74,8 +82,11 @@ func _physics_process(delta):
motion(delta)
rset("translation", translation)
func _input(event):
#print(event)
if str(get_tree().get_network_unique_id()) != name:
return
# Moouselook
if event is InputEventMouseMotion:
@ -112,6 +123,7 @@ func _input(event):
# Called when the node enters the scene tree for the first time.
func _ready():
rset_config("translation", MultiplayerAPI.RPC_MODE_SYNC)
# only show the debug label on local machine
if name != String(get_tree().get_network_unique_id()):

View file

@ -18,7 +18,6 @@ shape = SubResource( 1 )
[node name="MeshInstance" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, -1.62921e-07, 1, 0, -1, -1.62921e-07, 0, 0, 0 )
visible = false
mesh = SubResource( 2 )
material/0 = null