CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
EntityPredicate.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Component.hpp"
11#include "Entity.hpp"
12
14
16
17#include <utility>
18
19namespace CeresEngine {
20
24 template<CComponent... Cs> struct ComponentPredicate : Predicate<ComponentPredicate<Cs...>> {
29 inline bool operator()(const Entity& entity) const noexcept { return entity.has<Cs...>(); }
30 };
31
35 template<CComponent... Cs> struct ComponentPredicate<ComponentSet<Cs...>> : ComponentPredicate<Cs...> {};
36
41 template<CComponent... Cs> [[nodiscard]] inline constexpr auto withComponent() noexcept { return ComponentPredicate<Cs...>(); }
42
47 template<typename CS> [[nodiscard]] inline constexpr auto withComponents() noexcept { return CS::template Apply<ComponentPredicate>(); }
48
53 template<CComponent... Cs, typename Predicate> [[nodiscard]] inline constexpr auto withComponent(Predicate&& subpredicate) noexcept {
54 return withComponent<Cs...>() &&
55 predicate([subpredicate = std::forward<Predicate>(subpredicate)](Entity entity) noexcept { return subpredicate(entity.get<Cs>()...); });
56 }
57
60 struct ParentPredicate : public Predicate<ParentPredicate> {
63
66 explicit inline ParentPredicate(const Entity parent) : parent(parent) {}
67
72 inline bool operator()(const Entity& entity) const noexcept { return entity.getParent() == parent; }
73 };
74
79 [[nodiscard]] inline ParentPredicate withParent(const Entity& parent) noexcept { return ParentPredicate{parent}; }
80
81 struct NamePredicate : public Predicate<ParentPredicate> {
83 const String name;
84
87 explicit inline NamePredicate(String name) : name(std::move(name)) {}
88
93 inline bool operator()(const Entity& entity) const noexcept { return entity.getName() == name; }
94 };
95
99 [[nodiscard]] inline NamePredicate withName(String name) { return NamePredicate{std::move(name)}; }
100
101} // namespace CeresEngine
The base entity class.
Definition Entity.hpp:41
Definition Component.hpp:117
Definition Application.hpp:19
constexpr auto withComponents() noexcept
A predicate that checks if the entity has all of the given Cs components.
Definition EntityPredicate.hpp:47
NamePredicate withName(String name)
A predicate that checks if the entity is named name.
Definition EntityPredicate.hpp:99
auto move(Vector3 position)
Moves a entity to the given position.
Definition Helpers.hpp:22
ParentPredicate withParent(const Entity &parent) noexcept
A predicate that checks if the entity has parent as it's parent entity.
Definition EntityPredicate.hpp:79
auto parent(const Entity &parent)
Sets the entity parent.
Definition Helpers.hpp:52
constexpr auto predicate(Callable &&callable) noexcept
Helper function to create a new predicate from a lambda.
Definition Predicate.hpp:390
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
constexpr auto withComponent() noexcept
A predicate that checks if the entity has all of the given Cs components.
Definition EntityPredicate.hpp:41
Definition Span.hpp:668
A predicate that checks if the given entity has all components in Cs.
Definition EntityPredicate.hpp:24
bool operator()(const Entity &entity) const noexcept
Executes the predicate.
Definition EntityPredicate.hpp:29
Definition Component.hpp:346
Definition EntityPredicate.hpp:81
bool operator()(const Entity &entity) const noexcept
Executes the predicate.
Definition EntityPredicate.hpp:93
const String name
The name to be checked against.
Definition EntityPredicate.hpp:83
NamePredicate(String name)
Creates a new NamePredicate instance.
Definition EntityPredicate.hpp:87
A predicate that checks if the entity has parent as it's parent entity.
Definition EntityPredicate.hpp:60
ParentPredicate(const Entity parent)
Creates a new ParentPredicate instance.
Definition EntityPredicate.hpp:66
const Entity parent
The parent to be checked against.
Definition EntityPredicate.hpp:62
bool operator()(const Entity &entity) const noexcept
Executes the predicate.
Definition EntityPredicate.hpp:72
A type that predicate types must extend to allow automatic operator overloading.
Definition Predicate.hpp:19