-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs.h
114 lines (95 loc) · 2.42 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once
#include <cassert>
#include <memory>
#include <vector>
#include <iostream>
#include "events/Event.h"
#include "events/EventResult.h"
namespace wcs {
struct Event;
struct Entity;
struct Component;
using SharedEntity = std::shared_ptr<Entity>;
using SharedEvent = std::shared_ptr<Event>;
using WeakEntity = std::weak_ptr<Entity>;
using WeakComponent = std::weak_ptr<Component>;
struct Component
{
WeakEntity parent;
inline static bool dump = true;
virtual ~Component() = default;
virtual void Dump() {}
virtual void OnEvent(SharedEvent& event, EventResult& event_result) { }
virtual std::string description() const { return ""; }
virtual void OnEvent(SharedEvent&& event) { EventResult er; OnEvent(event, er); }
template<typename T>
std::shared_ptr<T> GetParent() const
{
const auto p = parent.lock();
const auto entity = std::dynamic_pointer_cast<T>(p);
assert( p != nullptr );
return entity;
}
};
struct Entity : std::enable_shared_from_this<Entity>
{
virtual ~Entity() = default;
std::vector<std::shared_ptr<Component>> component;
template<typename T, typename... Args>
std::shared_ptr<T> add(Args&&... args)
{
component.push_back(std::make_shared<T>(std::forward<Args>(args)...));
SetParent(component.back());
return std::dynamic_pointer_cast<T>(component.back());
}
void SetParent(std::shared_ptr<Component> c)
{
c->parent=weak_from_this();
}
template<typename T>
T* get()
{
for (auto& t : component) {
if (auto* cast = dynamic_cast<T*>(t.get()))
return cast;
}
return nullptr;
}
template<typename T>
std::vector<std::shared_ptr<T>> get_all()
{
std::vector<std::shared_ptr<T>> com;
for (auto& t : component) {
if (auto* cast = dynamic_cast<T*>(t.get()))
com.push_back(std::dynamic_pointer_cast<T>(t));
}
return com;
}
EventResult OnEvent(const SharedEvent& event)
{
EventResult event_result;
EventProcess(event, event_result);
return event_result;
}
void OnEvent(const SharedEvent& event, EventResult& event_result)
{
EventProcess(event, event_result);
}
template<typename T>
void Dump()
{
if (T* t = get<T>())
t->Dump();
}
private:
void EventProcess(SharedEvent event, EventResult& event_result)
{
for (auto& t : component) {
t->OnEvent(event, event_result);
if (event_result.result == EventProcess::kFinish ) {
break;
}
}
}
};
};