Godsim/scripts/world.gd

109 lines
3.5 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):
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.
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()
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()
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 = load("res://scenes/models/tile.tscn").instantiate()
#print(str(get_tree().root.get_node("world/Earth")))
get_tree().root.get_node("world/Earth").add_child(new_object)
new_object.position = Vector3(node_data["pos_x"], node_data["pos_y"], node_data["pos_z"])
# 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.call("update_tile")
get_tree().root.get_node("world/Earth").call("mark_free_tiles")