Added proper building de-serialization code
This commit is contained in:
@@ -30,15 +30,19 @@ void StreamWorld::read_buildings_json(const String &buildings_path)
|
|||||||
while (e) {
|
while (e) {
|
||||||
struct building b;
|
struct building b;
|
||||||
String key = e->get();
|
String key = e->get();
|
||||||
b.key = key;
|
String id = json[key].get("id");
|
||||||
b.id = json[key].get("id");
|
if (id == "empty") {
|
||||||
if (b.id == "empty") {
|
|
||||||
e = e->next();
|
e = e->next();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
building::from_dict(&b, json[key], key);
|
||||||
|
#if 0
|
||||||
|
b.key = key;
|
||||||
|
b.id = json[key].get("id");
|
||||||
String aabb_s = json[key].get("aabb");
|
String aabb_s = json[key].get("aabb");
|
||||||
b.xform = from_string<Transform>(key);
|
b.xform = from_string<Transform>(key);
|
||||||
b.aabb = from_string<AABB>(aabb_s);
|
b.aabb = from_string<AABB>(aabb_s);
|
||||||
|
#endif
|
||||||
buildings.push_back(b);
|
buildings.push_back(b);
|
||||||
e = e->next();
|
e = e->next();
|
||||||
}
|
}
|
||||||
@@ -58,6 +62,64 @@ void StreamWorld::read_buildings_json(const String &buildings_path)
|
|||||||
print_line("entries count: " + itos(buildings.size()));
|
print_line("entries count: " + itos(buildings.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void StreamWorld::save_buildings_json(const String &buildings_path)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
String buildings_json = FileAccess::get_file_as_string(buildings_path);
|
||||||
|
Variant json_v;
|
||||||
|
String es;
|
||||||
|
int eline;
|
||||||
|
Error status = JSON::parse(buildings_json, json_v, es, eline);
|
||||||
|
ERR_FAIL_COND_MSG(status != OK, "Can't parse json: " + es +
|
||||||
|
" at line: " + itos(eline));
|
||||||
|
|
||||||
|
Dictionary json = json_v;
|
||||||
|
for (i = 0; i < (int)buildings.size(); i++)
|
||||||
|
print_line("entries count: " + itos(buildings.size()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void StreamWorld::building::from_dict(building *b, const Dictionary &dict,
|
||||||
|
const String &key)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
b->key = key;
|
||||||
|
b->xform = from_string<Transform>(key);
|
||||||
|
b->id = dict.get("id", "empty");
|
||||||
|
b->pattern_id = dict.get("pattern_id", 0);
|
||||||
|
Array residents = dict.get("residents", Array());
|
||||||
|
b->residents.resize(residents.size());
|
||||||
|
for (i = 0; i < (int)residents.size(); i++)
|
||||||
|
b->residents.write[i] = residents[i];
|
||||||
|
Array workers = dict.get("workers", Array());
|
||||||
|
b->workers.resize(workers.size());
|
||||||
|
for (i = 0; i < (int)workers.size(); i++)
|
||||||
|
b->workers.write[i] = workers[i];
|
||||||
|
Array guests = dict.get("guests", Array());
|
||||||
|
b->guests.resize(guests.size());
|
||||||
|
for (i = 0; i < (int)guests.size(); i++)
|
||||||
|
b->guests.write[i] = guests[i];
|
||||||
|
String doors_s = dict.get("doors", "[ ]");
|
||||||
|
Array doors = from_string<Array>(doors_s);
|
||||||
|
b->doors.resize(doors.size());
|
||||||
|
for (i = 0; i < (int)doors.size(); i++)
|
||||||
|
b->doors.write[i] = doors[i];
|
||||||
|
String aabb_s = dict.get("aabb", AABB());
|
||||||
|
b->aabb = from_string<AABB>(aabb_s);
|
||||||
|
Array worktime = dict.get("worktime", Array());
|
||||||
|
if (worktime.size() == 0) {
|
||||||
|
worktime.resize(2);
|
||||||
|
worktime[0] = 0;
|
||||||
|
worktime[1] = 23;
|
||||||
|
}
|
||||||
|
b->worktime[0] = worktime[0];
|
||||||
|
b->worktime[1] = worktime[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
Dictionary StreamWorld::building::to_dict() const
|
||||||
|
{
|
||||||
|
return Dictionary();
|
||||||
|
}
|
||||||
|
|
||||||
void StreamWorld::create_tilemap()
|
void StreamWorld::create_tilemap()
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|||||||
@@ -15,9 +15,17 @@ private:
|
|||||||
Node *current_scene;
|
Node *current_scene;
|
||||||
struct building {
|
struct building {
|
||||||
String id;
|
String id;
|
||||||
|
int pattern_id;
|
||||||
|
Vector<int> residents, workers, guests;
|
||||||
|
Vector<Transform> doors;
|
||||||
String key;
|
String key;
|
||||||
Transform xform;
|
Transform xform;
|
||||||
AABB aabb;
|
AABB aabb;
|
||||||
|
int worktime[2];
|
||||||
|
static void from_dict(struct building *b,
|
||||||
|
const Dictionary &dict,
|
||||||
|
const String &key);
|
||||||
|
Dictionary to_dict() const;
|
||||||
};
|
};
|
||||||
struct scene_data {
|
struct scene_data {
|
||||||
Ref<PackedScene> packed_scene;
|
Ref<PackedScene> packed_scene;
|
||||||
@@ -48,6 +56,7 @@ private:
|
|||||||
int current_x, current_z;
|
int current_x, current_z;
|
||||||
void _notification(int which);
|
void _notification(int which);
|
||||||
void read_buildings_json(const String &buildings_path);
|
void read_buildings_json(const String &buildings_path);
|
||||||
|
void save_buildings_json(const String &buildings_path);
|
||||||
void create_tilemap();
|
void create_tilemap();
|
||||||
void update_view();
|
void update_view();
|
||||||
void viewer_dead();
|
void viewer_dead();
|
||||||
|
|||||||
Reference in New Issue
Block a user