Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Cave-Crawler/ECSystem.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
114 lines (96 sloc)
2.15 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <vector> | |
#include <memory> | |
#include <algorithm> | |
#include <bitset> | |
#include <array> | |
class Comp; | |
class Entity; | |
using CompID = std::size_t; | |
inline CompID getCompTypeID() | |
{ | |
static CompID ultimulID = 0; | |
return ultimulID++; | |
} | |
template <typename T> inline CompID getCompTypeID() noexcept | |
{ | |
static CompID typeID = getCompTypeID(); | |
return typeID; | |
} | |
constexpr std::size_t maxComps = 32; | |
using CompBitSet = std::bitset<maxComps>; | |
using CompArray = std::array<Comp*, maxComps>; | |
class Comp | |
{ | |
public: | |
Entity* entity; | |
virtual void init() {} | |
virtual void update() {} | |
virtual void draw() {} | |
virtual ~Comp() {} | |
}; | |
class Entity { | |
private: | |
bool active = true; | |
std::vector<std::unique_ptr<Comp>> comps; | |
CompArray compArray; | |
CompBitSet compBitSet; | |
public: | |
void update() { | |
for (auto& c : comps) c->update(); | |
} | |
void draw() { | |
for (auto& c : comps) c->draw(); | |
} | |
bool isActive() const { return active; } | |
void destroy() { active = false; } | |
template <typename T> bool hasComp() const | |
{ | |
return compBitSet[getCompTypeID<T>]; | |
} | |
template <typename T, typename... TArgs> | |
T& addComp(TArgs&&... mArgs) | |
{ | |
T* c(new T(std::forward<TArgs>(mArgs)...)); | |
c->entity = this; | |
std::unique_ptr<Comp> uPtr{ c }; | |
comps.emplace_back(std::move(uPtr)); | |
compArray[getCompTypeID<T>()] = c; | |
compBitSet[getCompTypeID<T>()] = true; | |
c->init(); | |
return *c; | |
} | |
template<typename T> T& getComp() const | |
{ | |
auto ptr(compArray[getCompTypeID<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; | |
} | |
}; | |