123 OAK_ERROR(
"Cannot get resource: world not initialized");
127 if (!resources->count(std::type_index(
typeid(R))))
129 OAK_ERROR(
"Resource not found: {}", std::type_index(
typeid(R)).name());
133 auto ret = resources->at(std::type_index(
typeid(R)));
135 return static_cast<R *
>(ret.get());
154 template <
typename C>
159 OAK_ERROR(
"Cannot add component: world not initialized");
163 auto component = std::make_shared<C>(new_component);
167 if (!components->count(std::type_index(
typeid(C))))
169 components->insert({std::type_index(
typeid(C)), {
component}});
173 components->at(std::type_index(
typeid(C))).push_back(
component);
176 OAK_INFO(
"Added component: {}", std::type_index(
typeid(C)).name());
193 OAK_ERROR(
"Cannot add resource: world not initialized");
198 {std::type_index(
typeid(R)), std::make_shared<R>(
resource)});
199 OAK_INFO(
"Added Resource: {}", std::type_index(
typeid(R)).name());
233 OAK_ERROR(
"Cannot remove resource: world not initialized");
237 if (!resources->count(std::type_index(
typeid(R))))
239 OAK_ERROR(
"Resource not found: {}", std::type_index(
typeid(R)).name());
243 resources->erase(std::type_index(
typeid(R)));
266 OAK_ERROR(
"Cannot get component: world not initialized");
270 if (!components->count(std::type_index(
typeid(C))))
272 OAK_ERROR(
"Component not found: {}", std::type_index(
typeid(C)).name());
276 for (
auto component : components->at(std::type_index(
typeid(C))))
280 return static_cast<C *
>(
component.get());
300 template <
typename Tuple, std::size_t... Is>
301 static void for_each_impl(Tuple &&tuple, std::index_sequence<Is...>)
303 (..., process(std::get<Is>(std::forward<Tuple>(tuple))));
305 template <
typename Tuple>
static void for_each(Tuple &&tuple)
307 for_each_impl(std::forward<Tuple>(tuple),
308 std::make_index_sequence<
309 std::tuple_size_v<std::remove_reference_t<Tuple>>>{});
311 template <
typename... T>
312 static std::vector<viotecs::entity_t> query_components_tuple(std::tuple<T...>)
314 return query_components<T...>();
316 template <
typename System>
static void process(
const System &system)
318 using dependencies =
typename System::dependencies;
319 std::vector<viotecs::entity_t> matches =
320 query_components_tuple(dependencies{});
324 template <
typename C,
typename... Components,
typename N = none>
325 static std::vector<entity_t> query_components()
327 if (!world::components)
329 OAK_ERROR(
"Cannot query: world not initialized");
333 std::vector<entity_t> matched;
335 if (!components->count(std::type_index(
typeid(C))))
340 for (
auto component : components->at(std::type_index(typeid(C))))
342 matched.push_back(component->entity);
347 if (
sizeof...(Components) == 0)
350 query_components_rec<Components..., none>(&matched);
355 template <
typename C,
typename... Components>
356 static void query_components_rec(std::vector<entity_t> *entities)
358 if (entities->empty())
360 std::vector<entity_t> matched;
362 if (!components->count(std::type_index(
typeid(C))))
367 for (
auto component : components->at(std::type_index(typeid(C))))
369 if (std::find(entities->begin(), entities->end(),
370 (
int &) component->entity)
373 matched.push_back(component->entity);
378 if (
sizeof...(Components) == 0)
381 (query_components_rec<Components>(entities), ...);