Compare commits
No commits in common. "f1f965a284f81bfe8e5149d5d9515714844d1255" and "c9a4e276309ec176746eb9d35a5966999ee5b07c" have entirely different histories.
f1f965a284
...
c9a4e27630
2 changed files with 10 additions and 77 deletions
|
@ -1,8 +1,6 @@
|
||||||
#include <catch2/catch_test_macros.hpp>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
#define CATCH_CONFIG_MAIN
|
#define CATCH_CONFIG_MAIN
|
||||||
#include <catch2/catch_all.hpp>
|
#include <catch2/catch_all.hpp>
|
||||||
|
|
||||||
#define MAX_ZECSY_ENTITIES 4
|
|
||||||
#include "../zecsy.hpp"
|
#include "../zecsy.hpp"
|
||||||
|
|
||||||
using namespace zecsy;
|
using namespace zecsy;
|
||||||
|
@ -12,9 +10,7 @@ TEST_CASE("Create a single entity and verify its existence")
|
||||||
world w;
|
world w;
|
||||||
|
|
||||||
auto e = w.make_entity();
|
auto e = w.make_entity();
|
||||||
|
|
||||||
REQUIRE(w.is_alive(e));
|
REQUIRE(w.is_alive(e));
|
||||||
REQUIRE(e.is_alive());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Destroy an entity and ensure it no longer exists in the world")
|
TEST_CASE("Destroy an entity and ensure it no longer exists in the world")
|
||||||
|
@ -23,30 +19,5 @@ TEST_CASE("Destroy an entity and ensure it no longer exists in the world")
|
||||||
|
|
||||||
auto e = w.make_entity();
|
auto e = w.make_entity();
|
||||||
w.destroy_entity(e);
|
w.destroy_entity(e);
|
||||||
|
|
||||||
REQUIRE_FALSE(w.is_alive(e));
|
REQUIRE_FALSE(w.is_alive(e));
|
||||||
REQUIRE_FALSE(e.is_alive());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("Entity #0 should be reserved and never used")
|
|
||||||
{
|
|
||||||
world w;
|
|
||||||
|
|
||||||
auto e = w.make_entity();
|
|
||||||
|
|
||||||
REQUIRE(e != 0);
|
|
||||||
REQUIRE(entity() == 0);
|
|
||||||
REQUIRE_FALSE(entity().is_alive());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("World should throw on id overflow")
|
|
||||||
{
|
|
||||||
world w;
|
|
||||||
|
|
||||||
for(int i = 0; i < MAX_ZECSY_ENTITIES; ++i)
|
|
||||||
{
|
|
||||||
REQUIRE_NOTHROW(w.make_entity());
|
|
||||||
}
|
|
||||||
|
|
||||||
REQUIRE_THROWS(w.make_entity());
|
|
||||||
}
|
}
|
||||||
|
|
56
zecsy.hpp
56
zecsy.hpp
|
@ -1,8 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <format>
|
#include <numeric>
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
#ifndef MAX_ZECSY_ENTITIES
|
#ifndef MAX_ZECSY_ENTITIES
|
||||||
#define MAX_ZECSY_ENTITIES 65536
|
#define MAX_ZECSY_ENTITIES 65536
|
||||||
|
@ -13,24 +12,6 @@ namespace zecsy
|
||||||
{
|
{
|
||||||
using entity_id = uint64_t;
|
using entity_id = uint64_t;
|
||||||
|
|
||||||
class entity final
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
entity(class world* w, entity_id id);
|
|
||||||
|
|
||||||
entity() = default;
|
|
||||||
entity(const entity &) = default;
|
|
||||||
entity(entity &&) = default;
|
|
||||||
entity &operator=(const entity &) = default;
|
|
||||||
entity &operator=(entity &&) = default;
|
|
||||||
operator entity_id() const;
|
|
||||||
|
|
||||||
bool is_alive() const;
|
|
||||||
private:
|
|
||||||
entity_id id = 0;
|
|
||||||
class world* w = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
class world final
|
class world final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -40,41 +21,22 @@ namespace zecsy
|
||||||
world &operator=(const world &) = default;
|
world &operator=(const world &) = default;
|
||||||
world &operator=(world &&) = default;
|
world &operator=(world &&) = default;
|
||||||
|
|
||||||
entity make_entity();
|
entity_id make_entity();
|
||||||
void destroy_entity(entity_id e);
|
void destroy_entity(entity_id e);
|
||||||
bool is_alive(entity_id e) const;
|
bool is_alive(entity_id e) const;
|
||||||
private:
|
private:
|
||||||
std::bitset<MAX_ZECSY_ENTITIES + 1> entities_bitset;
|
std::bitset<MAX_ZECSY_ENTITIES> entities_bitset;
|
||||||
entity_id entity_counter = 0;
|
entity_id entity_counter = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline entity::entity(class world* w, entity_id id): w(w), id(id)
|
inline entity_id world::make_entity()
|
||||||
{
|
{
|
||||||
|
auto id = entity_counter;
|
||||||
}
|
|
||||||
|
|
||||||
inline entity::operator entity_id() const
|
|
||||||
{
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool entity::is_alive() const
|
|
||||||
{
|
|
||||||
return w && w->is_alive(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline entity world::make_entity()
|
|
||||||
{
|
|
||||||
auto id = ++entity_counter;
|
|
||||||
|
|
||||||
if(id > MAX_ZECSY_ENTITIES)
|
|
||||||
{
|
|
||||||
throw std::runtime_error(std::format("Entity id {} exceeds "
|
|
||||||
"MAX_ZECSY_ENTITIES ({})", id, MAX_ZECSY_ENTITIES));
|
|
||||||
}
|
|
||||||
|
|
||||||
entities_bitset.set(id);
|
entities_bitset.set(id);
|
||||||
return entity(this, id);
|
|
||||||
|
entity_counter++;
|
||||||
|
|
||||||
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void world::destroy_entity(entity_id e)
|
inline void world::destroy_entity(entity_id e)
|
||||||
|
|
Loading…
Reference in a new issue