Skip to content

Commit

Permalink
Archetype static pool -> EntityMngr pool
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Aug 12, 2020
1 parent 7369f4a commit e2d4964
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
11 changes: 7 additions & 4 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ namespace Ubpa::UECS {

private:
friend class World;
friend class Archetype;

EntityMngr() = default;

static bool IsSet(const CmptType* types, size_t num) noexcept;

const std::set<Archetype*>& QueryArchetypes(const EntityQuery& query) const;

template<typename... Cmpts>
Archetype* GetOrCreateArchetypeOf();
// types not contain Entity
Expand All @@ -93,6 +93,9 @@ namespace Ubpa::UECS {
void AttachWithoutInit(Entity);
void AttachWithoutInit(Entity, const CmptType* types, size_t num);

const std::set<Archetype*>& QueryArchetypes(const EntityQuery& query) const;
mutable std::unordered_map<EntityQuery, std::set<Archetype*>> queryCache;

void GenEntityJob(World*, Job*, SystemFunc*) const;
void GenChunkJob(World*, Job*, SystemFunc*) const;
void GenJob(World*, Job*, SystemFunc*) const;
Expand All @@ -108,8 +111,8 @@ namespace Ubpa::UECS {
void RecycleEntityEntry(Entity e);

std::unordered_map<CmptTypeSet, std::unique_ptr<Archetype>> ts2a; // archetype's CmptTypeSet to archetype
mutable std::unordered_map<EntityQuery, std::set<Archetype*>> queryCache;

Pool<Chunk> sharedChunkPool;
};
}

Expand Down
15 changes: 6 additions & 9 deletions include/UECS/detail/Archetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include <UTemplate/Typelist.h>
#include <UTemplate/TypeID.h>

#include <UContainer/Pool.h>

#include <map>

namespace Ubpa::UECS {
Expand All @@ -25,10 +23,12 @@ namespace Ubpa::UECS {
// argument TypeList<Cmpts...> is for type deduction
// auto add Entity
template<typename... Cmpts>
Archetype(TypeList<Cmpts...>);
Archetype(EntityMngr*, TypeList<Cmpts...>);

~Archetype();

// auto add Entity, use RTDCmptTraits
static Archetype* New(const CmptType* types, size_t num);
static Archetype* New(EntityMngr*, const CmptType* types, size_t num);

// auto add Entity
template<typename... Cmpts>
Expand All @@ -38,8 +38,6 @@ namespace Ubpa::UECS {
// auto add Entity
static Archetype* Remove(const Archetype* from, const CmptType* types, size_t num);

~Archetype();

// Entity + Components
std::tuple<std::vector<Entity*>, std::vector<std::vector<CmptPtr>>, std::vector<size_t>>
Locate(const CmptLocator& locator) const;
Expand Down Expand Up @@ -94,7 +92,7 @@ namespace Ubpa::UECS {
static CmptTypeSet GenCmptTypeSet();

private:
Archetype() = default;
Archetype(EntityMngr* entityMngr) : entityMngr{ entityMngr } {}

// set type2alignment
// call after setting type2size and type2offset
Expand All @@ -104,6 +102,7 @@ namespace Ubpa::UECS {
static bool NotContainEntity(const CmptType* types, size_t num) noexcept;

friend class EntityMngr;
EntityMngr* entityMngr;

CmptTypeSet types; // Entity + Components
RTSCmptTraits cmptTraits;
Expand All @@ -113,8 +112,6 @@ namespace Ubpa::UECS {
std::vector<Chunk*> chunks;

size_t entityNum{ 0 }; // number of entities

inline static Pool<Chunk> sharedChunkPool; // no lock
};
}

Expand Down
8 changes: 5 additions & 3 deletions include/UECS/detail/Archetype.inl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Ubpa::UECS {
template<typename... Cmpts>
Archetype::Archetype(TypeList<Cmpts...>)
: types(GenCmptTypeSet<Cmpts...>())
Archetype::Archetype(EntityMngr* entityMngr, TypeList<Cmpts...>)
:
entityMngr{ entityMngr },
types(GenCmptTypeSet<Cmpts...>())
{
static_assert(IsSet_v<TypeList<Entity, Cmpts...>>,
"Archetype::Archetype: <Cmpts>... must be different");
Expand All @@ -19,7 +21,7 @@ namespace Ubpa::UECS {
static_assert(sizeof...(Cmpts) > 0);
assert(((!from->types.Contains(CmptType::Of<Cmpts>)) &&...));

Archetype* rst = new Archetype;
Archetype* rst = new Archetype{ from->entityMngr };

rst->types = from->types;
rst->types.data.insert(CmptType::Of<Cmpts>...);
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/detail/EntityMngr.inl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Ubpa::UECS {
if(target != ts2a.end())
return target->second.get();

auto archetype = new Archetype(TypeList<Cmpts...>{});
auto archetype = new Archetype(this, TypeList<Cmpts...>{});
ts2a.emplace(std::move(typeset), std::unique_ptr<Archetype>{ archetype });
for (auto& [query, archetypes] : queryCache) {
if (archetype->GetCmptTypeSet().IsMatch(query))
Expand Down
18 changes: 10 additions & 8 deletions src/core/Archetype.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <UECS/detail/Archetype.h>

#include <UECS/EntityMngr.h>

using namespace Ubpa::UECS;
using namespace std;

Expand All @@ -15,7 +17,7 @@ Archetype::~Archetype() {
}
}
for (Chunk* chunk : chunks)
sharedChunkPool.Recycle(chunk);
entityMngr->sharedChunkPool.Recycle(chunk);
}

void Archetype::SetLayout() {
Expand All @@ -41,10 +43,10 @@ void Archetype::SetLayout() {
type2offset.emplace(type, layout.offsets[i++]);
}

Archetype* Archetype::New(const CmptType* types, size_t num) {
Archetype* Archetype::New(EntityMngr* entityMngr, const CmptType* types, size_t num) {
assert(NotContainEntity(types, num));

auto rst = new Archetype;
auto rst = new Archetype{ entityMngr };
rst->types.Insert(types, num);
rst->types.data.insert(CmptType::Of<Entity>);
rst->cmptTraits.Register<Entity>();
Expand All @@ -58,7 +60,7 @@ Archetype* Archetype::Add(const Archetype* from, const CmptType* types, size_t n
assert(NotContainEntity(types, num));
assert(!from->types.ContainsAny(types, num));

Archetype* rst = new Archetype;
Archetype* rst = new Archetype{ from->entityMngr };

rst->types = from->types;
rst->cmptTraits = from->cmptTraits;
Expand All @@ -73,9 +75,9 @@ Archetype* Archetype::Add(const Archetype* from, const CmptType* types, size_t n

Archetype* Archetype::Remove(const Archetype* from, const CmptType* types, size_t num) {
assert(NotContainEntity(types, num));
assert(from->types.Contains(types, num));
assert(from->types.ContainsAny(types, num));

Archetype* rst = new Archetype;
Archetype* rst = new Archetype{ from->entityMngr };

rst->types = from->types;
rst->cmptTraits = from->cmptTraits;
Expand Down Expand Up @@ -116,7 +118,7 @@ size_t Archetype::Create(Entity e) {

size_t Archetype::RequestBuffer() {
if (entityNum == chunks.size() * chunkCapacity) {
auto chunk = sharedChunkPool.Request();
auto chunk = entityMngr->sharedChunkPool.Request();
chunks.push_back(chunk);
}
return entityNum++;
Expand Down Expand Up @@ -238,7 +240,7 @@ size_t Archetype::Erase(size_t idx) {

if (chunks.size() * chunkCapacity - entityNum >= chunkCapacity) {
Chunk* chunk = chunks.back();
sharedChunkPool.Recycle(chunk);
entityMngr->sharedChunkPool.Recycle(chunk);
chunks.pop_back();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Archetype* EntityMngr::GetOrCreateArchetypeOf(const CmptType* types, size_t num)
if (target != ts2a.end())
return target->second.get();

auto archetype = Archetype::New(types, num);
auto archetype = Archetype::New(this, types, num);

ts2a.emplace(std::move(typeset), std::unique_ptr<Archetype>{ archetype });
for (auto& [query, archetypes] : queryCache) {
Expand Down

0 comments on commit e2d4964

Please sign in to comment.