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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#include <climits>
#include <cstdint>
#define protected public
#include <crepe/ComponentManager.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Metadata.h>
#include <crepe/api/Transform.h>
#include <crepe/api/Vector2.h>
#include <crepe/api/Rigidbody.h>
#include <gtest/gtest.h>
using namespace std;
using namespace crepe;
class ECSTest : public ::testing::Test {
public:
ComponentManager mgr{};
};
TEST_F(ECSTest, createGameObject) {
GameObject obj = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1);
vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>();
vector<reference_wrapper<Transform>> transform = mgr.get_components_by_type<Transform>();
EXPECT_EQ(metadata.size(), 1);
EXPECT_EQ(transform.size(), 1);
EXPECT_EQ(metadata[0].get().name, "body");
EXPECT_EQ(metadata[0].get().tag, "person");
EXPECT_EQ(metadata[0].get().parent, -1);
EXPECT_EQ(metadata[0].get().children.size(), 0);
EXPECT_EQ(transform[0].get().position.x, 0);
EXPECT_EQ(transform[0].get().position.y, 0);
EXPECT_EQ(transform[0].get().rotation, 0);
EXPECT_EQ(transform[0].get().scale, 1);
}
TEST_F(ECSTest, deleteAllGameObjects) {
GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1);
GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1);
mgr.delete_all_components();
vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>();
vector<reference_wrapper<Transform>> transform = mgr.get_components_by_type<Transform>();
EXPECT_EQ(metadata.size(), 0);
EXPECT_EQ(transform.size(), 0);
GameObject obj2 = mgr.new_object("body2", "person2", Vector2{1, 0}, 5, 1);
metadata = mgr.get_components_by_type<Metadata>();
transform = mgr.get_components_by_type<Transform>();
EXPECT_EQ(metadata.size(), 1);
EXPECT_EQ(transform.size(), 1);
EXPECT_EQ(metadata[0].get().game_object_id, 0);
EXPECT_EQ(metadata[0].get().name, "body2");
EXPECT_EQ(metadata[0].get().tag, "person2");
EXPECT_EQ(metadata[0].get().parent, -1);
EXPECT_EQ(metadata[0].get().children.size(), 0);
EXPECT_EQ(transform[0].get().game_object_id, 0);
EXPECT_EQ(transform[0].get().position.x, 1);
EXPECT_EQ(transform[0].get().position.y, 0);
EXPECT_EQ(transform[0].get().rotation, 5);
EXPECT_EQ(transform[0].get().scale, 1);
}
TEST_F(ECSTest, deleteGameObject) {
GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1);
GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1);
mgr.delete_all_components_of_id(0);
vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>();
vector<reference_wrapper<Transform>> transform = mgr.get_components_by_type<Transform>();
EXPECT_EQ(metadata.size(), 1);
EXPECT_EQ(transform.size(), 1);
EXPECT_EQ(metadata[0].get().game_object_id, 1);
EXPECT_EQ(metadata[0].get().name, "body");
EXPECT_EQ(metadata[0].get().tag, "person");
EXPECT_EQ(metadata[0].get().parent, -1);
EXPECT_EQ(metadata[0].get().children.size(), 0);
EXPECT_EQ(transform[0].get().game_object_id, 1);
EXPECT_EQ(transform[0].get().position.x, 0);
EXPECT_EQ(transform[0].get().position.y, 0);
EXPECT_EQ(transform[0].get().rotation, 0);
EXPECT_EQ(transform[0].get().scale, 1);
}
TEST_F(ECSTest, manyGameObjects) {
for(int i = 0; i < 5000; i++) {
GameObject obj = mgr.new_object("body", "person", Vector2{0, 0}, 0, i);
}
vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>();
vector<reference_wrapper<Transform>> transform = mgr.get_components_by_type<Transform>();
EXPECT_EQ(metadata.size(), 5000);
EXPECT_EQ(transform.size(), 5000);
for(int i = 0; i < 5000; i++) {
EXPECT_EQ(metadata[i].get().game_object_id, i);
EXPECT_EQ(metadata[i].get().name, "body");
EXPECT_EQ(metadata[i].get().tag, "person");
EXPECT_EQ(metadata[i].get().parent, -1);
EXPECT_EQ(metadata[i].get().children.size(), 0);
EXPECT_EQ(transform[i].get().game_object_id, i);
EXPECT_EQ(transform[i].get().position.x, 0);
EXPECT_EQ(transform[i].get().position.y, 0);
EXPECT_EQ(transform[i].get().rotation, 0);
EXPECT_EQ(transform[i].get().scale, i);
}
mgr.delete_components<Metadata>();
metadata = mgr.get_components_by_type<Metadata>();
transform = mgr.get_components_by_type<Transform>();
EXPECT_EQ(metadata.size(), 0);
EXPECT_EQ(transform.size(), 5000);
for(int i = 0; i < 10000 - 5000; i++) {
string tag = "person" + to_string(i);
GameObject obj = mgr.new_object("body", tag, Vector2{0, 0}, i, 0);
}
metadata = mgr.get_components_by_type<Metadata>();
transform = mgr.get_components_by_type<Transform>();
EXPECT_EQ(metadata.size(), 10000 - 5000);
EXPECT_EQ(transform.size(), 10000);
}
TEST_F(ECSTest, getComponentsByID) {
GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1);
GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1);
vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_id<Metadata>(0);
vector<reference_wrapper<Transform>> transform = mgr.get_components_by_id<Transform>(1);
EXPECT_EQ(metadata.size(), 1);
EXPECT_EQ(transform.size(), 1);
EXPECT_EQ(metadata[0].get().game_object_id, 0);
EXPECT_EQ(metadata[0].get().name, "body");
EXPECT_EQ(metadata[0].get().tag, "person");
EXPECT_EQ(metadata[0].get().parent, -1);
EXPECT_EQ(metadata[0].get().children.size(), 0);
EXPECT_EQ(transform[0].get().game_object_id, 1);
EXPECT_EQ(transform[0].get().position.x, 0);
EXPECT_EQ(transform[0].get().position.y, 0);
EXPECT_EQ(transform[0].get().rotation, 0);
EXPECT_EQ(transform[0].get().scale, 1);
}
TEST_F(ECSTest, tooMuchComponents) {
try {
GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1);
obj0.add_component<Transform>(Vector2{10, 10}, 0, 1);
} catch (const exception & e) {
EXPECT_EQ(e.what(), string("Exceeded maximum number of instances for this component type"));
}
try {
GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1);
obj1.add_component<Metadata>("body", "person");
} catch (const exception & e) {
EXPECT_EQ(e.what(), string("Exceeded maximum number of instances for this component type"));
}
vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>();
EXPECT_EQ(metadata.size(), 2);
EXPECT_EQ(metadata[0].get().name, "body");
EXPECT_EQ(metadata[1].get().name, "body");
}
TEST_F(ECSTest, partentChild) {
{
GameObject body = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1);
GameObject right_leg = mgr.new_object("rightLeg", "person", Vector2{1, 1}, 0, 1);
GameObject left_leg = mgr.new_object("leftLeg", "person", Vector2{1, 1}, 0, 1);
GameObject right_foot = mgr.new_object("rightFoot", "person", Vector2{2, 2}, 0, 1);
GameObject left_foot = mgr.new_object("leftFoot", "person", Vector2{2, 2}, 0, 1);
// Set the parent of each GameObject
right_foot.set_parent(right_leg);
left_foot.set_parent(left_leg);
right_leg.set_parent(body);
left_leg.set_parent(body);
}
// Get the Metadata and Transform components of each GameObject
vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>();
// Check IDs
EXPECT_EQ(metadata[0].get().game_object_id, 0);
EXPECT_EQ(metadata[1].get().game_object_id, 1);
EXPECT_EQ(metadata[2].get().game_object_id, 2);
EXPECT_EQ(metadata[3].get().game_object_id, 3);
EXPECT_EQ(metadata[4].get().game_object_id, 4);
// Check the parent-child relationships
EXPECT_EQ(metadata[0].get().name, "body");
EXPECT_EQ(metadata[1].get().name, "rightLeg");
EXPECT_EQ(metadata[2].get().name, "leftLeg");
EXPECT_EQ(metadata[3].get().name, "rightFoot");
EXPECT_EQ(metadata[4].get().name, "leftFoot");
EXPECT_EQ(metadata[0].get().parent, -1);
EXPECT_EQ(metadata[1].get().parent, 0);
EXPECT_EQ(metadata[2].get().parent, 0);
EXPECT_EQ(metadata[3].get().parent, 1);
EXPECT_EQ(metadata[4].get().parent, 2);
EXPECT_EQ(metadata[0].get().children.size(), 2);
EXPECT_EQ(metadata[1].get().children.size(), 1);
EXPECT_EQ(metadata[2].get().children.size(), 1);
EXPECT_EQ(metadata[3].get().children.size(), 0);
EXPECT_EQ(metadata[4].get().children.size(), 0);
EXPECT_EQ(metadata[0].get().children[0], 1);
EXPECT_EQ(metadata[0].get().children[1], 2);
EXPECT_EQ(metadata[1].get().children[0], 3);
EXPECT_EQ(metadata[2].get().children[0], 4);
}
|