From: Mikko Rasa Date: Sun, 27 Nov 2022 21:45:30 +0000 (+0200) Subject: Emit events for colliding physical entities X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=4014851ac2ed8e86b5a781dcc3056088d40465a1;p=libs%2Fgame.git Emit events for colliding physical entities --- diff --git a/examples/bassteroids/source/events.h b/examples/bassteroids/source/events.h new file mode 100644 index 0000000..65adddb --- /dev/null +++ b/examples/bassteroids/source/events.h @@ -0,0 +1,18 @@ +#ifndef EVENTS_H_ +#define EVENTS_H_ + +#include + +class Collider; + +namespace Events { + +struct Collision +{ + Msp::Game::Handle collider1; + Msp::Game::Handle collider2; +}; + +} // namespace Events + +#endif diff --git a/examples/bassteroids/source/physics.cpp b/examples/bassteroids/source/physics.cpp index 67cdf6f..3c43502 100644 --- a/examples/bassteroids/source/physics.cpp +++ b/examples/bassteroids/source/physics.cpp @@ -8,6 +8,7 @@ using namespace Msp; Physics::Physics(Game::Stage &s): System(s), + event_source(stage.get_event_bus()), observer(stage.get_event_bus()) { observer.observe([this](auto &e){ entity_added(e); }); @@ -59,6 +60,9 @@ void Physics::tick(Time::TimeDelta dt) copy_out(entities[i]); for(unsigned i=fixture_count; i(entities[i]); + + for(const Collision &c: collisions) + event_source.emit(entities[c.body1].entity->get_collider(), entities[c.body2].entity->get_collider()); } template diff --git a/examples/bassteroids/source/physics.h b/examples/bassteroids/source/physics.h index 096e228..fcf4b0d 100644 --- a/examples/bassteroids/source/physics.h +++ b/examples/bassteroids/source/physics.h @@ -2,16 +2,21 @@ #define PHYSICS_H_ #include +#include #include #include #include #include #include +#include "events.h" class PhysicalEntity; class Physics: public Msp::Game::System { +public: + using EventSource = Msp::Game::EventSource; + private: struct SimulatedEntity { @@ -39,6 +44,7 @@ private: Msp::LinAl::Vector normal; }; + EventSource event_source; Msp::Game::EventObserver observer; std::vector entities; unsigned fixture_count = 0;