Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. ENTITY ITERATOR.H
  2. #include "Entity.h"
  3. #include "Scene.h"
  4.  
  5. class EntityIterator
  6. {
  7. public:
  8. EntityIterator(Scene* scene, size_t index, bool bIsEnd, bool bIncludePendingDestroy);
  9.  
  10. size_t GetIndex() const
  11. {
  12. return m_index;
  13. }
  14.  
  15. bool IsEnd() const;
  16.  
  17. bool IncludePendingDestroy() const
  18. {
  19. return m_bIncludePendingDestroy;
  20. }
  21.  
  22. Scene* GetScene() const
  23. {
  24. return m_scene;
  25. }
  26.  
  27. Entity* Get() const;
  28.  
  29. Entity* operator*() const
  30. {
  31. return Get();
  32. }
  33.  
  34. bool operator==(const EntityIterator& other) const;
  35.  
  36. bool operator!=(const EntityIterator& other) const;
  37. EntityIterator& operator++();
  38.  
  39. private:
  40. bool m_bIsEnd = false;
  41. size_t m_index;
  42. Scene* m_scene;
  43. bool m_bIncludePendingDestroy;
  44. };
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. _____________________________________________________________________________________________________________________________________
  52. SCENE.H
  53.  
  54. #pragma once
  55. #include <unordered_map>
  56. #include <functional>
  57. #include <vector>
  58. #include <algorithm>
  59. #include <stdint.h>
  60. #include <type_traits>
  61.  
  62. #include "Component.h"
  63. #include "Entity.h"
  64. #include "Allocators.h"
  65. #include "EntityIterator.h"
  66.  
  67. namespace VE {
  68. class Scene
  69. {
  70. public:
  71.  
  72.  
  73. Scene();
  74. Scene(Allocator alloc)
  75. {
  76.  
  77. }
  78.  
  79. std::vector<Entity*, EntityPtrAllocator>* GetEntities() { return &m_entities; }
  80.  
  81. EntityAllocator& GetPrimaryAllocator()
  82. {
  83. return m_entityAlloc;
  84. }
  85.  
  86. template<typename... Types>
  87. void Each(typename std::common_type<std::function<void(Entity*, ComponentHandle<Types>...)>>::type viewFunc, bool bIncludePendingDestroy = false);
  88.  
  89. void All(std::function<void(Entity*)> viewFunc, bool bIncludePendingDestroy = false);
  90. EntityView All(bool bIncludePendingDestroy = false);
  91.  
  92. template<typename... Types>
  93. EntityComponentView<Types...> Each(bool bIncludePendingDestroy = false)
  94. {
  95. EntityComponentIterator<Types...> first(this, 0, false, bIncludePendingDestroy);
  96. EntityComponentIterator<Types...> last(this, GetEntityCount(), true, bIncludePendingDestroy);
  97. return Internal::EntityComponentView<Types...>(first, last);
  98. }
  99.  
  100.  
  101. size_t GetEntityCount() const
  102. {
  103. return m_entities.size();
  104. }
  105.  
  106. Entity* GetEntityByIndex(size_t idx)
  107. {
  108. if (idx >= GetEntityCount())
  109. return nullptr;
  110.  
  111. return m_entities[idx];
  112. }
  113.  
  114. size_t GetLastEntityId() { return m_lastEntityId; }
  115.  
  116. void IncrementEntityId() { ++m_lastEntityId; }
  117.  
  118. Entity* GetById(size_t id) const;
  119.  
  120. ~Scene();
  121.  
  122. //Extended to have graphics(Initialize display), physics(RIGID BODY WORLD such as Gravity) and other properties(Scene title, scene size, etc) (DATA ONLY)
  123. private:
  124. std::vector<Entity*, EntityPtrAllocator> m_entities;
  125. EntityAllocator m_entityAlloc;
  126. size_t m_lastEntityId = 0;
  127.  
  128. };
  129.  
  130. _____________________________________________________________________________________________________________________________________
  131.  
  132. Entity.h
  133.  
  134.  
  135. #pragma once
  136. #include "Scene.h"
  137. #include "Component.h"
  138. #include <unordered_map>
  139.  
  140. namespace VE {
  141.  
  142. class Entity
  143. {
  144. public:
  145. Entity();
  146. ~Entity();
  147. Entity(Scene* scene, size_t id);
  148.  
  149.  
  150. const static size_t InvalidEntityId = 0;
  151.  
  152. Scene* GetScene() const
  153. {
  154. return m_scene;
  155. }
  156.  
  157. size_t GetEntityId() const
  158. {
  159. return m_id;
  160. }
  161.  
  162. void RemoveAll();
  163.  
  164. template<typename T>
  165. bool Has() const
  166. {
  167. auto index = GetTypeIndex<T>();
  168. return m_components.find(index) != m_components.end();
  169. }
  170.  
  171. template<typename T, typename V, typename... Types>
  172. bool Has() const
  173. {
  174. return Has<T>() && Has<V, Types...>();
  175. }
  176.  
  177. template<typename... Types>
  178. bool With(typename std::common_type<std::function<void(ComponentHandle<Types>...)>>::type view)
  179. {
  180. if (!Has<Types...>())
  181. return false;
  182.  
  183. view(Get<Types>()...);
  184. return true;
  185. }
  186.  
  187. template<typename T, typename... Args>
  188. ComponentHandle<T> Assign(Args&&... args);
  189.  
  190. template<typename T>
  191. ComponentHandle<T> Get();
  192.  
  193. template<typename T>
  194. bool remove()
  195. {
  196. auto found = m_components.find(getTypeIndex<T>());
  197. if (found != m_components.end())
  198. {
  199. found->second->removed(this);
  200. found->second->destroy(m_scene);
  201.  
  202. components.erase(found);
  203.  
  204. return true;
  205. }
  206.  
  207. return false;
  208. }
  209.  
  210. bool IsPendingDestroy() const
  211. {
  212. return m_pendingDestroy;
  213. }
  214.  
  215. private:
  216. std::unordered_map<TypeIndex, BaseComponentContainer*> m_components;
  217. class Scene* m_scene;
  218. size_t m_id;
  219. bool m_pendingDestroy = false;
  220. };
  221.  
  222. template<typename T, typename... Args>
  223. inline ComponentHandle<T> Entity::Assign(Args&&... args)
  224. {
  225. using ComponentAllocator = std::allocator_traits<EntityAllocator>::template rebind_alloc<ComponentContainer<T>>;
  226.  
  227. auto found = m_components.find(GetTypeIndex<T>());
  228. if (found != m_components.end())
  229. {
  230. ComponentContainer<T>* container = reinterpret_cast<ComponentContainer<T>*>(found->second);
  231. container->data = T(args...);
  232.  
  233. auto handle = ComponentHandle<T>(&container->data);
  234.  
  235. return handle;
  236. }
  237. else
  238. {
  239. ComponentAllocator alloc(SceneManager::Get().GetScene()->GetPrimaryAllocator());
  240.  
  241. ComponentContainer<T>* container = std::allocator_traits<ComponentAllocator>::allocate(alloc, 1);
  242. std::allocator_traits<ComponentAllocator>::construct(alloc, container, T(args...));
  243.  
  244. m_components.insert({ GetTypeIndex<T>(), container });
  245.  
  246. auto handle = ComponentHandle<T>(&container->data);
  247.  
  248. return handle;
  249. }
  250. }
  251.  
  252. template<typename T>
  253. inline ComponentHandle<T> Entity::Get()
  254. {
  255. auto found = m_components.find(GetTypeIndex<T>());
  256. if (found != m_components.end())
  257. {
  258. return ComponentHandle<T>(&reinterpret_cast<ComponentContainer<T>*>(found->second)->data);
  259. }
  260.  
  261. return ComponentHandle<T>();
  262. };
  263. }
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement