CoriEngine
Loading...
Searching...
No Matches
WorldSystem/Systems/Trigger.cpp
Go to the documentation of this file.
1#include "Trigger.hpp"
5
6namespace Cori {
7 namespace World {
8 namespace Systems {
11
12 auto system = m_Owner.GetSystem<PhysicsSystem>();
13 if (system) {
14 auto locked = system->lock();
16
17 for (const auto entity : view) {
19 }
20
21 auto [beginEvents, endEvents, beginCount, endCount] = locked->GetWorld().GetSensorEvents();
22
23 for (int32_t i = 0; i < beginCount; ++i) {
24 const b2SensorBeginTouchEvent* beginTouch = beginEvents + i;
25
26 Entity& visitor = static_cast<Physics::BodyUserData*>(static_cast<Physics::ShapeRef>(beginTouch->visitorShapeId).GetBody().GetUserData())->m_Entity;
27
28 Entity& trigger = static_cast<Physics::BodyUserData*>(static_cast<Physics::ShapeRef>(beginTouch->sensorShapeId).GetBody().GetUserData())->m_Entity;
29
30 if (trigger.IsActiveGlobally()) {
31 trigger.GetComponents<Components::Entity::Trigger>().OnEnter(visitor);
32 }
33 }
34
35 for (int32_t i = 0; i < endCount; ++i) {
36 const b2SensorEndTouchEvent* endTouch = endEvents + i;
37
38 Entity& visitor = static_cast<Physics::BodyUserData*>(static_cast<Physics::ShapeRef>(endTouch->visitorShapeId).GetBody().GetUserData())->m_Entity;
39
40 Entity& trigger = static_cast<Physics::BodyUserData*>(static_cast<Physics::ShapeRef>(endTouch->sensorShapeId).GetBody().GetUserData())->m_Entity;
41
42 if (trigger.IsActiveGlobally()) {
43 trigger.GetComponents<Components::Entity::Trigger>().OnExit(visitor);
44 }
45 }
46 }
47 }
48
50 m_Owner.GetRegistry().on_construct<Physics::BodyUserData>().connect<&Trigger::OnBodyUserDataCreate>(this);
51 m_Owner.GetRegistry().on_construct<Components::Entity::Trigger>().connect<&Trigger::OnTriggerCreate>(this);
52 return true;
53 }
54
55 void Trigger::OnBodyUserDataCreate(entt::registry& registry, entt::entity entity) {
56 Entity e = entt::handle{ registry, entity };
57 auto& bud = e.GetComponents<Physics::BodyUserData>();
58 bud.m_Entity = e;
59 }
60
61 void Trigger::OnTriggerCreate(entt::registry& registry, entt::entity entity) {
62 Entity e = entt::handle{ registry, entity };
63 auto& tr = e.GetComponents<Components::Entity::Trigger>();
64 auto& ud = e.GetOrAddComponent<Physics::BodyUserData>();
65 ud.m_Entity = e;
66 auto& rb = e.GetComponents<Components::Entity::RigidBody>();
67 rb.SetUserData(&ud);
68 tr.m_Trigger = e;
69 }
70 }
71 }
72}
#define CORI_PROFILE_FUNCTION()
Definition Profiler.hpp:9
A GameTimer is responsible for managing everything that is connected with time, ticks,...
Definition Time.hpp:8
float GetTimestep() const
Returns the current timeStep, scale is in seconds.
Definition Time.hpp:31
A Trigger components is used in combination with RigidBody component used to respond to an entity get...
Entities are the essential part of WorldSystem.
Definition Entity.hpp:25
bool IsActiveGlobally() const
Checks if the entity is active locally (doesn't have InactiveGloballyFlag), just a convenience functi...
Definition Entity.cpp:61
decltype(auto) GetComponents()
Retries the references to the requested components of the entity.
Definition Entity.hpp:74
A wrapper for an EnTT compile-time view that provides an iterator to access Entity instances directly...
decltype(auto) Get(Entity entity)
Retries the components from an entity, both the components and the entity should be in a view,...
SceneHandle m_Owner
Definition System.hpp:38
void OnTickUpdate(Core::GameTimer &gameTimer) override
Anything connected to WorldSystem (ECS) is in this namespace.
Global engine namespace.
Cori::World::Entity m_Entity
Helper to exclude certain components from a static view.