Skip to content

Instantly share code, notes, and snippets.

@maluramichael
Last active December 31, 2015 15:08
Show Gist options
  • Save maluramichael/8004463 to your computer and use it in GitHub Desktop.
Save maluramichael/8004463 to your computer and use it in GitHub Desktop.
Component Entity Sample
#include<iostream>
#include<vector>
#include<bitset>
const short MAX_COMPS = 4;
typedef std::bitset<MAX_COMPS> ComponentMask;
/************************************************************************/
/* COMPONENT */
/************************************************************************/
class Component {
protected:
public:
virtual short getBit() = 0;
};
typedef std::vector<Component*> ComponentList;
/************************************************************************/
/* POSITION - COMPONENT */
/************************************************************************/
class Position : public Component {
int _x;
int _y;
int _z;
public:
short getBit(){
return 0;
}
Position(int x, int y, int z){
_x = x;
_y = y;
_z = z;
}
int getX(){
return _x;
}
int getY(){
return _y;
}
int getZ(){
return _z;
}
void setX(int value){
_x = value;
}
void setY(int value){
_y = value;
}
void setZ(int value){
_z = value;
}
};
/************************************************************************/
/* SIZ - COMPONENT */
/************************************************************************/
class Size : public Component {
public:
short getBit(){
return 1;
}
};
/************************************************************************/
/* ENTITY */
/************************************************************************/
class Entity{
private:
ComponentList _components;
public:
~Entity(){
for (ComponentList::iterator it = _components.begin(); it < _components.end(); it++)
{
delete (*it);
}
}
void addComponent(Component* component){
bool add = true;
for (ComponentList::iterator it = _components.begin(); it < _components.end(); it++)
{
if ((*it)->getBit() == component->getBit()) add = false;
}
if (add) _components.push_back(component);
else delete component;
}
ComponentMask getComponentMask(){
ComponentMask m;
for (ComponentList::iterator it = _components.begin(); it < _components.end(); it++)
{
m.set((*it)->getBit());
}
return m;
}
Component* getComponent(short bit){
for (ComponentList::iterator it = _components.begin(); it < _components.end(); it++)
{
if ((*it)->getBit() == bit) return (*it);
}
return NULL;
}
};
typedef std::vector<Entity*> EntityList;
/************************************************************************/
/* SYSTEM */
/************************************************************************/
class System{
private:
ComponentMask _componentMask;
protected:
EntityList _entities;
public:
System(ComponentMask componentMask){
_componentMask = componentMask;
}
void addEntity(Entity* entity){
if ((entity->getComponentMask() & _componentMask) == _componentMask){
_entities.push_back(entity);
}
}
void update(double delta){
for (EntityList::iterator it = _entities.begin(); it < _entities.end(); it++)
{
callEntity((*it), delta);
}
}
virtual void callEntity(Entity* entity, double delta) = 0;
};
typedef std::vector<System*> SystemList;
/************************************************************************/
/* POSITION - ENTITY */
/************************************************************************/
class PositionSystem : public System{
public:
PositionSystem(ComponentMask componentMask) : System(componentMask){
}
void callEntity(Entity* entity, double delta){
//TODO: Position* p = (Position*)e->getComponent(Position::ComponentId);
Position* position = (Position*)entity->getComponent(0);
position->setX(position->getX()+1);
}
};
class World {
private:
SystemList _systemList;
public:
World(){
}
void addSystem(System* system){
_systemList.push_back(system);
}
void update(double delta){
for (SystemList::iterator it = _systemList.begin(); it < _systemList.end(); it++)
{
(*it)->update(delta);
}
}
};
int main(){
World world;
Entity entitiy;
entitiy.addComponent(new Position(10,20,10));
entitiy.addComponent(new Size());
ComponentMask componentMask;
//TODO: componentMask.set(Position::ComponentId);
componentMask.set(0);
PositionSystem positionSystem = PositionSystem(componentMask);
positionSystem.addEntity(&entitiy);
world.addSystem(&positionSystem);
world.update(10);
std::cout << entitiy.getComponentMask();
std::cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment