62 lines
1.6 KiB
GDScript
62 lines
1.6 KiB
GDScript
extends Node
|
|
|
|
class_name GameInstance
|
|
|
|
signal state_received()
|
|
|
|
onready var ui = $CanvasLayer/ui
|
|
onready var world = $world
|
|
onready var systems = $systems
|
|
onready var netgame = $"/root/Multiplayer"
|
|
|
|
func _ready() -> void:
|
|
ui.connect("command", world, "process_command")
|
|
if netgame.hosting:
|
|
world.load_map(netgame.get_current_map())
|
|
rpc("spawn_player", 1)
|
|
else:
|
|
world.load_map(GameWorld.Map.EMPTY)
|
|
rpc_id(1, "send_map")
|
|
yield(self, "state_received")
|
|
rpc_id(1, "ready_to_spawn")
|
|
|
|
master func send_map() -> void:
|
|
var id = get_tree().get_rpc_sender_id()
|
|
var map_data = world.map.serialize()
|
|
var systems_data = systems.serialize()
|
|
print("sending map data to ", id)
|
|
rpc_id(id, "receive_data", {
|
|
"map": map_data,
|
|
"systems": systems_data
|
|
})
|
|
|
|
puppet func receive_data(data: Dictionary) -> void:
|
|
print(data.systems)
|
|
world.map.deserialize(data["map"])
|
|
systems.deserialize(data["systems"])
|
|
print("Received map data from master")
|
|
emit_signal("state_received")
|
|
|
|
master func ready_to_spawn() -> void:
|
|
var id = get_tree().get_rpc_sender_id()
|
|
print(id, " is ready to spawn players")
|
|
|
|
# Tell him everyone to spawn
|
|
var players = $world/players.get_children()
|
|
for player in players:
|
|
rpc_id(id, "spawn_player", player.get_network_master())
|
|
|
|
# Then spawn him as well
|
|
rpc("spawn_player", id)
|
|
|
|
remotesync func spawn_player(id):
|
|
print("must spawn ", id)
|
|
world.spawn_player(id, multiplayer.get_network_unique_id() == id)
|
|
|
|
func process_command(cmd: UICommand):
|
|
match cmd.cmd_type:
|
|
UICommand.CommandType.SetShipSpeed:
|
|
world.map.ship_speed = cmd.cmd_args[0]
|
|
UICommand.CommandType.SetShipDirection:
|
|
world.map.ship_direction = cmd.cmd_args[0]
|
|
|