From 0626bf59edc0b0ab7f4c096bcc9e6fe5cb12f3a1 Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 3 Jun 2024 02:53:59 +0300 Subject: [PATCH] optimization --- scripts/Earth.gd | 2 -- scripts/Tile.gd | 2 +- scripts/world.gd | 31 ++++++++----------------------- 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/scripts/Earth.gd b/scripts/Earth.gd index fddb777..30e204b 100644 --- a/scripts/Earth.gd +++ b/scripts/Earth.gd @@ -15,8 +15,6 @@ func _ready(): add_child(start_tile); pass -func _physics_process(delta): - mark_empty_tiles() func mark_empty_tiles(): var tiles = get_children() var available = [] diff --git a/scripts/Tile.gd b/scripts/Tile.gd index 12b6cef..d3271ed 100644 --- a/scripts/Tile.gd +++ b/scripts/Tile.gd @@ -14,6 +14,7 @@ var tile_materials = [ # must be in the same order as an enum above! @export var tile_type: TileType = TileType.Empty @onready var visible_mesh = $"CollisionMesh/VisibleMesh" @onready var plus_sprite = $"CollisionMesh/plus_sprite" + func update_tile(): visible_mesh.material_override = tile_materials[tile_type] plus_sprite.visible = tile_type == TileType.Empty @@ -42,7 +43,6 @@ func get_empty_dirs_around(): if !result_east: empty_tiles_around.push_back("east") return empty_tiles_around - func save(): if tile_type == TileType.Empty: return {} diff --git a/scripts/world.gd b/scripts/world.gd index 0b847e3..a800255 100644 --- a/scripts/world.gd +++ b/scripts/world.gd @@ -47,18 +47,12 @@ func save_world(): func load_world(save_name): if not FileAccess.file_exists("user://%s.save" % save_name): - return # Error! We don't have a save to load. - - # We need to revert the game state so we're not cloning objects - # during loading. This will vary wildly depending on the needs of a - # project, so take care with this step. - # For our example, we will accomplish this by deleting saveable objects. + return + var save_nodes = get_tree().get_nodes_in_group("Persistent") for i in save_nodes: i.queue_free() - # Load the file line by line and process that dictionary to restore - # the object it represents. var save_game = FileAccess.open("user://%s.save" % save_name, FileAccess.READ) var json_string_world = save_game.get_line() @@ -71,40 +65,31 @@ func load_world(save_name): return world_info = json_world.get_data() + var earth = get_tree().root.get_node("world/Earth") while save_game.get_position() < save_game.get_length(): var json_string = save_game.get_line() - # Creates the helper class to interact with JSON var json = JSON.new() - # Check if there is any error while parsing the JSON string, skip in case of failure var parse_result = json.parse(json_string) if not parse_result == OK: print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) continue - - # Get the data from the JSON object + var node_data = json.get_data() - # Firstly, we need to create the object and add it to the tree and set its position. - #var new_object = load(node_data["filename"]).instantiate() var new_object match node_data["node"]: "tile": new_object = preload("res://scenes/models/tile.tscn").instantiate() - get_tree().root.get_node("world/Earth").add_child(new_object) + earth.add_child(new_object) new_object.position = Vector3(node_data["pos_x"], node_data["pos_y"], node_data["pos_z"]) new_object.add_to_group("Persistent") - # Now we set the remaining variables. + for i in node_data.keys(): if i == "node" or "pos" in i: continue new_object.set(i, node_data[i]) new_object.update_tile() - - #await get_tree().create_timer(1).timeout - - #get_tree().root.get_node("world/Earth").call("mark_free_tiles") - - # s - + await get_tree().physics_frame + earth.mark_empty_tiles()