#Entire Component System
5 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
ecs.h
#define ECS_HPP
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
#include <bitset>
#include <array>
class Component;
class Entity;
using ComponentID = std::size_t;
inline ComponentID getComponentTypeID()
{
static ComponentID lastID = 0;
return lastID++;
}
template <typename T> inline ComponentID getComponentTypeID() noexcept
{
static ComponentID typeID = getComponentTypeID();
return typeID;
}
constexpr std::size_t maxComponents = 32;
using ComponentBitset = std::bitset<maxComponents>;
using ComponentArray = std::array<Component*, maxComponents>;
class Component
{
public:
Entity* entity;
virtual void init() {}
virtual void update() {}
virtual void draw() {}
virtual ~Component() {}
};```
class Entity
{
public:
bool active = true;
std::vector<std::unique_ptr<Component>> components;
ComponentArray componentArray;
ComponentBitset componentBitset;
public:
void update()
{
for(auto& c : components)c->update();
for(auto& c : components)c->draw();
}
void draw() {}
bool isActive() const {return active;}void destroy() {active = false;}
template <typename T> bool hasComponent() const
{
return componentBitset[getComponentTypeID<T>()];
}
template <typename T, typename... TArgs>
T& addComponent(TArgs&&... mArgs)
{
T* c(new T(std::forward<TArgs>(mArgs)...));
c->entity = this;
std::unique_ptr<Component> uPtr( c );
components.emplace_back(std::move(uPtr));
componentArray[getComponentTypeID<T>()] = c;
componentBitset[getComponentTypeID<T>()] = true;
c->init();
return *c;
}
template<typename T> T& getComponent() const
{
auto ptr(componentArray[getComponentTypeID<T>()]);
return *static_cast<T>(ptr);
}
};
class Manager
{
private:
std::vector<std::unique_ptr<Entity>> entities;
public:
void update()
{
for (auto& e : entities) e->update();
}
void draw()
{
for (auto& e : entities) e->draw();
}
void refresh()
{
entities.erase(std::remove_if(std::begin(entities), std::end(entities), [](const std::unique_ptr<Entity> &mEntity)
{
return !mEntity->isActive();
}),
std::end(entities));
}
Entity& addEntity()
{
Entity* e = new Entity();
std::unique_ptr<Entity> uPtr( e );
entities.emplace_back(std::move(uPtr));
return *e;
}
};
#endif // ECS_HPP```
in game.cpp from line 8 - 14
9.GameObject *player;
10.Map *map;
11.//GameObject *player2;
12.
13.Manager manager;
14.auto& newPlayer(manager.addEntity());```
line 79 - 86
```79.void Game::update()
80.{
81. player->update();
82. manager.update();
83. //player2->update();
84. std::cout << newPlayer.getComponent<PositionComponent>.
().x() << "," <<
86. newPlayer.getComponent<PositionComponent>().y() << "," << 87.std::endl;
}```
also i uploaded all file into github
https://github.com/M14405/Entire-Component-System.git