aboutsummaryrefslogtreecommitdiff
path: root/src/example/ecs.cpp
blob: 9a008ea59bc306a6d34a0d279305503c07e9e920 (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
#include <iostream>

#include <crepe/ComponentManager.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Metadata.h>
#include <crepe/api/Transform.h>

using namespace crepe;
using namespace std;

int main() {
	ComponentManager mgr{};

	// Create a few GameObjects
	try {
		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);

		// Adding a second Transform component is not allowed and will invoke an exception
		body.add_component<Transform>(Vector2{10, 10}, 0, 1);
	} catch (const exception & e) {
		cerr << e.what() << endl;
	}

	// Get the Metadata and Transform components of each GameObject
	vector<reference_wrapper<Metadata>> metadata
		= mgr.get_components_by_type<Metadata>();
	vector<reference_wrapper<Transform>> transform
		= mgr.get_components_by_type<Transform>();

	// Print the Metadata and Transform components
	for (auto & m : metadata) {
		cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name
			 << " Tag: " << m.get().tag << " Parent: " << m.get().parent
			 << " Children: ";
		for (auto & c : m.get().children) {
			cout << c << " ";
		}
		cout << endl;
	}
	for (auto & t : transform) {
		cout << "Id: " << t.get().game_object_id << " Position: ["
			 << t.get().position.x << ", " << t.get().position.y << "]" << endl;
	}

	return 0;
}