Advertisement
raoul632

simple ECS

Mar 8th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #ifndef ENTITY_H
  2. #define ENTITY_H
  3. //SIMPLE ECS ENTITY AND COMPONENT WAS HERE ALL LOGICAL STUFF HAPPEN IN GAME
  4. //all ENTITY was STORE IN --> static vector<Entity*> sGameObject;
  5. #include <iostream>
  6. #include <vector>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include "Global.h"
  11.  
  12. using namespace std;
  13.  
  14. enum listWeapon {
  15.     sword,
  16.     axe,
  17.     spear
  18. };
  19.  
  20. enum listEnemy {
  21.     snake,
  22.     rat,
  23.     wolf,
  24. };
  25.  
  26. struct Counter {
  27.     static int c;
  28.  
  29. };
  30.  
  31. enum listImage {
  32.     herosImage,
  33.     solImage,
  34.     arbreImage,
  35.     orcImage,
  36.     ratImage,
  37.     mortImage,
  38.     snakeImage,
  39.     wolfImage,
  40.     princessImage,
  41.     peasantImage,
  42.  
  43. };
  44.  
  45.  
  46.  
  47.  
  48. template <typename T>
  49. struct ClassID : Counter {
  50.     static int id() {
  51.         static int c = Counter::c++;
  52.         return c;
  53.     }
  54.  
  55. };
  56.  
  57. template<typename T>
  58. int ClassID_v = ClassID < std::decay_t<T>>::id();
  59.  
  60. struct Component {};
  61.  
  62. struct Entity {
  63.     map<int, unique_ptr<Component>> components;
  64.     template<typename Component>
  65.     Component& get() {
  66.         return static_cast<Component&>(*components[ClassID_v<Component>]);
  67.     }
  68.  
  69.     template <typename Component>
  70.     bool has_component() { return components.count(ClassID_v<Component>); }
  71.  
  72.     template<typename Component, typename... Args>
  73.     Entity& add_component(Args&&... args) { //parameter pack
  74.                                             //map::emplace insert element if its key is unique
  75.         components.emplace(ClassID_v<Component>, std::make_unique<Component>(std::forward<Args>(args)...));
  76.  
  77.         return *this;
  78.     }
  79.  
  80.     template<typename Component>
  81.     void remove_component() {
  82.         components.erase(ClassID_v<Component>);
  83.     }
  84.  
  85.  
  86. };
  87.  
  88. struct Health :Component {
  89.     Health() = default;
  90.     Health(int current, int max) : current_health(current), max_health(max) {};
  91.     int current_health = 100, max_health = 100;
  92.  
  93. };
  94.  
  95. struct Name : Component {
  96.     string name = "no name";
  97.     Name() = default;
  98.     Name(std::string n) :name(n) {};
  99. };
  100.  
  101. struct Corpse : Component {
  102.     Corpse() = default;
  103. };
  104.  
  105.  
  106. struct Attack : Component {
  107.     int attack = 0;
  108.     Attack() = default;
  109.     Attack(int a) : attack(a) {};
  110.    
  111.  
  112. };
  113.  
  114. struct Goal : Component {
  115.     Goal() = default;
  116. };
  117.  
  118. struct Player : Component {
  119.     string name = "";
  120.     Player() = default;
  121.     Player(string n) : name(n) {};
  122.    
  123. };
  124.  
  125.  
  126. struct Enemy : Component {
  127.     string name = "";
  128.     Enemy() = default;
  129.     Enemy(string n) : name(n) {};
  130.    
  131.  
  132. };
  133.  
  134. struct Position :Component {
  135.     short x;
  136.     short y;
  137.     Position() = default;
  138.     Position(short _x, short _y) :x(_x), y(_y) {};
  139.  
  140.     friend bool operator== (const Position& lhs, const Position& rhs)
  141.     {
  142.         return (lhs.x == rhs.x) && (rhs.y == lhs.y);
  143.     }
  144.    
  145. };
  146.  
  147.  
  148.  
  149. struct Weapon : Component {
  150.     listWeapon weapon;
  151.     int dmg;
  152.     Weapon() = default;
  153.     Weapon(listWeapon w, int d) : weapon(w), dmg(d) {};
  154.  
  155.  
  156. };
  157.  
  158. enum listArmor {
  159.     leather,
  160.     chainshirt,
  161.     mudarmor,
  162.  
  163. };
  164.  
  165. struct Armor : Component {
  166.     listArmor armor;
  167.     int protection;
  168.     Armor() = default;
  169.     Armor(listArmor a, int p) : armor(a), protection(p) {};
  170.  
  171. };
  172.  
  173. struct Skill : Component {
  174.     int force;
  175.     int hability;
  176.     int Endurance;
  177.     Skill() = default;
  178.     Skill(int f, int h, int e) : force(f), hability(h), Endurance(e) {};
  179.  
  180. };
  181.  
  182.  
  183. struct Physical : Component {
  184.     bool blocksMovement;
  185.     bool blocksSight;
  186.     char glyph;
  187.     Physical() = default;
  188.     Physical(bool _blocksMovement, bool _blocksSight, char _glyph) : blocksMovement(_blocksMovement), blocksSight(_blocksSight), glyph(_glyph) {  };
  189.    
  190. };
  191.  
  192. struct Visibility : Component {
  193.     bool areVisible;
  194.     bool wasVisible;
  195.     Visibility() = default;
  196.     Visibility(bool are, bool was):areVisible(are = false), wasVisible(was = false){};
  197.  
  198. } ;
  199.  
  200. struct Plague : Component {
  201.  
  202. };
  203.  
  204.  
  205. static vector<Entity*> sGameObject;
  206.  
  207. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement