CoriEngine
Loading...
Searching...
No Matches
Physics/Triggers/Trigger.hpp
Go to the documentation of this file.
1#pragma once
7
8#ifndef CORI_MAX_TRIGGER_VISITORS
9 #define CORI_MAX_TRIGGER_VISITORS 4
10#endif
11
12namespace Cori {
13 namespace World {
14 namespace Components {
15 namespace Entity {
31 class Trigger {
32 public:
33 Trigger() = default;
34
40 template<std::derived_from<TriggerBehaviour> Behavior>
41 Behavior* SetBehavior() {
42 m_Behavior = std::make_unique<Behavior>();
43 m_BehaviourType = typeid(Behavior);
44 m_VisitorBuffer.clear();
45 return static_cast<Behavior*>(m_Behavior.get());
46 }
47
53 template<std::derived_from<TriggerBehaviour> Behavior>
54 [[nodiscard]] bool HasBehaviour() const {
55 return m_BehaviourType == std::type_index(typeid(Behavior));
56 }
57
63 template<std::derived_from<TriggerBehaviour> Behavior>
64 std::expected<Behavior*, Core::CoriError<std::type_index>> GetBehavior() {
65 if (std::type_index(typeid(Behavior)) != m_BehaviourType) {
66 return std::unexpected(Core::CoriError<std::type_index>(std::format("Failed to retrieve a pointer to <{}> type of TriggerBehavior, type of TriggerBehavior stored in the Trigger is <{}>, type mismatch.", CORI_CLEAN_TYPE_NAME(Behavior), CORI_DEMANGLE(m_BehaviourType.name())), "Stored Type", m_BehaviourType));
67 }
68
69 return static_cast<Behavior*>(m_Behavior.get());
70 }
71
72 private:
73 friend Systems::Trigger;
74 void OnEnter(World::Entity& entity);
75
76 void OnTickUpdate(const float timeStep);
77
78 void OnExit(World::Entity& entity);
80 std::unique_ptr<TriggerBehaviour> m_Behavior{ nullptr };
81 std::type_index m_BehaviourType{ typeid(nullptr) };
82 World::Entity m_Trigger;
83 };
84 }
85 }
86 }
87}
#define CORI_DEMANGLE(name)
#define CORI_CLEAN_TYPE_NAME(tn)
Custom error class mainly used in std::expected.
Definition Error.hpp:27
This is my packed/dense array custom implementation.
bool HasBehaviour() const
Checks if a specific behaviour is currently assigned to the trigger.
std::expected< Behavior *, Core::CoriError< std::type_index > > GetBehavior()
Retries the pointer to the active behaviour instance.
Behavior * SetBehavior()
Sets or changes the current Trigger behavior/script.
Entities are the essential part of WorldSystem.
Definition Entity.hpp:25
System responsible for handling physical triggers.
Global engine namespace.