aboutsummaryrefslogtreecommitdiff
path: root/source/30-game/Level.hpp
blob: c1170a3080f63a6ef8141aa048f35010141ec5fd (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
#pragma once

#include "GameObject.hpp"
#include "RcPtr.hpp"
#include "Uid.hpp"

#include <robin_hood.h>
#include <filesystem>
#include <vector>

// Forward declarations
class Level;
class LevelManager;

/// Represents a seralized GameObject tree.
class Level : public RefCounted {
	friend class LevelManager;

private:
	struct InstanciationEntry;

	LevelManager* mMan;
	std::vector<InstanciationEntry> mEntries;

public:
	Level();
	~Level();

	void Instanciate(GameObject* relRoot) const;
};

class LevelManager {
public:
	static inline LevelManager* instance = nullptr;

public: // NOTE: public for the editor; actual game components should not modify the map using this
	struct LoadableObject {
		RcPtr<Level> level; // TODO make weak pointer
		std::filesystem::path filePath;
		// NOTE: these fields are only loaded in dev mode
		std::string name;
		std::string description;
	};
	// We want pointer stability here for the editor (inspector object)
	robin_hood::unordered_node_map<Uid, LoadableObject> mObjByUid;

public:
	void DiscoverFilesDesignatedLocation();
	void DiscoverFiles(const std::filesystem::path& dir);

	Level* FindLevel(const Uid& uid) const;
	/// Get or load the given level
	Level* LoadLevel(const Uid& uid);
	/// Send the given level to be loaded on another thread
	void PrepareLevel(const Uid& uid);

	// These should only be used by the editor
	LoadableObject& AddLevel(const Uid& uid);
};

class LevelWrapperObject : public GameObject {
private:
	RcPtr<Level> mLevel;

public:
	LevelWrapperObject(GameWorld* world);
	~LevelWrapperObject() override;

	Level* GetBoundLevel() const;
	void SetBoundLevel(Level* level);
};