GCC Code Coverage Report


./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 of 111, 0 excluded
0.0%
Functions:
0 of 20, 0 excluded
0.0%
Branches:
0 of 138, 0 excluded
0.0%

apps/runner/src/eu/runner/entity.cc
Line Branch Exec Source
1 #include "eu/runner/entity.h"
2
3 #include <algorithm>
4
5 #include "dear_imgui/imgui.h"
6
7 namespace eu::runner
8 {
9
10 // ------------------------------------------------------------------------
11 // UpdateStageAndPrio
12
13 // ------------------------------------------------------------------------
14 // SystemWithPrio
15
16 // ------------------------------------------------------------------------
17 // UpdateHandler
18
19 // ------------------------------------------------------------------------
20 // Entity
21
22 Entity::Entity(const std::string& n, std::set<Hsh>&& t)
23 : name(n)
24 , tags(std::move(t))
25 {
26 }
27
28 void Entity::add_component(std::unique_ptr<Component> c)
29 {
30 components.emplace_back(std::move(c));
31 Component* nc = components.back().get();
32 for(auto& sys: systems)
33 {
34 sys->add_component(nc);
35 }
36
37 if (world)
38 {
39 world->on_add_component(this, nc);
40 }
41 }
42 void Entity::add_system(std::unique_ptr<EntitySystem> system)
43 {
44 systems.emplace_back(std::move(system));
45 EntitySystem* ne = systems.back().get();
46 updates.add(ne);
47 for (auto& c: components)
48 {
49 ne->add_component(c.get());
50 }
51 if (root)
52 {
53 ne->on_root_changed(root);
54 }
55 }
56
57 bool Entity::has_tag(const Hsh& h) const
58 {
59 return tags.contains(h);
60 }
61
62 SpatialComponent* Entity::get_root() const
63 {
64 return root;
65 }
66
67 void Entity::set_root(SpatialComponent* c)
68 {
69 root = c;
70
71 for (auto& sys : systems)
72 {
73 sys->on_root_changed(c);
74 }
75
76 if (world)
77 {
78 world->on_root_changed(this, c);
79 }
80 }
81
82 void Entity::imgui()
83 {
84 for (const auto& tag: tags)
85 {
86 const auto f = fmt::format("{}", tag.text);
87 ImGui::Bullet();
88 ImGui::TextUnformatted(f.c_str());
89 }
90 if (ImGui::TreeNodeEx("Components"))
91 {
92 for (auto& comp : components)
93 {
94 ImGui::PushID(comp.get());
95 if (ImGui::TreeNodeEx(comp->display()))
96 {
97 comp->imgui();
98 ImGui::TreePop();
99 }
100 ImGui::PopID();
101 }
102 ImGui::TreePop();
103 }
104
105 if (ImGui::TreeNodeEx("Systems"))
106 {
107 for (auto& sys : systems)
108 {
109 ImGui::PushID(sys.get());
110 if (ImGui::TreeNodeEx(sys->display()))
111 {
112 sys->imgui();
113 ImGui::TreePop();
114 }
115 ImGui::PopID();
116 }
117 ImGui::TreePop();
118 }
119 }
120
121 // ------------------------------------------------------------------------
122 // Component
123 const HshSt& Component::type()
124 {
125 static const auto r = HshSt{ .data = {}, .primary = "<Component>"sv};
126 return r;
127 }
128
129 const HshSt& Component::get_type()
130 {
131 return type();
132 }
133
134 // ------------------------------------------------------------------------
135 // SpatialComponent
136
137 EU_IMP_COMPONENT_TYPE(SpatialComponent, Component)
138
139 void SpatialComponent::set_transform(const m4& t)
140 {
141 transform = t;
142 }
143 const m4& SpatialComponent::get_transform() const
144 {
145 return transform;
146 }
147
148 void SpatialComponent::imgui()
149 {
150 // todo(Gustav): render transform
151 }
152
153 // ------------------------------------------------------------------------
154 // EntitySystem
155
156
157 // ------------------------------------------------------------------------
158 // EntitySystemUpdateStageList
159
160 // ---------------------------------------------------
161 // World
162
163 Entity* World::add_entity(const std::string& name, std::set<Hsh>&& tags)
164 {
165 entities.emplace_back(std::make_unique<Entity>(name, std::move(tags)));
166 Entity* ent = entities.back().get();
167 ent->world = this;
168 return ent;
169 }
170
171 void World::on_add_component(Entity* entity, Component* component)
172 {
173 for (auto& sys: systems)
174 {
175 sys->add_component(entity, component);
176 }
177 }
178
179 void World::on_root_changed(Entity* entity, SpatialComponent* component)
180 {
181 for (auto& sys : systems)
182 {
183 sys->on_root_changed(entity, component);
184 }
185 }
186
187 void World::add_system(std::unique_ptr<WorldSystem> system)
188 {
189 systems.emplace_back(std::move(system));
190 WorldSystem* sys = systems.back().get();
191
192 updates.add(sys);
193 for (auto& ent: entities)
194 {
195 for (auto& comp: ent->components) {
196 sys->add_component(ent.get(), comp.get());
197 }
198 if (ent->get_root())
199 {
200 sys->on_root_changed(ent.get(), ent->get_root());
201 }
202 }
203 }
204
205 void World::update(UpdateStage stage, float dt)
206 {
207 for (auto& ent: entities)
208 {
209 ent->updates.update(stage, dt);
210 }
211
212 updates.update(stage, dt);
213 }
214
215 void World::gui()
216 {
217 for (auto& ent: entities)
218 {
219 ImGui::PushID(ent.get());
220 if (ImGui::TreeNodeEx(ent->name.c_str()))
221 {
222 ent->imgui();
223 ImGui::TreePop();
224 }
225 ImGui::PopID();
226 }
227
228 for (auto& sys : systems)
229 {
230 if (ImGui::TreeNodeEx(sys->display()))
231 {
232 ImGui::PushID(sys.get());
233 sys->imgui();
234 ImGui::PopID();
235 ImGui::TreePop();
236 }
237 }
238 }
239 }
240