Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdarg.h>
  3. #include <vector>
  4.  
  5. class IComponent {
  6. public:
  7. virtual uint32_t GetID() const = 0;
  8. };
  9.  
  10. template <uint32_t ID>
  11. class Component : public IComponent {
  12. public:
  13. virtual ~Component() { }
  14.  
  15. public:
  16. constexpr static size_t TypeID = (1u << ID);
  17. virtual size_t GetID() const override { return TypeID; }
  18. };
  19.  
  20. class RenderComponent : public Component<0> {
  21. public:
  22. virtual ~RenderComponent() { }
  23.  
  24. public:
  25. uint32_t m_vboID;
  26. };
  27.  
  28. class PhysicsComponent : public Component<1> {
  29. public:
  30. virtual ~PhysicsComponent() { }
  31.  
  32. public:
  33. float m_Speed;
  34. float m_Size;
  35. };
  36.  
  37. class PositionComponent : public Component<2> {
  38. public:
  39. virtual ~PositionComponent() { }
  40.  
  41. public:
  42. float m_X, m_Y, m_Z;
  43. };
  44.  
  45. class Entity {
  46. public:
  47. Entity()
  48. : m_Mask(NULL)
  49. {
  50.  
  51. }
  52.  
  53. virtual ~Entity() { }
  54.  
  55. public:
  56. void AddComponent(IComponent* component)
  57. {
  58. m_Mask |= component->GetID();
  59. m_Components.push_back(component);
  60. }
  61.  
  62. public:
  63. uint32_t m_ID;
  64. uint32_t m_Mask;
  65. std::vector<IComponent*> m_Components;
  66. };
  67.  
  68. class IProcessor {
  69. public:
  70. virtual void Process(std::vector<Entity*>& entities) = 0;
  71. virtual uint32_t GetMask() = 0;
  72. };
  73.  
  74. template <class Type, uint32_t Mask>
  75. class Processor {
  76. public:
  77. virtual ~Processor() { }
  78.  
  79. public:
  80. virtual void Process(std::vector<Entity*>& entities)
  81. {
  82. for (auto entity : entities)
  83. {
  84. if ((entity->m_Mask & Mask) == Mask)
  85. {
  86. static_cast<Type*>(this)->Process(*entity);
  87. }
  88. }
  89. }
  90. };
  91.  
  92. class PhysicsProcessor : public Processor<PhysicsProcessor, PhysicsComponent::TypeID | PositionComponent::TypeID> {
  93. public:
  94. virtual ~PhysicsProcessor() { }
  95.  
  96. public:
  97. virtual void Process(Entity& entity)
  98. {
  99. printf("PhysicsAndMovement %d\n", entity.m_ID);
  100. }
  101. };
  102.  
  103. class EntityManager {
  104. public:
  105. virtual ~EntityManager() { }
  106.  
  107. public:
  108. void Update(float delta)
  109. {
  110. for (auto processor : m_Processors)
  111. {
  112. processor->Process(m_Entities);
  113. }
  114. }
  115.  
  116. Entity* CreateEntity()
  117. {
  118. Entity* nEntity = new Entity();
  119. nEntity->m_ID = m_Entities.size() + 1;
  120. m_Entities.push_back(nEntity);
  121. return nEntity;
  122. }
  123.  
  124. private:
  125. std::vector<IProcessor*> m_Processors;
  126. std::vector<Entity*> m_Entities;
  127. };
  128.  
  129. int main(int argc, char* argv[])
  130. {
  131. EntityManager manager;
  132. Entity* entityA = manager.CreateEntity();
  133.  
  134. PhysicsComponent pC;
  135. pC.m_Size = 2.0f;
  136. pC.m_Speed = 1.0f;
  137.  
  138. RenderComponent rC;
  139. rC.m_vboID = 5;
  140.  
  141. PositionComponent psC;
  142. psC.m_X = 23.5f;
  143. psC.m_Y = 1.0f;
  144. psC.m_Z = 500.2f;
  145.  
  146. entityA->AddComponent(&pC);
  147. entityA->AddComponent(&rC);
  148. entityA->AddComponent(&psC);
  149. std::cout << entityA->m_Mask << std::endl;
  150.  
  151. manager.Update(16.666f);
  152.  
  153. system("PAUSE");
  154. return EXIT_SUCCESS;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement