forked from unfa/liblast
Multiple players now works
This commit is contained in:
parent
8204bf91ba
commit
afbaac7647
4 changed files with 26 additions and 12 deletions
20
Game.gd
20
Game.gd
|
@ -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()
|
||||
|
@ -65,8 +62,11 @@ remote func check_players(player_names):
|
|||
player.name = player_name
|
||||
$Players.add_child(player)
|
||||
player.translation += Vector3(0.0, 3.0, 0.0)
|
||||
|
||||
if player_name == str(get_tree().get_network_unique_id()):
|
||||
player.camera.current = true
|
||||
|
||||
func add_player(id):
|
||||
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()
|
||||
rpc("check_players", 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)
|
||||
|
|
|
@ -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 )]
|
||||
|
||||
|
|
16
Player.gd
16
Player.gd
|
@ -19,14 +19,19 @@ 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
|
||||
|
||||
|
||||
remote func walk(direction: Vector2):
|
||||
|
||||
var walkDirectionNormalized = direction.normalized()
|
||||
|
@ -67,15 +72,21 @@ 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)
|
||||
walk(walkDirection)
|
||||
|
||||
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()):
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue