Advertisement
Patasuss

Base

Jul 28th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <functional>
  6. #include <stdexcept>
  7.  
  8. using EntityID_t = unsigned long;
  9.  
  10. class Entity;
  11.  
  12. class GameWorld {
  13. public:
  14.     GameWorld() : nextEntityID(0) {};
  15.     EntityID_t advanceEntityID() {return ++nextEntityID;}
  16.    
  17.     bool add(Entity* pEntity) {
  18.         return false;
  19.     }
  20.    
  21.     bool contains(Entity* pEntity) {
  22.         findOneByPred([&](const std::shared_ptr<Entity>& collEnt) {
  23.             return collEnt.get()==pEntity;
  24.         });
  25.         return true;
  26.     }
  27.    
  28.     std::shared_ptr<Entity> findOneByPred(std::function<bool(const std::shared_ptr<Entity>&)> predicate) {
  29.         auto result = std::find(entities.begin(), entities.end(), predicate);
  30.         if(result!=entities.end()) {
  31.             return *result;
  32.         }
  33.         throw std::runtime_error("Not found!");
  34.     }
  35.    
  36. private:
  37.     EntityID_t nextEntityID;
  38.     std::vector<std::shared_ptr<Entity>> entities;
  39. };
  40.  
  41. class Entity {
  42. public:
  43.     Entity(GameWorld& pGameWorld) : gameWorld(pGameWorld) {
  44.         id = gameWorld.advanceEntityID();
  45.         gameWorld.add(this);
  46.     }
  47.    
  48.     EntityID_t getID() {return id;}
  49.  
  50. private:
  51.     EntityID_t id;
  52.     GameWorld& gameWorld;    
  53. };
  54.  
  55.  
  56. int main() {
  57.    
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement