2020-05-01 01:16:46 +02:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
$Tiles.clear()
|
|
|
|
$"/root/NetworkManager".connect("world_data_recieved", self, "_on_world_update")
|
|
|
|
$"/root/NetworkManager".request_world_map()
|
|
|
|
|
|
|
|
func _on_world_update():
|
|
|
|
var data = $"/root/NetworkManager".world_data.split('\n')
|
|
|
|
for tileUpdate in data:
|
|
|
|
if len(tileUpdate) > 3:
|
2020-05-01 08:27:47 +02:00
|
|
|
if "delete," in tileUpdate:
|
|
|
|
var delete_data = tileUpdate.substr(len("delete,")).split(':')
|
|
|
|
delete_entity(delete_data[1],delete_data[0])
|
|
|
|
elif ',' in tileUpdate:
|
2020-05-01 01:16:46 +02:00
|
|
|
var tile_data = tileUpdate.split(',')
|
|
|
|
if ':' in tile_data[2]:
|
|
|
|
var pos : Vector2 = $Tiles.map_to_world(Vector2(int(tile_data[0]), int(tile_data[1])))
|
2020-05-04 08:45:04 +02:00
|
|
|
var entity_data = tile_data[2].split(':')
|
|
|
|
update_entity(entity_data[1], pos, entity_data[0])
|
2020-05-01 01:16:46 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
$Tiles.set_cell(int(tile_data[0]), int(tile_data[1]), int(tile_data[2]))
|
2020-05-04 08:45:04 +02:00
|
|
|
if get_node_or_null("Loading") != null:
|
2020-05-01 01:16:46 +02:00
|
|
|
$Loading.queue_free()
|
|
|
|
|
|
|
|
func display_error(error):
|
|
|
|
print("Error " + error)
|
|
|
|
|
|
|
|
func update_entity(entity_id : String, pos : Vector2, type : String):
|
2020-05-04 08:45:04 +02:00
|
|
|
var entity : Node2D = get_node_or_null( str(type + "-" + entity_id))
|
2020-05-01 01:16:46 +02:00
|
|
|
if not entity:
|
|
|
|
var entity_location = "res://nodes/entities/" + type + ".tscn"
|
|
|
|
if File.new().file_exists(entity_location):
|
|
|
|
var gobj = load(entity_location)
|
|
|
|
entity = gobj.instance()
|
|
|
|
add_child(entity, true)
|
|
|
|
entity.set_name(str(type + "-" + entity_id))
|
2020-05-04 08:45:04 +02:00
|
|
|
if type == "player":
|
|
|
|
if entity_id == $"/root/NetworkManager".username:
|
|
|
|
entity.set_main()
|
2020-05-01 01:16:46 +02:00
|
|
|
else:
|
|
|
|
display_error("Trying to load entity of type: " + type + ", but failed.")
|
|
|
|
if entity:
|
|
|
|
entity.position = pos
|
2020-05-02 22:36:51 +02:00
|
|
|
if entity.has_method("set_username"):
|
|
|
|
entity.set_username(entity_id)
|
2020-05-01 08:27:47 +02:00
|
|
|
|
|
|
|
func delete_entity(entity_id : String, type : String):
|
|
|
|
var entity : Node2D = get_node_or_null( str(type + "-" + entity_id))
|
|
|
|
if entity:
|
|
|
|
entity.queue_free()
|
|
|
|
|