parent
7936b0b333
commit
d4455997ea
3 changed files with 147 additions and 141 deletions
@ -0,0 +1,146 @@ |
||||
#ifndef KST_DGS_REGISTRY_HPP |
||||
#define KST_DGS_REGISTRY_HPP |
||||
|
||||
#include <memory> |
||||
#include <unordered_set> |
||||
#include <ranges> |
||||
#include <unordered_map> |
||||
#include <boost/functional/hash.hpp> |
||||
|
||||
#include "core.hpp" |
||||
#include "components/component.hpp" |
||||
#include "systems/system.hpp" |
||||
#include "entity.hpp" |
||||
|
||||
namespace kst::dgs { |
||||
template<components::Component C> |
||||
using component_collection = std::unordered_map<std::shared_ptr<dgs::entity>, |
||||
std::shared_ptr<C>>; |
||||
namespace { |
||||
inline std::unordered_set<std::shared_ptr<dgs::entity>> entities{}; |
||||
inline std::unordered_set<std::shared_ptr<dgs::entity>> dead_entities{}; |
||||
inline dgs::identifier component_count = 0; |
||||
} |
||||
|
||||
template<components::Component C> |
||||
auto get_id() { |
||||
static identifier id = ++component_count; |
||||
return id; |
||||
} |
||||
|
||||
template<components::Component C> |
||||
auto get() { |
||||
static auto components = std::make_shared<component_collection<C>>(); |
||||
if (!dead_entities.empty()) |
||||
{ |
||||
for (auto entity: dead_entities) |
||||
{ |
||||
if (entity->test_signature(get_id<C>())) |
||||
{ |
||||
entity->reset_signature(get_id<C>()); |
||||
components->erase(entity); |
||||
if (entity->empty_signature() && entity.unique()) |
||||
dead_entities.erase(entity); |
||||
} |
||||
} |
||||
} |
||||
return components; |
||||
} |
||||
|
||||
template<systems::System S> |
||||
auto get() { |
||||
static auto system = std::make_shared<S>(); |
||||
return system; |
||||
} |
||||
|
||||
template<components::Component C> |
||||
void add() { |
||||
get<C>(); |
||||
} |
||||
|
||||
auto create_entity() { |
||||
auto entity = std::make_shared<dgs::entity>(); |
||||
entities.insert(entity); |
||||
return entity; |
||||
}; |
||||
|
||||
template<components::Component C, typename...Args> |
||||
auto create_component(Args...args) { |
||||
auto component = std::make_shared<C>(std::forward<Args>(args)...); |
||||
return component; |
||||
}; |
||||
|
||||
template<systems::System S, components::Component C> |
||||
auto add_dependency() { |
||||
auto system = get<S>(); |
||||
system->set_signature(get_id<C>()); |
||||
} |
||||
|
||||
template<systems::System S, components::Component C> |
||||
auto add_dependency(std::shared_ptr<S> system) { |
||||
system->set_signature(get_id<C>()); |
||||
} |
||||
|
||||
template<systems::System S, components::Component...Components> |
||||
auto add_dependencies() { |
||||
auto system = get<S>(); |
||||
(add_dependency<S, Components>(system),...); |
||||
} |
||||
|
||||
template<components::Component C> |
||||
auto get_component(const std::shared_ptr<dgs::entity>& entity) { |
||||
auto components = get<C>(); |
||||
return components->find(entity)->second; |
||||
} |
||||
|
||||
template<components::Component C, typename...Args> |
||||
auto add_component(const std::shared_ptr<dgs::entity>& entity, Args...args) { |
||||
auto components = get<C>(); |
||||
entity->set_signature(get_id<C>()); |
||||
auto component_ptr = create_component<C>(std::forward<Args>(args)...); |
||||
components->insert(std::make_pair(entity, component_ptr)); |
||||
return entity; |
||||
}; |
||||
|
||||
template<components::Component C> |
||||
auto remove_component(const std::shared_ptr<dgs::entity>& entity) { |
||||
auto components = get<C>(); |
||||
entity->reset_signature(get_id<C>()); |
||||
components->erase(entity); |
||||
} |
||||
|
||||
auto remove_entity(const std::shared_ptr<dgs::entity>& entity) { |
||||
entities.erase(entity); |
||||
dead_entities.insert(entity); |
||||
} |
||||
|
||||
auto test_signature(const std::shared_ptr<dgs::systems::system>& system, |
||||
const std::shared_ptr<dgs::entity>& entity) { |
||||
for (int i = 0; i<MAX_COMPONENTS; i++) |
||||
if (system->test_signature(i)) |
||||
if (!entity->test_signature(i)) |
||||
return false; |
||||
return true; |
||||
} |
||||
|
||||
template<components::Component C> |
||||
auto test_signature(const std::shared_ptr<C>& component, |
||||
const std::shared_ptr<dgs::entity>& entity) { |
||||
for (int i = 0; i<MAX_COMPONENTS; i++) |
||||
if (component->test_signature(i)) |
||||
if (!entity->test_signature(i)) |
||||
return false; |
||||
return true; |
||||
} |
||||
|
||||
template<systems::System S> |
||||
auto get_entities() { |
||||
auto system = get<S>(); |
||||
std::vector<std::shared_ptr<dgs::entity>> es; |
||||
std::copy_if(entities.begin(), entities.end(), std::inserter(es, es.end()), [system](auto v) |
||||
{ return test_signature(system, v); }); |
||||
return es; |
||||
} |
||||
} |
||||
|
||||
#endif //KST_DGS_REGISTRY_HPP
|
@ -1,140 +0,0 @@ |
||||
#ifndef KST_DGS_REGISTRY_HPP |
||||
#define KST_DGS_REGISTRY_HPP |
||||
|
||||
#include <memory> |
||||
#include <unordered_set> |
||||
#include <ranges> |
||||
#include <unordered_map> |
||||
#include <boost/functional/hash.hpp> |
||||
|
||||
#include "core.hpp" |
||||
#include "components/component.hpp" |
||||
#include "systems/system.hpp" |
||||
#include "entity.hpp" |
||||
|
||||
namespace kst::dgs { |
||||
template<components::Component C> |
||||
using component_collection = std::unordered_map<std::shared_ptr<dgs::entity>, |
||||
std::shared_ptr<C>>; |
||||
|
||||
class registry { |
||||
static inline std::unordered_set<std::shared_ptr<dgs::entity>> _entities{}; |
||||
static inline std::unordered_set<std::shared_ptr<dgs::entity>> _dead_entities{}; |
||||
static inline dgs::identifier _component_count = 0; |
||||
public: |
||||
template<components::Component C> |
||||
static auto get() { |
||||
static auto components = std::make_shared<component_collection<C>>(); |
||||
if (!_dead_entities.empty()) |
||||
{ |
||||
for (auto entity: _dead_entities) |
||||
{ |
||||
if (entity->test_signature(get_id<C>())) |
||||
{ |
||||
entity->reset_signature(get_id<C>()); |
||||
components->erase(entity); |
||||
if (entity->empty_signature() && entity.unique()) |
||||
_dead_entities.erase(entity); |
||||
} |
||||
} |
||||
} |
||||
return components; |
||||
} |
||||
|
||||
template<systems::System S> |
||||
static auto get() { |
||||
static auto system = std::make_shared<S>(); |
||||
return system; |
||||
} |
||||
|
||||
template<components::Component C> |
||||
static void add() { |
||||
get<C>(); |
||||
} |
||||
|
||||
template<components::Component C> |
||||
static identifier get_id() { |
||||
static identifier id = _component_count++; |
||||
return id; |
||||
} |
||||
|
||||
static auto create_entity() { |
||||
auto entity = std::make_shared<dgs::entity>(); |
||||
_entities.insert(entity); |
||||
return entity; |
||||
}; |
||||
|
||||
template<components::Component C, typename...Args> |
||||
static auto create_component(Args...args) { |
||||
auto component = std::make_shared<C>(std::forward<Args>(args)...); |
||||
return component; |
||||
}; |
||||
|
||||
template<systems::System S> |
||||
static auto add_dependencies() { } |
||||
|
||||
template<systems::System S, components::Component C, components::Component...Components> |
||||
static auto add_dependencies() { |
||||
auto system = get<S>(); |
||||
system->set_signature(get_id<C>()); |
||||
add_dependencies<S, Components...>(); |
||||
} |
||||
|
||||
template<components::Component C> |
||||
static auto get_component(const std::shared_ptr<dgs::entity>& entity) { |
||||
auto components = get<C>(); |
||||
return components->find(entity)->second; |
||||
} |
||||
|
||||
template<components::Component C, typename...Args> |
||||
static auto add_component(const std::shared_ptr<dgs::entity>& entity, Args...args) { |
||||
auto components = get<C>(); |
||||
entity->set_signature(get_id<C>()); |
||||
auto component_ptr = create_component<C>(std::forward<Args>(args)...); |
||||
components->insert(std::make_pair(entity, component_ptr)); |
||||
return entity; |
||||
}; |
||||
|
||||
template<components::Component C> |
||||
static auto remove_component(const std::shared_ptr<dgs::entity>& entity) { |
||||
auto components = get<C>(); |
||||
entity->reset_signature(get_id<C>()); |
||||
components->erase(entity); |
||||
} |
||||
|
||||
static auto remove_entity(const std::shared_ptr<dgs::entity>& entity) { |
||||
_entities.erase(entity); |
||||
_dead_entities.insert(entity); |
||||
} |
||||
|
||||
static auto test_signature(const std::shared_ptr<dgs::systems::system>& system, |
||||
const std::shared_ptr<dgs::entity>& entity) { |
||||
for (int i = 0; i<MAX_COMPONENTS; i++) |
||||
if (system->test_signature(i)) |
||||
if (!entity->test_signature(i)) |
||||
return false; |
||||
return true; |
||||
} |
||||
|
||||
template<components::Component C> |
||||
static auto test_signature(const std::shared_ptr<C>& component, |
||||
const std::shared_ptr<dgs::entity>& entity) { |
||||
for (int i = 0; i<MAX_COMPONENTS; i++) |
||||
if (component->test_signature(i)) |
||||
if (!entity->test_signature(i)) |
||||
return false; |
||||
return true; |
||||
} |
||||
|
||||
template<systems::System S> |
||||
static auto get_entities() { |
||||
auto system = get<S>(); |
||||
std::vector<std::shared_ptr<dgs::entity>> entities; |
||||
std::copy_if(_entities.begin(), _entities.end(), std::inserter(entities, entities.end()), [system](auto v) |
||||
{ return test_signature(system, v); }); |
||||
return entities; |
||||
} |
||||
}; |
||||
} |
||||
|
||||
#endif //KST_DGS_REGISTRY_HPP
|
Loading…
Reference in new issue