.clang-format
This commit is contained in:
parent
432673fa0e
commit
d89827046a
6 changed files with 295 additions and 237 deletions
4
.clang-format
Normal file
4
.clang-format
Normal file
|
@ -0,0 +1,4 @@
|
|||
BasedOnStyle: LLVM
|
||||
BreakBeforeBraces: Allman
|
||||
AccessModifierOffset: -4
|
||||
IndentWidth: 4
|
30
dbc.cpp
30
dbc.cpp
|
@ -1,42 +1,52 @@
|
|||
#include "dbc.hpp"
|
||||
#include <iostream>
|
||||
|
||||
void dbc::log(const string &message) {
|
||||
void dbc::log(const string &message)
|
||||
{
|
||||
std::cerr << "!!!!!!!!!!" << message << std::endl;
|
||||
}
|
||||
|
||||
void dbc::sentinel(const string &message) {
|
||||
void dbc::sentinel(const string &message)
|
||||
{
|
||||
string err = fmt::format("[SENTINEL!] {}", message);
|
||||
dbc::log(err);
|
||||
throw dbc::SentinelError{err};
|
||||
}
|
||||
|
||||
void dbc::pre(const string &message, bool test) {
|
||||
if(!test) {
|
||||
void dbc::pre(const string &message, bool test)
|
||||
{
|
||||
if (!test)
|
||||
{
|
||||
string err = fmt::format("[PRE!] {}", message);
|
||||
dbc::log(err);
|
||||
throw dbc::PreCondError{err};
|
||||
}
|
||||
}
|
||||
|
||||
void dbc::pre(const string &message, std::function<bool()> tester) {
|
||||
void dbc::pre(const string &message, std::function<bool()> tester)
|
||||
{
|
||||
dbc::pre(message, tester());
|
||||
}
|
||||
|
||||
void dbc::post(const string &message, bool test) {
|
||||
if(!test) {
|
||||
void dbc::post(const string &message, bool test)
|
||||
{
|
||||
if (!test)
|
||||
{
|
||||
string err = fmt::format("[POST!] {}", message);
|
||||
dbc::log(err);
|
||||
throw dbc::PostCondError{err};
|
||||
}
|
||||
}
|
||||
|
||||
void dbc::post(const string &message, std::function<bool()> tester) {
|
||||
void dbc::post(const string &message, std::function<bool()> tester)
|
||||
{
|
||||
dbc::post(message, tester());
|
||||
}
|
||||
|
||||
void dbc::check(bool test, const string &message) {
|
||||
if(!test) {
|
||||
void dbc::check(bool test, const string &message)
|
||||
{
|
||||
if (!test)
|
||||
{
|
||||
string err = fmt::format("[CHECK!] {}\n", message);
|
||||
dbc::log(err);
|
||||
throw dbc::CheckError{err};
|
||||
|
|
44
dbc.hpp
44
dbc.hpp
|
@ -1,29 +1,39 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <fmt/core.h>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
using std::string;
|
||||
|
||||
namespace dbc {
|
||||
class Error {
|
||||
public:
|
||||
namespace dbc
|
||||
{
|
||||
class Error
|
||||
{
|
||||
public:
|
||||
const string message;
|
||||
Error(string m) : message{m} {}
|
||||
Error(const char *m) : message{m} {}
|
||||
};
|
||||
};
|
||||
|
||||
class CheckError : public Error {};
|
||||
class SentinelError : public Error {};
|
||||
class PreCondError : public Error {};
|
||||
class PostCondError : public Error {};
|
||||
class CheckError : public Error
|
||||
{
|
||||
};
|
||||
class SentinelError : public Error
|
||||
{
|
||||
};
|
||||
class PreCondError : public Error
|
||||
{
|
||||
};
|
||||
class PostCondError : public Error
|
||||
{
|
||||
};
|
||||
|
||||
void log(const string &message);
|
||||
void sentinel(const string &message);
|
||||
void pre(const string &message, bool test);
|
||||
void pre(const string &message, std::function<bool()> tester);
|
||||
void post(const string &message, bool test);
|
||||
void post(const string &message, std::function<bool()> tester);
|
||||
void check(bool test, const string &message);
|
||||
}
|
||||
void log(const string &message);
|
||||
void sentinel(const string &message);
|
||||
void pre(const string &message, bool test);
|
||||
void pre(const string &message, std::function<bool()> tester);
|
||||
void post(const string &message, bool test);
|
||||
void post(const string &message, std::function<bool()> tester);
|
||||
void check(bool test, const string &message);
|
||||
} // namespace dbc
|
||||
|
|
129
dinkyecs.hpp
129
dinkyecs.hpp
|
@ -1,156 +1,169 @@
|
|||
#pragma once
|
||||
|
||||
#include "dbc.hpp"
|
||||
#include <any>
|
||||
#include <functional>
|
||||
#include <queue>
|
||||
#include <tuple>
|
||||
#include <typeindex>
|
||||
#include <typeinfo>
|
||||
#include <unordered_map>
|
||||
#include <any>
|
||||
#include <tuple>
|
||||
#include <queue>
|
||||
#include "dbc.hpp"
|
||||
|
||||
namespace DinkyECS {
|
||||
typedef unsigned long Entity;
|
||||
namespace DinkyECS
|
||||
{
|
||||
typedef unsigned long Entity;
|
||||
|
||||
using EntityMap = std::unordered_map<Entity, std::any>;
|
||||
using EntityMap = std::unordered_map<Entity, std::any>;
|
||||
|
||||
struct Event {
|
||||
struct Event
|
||||
{
|
||||
int event = 0;
|
||||
Entity entity = 0;
|
||||
std::any data;
|
||||
};
|
||||
};
|
||||
|
||||
typedef std::queue<Event> EventQueue;
|
||||
typedef std::queue<Event> EventQueue;
|
||||
|
||||
struct World {
|
||||
struct World
|
||||
{
|
||||
unsigned long entity_count = 0;
|
||||
std::unordered_map<std::type_index, EntityMap> $components;
|
||||
std::unordered_map<std::type_index, std::any> $facts;
|
||||
std::unordered_map<std::type_index, EventQueue> $events;
|
||||
std::vector<Entity> $constants;
|
||||
|
||||
Entity entity() {
|
||||
return ++entity_count;
|
||||
}
|
||||
Entity entity() { return ++entity_count; }
|
||||
|
||||
void clone_into(DinkyECS::World &to_world) {
|
||||
void clone_into(DinkyECS::World &to_world)
|
||||
{
|
||||
to_world.$constants = $constants;
|
||||
to_world.$facts = $facts;
|
||||
to_world.entity_count = entity_count;
|
||||
|
||||
for(auto eid : $constants) {
|
||||
for(const auto &[tid, eid_map] : $components) {
|
||||
auto& their_map = to_world.$components[tid];
|
||||
if(eid_map.contains(eid)) {
|
||||
for (auto eid : $constants)
|
||||
{
|
||||
for (const auto &[tid, eid_map] : $components)
|
||||
{
|
||||
auto &their_map = to_world.$components[tid];
|
||||
if (eid_map.contains(eid))
|
||||
{
|
||||
their_map.insert_or_assign(eid, eid_map.at(eid));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void make_constant(DinkyECS::Entity entity) {
|
||||
void make_constant(DinkyECS::Entity entity)
|
||||
{
|
||||
$constants.push_back(entity);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
EntityMap& entity_map_for() {
|
||||
template <typename Comp> EntityMap &entity_map_for()
|
||||
{
|
||||
return $components[std::type_index(typeid(Comp))];
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
EventQueue& queue_map_for() {
|
||||
template <typename Comp> EventQueue &queue_map_for()
|
||||
{
|
||||
return $events[std::type_index(typeid(Comp))];
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void remove(Entity ent) {
|
||||
template <typename Comp> void remove(Entity ent)
|
||||
{
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
map.erase(ent);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void set_the(Comp val) {
|
||||
template <typename Comp> void set_the(Comp val)
|
||||
{
|
||||
$facts.insert_or_assign(std::type_index(typeid(Comp)), val);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
Comp &get_the() {
|
||||
template <typename Comp> Comp &get_the()
|
||||
{
|
||||
auto comp_id = std::type_index(typeid(Comp));
|
||||
dbc::check($facts.contains(comp_id),
|
||||
fmt::format("!!!! ATTEMPT to access world fact that hasn't been set yet: {}", typeid(Comp).name()));
|
||||
fmt::format("!!!! ATTEMPT to access world fact that hasn't "
|
||||
"been set yet: {}",
|
||||
typeid(Comp).name()));
|
||||
|
||||
// use .at to get std::out_of_range if fact not set
|
||||
std::any &res = $facts.at(comp_id);
|
||||
return std::any_cast<Comp&>(res);
|
||||
return std::any_cast<Comp &>(res);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
bool has_the() {
|
||||
template <typename Comp> bool has_the()
|
||||
{
|
||||
auto comp_id = std::type_index(typeid(Comp));
|
||||
return $facts.contains(comp_id);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void set(Entity ent, Comp val) {
|
||||
template <typename Comp> void set(Entity ent, Comp val)
|
||||
{
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
map.insert_or_assign(ent, val);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
Comp &get(Entity ent) {
|
||||
template <typename Comp> Comp &get(Entity ent)
|
||||
{
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
// use .at for bounds checking
|
||||
std::any &res = map.at(ent);
|
||||
return std::any_cast<Comp&>(res);
|
||||
return std::any_cast<Comp &>(res);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
bool has(Entity ent) {
|
||||
template <typename Comp> bool has(Entity ent)
|
||||
{
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
return map.contains(ent);
|
||||
}
|
||||
|
||||
template<typename Comp>
|
||||
void query(std::function<void(const Entity&, Comp&)> cb) {
|
||||
template <typename Comp>
|
||||
void query(std::function<void(const Entity &, Comp &)> cb)
|
||||
{
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
for(auto& [entity, any_comp] : map) {
|
||||
Comp &res = std::any_cast<Comp&>(any_comp);
|
||||
for (auto &[entity, any_comp] : map)
|
||||
{
|
||||
Comp &res = std::any_cast<Comp &>(any_comp);
|
||||
cb(entity, res);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename CompA, typename CompB>
|
||||
void query(std::function<void(const Entity&, CompA&, CompB&)> cb) {
|
||||
template <typename CompA, typename CompB>
|
||||
void query(std::function<void(const Entity &, CompA &, CompB &)> cb)
|
||||
{
|
||||
EntityMap &map_a = entity_map_for<CompA>();
|
||||
EntityMap &map_b = entity_map_for<CompB>();
|
||||
|
||||
for(auto& [entity, any_a] : map_a) {
|
||||
if(map_b.contains(entity)) {
|
||||
CompA &res_a = std::any_cast<CompA&>(any_a);
|
||||
for (auto &[entity, any_a] : map_a)
|
||||
{
|
||||
if (map_b.contains(entity))
|
||||
{
|
||||
CompA &res_a = std::any_cast<CompA &>(any_a);
|
||||
CompB &res_b = get<CompB>(entity);
|
||||
cb(entity, res_a, res_b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Comp>
|
||||
void send(Comp event, Entity entity, std::any data) {
|
||||
template <typename Comp> void send(Comp event, Entity entity, std::any data)
|
||||
{
|
||||
EventQueue &queue = queue_map_for<Comp>();
|
||||
queue.push({event, entity, data});
|
||||
}
|
||||
|
||||
template<typename Comp>
|
||||
Event recv() {
|
||||
template <typename Comp> Event recv()
|
||||
{
|
||||
EventQueue &queue = queue_map_for<Comp>();
|
||||
Event evt = queue.front();
|
||||
queue.pop();
|
||||
return evt;
|
||||
}
|
||||
|
||||
template<typename Comp>
|
||||
bool has_event() {
|
||||
template <typename Comp> bool has_event()
|
||||
{
|
||||
EventQueue &queue = queue_map_for<Comp>();
|
||||
return !queue.empty();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
} // namespace DinkyECS
|
||||
|
|
64
main.cpp
64
main.cpp
|
@ -10,51 +10,58 @@ using namespace fmt;
|
|||
using DinkyECS::Entity;
|
||||
using std::string;
|
||||
|
||||
struct Player {
|
||||
struct Player
|
||||
{
|
||||
string name;
|
||||
Entity eid;
|
||||
};
|
||||
|
||||
struct Position {
|
||||
struct Position
|
||||
{
|
||||
Point location;
|
||||
};
|
||||
|
||||
// DINKY_HAS_COMPONENT(Point, x, y);
|
||||
// DINKY_HAS_COMPONENT(Position, location);
|
||||
|
||||
struct Motion {
|
||||
struct Motion
|
||||
{
|
||||
int dx;
|
||||
int dy;
|
||||
bool random=false;
|
||||
bool random = false;
|
||||
};
|
||||
|
||||
// DINKY_HAS_COMPONENT(Motion, dx, dy, random);
|
||||
|
||||
struct Velocity {
|
||||
struct Velocity
|
||||
{
|
||||
double x, y;
|
||||
};
|
||||
|
||||
// DINKY_HAS_COMPONENT(Velocity, x, y);
|
||||
|
||||
struct Gravity {
|
||||
struct Gravity
|
||||
{
|
||||
double level;
|
||||
};
|
||||
|
||||
// DINKY_HAS_COMPONENT(Gravity, level);
|
||||
|
||||
struct DaGUI {
|
||||
struct DaGUI
|
||||
{
|
||||
int event;
|
||||
};
|
||||
|
||||
void configure(DinkyECS::World &world, Entity &test) {
|
||||
void configure(DinkyECS::World &world, Entity &test)
|
||||
{
|
||||
println("---Configuring the base system.");
|
||||
Entity test2 = world.entity();
|
||||
|
||||
world.set<Position>(test, {10,20});
|
||||
world.set<Velocity>(test, {1,2});
|
||||
world.set<Position>(test, {10, 20});
|
||||
world.set<Velocity>(test, {1, 2});
|
||||
|
||||
world.set<Position>(test2, {1,1});
|
||||
world.set<Velocity>(test2, {9,19});
|
||||
world.set<Position>(test2, {1, 1});
|
||||
world.set<Velocity>(test2, {9, 19});
|
||||
|
||||
println("---- Setting up the player as a fact in the system.");
|
||||
|
||||
|
@ -63,18 +70,19 @@ void configure(DinkyECS::World &world, Entity &test) {
|
|||
// just set some player info as a fact with the entity id
|
||||
world.set_the<Player>(player_info);
|
||||
|
||||
world.set<Velocity>(player_eid, {0,0});
|
||||
world.set<Position>(player_eid, {0,0});
|
||||
world.set<Velocity>(player_eid, {0, 0});
|
||||
world.set<Position>(player_eid, {0, 0});
|
||||
|
||||
auto enemy = world.entity();
|
||||
world.set<Velocity>(enemy, {0,0});
|
||||
world.set<Position>(enemy, {0,0});
|
||||
world.set<Velocity>(enemy, {0, 0});
|
||||
world.set<Position>(enemy, {0, 0});
|
||||
|
||||
println("--- Creating facts (singletons)");
|
||||
world.set_the<Gravity>({0.9});
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
DinkyECS::World world;
|
||||
Entity test = world.entity();
|
||||
|
||||
|
@ -88,20 +96,26 @@ int main() {
|
|||
REQUIRE(vel.x == 1);
|
||||
REQUIRE(vel.y == 2);
|
||||
|
||||
world.query<Position>([](const auto &ent, auto &pos) {
|
||||
world.query<Position>(
|
||||
[](const auto &ent, auto &pos)
|
||||
{
|
||||
REQUIRE(ent > 0);
|
||||
REQUIRE(pos.location.x >= 0);
|
||||
REQUIRE(pos.location.y >= 0);
|
||||
});
|
||||
|
||||
world.query<Velocity>([](const auto &ent, auto &vel) {
|
||||
world.query<Velocity>(
|
||||
[](const auto &ent, auto &vel)
|
||||
{
|
||||
REQUIRE(ent > 0);
|
||||
REQUIRE(vel.x >= 0);
|
||||
REQUIRE(vel.y >= 0);
|
||||
});
|
||||
|
||||
println("--- Manually get the velocity in position system:");
|
||||
world.query<Position>([&](const auto &ent, auto &pos) {
|
||||
world.query<Position>(
|
||||
[&](const auto &ent, auto &pos)
|
||||
{
|
||||
Velocity &vel = world.get<Velocity>(ent);
|
||||
|
||||
REQUIRE(ent > 0);
|
||||
|
@ -113,7 +127,9 @@ int main() {
|
|||
});
|
||||
|
||||
println("--- Query only entities with Position and Velocity:");
|
||||
world.query<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||
world.query<Position, Velocity>(
|
||||
[&](const auto &ent, auto &pos, auto &vel)
|
||||
{
|
||||
Gravity &grav = world.get_the<Gravity>();
|
||||
REQUIRE(grav.level <= 1.0f);
|
||||
REQUIRE(grav.level > 0.5f);
|
||||
|
@ -128,11 +144,13 @@ int main() {
|
|||
// now remove Velocity
|
||||
REQUIRE(world.has<Velocity>(test));
|
||||
world.remove<Velocity>(test);
|
||||
//REQUIRE_THROWS(world.get<Velocity>(test));
|
||||
// REQUIRE_THROWS(world.get<Velocity>(test));
|
||||
REQUIRE(!world.has<Velocity>(test));
|
||||
|
||||
println("--- After remove test, should only result in test2:");
|
||||
world.query<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||
world.query<Position, Velocity>(
|
||||
[&](const auto &ent, auto &pos, auto &vel)
|
||||
{
|
||||
auto &in_position = world.get<Position>(ent);
|
||||
auto &in_velocity = world.get<Velocity>(ent);
|
||||
REQUIRE(pos.location.x >= 0);
|
||||
|
|
13
point.hpp
13
point.hpp
|
@ -1,20 +1,23 @@
|
|||
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
struct Point {
|
||||
struct Point
|
||||
{
|
||||
size_t x = 0;
|
||||
size_t y = 0;
|
||||
|
||||
bool operator==(const Point& other) const {
|
||||
bool operator==(const Point &other) const
|
||||
{
|
||||
return other.x == x && other.y == y;
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::vector<Point> PointList;
|
||||
|
||||
template<> struct std::hash<Point> {
|
||||
size_t operator()(const Point& p) const {
|
||||
template <> struct std::hash<Point>
|
||||
{
|
||||
size_t operator()(const Point &p) const
|
||||
{
|
||||
auto hasher = std::hash<int>();
|
||||
return hasher(p.x) ^ hasher(p.y);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue