-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECS.h
72 lines (58 loc) · 1.77 KB
/
ECS.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright(C) 2021 Kobe Vrijsen <kobevrijsen@posteo.be>
//
// A simple ECS example
//
// This file is free software and distributed under the terms of the European Union
// Public Lincense as published by the European Commision; either version 1.2 of the
// License, or , at your option, any later version.
#pragma once
#include "Entity.h"
#include "System.h"
#include <unordered_map>
#include <any>
#include <vector>
#include <memory>
class EntityStore
{
public:
template <typename Entity>
void AddEntity(Entity entity)
{
auto const [node, isNew] = m_EntityStore.emplace(std::piecewise_construct, std::make_tuple(entity.Hash()), std::make_tuple());
node->second.push_back(std::unique_ptr<EntityHandle>{ new Entity{ std::move(entity) } });
if (!isNew)
return;
for (auto& [system, entities] : m_SystemStore)
if (std::ranges::includes(entity.ComponentHashes(), system->ArgumentHashesSorted()))
entities.push_back(entity.Hash());
}
template <typename System>
void AddSystem(System system)
{
auto& [systemPtr, matchedEntities] = m_SystemStore.emplace_back();
systemPtr.reset(new System{ std::move(system) });
auto const& hashes{ systemPtr->ArgumentHashesSorted() };
for (auto& [key, entities] : m_EntityStore)
if (std::ranges::includes(entities.front()->ComponentHashes(), hashes))
matchedEntities.push_back(key);
}
inline void CallHandles()
{
for (auto& [system, entityKeys] : m_SystemStore)
for (auto& entityKey : entityKeys)
for (auto& entity : m_EntityStore[entityKey])
entity->Handle(*system);
}
private:
std::unordered_map<
uint64_t,
std::vector<std::unique_ptr<EntityHandle>>,
std::identity
> m_EntityStore{};
std::vector<
std::pair<
std::unique_ptr<SystemHandle>,
std::vector<uint64_t>
>
> m_SystemStore{};
};