-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecs.hpp
More file actions
72 lines (59 loc) · 1.57 KB
/
ecs.hpp
File metadata and controls
72 lines (59 loc) · 1.57 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
#pragma once
#include <SDL2/SDL.h>
#include <vector>
#include <iostream>
#include "game.hpp"
#define MAX_COMPONENTS 32
enum { INPUT_COMPONENT, GRAPHICS_COMPONENT, PHYSICS_COMPONENT, JUMPING_COMPONENT, LIGHT_COMPONENT, CAMERA_COMPONENT, HEALTH_COMPONENT, FLYING_COMPONENT, GRAB_COMPONENT, COLLIDER_COMPONENT };
class Component;
class Entity;
class Component {
public:
virtual void update(Entity*) {}
virtual void draw(Entity*) {}
virtual void init(Entity*) {}
};
class Entity {
public:
std::vector<Component*> components = std::vector<Component*>(MAX_COMPONENTS, nullptr);
bool mark_active = true;
bool mark_remove = false;
int xpos = 0, ypos = 0;
int n_xpos = 0, n_ypos = 0;
int width = 0, height = 0;
int tag;
virtual void priority_update() {}
virtual void update() {}
virtual void draw() {}
template<typename T> T& getComponent(const int c) {
auto ptr(components[c]);
return *static_cast<T*>(ptr);
}
template<typename T, typename ... Args> void addComponent(const int c, Args ... TArgs) {
if (components[c] == nullptr) {
components[c] = new T(TArgs...);
Game::manager->addEntityToGroup(this, c);
}
}
template<typename T> void deleteComponent(const int c) {
if (components[c] != nullptr) {
delete components[c];
components[c] = nullptr;
}
}
void updateComponents();
void initComponents();
void drawComponents();
void generateTag();
};
namespace std
{
template<>
struct hash<Entity>
{
size_t operator()(const Entity& k) const
{
return hash<int>()(k.tag);
}
};
}