Godsim/scripts/world.gd

96 lines
2.8 KiB
GDScript3
Raw Normal View History

extends Node
var world_seed = 0;
var world_info = {
"seed": 0,
"name": "world"
}
func _ready():
#new_world()
pass
func new_world():
world_info.seed = round(Time.get_unix_time_from_system())
print("World seed is " + str(world_info.seed))
print("World name is " + str(world_info.name))
func save_world():
print("Saving world")
var save_game = FileAccess.open("user://%s.save" % world_info.name, FileAccess.WRITE)
# Saving world info
save_game.store_line(JSON.stringify(world_info))
var save_nodes = get_tree().get_nodes_in_group("Persistent")
for node in save_nodes:
# Check the node is an instanced scene so it can be instanced again during load.
if node.scene_file_path.is_empty():
print("persistent node '%s' is not an instanced scene, skipped" % node.name)
continue
# Check the node has a save function.
if !node.has_method("save"):
print("persistent node '%s' is missing a save() function, skipped" % node.name)
continue
# Call the node's save function.
var node_data = node.call("save")
#print(node_data)
if (node_data == {}): continue
# JSON provides a static method to serialized JSON string.
var json_string = JSON.stringify(node_data)
# Store the save dictionary as a new line in the save file.
save_game.store_line(json_string)
func load_world(save_name):
if not FileAccess.file_exists("user://%s.save" % save_name):
2024-06-03 02:53:59 +03:00
return
var save_nodes = get_tree().get_nodes_in_group("Persistent")
for i in save_nodes:
i.queue_free()
var save_game = FileAccess.open("user://%s.save" % save_name, FileAccess.READ)
var json_string_world = save_game.get_line()
var json_world = JSON.new()
var parse_result_world = json_world.parse(json_string_world)
if not parse_result_world == OK:
print("JSON Parse Error: ", json_world.get_error_message(), " in ", json_string_world, " at line ", json_world.get_error_line())
return
world_info = json_world.get_data()
2024-06-03 02:53:59 +03:00
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()
var json = JSON.new()
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
2024-06-03 02:53:59 +03:00
var node_data = json.get_data()
var new_object
match node_data["node"]:
"tile":
2024-05-31 22:46:56 +03:00
new_object = preload("res://scenes/models/tile.tscn").instantiate()
2024-06-03 02:53:59 +03:00
earth.add_child(new_object)
new_object.position = Vector3(node_data["pos_x"], node_data["pos_y"], node_data["pos_z"])
2024-05-31 22:46:56 +03:00
new_object.add_to_group("Persistent")
2024-06-03 02:53:59 +03:00
for i in node_data.keys():
if i == "node" or "pos" in i:
continue
new_object.set(i, node_data[i])
2024-05-31 22:46:56 +03:00
new_object.update_tile()
2024-06-03 02:53:59 +03:00
await get_tree().physics_frame
earth.mark_empty_tiles()