#include "physics.h"
-#include <algorithm>
+#include <msp/core/algorithm.h>
#include <msp/game/transform.h>
#include "physicalentity.h"
observer(stage.get_event_bus())
{
observer.observe<Game::Events::EntityCreated>([this](auto &e){ entity_added(e); });
+ observer.observe<Game::Events::EntityDestroyed>([this](auto &e){ entity_removed(e); });
stage.synthesize_initial_events(observer);
}
}
}
+void Physics::entity_removed(const Game::Events::EntityDestroyed &e)
+{
+ if(Game::Handle<PhysicalEntity> physical = dynamic_handle_cast<PhysicalEntity>(e.entity))
+ {
+ auto i = find_member(entities, physical, &SimulatedEntity::entity);
+ if(i!=entities.end())
+ {
+ size_t index = distance(entities.begin(), i);
+ if(index<fixture_count)
+ {
+ if(index+1!=fixture_count)
+ *i = std::move(entities[fixture_count-1]);
+ entities[fixture_count-1] = std::move(entities.back());
+ }
+ else
+ *i = std::move(entities.back());
+ entities.pop_back();
+ }
+ }
+}
+
void Physics::tick(Time::TimeDelta dt)
{
float dt_secs = dt/Time::sec;