From a194bfb86ec7bb390ac0b8905120568498103652 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 7 Aug 2021 22:10:09 +0200 Subject: [PATCH] Mario: destroy moving objects once they leave left side of screen, remove destroyed objects from moving_objects --- mario/main.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mario/main.cpp b/mario/main.cpp index 2662f45..ac1785c 100644 --- a/mario/main.cpp +++ b/mario/main.cpp @@ -147,6 +147,22 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) { getVisitor( *moving_objects[i], *scene, quit, coin_count, moving_objects ); scene->visitCollisions( *moving_objects[i], *visitor ); moving_objects[i]->handleVisitor( *visitor ); + auto rightmost_pos = moving_objects[i]->getAbsolutePos().getX() + + moving_objects[i]->getDoubleRect().second.getX(); + if(rightmost_pos < 0) { + moving_objects[i]->destroy(); + std::cout << "DESTRUCTION!!!!" << std::endl; + } + } + std::vector killed_indices{}; + for(size_t i = 0; i < moving_objects.size(); i++) { + if(moving_objects[i]->getKilled()) { + killed_indices.push_back(i); + } + } + std::reverse( killed_indices.begin(), killed_indices.end() ); + for(auto &index : killed_indices) { + moving_objects.erase(moving_objects.begin() + index); } if ( coin_count != prev_coin_count ) {