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
116
117
118
119
120
121
122
123
124
125
126
127
128
|
extends Node
var mapname = "map0.json"
var mappath = "user://"
var _world : VoxelMesh = null
var _path_holder : Node = null
var _attach_point_holder : Node = null
var saveload : 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 saveload == null: saveload = root.get_node("saveload")
if _load_scenes == null:
_load_scenes = 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(mappath+mapname, 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(mappath+mapname, 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 map_delete (mapname : String):
var dir = Directory.new()
dir.remove(mappath+mapname)
func get_mapnames ():
return saveload.parse_dir(mappath, ".json")
func _process(_delta):
if Input.is_action_just_released("save"):
map_save()
if Input.is_action_just_released("load"):
#map_load()
pass
|