Basic simple animations where the enemies just move forward.
This commit is contained in:
parent
947ccbe180
commit
80a0f2ba75
9 changed files with 92 additions and 80 deletions
104
dinkyecs.hpp
104
dinkyecs.hpp
|
@ -8,6 +8,7 @@
|
|||
#include <typeindex>
|
||||
#include <typeinfo>
|
||||
#include <unordered_map>
|
||||
#include <optional>
|
||||
|
||||
namespace DinkyECS
|
||||
{
|
||||
|
@ -16,14 +17,12 @@ namespace DinkyECS
|
|||
using EntityMap = std::unordered_map<Entity, size_t>;
|
||||
|
||||
template <typename T>
|
||||
struct ComponentStorage
|
||||
{
|
||||
struct ComponentStorage {
|
||||
std::vector<T> data;
|
||||
std::queue<size_t> free_indices;
|
||||
};
|
||||
|
||||
struct Event
|
||||
{
|
||||
struct Event {
|
||||
int event = 0;
|
||||
Entity entity = 0;
|
||||
std::any data;
|
||||
|
@ -31,8 +30,7 @@ namespace DinkyECS
|
|||
|
||||
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;
|
||||
|
@ -42,44 +40,35 @@ namespace DinkyECS
|
|||
|
||||
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;
|
||||
to_world.$component_storages = $component_storages;
|
||||
|
||||
for (auto eid : $constants)
|
||||
{
|
||||
for (const auto &[tid, eid_map] : $components)
|
||||
{
|
||||
for(auto eid : $constants) {
|
||||
for(const auto &[tid, eid_map] : $components) {
|
||||
auto &their_map = to_world.$components[tid];
|
||||
if (eid_map.contains(eid))
|
||||
{
|
||||
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>
|
||||
size_t make_component()
|
||||
{
|
||||
size_t make_component() {
|
||||
auto &storage = component_storage_for<Comp>();
|
||||
size_t index;
|
||||
|
||||
if (!storage.free_indices.empty())
|
||||
{
|
||||
if(!storage.free_indices.empty()) {
|
||||
index = storage.free_indices.front();
|
||||
storage.free_indices.pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
storage.data.emplace_back();
|
||||
index = storage.data.size() - 1;
|
||||
}
|
||||
|
@ -88,8 +77,7 @@ namespace DinkyECS
|
|||
}
|
||||
|
||||
template <typename Comp>
|
||||
ComponentStorage<Comp> &component_storage_for()
|
||||
{
|
||||
ComponentStorage<Comp> &component_storage_for() {
|
||||
auto type_index = std::type_index(typeid(Comp));
|
||||
$component_storages.try_emplace(type_index, ComponentStorage<Comp>{});
|
||||
return std::any_cast<ComponentStorage<Comp> &>(
|
||||
|
@ -97,24 +85,20 @@ namespace DinkyECS
|
|||
}
|
||||
|
||||
template <typename Comp>
|
||||
EntityMap &entity_map_for()
|
||||
{
|
||||
EntityMap &entity_map_for() {
|
||||
return $components[std::type_index(typeid(Comp))];
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
EventQueue &queue_map_for()
|
||||
{
|
||||
EventQueue &queue_map_for() {
|
||||
return $events[std::type_index(typeid(Comp))];
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void remove(Entity ent)
|
||||
{
|
||||
void remove(Entity ent) {
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
|
||||
if (map.contains(ent))
|
||||
{
|
||||
if(map.contains(ent)) {
|
||||
size_t index = map.at(ent);
|
||||
component_storage_for<Comp>().free_indices.push(index);
|
||||
}
|
||||
|
@ -123,14 +107,12 @@ namespace DinkyECS
|
|||
}
|
||||
|
||||
template <typename Comp>
|
||||
void set_the(Comp val)
|
||||
{
|
||||
void set_the(Comp val) {
|
||||
$facts.insert_or_assign(std::type_index(typeid(Comp)), val);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
Comp &get_the()
|
||||
{
|
||||
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 "
|
||||
|
@ -143,19 +125,16 @@ namespace DinkyECS
|
|||
}
|
||||
|
||||
template <typename Comp>
|
||||
bool has_the()
|
||||
{
|
||||
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)
|
||||
{
|
||||
void set(Entity ent, Comp val) {
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
|
||||
if (has<Comp>(ent))
|
||||
{
|
||||
if(has<Comp>(ent)) {
|
||||
get<Comp>(ent) = val;
|
||||
return;
|
||||
}
|
||||
|
@ -165,8 +144,7 @@ namespace DinkyECS
|
|||
}
|
||||
|
||||
template <typename Comp>
|
||||
Comp &get(Entity ent)
|
||||
{
|
||||
Comp &get(Entity ent) {
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
auto &storage = component_storage_for<Comp>();
|
||||
auto index = map.at(ent);
|
||||
|
@ -174,48 +152,40 @@ namespace DinkyECS
|
|||
}
|
||||
|
||||
template <typename Comp>
|
||||
bool has(Entity ent)
|
||||
{
|
||||
bool has(Entity ent) {
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
return map.contains(ent);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void query(std::function<void(Entity, Comp &)> cb)
|
||||
{
|
||||
void query(std::function<void(Entity, Comp &)> cb) {
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
|
||||
for (auto &[entity, index] : map)
|
||||
{
|
||||
for(auto &[entity, index] : map) {
|
||||
cb(entity, get<Comp>(entity));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename CompA, typename CompB>
|
||||
void query(std::function<void(Entity, CompA &, CompB &)> cb)
|
||||
{
|
||||
void query(std::function<void(Entity, CompA &, CompB &)> cb) {
|
||||
EntityMap &map_a = entity_map_for<CompA>();
|
||||
EntityMap &map_b = entity_map_for<CompB>();
|
||||
|
||||
for (auto &[entity, index_a] : map_a)
|
||||
{
|
||||
if (map_b.contains(entity))
|
||||
{
|
||||
for(auto &[entity, index_a] : map_a) {
|
||||
if(map_b.contains(entity)) {
|
||||
cb(entity, get<CompA>(entity), get<CompB>(entity));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void send(Comp event, Entity entity, std::any data)
|
||||
{
|
||||
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()
|
||||
{
|
||||
Event recv() {
|
||||
EventQueue &queue = queue_map_for<Comp>();
|
||||
Event evt = queue.front();
|
||||
queue.pop();
|
||||
|
@ -223,10 +193,18 @@ namespace DinkyECS
|
|||
}
|
||||
|
||||
template <typename Comp>
|
||||
bool has_event()
|
||||
{
|
||||
bool has_event() {
|
||||
EventQueue &queue = queue_map_for<Comp>();
|
||||
return !queue.empty();
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
std::optional<Comp> get_if(DinkyECS::Entity entity) {
|
||||
if(has<Comp>(entity)) {
|
||||
return std::make_optional<Comp>(get<Comp>(entity));
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace DinkyECS
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue