-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.hpp
More file actions
70 lines (57 loc) · 2.3 KB
/
components.hpp
File metadata and controls
70 lines (57 loc) · 2.3 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
#include "ecs/ecs.hpp"
/*
* [WARNING!] This header file has been created for the sole purpose of testing the Entity Component System.
* This file has no connection with the main project. The components in this file were created to test the system in the header file named "ecs.hpp".
* When using this system, you need to create different components depending on the project you have made.
* The type and number of components will vary according to your project requirements.
*/
// Test component for examples. Used in example1, example2
struct PositionComponent : public Component {
int x;
int y;
PositionComponent(int x_, int y_) : x(x_), y(y_) {}
};
// Test component for examples. Used in example1, example2
struct VelocityComponent : public Component {
int dx;
int dy;
VelocityComponent(int dx_, int dy_) : dx(dx_), dy(dy_) {}
};
// Test system for components. Used in example1, example2
class MovementSystem {
public:
void Process(std::shared_ptr<Entity> entity)
{
auto position = entity->GetComponent<PositionComponent>();
auto velocity = entity->GetComponent<VelocityComponent>();
if (position && velocity) {
position->x += velocity->dx;
position->y += velocity->dy;
}
}
};
// Test component for examples. Used in example2
struct HealthComponent : public Component {
int health;
HealthComponent(int initialHealth) : health(initialHealth) {}
};
// Test system for components. Used in example2
class CollisionSystem {
public:
void Process(std::shared_ptr<Entity> entity1, std::shared_ptr<Entity> entity2) {
auto pos1 = entity1->GetComponent<PositionComponent>();
auto pos2 = entity2->GetComponent<PositionComponent>();
if (CheckCollision(pos1, pos2)) {
auto health1 = entity1->GetComponent<HealthComponent>();
auto health2 = entity2->GetComponent<HealthComponent>();
if (health1 && health2) {
health1->health -= 10;
health2->health -= 10;
}
}
}
private:
bool CheckCollision(const std::shared_ptr<PositionComponent>& pos1, const std::shared_ptr<PositionComponent>& pos2) {
return (pos1->x == pos2->x) && (pos1->y == pos2->y);
}
};