Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Mar 20, 2021
1 parent 3c6ece7 commit 259579b
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 39 deletions.
27 changes: 10 additions & 17 deletions include/UECS/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@

#include "details/Util.hpp"

#include <compare>
#include <type_traits>

namespace Ubpa::UECS {
// index + version
class Entity {
public:
constexpr Entity(std::size_t idx, std::size_t version) noexcept : idx(idx), version(version) {}
constexpr Entity() noexcept : Entity{ Invalid() } {}
constexpr std::size_t Idx() const noexcept { return idx; }
constexpr std::size_t Version() const noexcept { return version; }
static constexpr Entity Invalid() noexcept { return { static_cast<std::size_t>(-1),static_cast<std::size_t>(-1) }; }
constexpr bool Valid() const noexcept { return idx != static_cast<std::size_t>(-1); }
constexpr bool operator==(const Entity& rhs) const noexcept {
return idx == rhs.idx && version == rhs.version;
}
constexpr bool operator<(const Entity& rhs) const noexcept {
return idx < rhs.idx || (idx == rhs.idx && version < rhs.version);
}
private:
friend class EntityMngr;
std::size_t idx;
struct Entity {
std::size_t index;
std::size_t version;

static constexpr Entity Invalid() noexcept { return { .index = static_cast<std::size_t>(-1), .version = static_cast<std::size_t>(-1) }; }
constexpr bool Valid() const noexcept { return index != static_cast<std::size_t>(-1); }
constexpr auto operator<=>(const Entity&) const = default;
};
static_assert(std::is_trivial_v<Entity>);
}
11 changes: 8 additions & 3 deletions include/UECS/EntityQuery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ namespace Ubpa::UECS {
std::size_t GetValue() const noexcept { return hash_combine(filter.GetValue(), locator.GetValue()); }

bool IsMatch(const small_flat_set<TypeID>& types) const noexcept{
return std::find_if_not(filter.all.begin(), filter.all.end(), [&](const auto& type) { return types.contains(type); }) == filter.all.end()
return std::find_if_not(filter.all.begin(), filter.all.end(),
[&](const auto& type) { return types.contains(type); }) == filter.all.end()
&& (filter.any.empty()
|| std::find_if(filter.any.begin(), filter.any.end(), [&](const auto& type) { return types.contains(type); }) != filter.any.end())
&& std::find_if(filter.none.begin(), filter.none.end(), [&](const auto& type) { return types.contains(type); }) == filter.none.end();
|| std::find_if(filter.any.begin(), filter.any.end(),
[&](const auto& type) { return types.contains(type); }) != filter.any.end())
&& std::find_if(filter.none.begin(), filter.none.end(),
[&](const auto& type) { return types.contains(type); }) == filter.none.end()
&& std::find_if_not(locator.AccessTypeIDs().begin(), locator.AccessTypeIDs().end(),
[&](const auto& type) { return types.contains(type); }) == locator.AccessTypeIDs().end();
}

friend bool operator==(const EntityQuery& lhs, const EntityQuery& rhs) noexcept {
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/IListener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Ubpa::UECS {
class World;
class EntityMngr;
class Entity;
struct Entity;
class CmptPtr;

class IListener {
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/details/CmptTag.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <UTemplate/Util.hpp>

namespace Ubpa::UECS {
class Entity;
struct Entity;
class CmptLocator;
class World;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/Archetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ Archetype::Archetype(std::pmr::memory_resource* rsrc, const Archetype& src)
cmptTraits = src.cmptTraits;
entityNum = src.entityNum;
chunkCapacity = src.chunkCapacity;
offsets = src.offsets;

chunks.resize(src.chunks.size(), nullptr);


if (cmptTraits.IsTrivial()) {
// [0, src.chunks.size() - 1)
for (std::size_t i = 0; i < src.chunks.size() - 1; i++) {
Expand Down Expand Up @@ -133,7 +133,7 @@ Archetype* Archetype::New(RTDCmptTraits& rtdCmptTraits, std::pmr::memory_resourc

Archetype* Archetype::Add(RTDCmptTraits& rtdCmptTraits, const Archetype* from, std::span<const TypeID> types) {
assert(std::find(types.begin(), types.end(), TypeID_of<Entity>) == types.end());
assert(std::find_if_not(types.begin(), types.end(), [&](const auto& type) { return from->cmptTraits.GetTypes().contains(type); }) == types.end());
assert(std::find_if_not(types.begin(), types.end(), [&](const auto& type) { return from->cmptTraits.GetTypes().contains(type); }) != types.end());

auto* rst = new Archetype{ from->chunkAllocator.resource() };

Expand Down Expand Up @@ -306,7 +306,7 @@ std::size_t Archetype::Erase(std::size_t idx) {
byte* src = srcBuffer + offset + srcIdxInChunk * size;

if (cmptTraits.GetTypes().data()[i].Is<Entity>())
movedIdx = reinterpret_cast<Entity*>(src)->Idx();
movedIdx = reinterpret_cast<Entity*>(src)->index;

trait.MoveAssign(dst, src);
trait.Destruct(src);
Expand Down
20 changes: 10 additions & 10 deletions src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ void EntityMngr::Clear() {
bool EntityMngr::Have(Entity e, TypeID type) const {
assert(!type.Is<Entity>());
if (!Exist(e)) throw std::invalid_argument("EntityMngr::Have: Entity is invalid");
return entityTable[e.Idx()].archetype->GetCmptTraits().GetTypes().contains(type);
return entityTable[e.index].archetype->GetCmptTraits().GetTypes().contains(type);
}

CmptPtr EntityMngr::Get(Entity e, TypeID type) const {
assert(!type.Is<Entity>());
if (!Exist(e)) throw std::invalid_argument("EntityMngr::Get: Entity is invalid");
const auto& info = entityTable[e.Idx()];
const auto& info = entityTable[e.index];
return { type, info.archetype->At(type, info.idxInArchetype) };
}

bool EntityMngr::Exist(Entity e) const noexcept {
return e.Idx() < entityTable.size() && e.Version() == entityTable[e.Idx()].version;
return e.index < entityTable.size() && e.version == entityTable[e.index].version;
}

std::size_t EntityMngr::RequestEntityFreeEntry() {
Expand All @@ -88,12 +88,12 @@ std::size_t EntityMngr::RequestEntityFreeEntry() {
void EntityMngr::RecycleEntityEntry(Entity e) {
assert(Exist(e));

auto& info = entityTable[e.Idx()];
auto& info = entityTable[e.index];
info.archetype = nullptr;
info.idxInArchetype = static_cast<std::size_t>(-1);
info.version++;

entityTableFreeEntry.push_back(e.Idx());
entityTableFreeEntry.push_back(e.index);
}

Archetype* EntityMngr::GetOrCreateArchetypeOf(std::span<const TypeID> types) {
Expand Down Expand Up @@ -127,7 +127,7 @@ void EntityMngr::Attach(Entity e, std::span<const TypeID> types) {
assert(IsSet(types));
if (!Exist(e)) throw std::invalid_argument("Entity is invalid");

auto& info = entityTable[e.Idx()];
auto& info = entityTable[e.index];
Archetype* srcArchetype = info.archetype;
std::size_t srcIdxInArchetype = info.idxInArchetype;

Expand Down Expand Up @@ -187,7 +187,7 @@ void EntityMngr::Detach(Entity e, std::span<const TypeID> types) {
assert(IsSet(types));
if (!Exist(e)) throw std::invalid_argument("EntityMngr::Detach: Entity is invalid");

auto& info = entityTable[e.Idx()];
auto& info = entityTable[e.index];
Archetype* srcArchetype = info.archetype;

const auto& srcTypeIDSet = srcArchetype->GetCmptTraits().GetTypes();
Expand Down Expand Up @@ -241,15 +241,15 @@ void EntityMngr::Detach(Entity e, std::span<const TypeID> types) {
vector<CmptPtr> EntityMngr::Components(Entity e) const {
if (!Exist(e)) throw std::invalid_argument("Entity is invalid");

const auto& info = entityTable[e.Idx()];
const auto& info = entityTable[e.index];
return info.archetype->Components(info.idxInArchetype);
}

Entity EntityMngr::Instantiate(Entity srcEntity) {
if (!Exist(srcEntity)) throw std::invalid_argument("Entity is invalid");

std::size_t dstEntityIndex = RequestEntityFreeEntry();
const auto& srcInfo = entityTable[srcEntity.Idx()];
const auto& srcInfo = entityTable[srcEntity.index];
auto& dstInfo = entityTable[dstEntityIndex];
Entity dstEntity{ dstEntityIndex, dstInfo.version };
std::size_t dstIndexInArchetype = srcInfo.archetype->Instantiate(dstEntity, srcInfo.idxInArchetype);
Expand Down Expand Up @@ -293,7 +293,7 @@ std::size_t EntityMngr::EntityNum(const EntityQuery& query) const {
void EntityMngr::Destroy(Entity e) {
if (!Exist(e)) throw std::invalid_argument("Entity is invalid");

auto info = entityTable[e.Idx()];
auto info = entityTable[e.index];
auto* archetype = info.archetype;
auto idxInArchetype = info.idxInArchetype;

Expand Down
4 changes: 2 additions & 2 deletions src/test/01_tag/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ struct DataSystem {

int main() {
World w;
w.entityMngr.cmptTraits.Register<DataSystem>();
w.entityMngr.cmptTraits.Register<Data>();
w.systemMngr.RegisterAndActivate<DataSystem>();

w.entityMngr.Create(Ubpa::TypeIDs_of<DataSystem>);
w.entityMngr.Create(Ubpa::TypeIDs_of<Data>);

w.Update();

Expand Down
2 changes: 1 addition & 1 deletion src/test/10_instantiate/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct A { float val; };
struct MySystem {
static void OnUpdate(Schedule& schedule) {
schedule.RegisterEntityJob([](Entity e, const A* a) {
cout << e.Idx() << ": " << a->val << endl;
cout << e.index << ": " << a->val << endl;
}, "MySystem", false);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/test/17_serial/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int main() {
w.entityMngr.Create();

w.RunEntityJob([](Entity e) {
std::cout << e.Idx() << std::endl;
std::cout << e.index << std::endl;
}, false);

std::cout << w.DumpUpdateJobGraph() << std::endl;
Expand Down

0 comments on commit 259579b

Please sign in to comment.