optimization

This commit is contained in:
leca 2024-06-03 02:53:59 +03:00
parent a7eadfcd06
commit 0626bf59ed
3 changed files with 9 additions and 26 deletions

View File

@ -15,8 +15,6 @@ func _ready():
add_child(start_tile); add_child(start_tile);
pass pass
func _physics_process(delta):
mark_empty_tiles()
func mark_empty_tiles(): func mark_empty_tiles():
var tiles = get_children() var tiles = get_children()
var available = [] var available = []

View File

@ -14,6 +14,7 @@ var tile_materials = [ # must be in the same order as an enum above!
@export var tile_type: TileType = TileType.Empty @export var tile_type: TileType = TileType.Empty
@onready var visible_mesh = $"CollisionMesh/VisibleMesh" @onready var visible_mesh = $"CollisionMesh/VisibleMesh"
@onready var plus_sprite = $"CollisionMesh/plus_sprite" @onready var plus_sprite = $"CollisionMesh/plus_sprite"
func update_tile(): func update_tile():
visible_mesh.material_override = tile_materials[tile_type] visible_mesh.material_override = tile_materials[tile_type]
plus_sprite.visible = tile_type == TileType.Empty 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") if !result_east: empty_tiles_around.push_back("east")
return empty_tiles_around return empty_tiles_around
func save(): func save():
if tile_type == TileType.Empty: return {} if tile_type == TileType.Empty: return {}

View File

@ -47,18 +47,12 @@ func save_world():
func load_world(save_name): func load_world(save_name):
if not FileAccess.file_exists("user://%s.save" % save_name): if not FileAccess.file_exists("user://%s.save" % save_name):
return # Error! We don't have a save to load. return
# 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.
var save_nodes = get_tree().get_nodes_in_group("Persistent") var save_nodes = get_tree().get_nodes_in_group("Persistent")
for i in save_nodes: for i in save_nodes:
i.queue_free() 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 save_game = FileAccess.open("user://%s.save" % save_name, FileAccess.READ)
var json_string_world = save_game.get_line() var json_string_world = save_game.get_line()
@ -71,40 +65,31 @@ func load_world(save_name):
return return
world_info = json_world.get_data() world_info = json_world.get_data()
var earth = get_tree().root.get_node("world/Earth")
while save_game.get_position() < save_game.get_length(): while save_game.get_position() < save_game.get_length():
var json_string = save_game.get_line() var json_string = save_game.get_line()
# Creates the helper class to interact with JSON
var json = JSON.new() 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) var parse_result = json.parse(json_string)
if not parse_result == OK: if not parse_result == OK:
print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
continue continue
# Get the data from the JSON object
var node_data = json.get_data() 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 var new_object
match node_data["node"]: match node_data["node"]:
"tile": "tile":
new_object = preload("res://scenes/models/tile.tscn").instantiate() 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.position = Vector3(node_data["pos_x"], node_data["pos_y"], node_data["pos_z"])
new_object.add_to_group("Persistent") new_object.add_to_group("Persistent")
# Now we set the remaining variables.
for i in node_data.keys(): for i in node_data.keys():
if i == "node" or "pos" in i: if i == "node" or "pos" in i:
continue continue
new_object.set(i, node_data[i]) new_object.set(i, node_data[i])
new_object.update_tile() new_object.update_tile()
await get_tree().physics_frame
#await get_tree().create_timer(1).timeout earth.mark_empty_tiles()
#get_tree().root.get_node("world/Earth").call("mark_free_tiles")
# s