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

#include "EditorAttachment.hpp"
#include "Material.hpp"
#include "PodVector.hpp"
#include "Renderer.hpp"
#include "VertexIndex.hpp"

#include <rapidjson/fwd.h>
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include <span>
#include <vector>

class GameWorld;
class GameObject {
public:
	enum Kind {
		KD_Generic,
		KD_Player,
		KD_SimpleGeometry,
		KD_Building,
		KD_LevelWrapper,
		KD_COUNT,
	};

private:
	std::unique_ptr<EditorAttachment> mEditorAttachment;
	GameWorld* mWorld;
	GameObject* mParent;
	PodVector<GameObject*> mChildren;
	glm::quat mRot;
	glm::vec3 mPos;
	glm::vec3 mScale;
	Kind mKind;

protected:
	bool mStopFreePropagation : 1 = false;

public:
	static void FreeRecursive(GameObject* object);

	// TODO allow moving between worlds
	GameObject(GameWorld* world);
	GameObject(Kind kind, GameWorld* world);
	virtual ~GameObject();

	GameObject(const GameObject&) = delete;
	GameObject& operator=(const GameObject&) = delete;
	GameObject(GameObject&&) = default;
	GameObject& operator=(GameObject&&) = default;

	static std::string_view ToString(Kind kind);
	static Kind FromString(std::string_view name);
	Kind GetKind() const;

	GameWorld* GetWorld() const;
	GameObject* GetParent() const;
	const PodVector<GameObject*>& GetChildren() const;
	void AddChild(GameObject* child);
	GameObject* RemoveChild(int index);
	GameObject* RemoveChild(GameObject* child);
	void RemoveSelfFromParent();
	PodVector<GameObject*> RemoveAllChildren();

	EditorAttachment* GetEditorAttachment() const { return mEditorAttachment.get(); }
	void SetEditorAttachment(EditorAttachment* attachment) { mEditorAttachment.reset(attachment); }

	const glm::vec3& GetPos() const;
	void SetPos(const glm::vec3& pos);

	const glm::quat& GetRotation() const;
	void SetRotation(const glm::quat& rotation);

	const glm::vec3& GetScale() const;
	void SetScale(const glm::vec3& scale);

	// Visuals
	virtual std::span<const RenderObject> GetRenderObjects() const;

	// Lifetime hooks
	virtual void OnInitialized();
	virtual void Awaken();
	virtual void Resleep();
	virtual void Update();

	static rapidjson::Value Serialize(GameObject* obj, rapidjson::Document& root);
	static std::unique_ptr<GameObject> Deserialize(const rapidjson::Value& value, GameWorld* world);
	virtual void ReadSaveFormat(const rapidjson::Value& value);
	virtual void WriteSaveFormat(rapidjson::Value& value, rapidjson::Document& root);

protected:
	void SetParent(GameObject* parent);
};