bool SSaPCollisionManager::collide_(CollisionObject* obj, void* cdata, CollisionCallBack callback) const { static const unsigned int CUTOFF = 100; DummyCollisionObject dummyHigh(AABB(obj->getAABB().max_)); bool coll_res = false; std::vector<CollisionObject*>::const_iterator pos_start1 = objs_x.begin(); std::vector<CollisionObject*>::const_iterator pos_end1 = std::upper_bound(pos_start1, objs_x.end(), &dummyHigh, SortByXLow()); unsigned int d1 = pos_end1 - pos_start1; if(d1 > CUTOFF) { std::vector<CollisionObject*>::const_iterator pos_start2 = objs_y.begin(); std::vector<CollisionObject*>::const_iterator pos_end2 = std::upper_bound(pos_start2, objs_y.end(), &dummyHigh, SortByYLow()); unsigned int d2 = pos_end2 - pos_start2; if(d2 > CUTOFF) { std::vector<CollisionObject*>::const_iterator pos_start3 = objs_z.begin(); std::vector<CollisionObject*>::const_iterator pos_end3 = std::upper_bound(pos_start3, objs_z.end(), &dummyHigh, SortByZLow()); unsigned int d3 = pos_end3 - pos_start3; if(d3 > CUTOFF) { if(d3 <= d2 && d3 <= d1) coll_res = checkColl(pos_start3, pos_end3, obj, cdata, callback); else { if(d2 <= d3 && d2 <= d1) coll_res = checkColl(pos_start2, pos_end2, obj, cdata, callback); else coll_res = checkColl(pos_start1, pos_end1, obj, cdata, callback); } } else coll_res = checkColl(pos_start3, pos_end3, obj, cdata, callback); } else coll_res = checkColl(pos_start2, pos_end2, obj, cdata, callback); } else coll_res = checkColl(pos_start1, pos_end1, obj, cdata, callback); return coll_res; }
void Game::loop() { while(App.isOpen()) { //Generic application stuff runEvents(); //Allow the player to query the state of controls player.control(); //Update the player based on its current state player.update(); //That cool formation logic you see invaderLogic(); App.clear(); //Draw the invaders for(Invader inv: invaders) { App.draw(inv.getRect()); } //An integer to keep track of our offset unsigned int i = 0; //Should we delete the invader at that offest? bool del = false; //If the player's bullet is in the air if(player.getBull()->getState()) { for(Invader inv: invaders) { //If it isn't colliding if(!checkColl(inv.getRect(),player.getBull()->getRect())) { //Continue checking ++i; continue; } else { //Set the delete flag and break, we've hit one! del = true; break; } } } //Flag to keep track of whether the player has been hit bool playerHit = false; for(Invader & inv: invaders) { //If the invader has a bullet in the air... if(inv.getBull()->getState()) { //Check for a collision if(checkColl(player.getRect(), inv.getBull()->getRect())) { //Close as game over if there was one playerHit = true; App.close(); break; } } } //Delete the invader at offset i if(del) { invaders.erase(invaders.begin()+i); player.getBull()->resetBull(player.getRect().getPosition()); } //Draw all active invader bullets for(Invader inv: invaders) { if(inv.getBull()->getState()) { App.draw(inv.getBull()->getRect()); } } //Draw the player bullet if active if(player.getBull()->getState()) { App.draw(player.getBull()->getRect()); } //draw the player App.draw(player.getRect()); App.display(); } }