Skip to content

Commit

Permalink
IListener
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Jul 29, 2020
1 parent 0749089 commit 1c71e3f
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(UECS VERSION 0.9.6)
project(UECS VERSION 0.9.7)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand Down
4 changes: 4 additions & 0 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Ubpa {
class World;

class IListener;

// Entity Manager of World
// auto maintain Component's lifecycle ({default|copy|move} constructor, destructor)
// [API]
Expand Down Expand Up @@ -77,6 +79,8 @@ namespace Ubpa {

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

void Accept(IListener* listener) const;

private:
friend class World;
EntityMngr() = default;
Expand Down
32 changes: 32 additions & 0 deletions include/UECS/IListener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <string_view>

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

class IListener {
public:
virtual void EnterWorld(const World* w) = 0;
virtual void ExistWorld(const World* w) = 0;

virtual void EnterSystemMngr(const SystemMngr* sm) = 0;
virtual void ExistSystemMngr(const SystemMngr* sm) = 0;

virtual void EnterSystem(std::string_view s) = 0;
virtual void ExistSystem(std::string_view s) = 0;

virtual void EnterEntityMngr(const EntityMngr* em) = 0;
virtual void ExistEntityMngr(const EntityMngr* em) = 0;

virtual void EnterEntity(const Entity* e) = 0;
virtual void ExistEntity(const Entity* e) = 0;

virtual void EnterCmptPtr(const CmptPtr* cmpt) = 0;
virtual void ExistCmptPtr(const CmptPtr* cmpt) = 0;
};
}
3 changes: 3 additions & 0 deletions include/UECS/SystemMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <UContainer/xSTL/xMap.h>

namespace Ubpa{
class IListener;

// System Manager
// System is a struct with specific function
// signature: static void OnUpdate(Schedule&)
Expand All @@ -27,6 +29,7 @@ namespace Ubpa{
template<typename... Systems>
void Deregister() noexcept;

void Accept(IListener* listener) const;

private:
template<typename System>
Expand Down
4 changes: 4 additions & 0 deletions include/UECS/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <UGraphviz/UGraphviz.h>

namespace Ubpa {
class IListener;

// SystemMngr + EntityMngr
class World {
public:
Expand All @@ -29,6 +31,8 @@ namespace Ubpa {
// use RTDCmptTraits' registered component name
Graphviz::Graph GenUpdateFrameGraph() const;

void Accept(IListener* listener) const;

private:
mutable JobExecutor executor;
Schedule schedule;
Expand Down
18 changes: 18 additions & 0 deletions src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <UECS/EntityMngr.h>

#include <UECS/IListener.h>

using namespace Ubpa;
using namespace std;

Expand Down Expand Up @@ -283,3 +285,19 @@ void EntityMngr::RunCommands() {
command();
commandBuffer.clear();
}

void EntityMngr::Accept(IListener* listener) const {
listener->EnterEntityMngr(this);
for (const auto& [h, a] : h2a) {
for (size_t i = 0; i < a->EntityNum(); i++) {
auto e = a->At<Entity>(i);
listener->EnterEntity(e);
for (const auto& cmpt : a->Components(i)) {
listener->EnterCmptPtr(&cmpt);
listener->ExistCmptPtr(&cmpt);
}
listener->ExistEntity(e);
}
}
listener->ExistEntityMngr(this);
}
14 changes: 14 additions & 0 deletions src/core/SystemMngr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <UECS/SystemMngr.h>

#include <UECS/IListener.h>

using namespace Ubpa;

void SystemMngr::Accept(IListener* listener) const {
listener->EnterSystemMngr(this);
for (const auto& [n, f] : onUpdateMap) {
listener->EnterSystem(n);
listener->ExistSystem(n);
}
listener->ExistSystemMngr(this);
}
9 changes: 9 additions & 0 deletions src/core/World.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <UECS/World.h>

#include <UECS/IListener.h>

using namespace Ubpa;
using namespace std;

Expand Down Expand Up @@ -159,3 +161,10 @@ Graphviz::Graph World::GenUpdateFrameGraph() const {

return graph;
}

void World::Accept(IListener* listener) const {
listener->EnterWorld(this);
systemMngr.Accept(listener);
entityMngr.Accept(listener);
listener->ExistWorld(this);
}
6 changes: 6 additions & 0 deletions src/test/14_serialize/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Ubpa_GetTargetName(core "${PROJECT_SOURCE_DIR}/src/core")
Ubpa_AddTarget(
TEST
MODE EXE
LIB ${core}
)
162 changes: 162 additions & 0 deletions src/test/14_serialize/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include <UECS/World.h>
#include <UECS/IListener.h>

#include <iostream>

using namespace Ubpa;
using namespace std;

struct Position { float val; };
struct Velocity { float val; };

class Dumper : public IListener {
size_t indent{ 0 };
bool firstSystem{ true };
bool firstEntity{ true };
bool firstCmpt{ true };

void PrintIndent() {
for (size_t i = 0; i < indent; i++)
cout << " ";
}

// new line
virtual void EnterWorld(const World* w) override {
cout << "{" << endl;
indent++;
PrintIndent();
cout << "\"type\" : \"World\"," << endl;
}

virtual void ExistWorld(const World* w) override {
indent--;
cout << "}";
}

virtual void EnterSystemMngr(const SystemMngr* sm) override {
PrintIndent();
cout << "\"systemMngr\" : {" << endl;
indent++;
PrintIndent();
cout << "\"type\" : \"SystemMngr\"," << endl;
PrintIndent();
cout << "\"systems\" : [";
indent++;
firstSystem = true;
}

virtual void ExistSystemMngr(const SystemMngr* sm) override {
cout << endl;
indent--;
PrintIndent();
cout << "]" << endl;
indent--;
PrintIndent();
cout << "}," << endl;
}

virtual void EnterSystem(std::string_view s) override {
if (!firstSystem)
cout << ",";
else
firstSystem = false;
cout << endl;
PrintIndent();
cout << "\"" << s << "\"";
}

virtual void ExistSystem(std::string_view s) override {

}

virtual void EnterEntityMngr(const EntityMngr* em) override {
PrintIndent();
cout << "\"entityMngr\" : {" << endl;
indent++;
PrintIndent();
cout << "\"type\" : \"EntityMngr\"," << endl;
PrintIndent();
cout << "\"entities\" : [";
indent++;
firstEntity = true;
}

virtual void ExistEntityMngr(const EntityMngr* em) override {
cout << endl;
indent--;
PrintIndent();
cout << "]" << endl;
indent--;
PrintIndent();
cout << "}" << endl;
}

virtual void EnterEntity(const Entity* e) override {
if (!firstEntity)
cout << ",";
else
firstEntity = false;
cout << endl;
PrintIndent();
cout << "{" << endl;
indent++;
PrintIndent();
cout << "\"type\" : \"Entity\"," << endl;
PrintIndent();
cout << "\"components\" : [" << endl;
indent++;
firstCmpt = true;
}

virtual void ExistEntity(const Entity* e) override {
cout << endl;
indent--;
PrintIndent();
cout << "]" << endl;
indent--;
PrintIndent();
cout << "}";
}

virtual void EnterCmptPtr(const CmptPtr* cmpt) override {
if (!firstCmpt)
cout << ",";
else
firstCmpt = false;
cout << endl;
PrintIndent();
cout << "{" << endl;
indent++;
PrintIndent();
cout << "\"type\" : \"" << RTDCmptTraits::Instance().Nameof(cmpt->Type()) << "\"";
}

virtual void ExistCmptPtr(const CmptPtr* cmpt) override {
cout << endl;
indent--;
PrintIndent();
cout << "}";
}
};

struct MoverSystem {
static void OnUpdate(Schedule& schedule) {
schedule.Register(
[](const Velocity* v, Position* p) {
p->val += v->val;
}, "Mover");
}
};

int main() {
World w;
RTDCmptTraits::Instance().Register<Position, Velocity>();
w.systemMngr.Register<MoverSystem>();
w.entityMngr.Create<Position, Velocity>();
w.entityMngr.Create<Position>();
w.entityMngr.Create<Velocity>();
w.entityMngr.Create<>();
w.Update();
Dumper dumper;
w.Accept(&dumper);
}

0 comments on commit 1c71e3f

Please sign in to comment.