aboutsummaryrefslogtreecommitdiff
path: root/source/30-game/Level.cpp
blob: 588108410bfc582dadd9bd10236775d99b47a122 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "Level.hpp"

#include "AppConfig.hpp"
#include "PodVector.hpp"
#include "RapidJsonHelper.hpp"
#include "ScopeGuard.hpp"
#include "Utils.hpp"

#include <rapidjson/document.h>
#include <rapidjson/filereadstream.h>
#include <rapidjson/filewritestream.h>
#include <rapidjson/writer.h>
#include <cstdio>
#include <filesystem>

using namespace std::literals;
namespace fs = std::filesystem;

constexpr auto kInvalidEntryId = std::numeric_limits<size_t>::max();

struct Level::InstanciationEntry {
	// If set to std::numeric_limits<size_t>::max(), this object is parented to the "root" provided when instanciating
	size_t parentId;
	rapidjson::Document data;
};

Level::Level() = default;

Level::~Level() = default;

void Level::Instanciate(GameObject* relRoot) const {
}

void LevelManager::DiscoverFilesDesignatedLocation() {
	auto path = AppConfig::assetDirPath / "Levels";
	DiscoverFiles(path);
}

void LevelManager::DiscoverFiles(const std::filesystem::path& dir) {
	for (auto& item : fs::directory_iterator(dir)) {
		auto& path = item.path();
		if (!item.is_regular_file()) {
			continue;
		}
		if (path.extension() != ".json") {
			continue;
		}

		// Parse uid from filename, map key
		Uid uid;
		uid.ReadString(path.filename().string());

		// Map value
		LoadableObject obj;
		obj.filePath = path;

		mObjByUid.try_emplace(uid, std::move(obj));
	}
}

Level* LevelManager::FindLevel(const Uid& uid) const {
	auto iter = mObjByUid.find(uid);
	if (iter != mObjByUid.end()) {
		return iter->second.level.Get();
	} else {
		return nullptr;
	}
}

#define BRUSSEL_DEF_LEVEL_NAME "New Level"
#define BRUSSEL_DEF_LEVEL_DESC "No description."

Level* LevelManager::LoadLevel(const Uid& uid) {
	auto iter = mObjByUid.find(uid);
	if (iter != mObjByUid.end()) {
		auto& ldObj = iter->second;
		if (ldObj.level != nullptr) {
			auto file = Utils::OpenCstdioFile(ldObj.filePath, Utils::Read, false);
			if (!file) {
				fprintf(stderr, "Cannot open file level file %s that was discovered on game startup.", ldObj.filePath.string().c_str());
				return nullptr;
			}
			DEFER { fclose(file); };

			char readerBuffer[65536];
			rapidjson::FileReadStream stream(file, readerBuffer, sizeof(readerBuffer));

			rapidjson::Document root;
			root.ParseStream(stream);

			Level* level;
			ldObj.level.Attach(level = new Level());

			level->mMan = this;

#if defined(BRUSSEL_DEV_ENV)
			BRUSSEL_JSON_GET_DEFAULT(root, "Name", std::string, ldObj.name, BRUSSEL_DEF_LEVEL_NAME);
			BRUSSEL_JSON_GET_DEFAULT(root, "Description", std::string, ldObj.description, BRUSSEL_DEF_LEVEL_DESC)
#endif

			auto rvEntries = rapidjson::GetProperty(root, rapidjson::kArrayType, "DataEntries"sv);
			if (!rvEntries) return nullptr;
			for (auto iter = rvEntries->Begin(); iter != rvEntries->End(); ++iter) {
				Level::InstanciationEntry entry;

				BRUSSEL_JSON_GET_DEFAULT(*iter, "ParentId", int, entry.parentId, kInvalidEntryId);

				auto rvDataEntry = rapidjson::GetProperty(*iter, "Data"sv);
				if (!rvDataEntry) return nullptr;
				entry.data.CopyFrom(*iter, entry.data.GetAllocator());

				level->mEntries.push_back(std::move(entry));
			}
		}
		return ldObj.level.Get();
	} else {
		return nullptr;
	}
}

void LevelManager::PrepareLevel(const Uid& uid) {
	// TODO
}

LevelManager::LoadableObject& LevelManager::AddLevel(const Uid& uid) {
	auto&& [iter, inserted] = mObjByUid.try_emplace(uid, LoadableObject{});
	auto& ldObj = iter->second;
#if defined(BRUSSEL_DEV_ENV)
	ldObj.name = BRUSSEL_DEF_LEVEL_NAME;
	ldObj.description = BRUSSEL_DEF_LEVEL_DESC;
#endif
	return ldObj;
}

LevelWrapperObject::LevelWrapperObject(GameWorld* world)
	: GameObject(KD_LevelWrapper, world) //
{
	mStopFreePropagation = true;
}

LevelWrapperObject::~LevelWrapperObject() {
	// Destruction/freeing of this object is handled by our parent
	for (auto child : GetChildren()) {
		FreeRecursive(child);
	}
}

void LevelWrapperObject::SetBoundLevel(Level* level) {
	if (mLevel != level) {
		mLevel.Attach(level);

		// Cleanup old children
		// TODO needs to Resleep()?
		auto children = RemoveAllChildren();
		for (auto child : children) {
			FreeRecursive(child);
		}
	}

	level->Instanciate(this);

	PodVector<GameObject*> stack;
	stack.push_back(this);

	while (!stack.empty()) {
		auto obj = stack.back();
		stack.pop_back();

		for (auto child : obj->GetChildren()) {
			stack.push_back(child);
		}

		obj->Awaken();
	}
}