Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
ContactCallback.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "core/Core.hpp"
5#include "entity/Entity.hpp"
6
7#include <functional>
8
9namespace Physics::Utils {
24template <typename... Components> class ContactCallback : public BaseCallback {
25 public:
26 using CallbackFunc = std::function<void(Engine::Core &, Engine::Entity &, Engine::Entity &)>;
27
28 explicit ContactCallback(CallbackFunc cb) : _callback(std::move(cb)) {}
29
30 void operator()(Engine::Core &core, Engine::Entity &a, Engine::Entity &b) const final
31 {
32 static_assert(sizeof...(Components) <= 2, "ContactCallback can only have up to 2 components.");
33
34 if constexpr (sizeof...(Components) == 0)
35 {
36 _callback(core, a, b);
37 }
38 else if constexpr (sizeof...(Components) == 1)
39 {
41 {
42 _callback(core, a, b);
43 }
44 }
45 else if constexpr (sizeof...(Components) == 2)
46 {
47 callIfComponentMatch<Components...>(core, a, b);
48 }
49 }
50
51 FunctionUtils::FunctionID GetID() const final { return _callback.target_type().hash_code(); }
52
53 private:
54 template <typename... Cs> inline bool hasAllComponents(Engine::Core &core, const Engine::Entity &entity) const
55 {
56 return ((entity.HasComponents<Cs>(core) && ...));
57 }
58
59 template <typename C1, typename C2>
61 {
62 if ((a.HasComponents<C1>(core) && b.HasComponents<C2>(core)))
63 {
64 _callback(core, a, b);
65 }
66 else if ((a.HasComponents<C2>(core) && b.HasComponents<C1>(core)))
67 {
68 _callback(core, b, a);
69 }
70 }
71
73};
74} // namespace Physics::Utils
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
Wrapper class providing a convenient interface for entity manipulation with the Core....
Definition Entity.hpp:20
bool HasComponents() const
Check if entity have one or multiple component's type.
Definition Entity.hpp:109
Base class for all functions contained in a FunctionContainer.
Definition BaseFunction.hpp:10
void callIfComponentMatch(Engine::Core &core, Engine::Entity &a, Engine::Entity &b) const
Definition ContactCallback.hpp:60
bool hasAllComponents(Engine::Core &core, const Engine::Entity &entity) const
Definition ContactCallback.hpp:54
void operator()(Engine::Core &core, Engine::Entity &a, Engine::Entity &b) const final
Definition ContactCallback.hpp:30
CallbackFunc _callback
The callback function to call.
Definition ContactCallback.hpp:72
ContactCallback(CallbackFunc cb)
Definition ContactCallback.hpp:28
std::function< void(Engine::Core &, Engine::Entity &, Engine::Entity &)> CallbackFunc
Definition ContactCallback.hpp:26
FunctionUtils::FunctionID GetID() const final
Pure virtual function to get the unique ID of the function.
Definition ContactCallback.hpp:51
std::size_t FunctionID
FunctionID class to represent a unique identifier for functions.
Definition FunctionID.hpp:9
Definition BiMap.hpp:7
FunctionUtils::BaseFunction< void, Engine::Core &, Engine::Entity &, Engine::Entity & > BaseCallback
A utility class for handling contact callbacks in a physics engine.
Definition ContactCallback.hpp:23