X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=examples%2Fbassteroids%2Fsource%2Fphysics.cpp;h=73d858295db7df316f55a7e8396c9312e09c5c81;hb=338031de484ee117762d80449d9ca472a6918c92;hp=67cdf6fe596bbf29199b7af96174c92eecaefc92;hpb=e7223a520b12663127b21afe8a68965898f34ded;p=libs%2Fgame.git diff --git a/examples/bassteroids/source/physics.cpp b/examples/bassteroids/source/physics.cpp index 67cdf6f..73d8582 100644 --- a/examples/bassteroids/source/physics.cpp +++ b/examples/bassteroids/source/physics.cpp @@ -1,5 +1,5 @@ #include "physics.h" -#include +#include #include #include "physicalentity.h" @@ -8,9 +8,11 @@ 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); }); + observer.observe([this](auto &e){ entity_removed(e); }); stage.synthesize_initial_events(observer); } @@ -35,6 +37,27 @@ void Physics::entity_added(const Game::Events::EntityCreated &e) } } +void Physics::entity_removed(const Game::Events::EntityDestroyed &e) +{ + if(Game::Handle physical = dynamic_handle_cast(e.entity)) + { + auto i = find_member(entities, physical, &SimulatedEntity::entity); + if(i!=entities.end()) + { + size_t index = distance(entities.begin(), i); + if(index(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