Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- using namespace std;
- typedef unsigned long EntityId;
- typedef unsigned int EntityType;
- enum {PLAYER, ALIEN, TOWER};
- template <typename C> class System
- {
- public:
- std::map<EntityId, C * > components;
- };
- class Component
- {
- EntityId id;
- EntityType type;
- public:
- Component(EntityId pId, EntityType pType) : id(pId), type(pType) {}
- getId()
- {
- return id;
- }
- getType()
- {
- return type;
- }
- void render()
- {
- cout << "---------------------" << endl;
- cout << " - Id = " << getId() << endl;
- cout << " - Type = " << getType() << endl;
- cout << "---------------------" << endl;
- }
- };
- class PositionComponent: public Component
- {
- int x;
- int y;
- int z;
- public:
- PositionComponent(EntityId id, EntityType type) : Component(id, type), x(0), y(0), z(0) {}
- getX(){return x;}
- getY(){return y;}
- getZ(){return z;}
- };
- class RenderComponent : public Component
- {
- public:
- RenderComponent(EntityId id, EntityType type) : Component(id, type) {}
- };
- EntityId uniqId=0;
- System<Component> mapComponent;
- System<PositionComponent> mapPos;
- System<RenderComponent> mapRender;
- class EntityManager
- {
- public:
- static void addEntity (EntityType type, int x, int y, int z)
- {
- uniqId++;
- switch (type)
- {
- case PLAYER:
- mapComponent.components[uniqId] = new Component(uniqId, type);
- mapPos.components[uniqId] = new PositionComponent(uniqId, type);
- mapRender.components[uniqId] = new RenderComponent(uniqId, type);
- break;
- case ALIEN:
- mapComponent.components[uniqId] = new Component(uniqId, type);
- mapPos.components[uniqId] = new PositionComponent(uniqId, type);
- mapRender.components[uniqId] = new RenderComponent(uniqId, type);
- break;
- case TOWER:
- mapComponent.components[uniqId] = new Component(uniqId, type);
- mapPos.components[uniqId] = new PositionComponent(uniqId, type);
- //mapRender.components[uniqId] = new RenderComponent(uniqId, type);
- break;
- default:
- break;
- }
- }
- static void render()
- {
- for (std::map<EntityId, RenderComponent*>::iterator it = mapRender.components.begin(); it != mapRender.components.end(); ++it)
- {
- (*it).second->render();
- }
- }
- };
- int main()
- {
- EntityManager::addEntity(PLAYER, 10,10,0);
- EntityManager::addEntity(ALIEN, 10,10,0);
- EntityManager::addEntity(PLAYER, 10,10,0);
- EntityManager::addEntity(TOWER, 10,10,0);
- EntityManager::render();
- cout << "MugenStudio" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement