SDLPP: add collider IDs

This commit is contained in:
2021-04-26 21:57:31 +02:00
parent 19e66bf34a
commit dd6f37264c
16 changed files with 112 additions and 41 deletions
+23 -17
View File
@@ -20,11 +20,15 @@ void Scene::addObject( const std::shared_ptr< RenderObject > &obj ) {
} else {
auto rect = obj->getDoubleRect();
auto leftmost_rect = leftmost_obj->getDoubleRect();
if ( rect.first.getX() < leftmost_rect.first.getX() )
if ( ( rect.first.getX() < leftmost_rect.first.getX() &&
!obj->getPermanent() ) ||
( leftmost_obj->getPermanent() && !obj->getPermanent() ) )
leftmost_obj = obj;
auto rightmost_rect = rightmost_obj->getDoubleRect();
if ( rect.first.getX() + rect.second.getX() >
rightmost_rect.first.getX() + rightmost_rect.second.getX() )
if ( ( rect.first.getX() + rect.second.getX() >
rightmost_rect.first.getX() + rightmost_rect.second.getX() &&
!obj->getPermanent() ) ||
( rightmost_obj->getPermanent() && !obj->getPermanent() ) )
rightmost_obj = obj;
}
}
@@ -95,33 +99,35 @@ void Scene::updateScene() {
}
prev_ticks = now_ticks;
}
std::vector< std::shared_ptr< RenderObject > >
std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > >
Scene::getCollisions( RenderObject &r ) {
if ( r.getHidden() )
return {};
std::vector< std::shared_ptr< RenderObject > > ret{};
std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > > ret{};
for ( const auto &x : collision_objects ) {
if ( x->colidesWith( r ) ) {
ret.push_back( x );
for ( auto id : r.colidesWith( *x ) ) {
ret.emplace_back( id, x );
}
}
return ret;
}
void Scene::visitCollisions( RenderObject &r, Visitor &v ) {
for(auto &collision : getCollisions(r)) {
collision->visit(v);
for ( auto &collision : getCollisions( r ) ) {
v.fromId( collision.first );
collision.second->visit( v );
}
}
std::vector< std::shared_ptr< RenderObject > >
std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > >
Scene::getCollisions( RenderObject &r,
const std::unordered_set< int > &objectIDs ) {
if ( r.getHidden() )
return {};
std::vector< std::shared_ptr< RenderObject > > ret{};
std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > > ret{};
for ( const auto &x : collision_objects ) {
if ( objectIDs.find( x->getId() ) != objectIDs.end() &&
x->colidesWith( r ) ) {
ret.push_back( x );
if ( objectIDs.find( x->getId() ) != objectIDs.end() ) {
for ( auto id : r.colidesWith( *x ) ) {
ret.push_back( { id, x } );
}
}
}
return ret;
@@ -154,7 +160,8 @@ void Scene::updateSizeAndPosition() {
x->updateSizeAndPosition();
for ( auto &col : x->getCollisions() ) {
col->updateCollision( x->collisionPushX(), x->collisionPushY(),
x->collisionWidth(), x->collisionHeight() );
x->collisionWidth(), x->collisionHeight(),
x->getId() );
}
}
}
@@ -164,8 +171,7 @@ void Scene::moveEverything( double x, double y ) {
for ( auto &obj : render_objects ) {
if ( obj->getPermanent() )
continue;
auto curPos = obj->getDoubleRect();
obj->setPos( curPos.first.getX() + x, curPos.first.getY() + y );
obj->setPos( obj->getDoubleRect().first + Vec2D< double >( x, y ) );
}
}
const std::shared_ptr< RenderObject > &Scene::leftmost() {