Just wrote my own entity system to figure it out.
This commit is contained in:
parent
a3eaf78fd3
commit
cc4f83a1d1
8 changed files with 229 additions and 59 deletions
8
gui.cpp
8
gui.cpp
|
@ -168,8 +168,8 @@ void GUI::handle_events() {
|
||||||
void GUI::burn() {
|
void GUI::burn() {
|
||||||
for(int i = 0; i < 20; ++i) {
|
for(int i = 0; i < 20; ++i) {
|
||||||
$map_text.setFillColor(color(i % VALUES.size()));
|
$map_text.setFillColor(color(i % VALUES.size()));
|
||||||
int x = Random::rand_int(-10,10);
|
int x = Random::uniform<int>(-10,10);
|
||||||
int y = Random::rand_int(-10,10);
|
int y = Random::uniform<int>(-10,10);
|
||||||
draw_screen(false, x, y);
|
draw_screen(false, x, y);
|
||||||
std::this_thread::sleep_for(2ms);
|
std::this_thread::sleep_for(2ms);
|
||||||
}
|
}
|
||||||
|
@ -187,8 +187,8 @@ void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) {
|
||||||
|
|
||||||
void GUI::shake() {
|
void GUI::shake() {
|
||||||
for(int i = 0; i < 10; ++i) {
|
for(int i = 0; i < 10; ++i) {
|
||||||
int x = Random::rand_int(-10,10);
|
int x = Random::uniform<int>(-10,10);
|
||||||
int y = Random::rand_int(-10,10);
|
int y = Random::uniform<int>(-10,10);
|
||||||
// add x/y back to draw screen
|
// add x/y back to draw screen
|
||||||
draw_screen(true, x, y);
|
draw_screen(true, x, y);
|
||||||
std::this_thread::sleep_for(1ms);
|
std::this_thread::sleep_for(1ms);
|
||||||
|
|
11
map.cpp
11
map.cpp
|
@ -115,7 +115,7 @@ inline int make_split(Room &cur, bool horiz) {
|
||||||
int min = dimension / 4;
|
int min = dimension / 4;
|
||||||
int max = dimension - min;
|
int max = dimension - min;
|
||||||
|
|
||||||
return Random::rand_int(min, max);
|
return Random::uniform<int>(min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Map::partition_map(Room &cur, int depth) {
|
void Map::partition_map(Room &cur, int depth) {
|
||||||
|
@ -125,7 +125,6 @@ void Map::partition_map(Room &cur, int depth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::uniform_int_distribution<int> rsplit(0, 1);
|
|
||||||
bool horiz = cur.width > cur.height ? false : true;
|
bool horiz = cur.width > cur.height ? false : true;
|
||||||
int split = make_split(cur, horiz);
|
int split = make_split(cur, horiz);
|
||||||
Room left = cur;
|
Room left = cur;
|
||||||
|
@ -210,10 +209,10 @@ void Map::set_door(Room &room, int value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void rand_side(Room &room, Point &door) {
|
void rand_side(Room &room, Point &door) {
|
||||||
int rand_x = Random::rand_int(0, room.width - 1);
|
int rand_x = Random::uniform<int>(0, room.width - 1);
|
||||||
int rand_y = Random::rand_int(0, room.height - 1);
|
int rand_y = Random::uniform<int>(0, room.height - 1);
|
||||||
|
|
||||||
switch(Random::rand_int(0,3)) {
|
switch(Random::uniform<int>(0,3)) {
|
||||||
case 0: // north
|
case 0: // north
|
||||||
door.x = room.x + rand_x;
|
door.x = room.x + rand_x;
|
||||||
door.y = room.y-1;
|
door.y = room.y-1;
|
||||||
|
@ -306,7 +305,7 @@ void Map::generate() {
|
||||||
println("ROOM NOT FOUND!");
|
println("ROOM NOT FOUND!");
|
||||||
}
|
}
|
||||||
clear_target(target.entry);
|
clear_target(target.entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
Room &src = $rooms.back();
|
Room &src = $rooms.back();
|
||||||
Room &target = $rooms.front();
|
Room &target = $rooms.front();
|
||||||
|
|
|
@ -35,10 +35,14 @@ roguish = executable('roguish', [
|
||||||
],
|
],
|
||||||
dependencies: dependencies)
|
dependencies: dependencies)
|
||||||
|
|
||||||
runtests = executable('flecstest', [
|
flectest = executable('flecstest', [
|
||||||
'./scratchpad/flecs.cpp'
|
'./scratchpad/flecstest.cpp'
|
||||||
],
|
],
|
||||||
dependencies: dependencies)
|
dependencies: dependencies)
|
||||||
|
|
||||||
|
myecstest = executable('myecstest', [
|
||||||
|
'./scratchpad/myecstest.cpp'
|
||||||
|
],
|
||||||
|
dependencies: dependencies)
|
||||||
|
|
||||||
test('tests', runtests)
|
test('tests', runtests)
|
||||||
|
|
10
rand.cpp
10
rand.cpp
|
@ -1,10 +1,6 @@
|
||||||
#include "rand.hpp"
|
#include "rand.hpp"
|
||||||
|
|
||||||
std::random_device RNG;
|
namespace Random {
|
||||||
std::mt19937 GENERATOR(RNG());
|
std::random_device RNG;
|
||||||
|
std::mt19937 GENERATOR(RNG());
|
||||||
int Random::rand_int(int from, int to) {
|
|
||||||
std::uniform_int_distribution<int> rand(from, to);
|
|
||||||
|
|
||||||
return rand(GENERATOR);
|
|
||||||
}
|
}
|
||||||
|
|
24
rand.hpp
24
rand.hpp
|
@ -1,6 +1,28 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
|
|
||||||
namespace Random {
|
namespace Random {
|
||||||
int rand_int(int from, int to);
|
extern std::mt19937 GENERATOR;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T uniform(T from, T to) {
|
||||||
|
std::uniform_int_distribution<T> rand(from, to);
|
||||||
|
|
||||||
|
return rand(GENERATOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T uniform_real(T from, T to) {
|
||||||
|
std::uniform_real_distribution<T> rand(from, to);
|
||||||
|
|
||||||
|
return rand(GENERATOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T normal(T from, T to) {
|
||||||
|
std::normal_distribution<T> rand(from, to);
|
||||||
|
|
||||||
|
return rand(GENERATOR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
#include <flecs.h>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
struct Position {
|
|
||||||
double x, y;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Velocity {
|
|
||||||
double x, y;
|
|
||||||
};
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
flecs::world ecs;
|
|
||||||
|
|
||||||
// Create a system for Position, Velocity. Systems are like queries (see
|
|
||||||
// queries) with a function that can be ran or scheduled (see pipeline).
|
|
||||||
flecs::system s = ecs.system<Position, const Velocity>()
|
|
||||||
.each([](flecs::entity e, Position& p, const Velocity& v) {
|
|
||||||
p.x += v.x;
|
|
||||||
p.y += v.y;
|
|
||||||
std::cerr << e.name() << ": {" << p.x << ", " << p.y << "}\n";
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create a few test entities for a Position, Velocity query
|
|
||||||
ecs.entity("e1")
|
|
||||||
.set<Position>({10, 20})
|
|
||||||
.set<Velocity>({1, 2});
|
|
||||||
|
|
||||||
ecs.entity("e2")
|
|
||||||
.set<Position>({10, 20})
|
|
||||||
.set<Velocity>({3, 4});
|
|
||||||
|
|
||||||
// This entity will not match as it does not have Position, Velocity
|
|
||||||
ecs.entity("e3")
|
|
||||||
.set<Position>({10, 20});
|
|
||||||
|
|
||||||
// Run the system
|
|
||||||
s.run();
|
|
||||||
}
|
|
61
scratchpad/flecstest.cpp
Normal file
61
scratchpad/flecstest.cpp
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
#include <flecs.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <typeindex>
|
||||||
|
#include <typeinfo>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
struct Position {
|
||||||
|
double x, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Velocity {
|
||||||
|
double x, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
flecs::world ecs;
|
||||||
|
|
||||||
|
flecs::system fight = ecs.system<const Position>()
|
||||||
|
.each([&](flecs::entity e, const Position &other) {
|
||||||
|
flecs::entity player = ecs.lookup("player");
|
||||||
|
if(e == player) return; // don't process the player
|
||||||
|
|
||||||
|
const Position *player_p = player.get<Position>();
|
||||||
|
if(player_p->x == other.x && player_p->y == other.y) {
|
||||||
|
std::cerr << "HIT\n";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create a system for Position, Velocity. Systems are like queries (see
|
||||||
|
// queries) with a function that can be ran or scheduled (see pipeline).
|
||||||
|
flecs::system move = ecs.system<Position, const Velocity>()
|
||||||
|
.each([&](flecs::entity e, Position& p, const Velocity& v) {
|
||||||
|
p.x += v.x;
|
||||||
|
p.y += v.y;
|
||||||
|
std::cerr << e.name() << ": {" << p.x << ", " << p.y << "}\n";
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create a few test entities for a Position, Velocity query
|
||||||
|
ecs.entity("player")
|
||||||
|
.set<Position>({10, 10})
|
||||||
|
.set<Velocity>({1, 1});
|
||||||
|
|
||||||
|
ecs.entity("e2")
|
||||||
|
.set<Position>({10, 20})
|
||||||
|
.set<Velocity>({1, 1});
|
||||||
|
|
||||||
|
// This entity will not match as it does not have Position, Velocity
|
||||||
|
ecs.entity("e3")
|
||||||
|
.set<Position>({10, 20});
|
||||||
|
|
||||||
|
std::unordered_map<std::type_index, std::string> type_names;
|
||||||
|
type_names[std::type_index(typeid(Position))] = "Position";
|
||||||
|
type_names[std::type_index(typeid(Velocity))] = "Velocity";
|
||||||
|
|
||||||
|
for(const auto& [key, value] : type_names) {
|
||||||
|
std::cout << " VAL " << value << std::endl;
|
||||||
|
}
|
||||||
|
// Run the system
|
||||||
|
move.run();
|
||||||
|
fight.run();
|
||||||
|
}
|
127
scratchpad/myecstest.cpp
Normal file
127
scratchpad/myecstest.cpp
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <functional>
|
||||||
|
#include <typeindex>
|
||||||
|
#include <typeinfo>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <fmt/core.h>
|
||||||
|
#include <any>
|
||||||
|
|
||||||
|
using namespace fmt;
|
||||||
|
|
||||||
|
struct Position {
|
||||||
|
double x, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Velocity {
|
||||||
|
double x, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef unsigned long Entity;
|
||||||
|
|
||||||
|
typedef std::unordered_map<Entity, std::any> EntityMap;
|
||||||
|
|
||||||
|
struct World {
|
||||||
|
unsigned long entity_count = 0;
|
||||||
|
std::unordered_map<std::type_index, EntityMap> $components;
|
||||||
|
|
||||||
|
Entity entity() {
|
||||||
|
return ++entity_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Comp>
|
||||||
|
EntityMap& entity_map_for() {
|
||||||
|
return $components[std::type_index(typeid(Comp))];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Comp>
|
||||||
|
void assign(Entity ent, Comp val) {
|
||||||
|
EntityMap &map = entity_map_for<Comp>();
|
||||||
|
map[ent] = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Comp>
|
||||||
|
Comp &component(Entity ent) {
|
||||||
|
EntityMap &map = entity_map_for<Comp>();
|
||||||
|
std::any &res = map[ent];
|
||||||
|
return std::any_cast<Comp&>(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Comp>
|
||||||
|
void system(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);
|
||||||
|
cb(entity, res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Comp>
|
||||||
|
void remove(Entity ent) {
|
||||||
|
EntityMap &map = entity_map_for<Comp>();
|
||||||
|
map.erase(ent);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename CompA, typename CompB>
|
||||||
|
void system(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);
|
||||||
|
CompB &res_b = component<CompB>(entity);
|
||||||
|
cb(entity, res_a, res_b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
World me;
|
||||||
|
|
||||||
|
Entity test = me.entity();
|
||||||
|
Entity test2 = me.entity();
|
||||||
|
|
||||||
|
me.assign<Position>(test, {10,20});
|
||||||
|
me.assign<Velocity>(test, {1,2});
|
||||||
|
|
||||||
|
me.assign<Position>(test2, {1,1});
|
||||||
|
me.assign<Velocity>(test2, {10,20});
|
||||||
|
|
||||||
|
Position &pos = me.component<Position>(test);
|
||||||
|
println("GOT POS x={}, y={}", pos.x, pos.y);
|
||||||
|
|
||||||
|
Velocity &vel = me.component<Velocity>(test);
|
||||||
|
println("GOT VELOCITY x={}, y={}", vel.x, vel.y);
|
||||||
|
|
||||||
|
println("--- Position only system:");
|
||||||
|
me.system<Position>([](const auto &ent, auto &pos) {
|
||||||
|
println("entity={}, pos.x={}, pos.y={}", ent, pos.x, pos.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
println("--- Velocity only system:");
|
||||||
|
me.system<Velocity>([](const auto &, auto &vel) {
|
||||||
|
println("vel.x={}, vel.y={}", vel.x, vel.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
println("--- Manually get the velocity in position system:");
|
||||||
|
me.system<Position>([&](const auto &ent, auto &pos) {
|
||||||
|
Velocity &vel = me.component<Velocity>(ent);
|
||||||
|
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
println("--- Query only entities with Position and Velocity:");
|
||||||
|
me.system<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||||
|
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
// now remove Velocity
|
||||||
|
me.remove<Velocity>(test);
|
||||||
|
|
||||||
|
println("--- After remove test, should only result in test2:");
|
||||||
|
me.system<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||||
|
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue