Skip to content

Commit

Permalink
store EntityMngr and SystemMngr instead World in Schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed May 13, 2020
1 parent 53e5f26 commit f6796d2
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 45 deletions.
9 changes: 9 additions & 0 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <UContainer/Pool.h>

#include <mutex>

namespace Ubpa {
class World;

Expand Down Expand Up @@ -37,6 +39,8 @@ namespace Ubpa {

size_t EntityNum(const EntityQuery& query) const;

void AddCommand(const std::function<void()>& command);

private:
friend class World;
EntityMngr() = default;
Expand Down Expand Up @@ -65,6 +69,11 @@ namespace Ubpa {
std::unordered_map<size_t, Archetype*> h2a; // archetype's hashcode to archetype

mutable std::unordered_map<EntityQuery, std::set<Archetype*>> queryCache;

// command
std::vector<std::function<void()>> commandBuffer;
std::mutex commandBufferMutex;
void RunCommands();
};
}

Expand Down
11 changes: 7 additions & 4 deletions include/UECS/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#include <map>

namespace Ubpa {
class World;
class EntityMngr;
class SystemMngr;

class Schedule {
public:
Expand All @@ -23,7 +24,8 @@ namespace Ubpa {
// if sys is not register, return static_cast<size_t>(-1)
size_t EntityNumInQuery(std::string_view sys) const;

World* GetWorld() const noexcept { return world; }
EntityMngr* GetEntityMngr() const noexcept { return entityMngr; }
SystemMngr* GetSystemMngr() const noexcept { return systemMngr; }

Schedule& Order(std::string_view x, std::string_view y);

Expand All @@ -41,7 +43,7 @@ namespace Ubpa {
template<typename Cmpt> Schedule& EraseNone(std::string_view sys) { return EraseNone(sys, CmptType::Of<Cmpt>()); }

private:
Schedule(World* world) : world{ world } {}
Schedule(EntityMngr* entityMngr, SystemMngr* systemMngr) : entityMngr{ entityMngr }, systemMngr{ systemMngr }{}
void Clear();
SysFuncGraph GenSysFuncGraph() const;

Expand Down Expand Up @@ -82,7 +84,8 @@ namespace Ubpa {
std::unordered_map<size_t, FilterChange> sysFilterChange;

Pool<SystemFunc> sysFuncPool;
World* world;
EntityMngr* entityMngr;
SystemMngr* systemMngr;
friend class World;
};
}
13 changes: 2 additions & 11 deletions include/UECS/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
#include "SystemMngr.h"
#include "EntityMngr.h"

#include <mutex>

namespace Ubpa {
class World {
public:
World() : schedule{ this } {}
World() : schedule{ &entityMngr, &systemMngr } {}

SystemMngr systemMngr;
EntityMngr entityMngr;
Expand All @@ -19,9 +17,7 @@ namespace Ubpa {
// Commands, one-by-one
void Update();

std::string DumpUpdateJobGraph();

void AddCommand(const std::function<void()>& command);
std::string DumpUpdateJobGraph() const;

private:
mutable JobExecutor executor;
Expand All @@ -30,10 +26,5 @@ namespace Ubpa {
Job jobGraph;
std::vector<Job*> jobs;
Pool<Job> jobPool;

// command
std::vector<std::function<void()>> commandBuffer;
std::mutex commandBufferMutex;
void RunCommands();
};
}
2 changes: 1 addition & 1 deletion include/UECS/detail/Archetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace Ubpa {

CmptTypeSet types; // Entity + Components
RuntimeCmptTraits cmptTraits;
std::map<CmptType, size_t> type2offset; // CmptType to offset in chunk (include Entity)
std::unordered_map<CmptType, size_t> type2offset; // CmptType to offset in chunk (include Entity)

size_t chunkCapacity{ static_cast<size_t>(-1) };
std::vector<Chunk*> chunks;
Expand Down
12 changes: 12 additions & 0 deletions src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,15 @@ void EntityMngr::GenJob(Job* job, SystemFunc* sys) const {
indexOffsetInQuery += num;
}
}

void EntityMngr::AddCommand(const function<void()>& command) {
lock_guard<mutex> guard(commandBufferMutex);
commandBuffer.push_back(command);
}

void EntityMngr::RunCommands() {
lock_guard<mutex> guard(commandBufferMutex);
for (const auto& command : commandBuffer)
command();
commandBuffer.clear();
}
2 changes: 1 addition & 1 deletion src/core/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ size_t Schedule::EntityNumInQuery(std::string_view sys) const {
if (target == sysFuncs.end())
return static_cast<size_t>(-1);
auto func = target->second;
return world->entityMngr.EntityNum(func->query);
return entityMngr->EntityNum(func->query);
}

Schedule& Schedule::InsertAll(string_view sys, CmptType type) {
Expand Down
16 changes: 2 additions & 14 deletions src/core/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,9 @@ void World::Update() {

executor.run(jobGraph).wait();

RunCommands();
entityMngr.RunCommands();
}

void World::AddCommand(const function<void()>& command) {
lock_guard<mutex> guard(commandBufferMutex);
commandBuffer.push_back(command);
}

void World::RunCommands() {
lock_guard<mutex> guard(commandBufferMutex);
for (const auto& command : commandBuffer)
command();
commandBuffer.clear();
}

string World::DumpUpdateJobGraph() {
string World::DumpUpdateJobGraph() const {
return jobGraph.dump();
}
20 changes: 10 additions & 10 deletions src/test/03_query_entity/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ struct C {};
struct MySystem {
static void OnUpdate(Schedule& schedule) {
schedule.Request(
[w = schedule.GetWorld()](Entity e, const A* a, const B* b) {
w->AddCommand(
[e, w]() {
if (!w->entityMngr.Have<C>(e)) {
[em = schedule.GetEntityMngr()](Entity e, const A* a, const B* b) {
em->AddCommand(
[e, em]() {
if (!em->Have<C>(e)) {
cout << "Attach C" << endl;
w->entityMngr.Attach<C>(e);
em->Attach<C>(e);
}
}
);
}, "AB"
);
schedule.Request(
[w = schedule.GetWorld()](Entity e, const A* a, const B* b, const C* c) {
w->AddCommand(
[e, w]() {
if (w->entityMngr.Have<C>(e)) {
[em = schedule.GetEntityMngr()](Entity e, const A* a, const B* b, const C* c) {
em->AddCommand(
[e, em]() {
if (em->Have<C>(e)) {
cout << "Dettach C" << endl;
w->entityMngr.Detach<C>(e);
em->Detach<C>(e);
}
}
);
Expand Down
7 changes: 3 additions & 4 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

### important

- [x] ~~EntityData: version~~
- [x] CmptIDSet hash
- [x] Entity += version
- [x] CmptTypeSet hash
- [x] CmptType
- [x] EntityQuery
- [x] Query Entity
- [x] ~~SystemMngr += ScheduleRegistrar~~
- [x] alignment
- [x] `Entity` alias table: `Entity::idx -> (Archetype, idx)`
- [x] `Entity` as special `Component` stored in `Chunk`
Expand Down Expand Up @@ -43,5 +42,5 @@
- [ ] parallel `Schedule`
- [x] `constexpr SystemFunc::HashCode( )`
- [x] cache `CmptIDSet`'s hashcode
- [ ] store `EntityMngr` instead `World` in `Schedule`
- [x] store `EntityMngr` and `SystemMngr` instead `World` in `Schedule`

0 comments on commit f6796d2

Please sign in to comment.