2020-07-06 22:00:39 +00:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
class_name GameInstance
|
|
|
|
|
2020-07-13 20:18:02 +00:00
|
|
|
signal state_received()
|
|
|
|
|
2020-07-10 00:09:54 +00:00
|
|
|
onready var ui = $CanvasLayer/ui
|
2020-07-10 08:45:57 +00:00
|
|
|
onready var world = $world
|
2020-07-08 22:12:14 +00:00
|
|
|
onready var systems = $systems
|
2020-07-13 19:01:36 +00:00
|
|
|
onready var netgame = $"/root/Multiplayer"
|
2020-07-23 12:21:27 +00:00
|
|
|
onready var physics = world.get_world_2d().direct_space_state as Physics2DDirectSpaceState
|
2020-07-06 22:00:39 +00:00
|
|
|
|
2020-07-20 09:15:39 +00:00
|
|
|
var writing = false
|
|
|
|
|
2020-07-13 19:01:36 +00:00
|
|
|
func _ready() -> void:
|
2020-07-23 08:51:39 +00:00
|
|
|
$"/root/Music/BGM".stop()
|
2020-07-14 21:28:13 +00:00
|
|
|
randomize()
|
2020-07-13 19:01:36 +00:00
|
|
|
if netgame.hosting:
|
|
|
|
world.load_map(netgame.get_current_map())
|
2020-07-14 21:28:13 +00:00
|
|
|
world.map.current_ship_position = Vector2(randf() * 1e4, randf() * 1e4)
|
2020-07-20 09:15:39 +00:00
|
|
|
world.map.current_ship_target = world.map.current_ship_position + Vector2(randf() * 1e2, randf() * 1e2)
|
2020-07-13 19:01:36 +00:00
|
|
|
rpc("spawn_player", 1)
|
|
|
|
else:
|
|
|
|
world.load_map(GameWorld.Map.EMPTY)
|
|
|
|
rpc_id(1, "send_map")
|
2020-07-13 20:18:02 +00:00
|
|
|
yield(self, "state_received")
|
2020-07-13 19:01:36 +00:00
|
|
|
rpc_id(1, "ready_to_spawn")
|
|
|
|
|
|
|
|
master func send_map() -> void:
|
|
|
|
var id = get_tree().get_rpc_sender_id()
|
2020-07-13 20:18:02 +00:00
|
|
|
var map_data = world.map.serialize()
|
|
|
|
var systems_data = systems.serialize()
|
2020-07-23 09:12:07 +00:00
|
|
|
print("sending map data to %d" % id)
|
2020-07-13 20:18:02 +00:00
|
|
|
rpc_id(id, "receive_data", {
|
|
|
|
"map": map_data,
|
|
|
|
"systems": systems_data
|
|
|
|
})
|
|
|
|
|
|
|
|
puppet func receive_data(data: Dictionary) -> void:
|
|
|
|
world.map.deserialize(data["map"])
|
|
|
|
systems.deserialize(data["systems"])
|
|
|
|
print("Received map data from master")
|
|
|
|
emit_signal("state_received")
|
2020-07-13 19:01:36 +00:00
|
|
|
|
|
|
|
master func ready_to_spawn() -> void:
|
|
|
|
var id = get_tree().get_rpc_sender_id()
|
2020-07-23 09:12:07 +00:00
|
|
|
print("%d is ready to spawn players" % id)
|
2020-07-13 19:01:36 +00:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2020-07-21 17:47:33 +00:00
|
|
|
master func broadcast_zone(message: String, origin: Vector2, radius: float) -> void:
|
|
|
|
var shape = CircleShape2D.new()
|
|
|
|
shape.radius = radius
|
|
|
|
var query = Physics2DShapeQueryParameters.new()
|
2020-07-22 15:34:04 +00:00
|
|
|
query.collide_with_areas = true
|
|
|
|
query.collide_with_bodies = false
|
2020-07-21 17:47:33 +00:00
|
|
|
query.collision_layer = 32
|
2020-07-22 15:34:04 +00:00
|
|
|
query.transform.origin = origin
|
2020-07-21 17:47:33 +00:00
|
|
|
query.set_shape(shape)
|
|
|
|
var res = physics.intersect_shape(query, 100)
|
2020-07-22 15:34:04 +00:00
|
|
|
for col in res:
|
|
|
|
rpc_id(col.collider.get_network_master(), "add_log", message)
|
2020-07-21 17:47:33 +00:00
|
|
|
|
|
|
|
master func broadcast(message: String) -> void:
|
|
|
|
rpc("add_log", message)
|
|
|
|
|
|
|
|
remotesync func add_log(message: String) -> void:
|
|
|
|
ui.logs.add_line(message)
|
|
|
|
|
|
|
|
remotesync func spawn_player(id: int) -> void:
|
2020-07-13 19:01:36 +00:00
|
|
|
world.spawn_player(id, multiplayer.get_network_unique_id() == id)
|
2020-07-06 22:00:39 +00:00
|
|
|
|
2020-07-21 17:47:33 +00:00
|
|
|
remotesync func process_command(cmd: UICommand) -> void:
|
2020-07-16 12:37:21 +00:00
|
|
|
match cmd.cmd_type:
|
|
|
|
UICommand.CommandType.SetShipTarget:
|
|
|
|
world.map.current_ship_target = cmd.cmd_args[0]
|