diff options
author | jacopograndi <jacopo.grandi@outlook.it> | 2021-12-24 21:11:39 +0100 |
---|---|---|
committer | jacopograndi <jacopo.grandi@outlook.it> | 2021-12-24 21:11:39 +0100 |
commit | 7cb4290601010459b09376396c7833ec391c25bf (patch) | |
tree | be6243e49ad764f90418e6d41641f99209da2b29 /scripts/load_map.gd | |
parent | e9598b88841374301646a7115253ab38ac904e4c (diff) |
working on it
Diffstat (limited to 'scripts/load_map.gd')
-rw-r--r-- | scripts/load_map.gd | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/scripts/load_map.gd b/scripts/load_map.gd new file mode 100644 index 0000000..8130997 --- /dev/null +++ b/scripts/load_map.gd @@ -0,0 +1,113 @@ +extends Node + +var _world : VoxelMesh = null +var _path_holder : Node = null +var _attach_point_holder : Node = null + +var _load_scenes : Node = null + +func _ready(): + map_load() + +func fetch (): + var root = get_tree().root.get_child(0) + if _world == null: + _world = root.get_node("world") + if _path_holder == null: + _path_holder = root.get_node("path") + if _attach_point_holder == null: + _attach_point_holder = root.get_node("attach") + if _load_scenes == null: + _load_scenes = root.get_node("saveload").get_node("load_scenes") + +func get_map_state (): + fetch() + var state = {} + + state["voxels"] = [] + for pos in _world.get_voxels(): + var vox = { "pos": [pos.x, pos.y, pos.z], "id":_world.get_voxel_id(pos) } + state["voxels"] += [vox] + + state["path"] = [] + for node in _path_holder.get_children(): + var pos = node.transform.origin; + var rot = node.transform.basis.get_rotation_quat(); + var pobj = { + "pos": [pos.x, pos.y, pos.z], + "rot": [rot.x, rot.y, rot.z, rot.w], + "id": node.name + } + state["path"] += [pobj] + + state["attach"] = [] + for node in _attach_point_holder.get_children(): + var pos = node.transform.origin; + var rot = node.transform.basis.get_rotation_quat(); + var pobj = { + "pos": [pos.x, pos.y, pos.z], + "rot": [rot.x, rot.y, rot.z, rot.w], + "id": node.name + } + state["attach"] += [pobj] + + return state + +func set_map_state (state): + fetch() + _world.erase_voxels() + for vox in state["voxels"]: + var vecpos = Vector3(vox.pos[0], vox.pos[1], vox.pos[2]); + _world.set_voxel(vecpos, vox.id) + _world.update_mesh() + + for n in _path_holder.get_children(): n.queue_free() + + for pobj in state["path"]: + var blue = _load_scenes.path + if "start" in pobj.id: + blue = _load_scenes.path_start + if "end" in pobj.id: + blue = _load_scenes.path_end + var instance = blue.instance() + _path_holder.add_child(instance) + var vecpos = Vector3(pobj.pos[0], pobj.pos[1], pobj.pos[2]); + instance.transform.origin = vecpos; + var quat = Quat(pobj.rot[0], pobj.rot[1], pobj.rot[2], pobj.rot[3]); + instance.transform.basis = Basis(quat); + + for n in _attach_point_holder.get_children(): n.queue_free() + + for pobj in state["attach"]: + var blue = _load_scenes.attach_point + var instance = blue.instance() + _attach_point_holder.add_child(instance) + var vecpos = Vector3(pobj.pos[0], pobj.pos[1], pobj.pos[2]); + instance.transform.origin = vecpos; + var quat = Quat(pobj.rot[0], pobj.rot[1], pobj.rot[2], pobj.rot[3]); + instance.transform.basis = Basis(quat); + + +func map_save(): + var save_game = File.new() + save_game.open("user://map0.json", File.WRITE) + save_game.store_string(to_json(get_map_state())) + save_game.close() + print("saved") + +func map_load(): + var save_game = File.new() + save_game.open("user://map0.json", File.READ) + var raw = save_game.get_as_text() + save_game.close() + + var state = parse_json(raw) + set_map_state(state) + print("loaded") + + +func _process(_delta): + if Input.is_action_just_released("save"): + map_save() + if Input.is_action_just_released("load"): + map_load() |