Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ENTITY ITERATOR.H
- #include "Entity.h"
- #include "Scene.h"
- class EntityIterator
- {
- public:
- EntityIterator(Scene* scene, size_t index, bool bIsEnd, bool bIncludePendingDestroy);
- size_t GetIndex() const
- {
- return m_index;
- }
- bool IsEnd() const;
- bool IncludePendingDestroy() const
- {
- return m_bIncludePendingDestroy;
- }
- Scene* GetScene() const
- {
- return m_scene;
- }
- Entity* Get() const;
- Entity* operator*() const
- {
- return Get();
- }
- bool operator==(const EntityIterator& other) const;
- bool operator!=(const EntityIterator& other) const;
- EntityIterator& operator++();
- private:
- bool m_bIsEnd = false;
- size_t m_index;
- Scene* m_scene;
- bool m_bIncludePendingDestroy;
- };
- _____________________________________________________________________________________________________________________________________
- SCENE.H
- #pragma once
- #include <unordered_map>
- #include <functional>
- #include <vector>
- #include <algorithm>
- #include <stdint.h>
- #include <type_traits>
- #include "Component.h"
- #include "Entity.h"
- #include "Allocators.h"
- #include "EntityIterator.h"
- namespace VE {
- class Scene
- {
- public:
- Scene();
- Scene(Allocator alloc)
- {
- }
- std::vector<Entity*, EntityPtrAllocator>* GetEntities() { return &m_entities; }
- EntityAllocator& GetPrimaryAllocator()
- {
- return m_entityAlloc;
- }
- template<typename... Types>
- void Each(typename std::common_type<std::function<void(Entity*, ComponentHandle<Types>...)>>::type viewFunc, bool bIncludePendingDestroy = false);
- void All(std::function<void(Entity*)> viewFunc, bool bIncludePendingDestroy = false);
- EntityView All(bool bIncludePendingDestroy = false);
- template<typename... Types>
- EntityComponentView<Types...> Each(bool bIncludePendingDestroy = false)
- {
- EntityComponentIterator<Types...> first(this, 0, false, bIncludePendingDestroy);
- EntityComponentIterator<Types...> last(this, GetEntityCount(), true, bIncludePendingDestroy);
- return Internal::EntityComponentView<Types...>(first, last);
- }
- size_t GetEntityCount() const
- {
- return m_entities.size();
- }
- Entity* GetEntityByIndex(size_t idx)
- {
- if (idx >= GetEntityCount())
- return nullptr;
- return m_entities[idx];
- }
- size_t GetLastEntityId() { return m_lastEntityId; }
- void IncrementEntityId() { ++m_lastEntityId; }
- Entity* GetById(size_t id) const;
- ~Scene();
- //Extended to have graphics(Initialize display), physics(RIGID BODY WORLD such as Gravity) and other properties(Scene title, scene size, etc) (DATA ONLY)
- private:
- std::vector<Entity*, EntityPtrAllocator> m_entities;
- EntityAllocator m_entityAlloc;
- size_t m_lastEntityId = 0;
- };
- _____________________________________________________________________________________________________________________________________
- Entity.h
- #pragma once
- #include "Scene.h"
- #include "Component.h"
- #include <unordered_map>
- namespace VE {
- class Entity
- {
- public:
- Entity();
- ~Entity();
- Entity(Scene* scene, size_t id);
- const static size_t InvalidEntityId = 0;
- Scene* GetScene() const
- {
- return m_scene;
- }
- size_t GetEntityId() const
- {
- return m_id;
- }
- void RemoveAll();
- template<typename T>
- bool Has() const
- {
- auto index = GetTypeIndex<T>();
- return m_components.find(index) != m_components.end();
- }
- template<typename T, typename V, typename... Types>
- bool Has() const
- {
- return Has<T>() && Has<V, Types...>();
- }
- template<typename... Types>
- bool With(typename std::common_type<std::function<void(ComponentHandle<Types>...)>>::type view)
- {
- if (!Has<Types...>())
- return false;
- view(Get<Types>()...);
- return true;
- }
- template<typename T, typename... Args>
- ComponentHandle<T> Assign(Args&&... args);
- template<typename T>
- ComponentHandle<T> Get();
- template<typename T>
- bool remove()
- {
- auto found = m_components.find(getTypeIndex<T>());
- if (found != m_components.end())
- {
- found->second->removed(this);
- found->second->destroy(m_scene);
- components.erase(found);
- return true;
- }
- return false;
- }
- bool IsPendingDestroy() const
- {
- return m_pendingDestroy;
- }
- private:
- std::unordered_map<TypeIndex, BaseComponentContainer*> m_components;
- class Scene* m_scene;
- size_t m_id;
- bool m_pendingDestroy = false;
- };
- template<typename T, typename... Args>
- inline ComponentHandle<T> Entity::Assign(Args&&... args)
- {
- using ComponentAllocator = std::allocator_traits<EntityAllocator>::template rebind_alloc<ComponentContainer<T>>;
- auto found = m_components.find(GetTypeIndex<T>());
- if (found != m_components.end())
- {
- ComponentContainer<T>* container = reinterpret_cast<ComponentContainer<T>*>(found->second);
- container->data = T(args...);
- auto handle = ComponentHandle<T>(&container->data);
- return handle;
- }
- else
- {
- ComponentAllocator alloc(SceneManager::Get().GetScene()->GetPrimaryAllocator());
- ComponentContainer<T>* container = std::allocator_traits<ComponentAllocator>::allocate(alloc, 1);
- std::allocator_traits<ComponentAllocator>::construct(alloc, container, T(args...));
- m_components.insert({ GetTypeIndex<T>(), container });
- auto handle = ComponentHandle<T>(&container->data);
- return handle;
- }
- }
- template<typename T>
- inline ComponentHandle<T> Entity::Get()
- {
- auto found = m_components.find(GetTypeIndex<T>());
- if (found != m_components.end())
- {
- return ComponentHandle<T>(&reinterpret_cast<ComponentContainer<T>*>(found->second)->data);
- }
- return ComponentHandle<T>();
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement