59 lines
1.7 KiB
GDScript
59 lines
1.7 KiB
GDScript
extends Node2D
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
$Map.load_map("res://Maps/default.xp")
|
|
# $"Player ship".position = Vector2(500., 500.)
|
|
if not multiplayer.is_server():
|
|
return
|
|
|
|
multiplayer.peer_connected.connect(add_player)
|
|
multiplayer.peer_disconnected.connect(del_player)
|
|
|
|
# Spawn already connected players
|
|
for id in multiplayer.get_peers():
|
|
add_player(id)
|
|
|
|
# Spawn the local player unless this is a dedicated server export.
|
|
if not OS.has_feature("dedicated_server"):
|
|
add_player(1)
|
|
|
|
func _exit_tree():
|
|
if not multiplayer.is_server():
|
|
return
|
|
multiplayer.peer_connected.disconnect(add_player)
|
|
multiplayer.peer_disconnected.disconnect(del_player)
|
|
|
|
func add_player(id: int):
|
|
var player_ship = preload("res://World/PlayerShip.tscn").instantiate()
|
|
# Set player id.
|
|
player_ship.player = id
|
|
player_ship.position = 64*$Map.base_positions[randi() % $Map.base_positions.size()] + Vector2i(32, 32)
|
|
player_ship.rotation_degrees = -90
|
|
player_ship.name = str(id)
|
|
$Players.add_child(player_ship, true)
|
|
|
|
|
|
func del_player(id: int):
|
|
if not $Players.has_node(str(id)):
|
|
return
|
|
$Players.get_node(str(id)).queue_free()
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta):
|
|
pass
|
|
|
|
func _on_map_boundaries_body_exited(body):
|
|
|
|
var width = MapConfig.mapwidth
|
|
var height = MapConfig.mapheight
|
|
|
|
if body.position.x < $"Map boundaries".position.x - 32*width:
|
|
body.position.x += 64 * width
|
|
elif body.position.x > $"Map boundaries".position.x + 32*width:
|
|
body.position.x -= 64 * width
|
|
if body.position.y < $"Map boundaries".position.y - 32*height:
|
|
body.position.y += 64 * height
|
|
elif body.position.y > $"Map boundaries".position.y + 32*height:
|
|
body.position.y -= 64 * height
|