Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- template <class M = std::string, class E = int>
- E log(M msg, E error = 0)
- {
- std::cout << msg;
- return error;
- }
- static int const POSITION = 10;
- static int const VELOCITY = 11;
- struct Component
- {
- int _type;
- virtual Component *clone() const = 0;
- };
- template <class DERIVED>
- struct ComponentHelper : public Component
- {
- virtual Component *clone() const
- {
- return new DERIVED(static_cast<const DERIVED&>(*this));
- }
- };
- struct Position : public ComponentHelper<Position>
- {
- int _x = 0;
- int _y = 0;
- int _z = 0;
- Position()
- {
- _type = POSITION;
- }
- Position(int x, int y, int z)
- {
- _type = POSITION;
- _x = x;
- _y = y;
- _z = z;
- }
- };
- struct Velocity : public ComponentHelper<Velocity>
- {
- int _x = 0;
- int _y = 0;
- int _z = 0;
- Velocity()
- {
- _type = VELOCITY;
- }
- Velocity(int x, int y, int z)
- {
- _type = VELOCITY;
- _x = x;
- _y = y;
- _z = z;
- }
- };
- struct Entity
- {
- std::string _name="";
- Entity *_parent = nullptr;
- std::map<int, Component*> _mapMember;
- static Entity *cloneOf(Entity *original)
- {
- Entity *clone = new Entity(*original);
- //clone = original;
- log ("--- Begin deep copy ---\n");
- std::map<int,Component*>::const_iterator it = original->_mapMember.begin();
- while (it != original->_mapMember.end())
- {
- //clone->_mapMember[it->first] = new Component(*(it->second));
- clone->_mapMember[it->first] = (*(it->second)).clone();
- it++;
- }
- return clone;
- }
- Entity(Entity *parent = nullptr, std::string name="")
- {
- _name = name;
- _parent = parent;
- log("- Entity created !\n");
- }
- ~Entity()
- {
- std::map<int,Component*>::iterator it = _mapMember.begin();
- while (it != _mapMember.end())
- {
- if (it->second != nullptr)
- {
- log ("- Delete Component:"+ std::to_string(it->second->_type) + " of Entity: " + _name + "\n");
- delete it->second;
- it->second = nullptr;
- }
- it++;
- }
- _mapMember.clear();
- log("- Delete Entity: " + _name + "\n");
- }
- void add(Component* component)
- {
- _mapMember[component->_type] = component;
- }
- template <class COMPONENT>
- COMPONENT *get()
- {
- COMPONENT component;
- return static_cast<COMPONENT*>(_mapMember[component._type]);
- }
- void update()
- {
- }
- void render()
- {
- log("****************\n");
- log("* Name = ");log(_name);log("\n");
- log("****************\n");
- std::map<int,Component*>::iterator it = _mapMember.begin();
- while (it != _mapMember.end())
- {
- if (it->second != nullptr)
- {
- if (it->second->_type == POSITION)
- {
- log("--- Position ---\n");
- log("- x = ");log(get<Position>()->_x);log("\n");
- log("- y = ");log(get<Position>()->_y);log("\n");
- log("- z = ");log(get<Position>()->_z);log("\n");
- }
- if (it->second->_type == VELOCITY)
- {
- log("--- Velocity ---\n");
- log("- x = ");log(get<Velocity>()->_x);log("\n");
- log("- y = ");log(get<Velocity>()->_y);log("\n");
- log("- z = ");log(get<Velocity>()->_z);log("\n");
- }
- }
- it++;
- }
- }
- };
- int main()
- {
- // Init
- Entity *player = new Entity(nullptr, "Mugen");
- player->add(new Position(10,20,30));
- player->add(new Velocity(1,2,3));
- player->get<Position>()->_x = 99;
- player->get<Position>()->_y = 88;
- player->get<Velocity>()->_z = 888888;
- Entity *monster0 = new Entity(nullptr, "Monster");
- monster0->add(new Position(320,240,0));
- monster0->add(new Velocity(77,55,33));
- monster0->get<Velocity>()->_x = -666;
- monster0->get<Velocity>()->_y = -666;
- //monster0->get<Position>()->_z = 1111111;
- //Entity *copyPlayer = new Entity(nullptr, "MugenCopy");
- Entity *copyPlayer = Entity::cloneOf(player);
- Entity *monster1 = Entity::cloneOf(monster0);
- Entity *monster2 = Entity::cloneOf(monster0);
- monster1->get<Position>()->_z = 11111111;
- monster1->get<Velocity>()->_z = 11111111;
- monster2->get<Position>()->_x = 22222222;
- monster2->get<Position>()->_y = 22222222;
- monster2->get<Position>()->_z = 22222222;
- // Render
- player->render();
- copyPlayer->render();
- monster0->render();
- monster1->render();
- monster2->render();
- // done
- delete player;
- delete copyPlayer;
- delete monster0;
- delete monster1;
- delete monster2;
- return log("--- --- End of main OK --- ---\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement