Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <memory>
- #include <vector>
- #include <algorithm>
- #include <functional>
- #include <stdexcept>
- using EntityID_t = unsigned long;
- class Entity;
- class GameWorld {
- public:
- GameWorld() : nextEntityID(0) {};
- EntityID_t advanceEntityID() {return ++nextEntityID;}
- bool add(Entity* pEntity) {
- return false;
- }
- bool contains(Entity* pEntity) {
- findOneByPred([&](const std::shared_ptr<Entity>& collEnt) {
- return collEnt.get()==pEntity;
- });
- return true;
- }
- std::shared_ptr<Entity> findOneByPred(std::function<bool(const std::shared_ptr<Entity>&)> predicate) {
- auto result = std::find(entities.begin(), entities.end(), predicate);
- if(result!=entities.end()) {
- return *result;
- }
- throw std::runtime_error("Not found!");
- }
- private:
- EntityID_t nextEntityID;
- std::vector<std::shared_ptr<Entity>> entities;
- };
- class Entity {
- public:
- Entity(GameWorld& pGameWorld) : gameWorld(pGameWorld) {
- id = gameWorld.advanceEntityID();
- gameWorld.add(this);
- }
- EntityID_t getID() {return id;}
- private:
- EntityID_t id;
- GameWorld& gameWorld;
- };
- int main() {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement