aboutsummaryrefslogtreecommitdiff
path: root/scripts/load_map.gd
blob: 1e4405c65a300384cf76d07a504fe6ac895b56ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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_node("world")
	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")
	
	_path_holder.refresh_path(false)
	

func _process(_delta):
	if Input.is_action_just_released("save"):
		map_save()
	if Input.is_action_just_released("load"):
		map_load()