-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentArray.h
More file actions
101 lines (98 loc) · 2.84 KB
/
ComponentArray.h
File metadata and controls
101 lines (98 loc) · 2.84 KB
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
#pragma once
#include <mono/metadata/object.h>
#include <unordered_map>
#include <iostream>
#include <rapidjson/prettywriter.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/document.h>
#include "ScriptingTypes.h"
#include "imgui.h"
class ComponentArray {
public:
std::string name;
virtual ~ComponentArray() = default;
virtual void OnEntityDestroyed(unsigned int entity) = 0;
virtual void Start() = 0;
virtual void Update() = 0;
virtual void Render() = 0;
virtual void Clear() = 0;
virtual void Add(unsigned int entity) = 0;
virtual bool HasComponent(unsigned int entity) = 0;
virtual void DrawComponentGUI(unsigned int entity) = 0;
virtual void DeserializeComponent(unsigned int entity, rapidjson::Value& obj) = 0;
virtual void SerializeComponent(unsigned int entity, rapidjson::PrettyWriter<rapidjson::StringBuffer>* json) = 0;
virtual void GetObject(unsigned int entity, ClassInstance* instance) = 0;
virtual void SetObject(unsigned int entity, ClassInstance* instance) = 0;
};
template<typename T>
class Components : public ComponentArray {
public:
std::unordered_map<unsigned int, T> components;
void Add(unsigned int entity, T component) {
components[entity] = component;
components[entity].entity = entity;
components[entity].Initialize();
components[entity].Start();
}
void Add(unsigned int entity) {
T value;
value.entity = entity;
components[entity] = value;
components[entity].Initialize();
}
void Clear() {
components.clear();
}
void Render() {
for (auto& comp : components)
comp.second.Render();
}
bool HasComponent(unsigned int entity) {
return components.find(entity) != components.end();
}
void Remove(unsigned int entity) {
components.erase(entity);
}
void DeserializeComponent(unsigned int entity, rapidjson::Value& obj) {
components[entity].Deserialize(obj);
}
void SerializeComponent(unsigned int entity, rapidjson::PrettyWriter<rapidjson::StringBuffer>* json) {
components[entity].Serialize(json);
}
T& Get(unsigned int entity) {
return components[entity];
}
T* GetPointer(unsigned int entity) {
return &components[entity];
}
void DrawComponentGUI(unsigned int entity) {
if (HasComponent(entity)) {
bool componentExists = true;
if (ImGui::CollapsingHeader(name.c_str(), &componentExists)) {
components[entity].DrawGUI(entity);
}
if (!componentExists)
Remove(entity);
}
}
void Start() {
for (auto& comp : components) {
comp.second.Start();
}
}
void Update() {
for (auto& comp : components) {
comp.second.Update();
}
}
void GetObject(unsigned int entity, ClassInstance* instance) {
components[entity].GetObject(instance);
}
void SetObject(unsigned int entity, ClassInstance* instance) {
components[entity].SetObject(instance);
}
void OnEntityDestroyed(unsigned int entity) {
if (components.find(entity) != components.end())
Remove(entity);
}
};