/// End contact void AFP::ContactListener::EndContact(b2Contact* contact) { SceneNode::Pair collisionPair; if (!createPair(contact, collisionPair)) { return; } // Sensor ends contact with scene if (matchesCategories(collisionPair, Category::Sensor, Category::Scene)) { auto& sensor = static_cast<Sensor&>(*collisionPair.first); sensor.endContact(); } // Sensor ends contact with tile else if (matchesCategories(collisionPair, Category::Sensor, Category::Tile)) { auto& sensor = static_cast<Sensor&>(*collisionPair.first); sensor.endContact(); } // Sensor ends contact with player else if (matchesCategories(collisionPair, Category::Sensor, Category::PlayerCharacter)) { auto& sensor = static_cast<Sensor&>(*collisionPair.first); auto& player = static_cast<Character&>(*collisionPair.second); sensor.endContact(player); } }
void World::handleCollisions() { std::set<SceneNode::Pair> collisionPairs; mSceneGraph.CheckSceneCollision( mSceneGraph, collisionPairs ); for ( SceneNode::Pair pair : collisionPairs ) { if ( matchesCategories( pair, Category::PlayerAircraft, Category::EnemyAircraft ) ) { auto& player = static_cast<Aircraft&>( *pair.first ); auto& enemy = static_cast<Aircraft&>( *pair.second ); player.Damage( enemy.GetHitpoints() ); enemy.Destroy(); } else if ( matchesCategories( pair, Category::PlayerAircraft, Category::Pickup ) ) { auto& player = static_cast<Aircraft&>( *pair.first ); auto& pickup = static_cast<Pickup&>( *pair.second ); pickup.Apply( player ); pickup.Destroy(); player.PlayLocalSound( mCommandQueue, SoundEffect::CollectPickup ); } else if ( matchesCategories( pair, Category::EnemyAircraft, Category::AlliedProjectile ) || matchesCategories( pair, Category::PlayerAircraft, Category::EnemyProjectile ) ) { auto& aircraft = static_cast<Aircraft&>( *pair.first ); auto& projectile = static_cast<Projectile&>( *pair.second ); aircraft.Damage( projectile.GetDamage() ); projectile.Destroy(); } } }
void World::handleCollisions() { std::set<SceneNode::Pair> collisionPairs; mSceneGraph.checkSceneCollision(mSceneGraph, collisionPairs); FOREACH(SceneNode::Pair pair, collisionPairs) { if (matchesCategories(pair, Category::PlayerAircraft, Category::EnemyAircraft)) { auto& player = static_cast<Aircraft&>(*pair.first); auto& enemy = static_cast<Aircraft&>(*pair.second); // Collision: Player damage = enemy's remaining HP player.damage(enemy.getHitpoints()); enemy.destroy(); } else if (matchesCategories(pair, Category::PlayerAircraft, Category::Pickup)) { auto& player = static_cast<Aircraft&>(*pair.first); auto& pickup = static_cast<Pickup&>(*pair.second); // Apply pickup effect to player, destroy projectile pickup.apply(player); pickup.destroy(); player.playLocalSound(mCommandQueue, SoundEffect::CollectPickup); } else if (matchesCategories(pair, Category::EnemyAircraft, Category::AlliedProjectile) || matchesCategories(pair, Category::PlayerAircraft, Category::EnemyProjectile)) { auto& aircraft = static_cast<Aircraft&>(*pair.first); auto& projectile = static_cast<Projectile&>(*pair.second); // Apply projectile damage to aircraft, destroy projectile aircraft.damage(projectile.getDamage()); projectile.destroy(); } } }
void World::handleCollisions() { std::set<SceneNode::Pair> collisionPairs; mSceneGraph.checkSceneCollision(mSceneGraph, collisionPairs); for (SceneNode::Pair pair : collisionPairs) { if (matchesCategories(pair, Category::PlayerSpaceship, Category::Planet)) { auto& player = static_cast<Spaceship&>(*pair.first); auto& planet = static_cast<Planet&>(*pair.second); player.destroy(); } } }
void World::handleCollisions() { std::set<SceneNode::Pair> collisionPairs; mSceneGraph.checkSceneCollision(mSceneGraph, collisionPairs); for(SceneNode::Pair pair : collisionPairs) { if (matchesCategories(pair, Category::Enemy, Category::Trap)) { auto& kid = static_cast<Entity&>(*pair.first); auto& trap = static_cast<Entity&>(*pair.second); if(!kid.isCollidable()) break; kid.destroy(); trap.destroy(); mSounds.play(Sounds::CarrotHit); mCommandQueue.push(mKidKilledCommand); if(randomInt(5) == 0) { mSounds.play(Sounds::Laugh); mPlayer->emote(); } } else if (matchesCategories(pair, Category::Enemy, Category::Explosion)) { auto& kid = static_cast<Entity&>(*pair.first); if(!kid.isCollidable()) break; kid.destroy(); mSounds.play(Sounds::CarrotHit); mCommandQueue.push(mKidKilledCommand); if(randomInt(2) == 0) { mSounds.play(Sounds::Laugh); mPlayer->emote(); } } else if (matchesCategories(pair, Category::Trap, Category::Trap)) { auto& trap = static_cast<Entity&>(*pair.first); trap.destroy(); } else if (matchesCategories(pair, Category::Trap, Category::Explosion)) { auto& trap = static_cast<Entity&>(*pair.first); trap.destroy(); } else if (matchesCategories(pair, Category::Player, Category::Explosion)) { auto& player = static_cast<Entity&>(*pair.first); mPlayer->bubble()->setString("Noooooooooooo!"); player.destroy(); } else if (matchesCategories(pair, Category::Player, Category::Enemy)) { auto& player = static_cast<Entity&>(*pair.first); auto& kid = static_cast<Entity&>(*pair.second); if(!kid.isCollidable()) break; mPlayer->bubble()->setString("Arrrrggg!"); player.destroy(); } else if (matchesCategories(pair, Category::FriendlyBullet, Category::Enemy)) { auto& bullet = static_cast<Entity&>(*pair.first); auto& kid = static_cast<Entity&>(*pair.second); if(!kid.isCollidable()) break; bullet.destroy(); kid.destroy(); mSounds.play(Sounds::CarrotHit); if(randomInt(3) == 0) { mSounds.play(Sounds::Laugh); mPlayer->emote(); } mCommandQueue.push(mKidKilledCommand); } else if (matchesCategories(pair, Category::FriendlyBullet, Category::Trap)) { auto& bullet = static_cast<Entity&>(*pair.first); auto trap = dynamic_cast<TntTrap*>(pair.second); if(trap) { bullet.destroy(); trap->destroy(); } } } }
/// Begin contact void AFP::ContactListener::BeginContact(b2Contact* contact) { SceneNode::Pair collisionPair; // Try to create a pair and return if not a valid pair if (!createPair(contact, collisionPair)) { return; } // Check collision pairs // Allied bullet collides with enemy or enemy bullet collides with allied character if(matchesCategories(collisionPair, Category::Character, Category::Projectile)) { // Cast to correct classes auto& character = static_cast<Character&>(*collisionPair.first); auto& projectile = static_cast<Projectile&>(*collisionPair.second); character.damage(projectile.getDamage()); projectile.destroy(); } // Player collides with a collectable else if (matchesCategories(collisionPair, Category::PlayerCharacter, Category::Collectable)) { // Cast into correct classes auto& player = static_cast<Character&>(*collisionPair.first); auto& collectable = static_cast<Collectable&>(*collisionPair.second); if (collectable.apply(player)) { collectable.destroy(); } } // Projectile collides with tile else if (matchesCategories(collisionPair, Category::Projectile, Category::Tile)) { auto& projectile = static_cast<Projectile&>(*collisionPair.first); auto& tile = static_cast<Tile&>(*collisionPair.second); tile.damage(projectile.getDamage()); projectile.destroy(); } // Projectile collides with walls else if (matchesCategories(collisionPair, Category::Projectile, Category::Scene)) { auto& projectile = static_cast<Projectile&>(*collisionPair.first); projectile.destroy(); } // Sensor collides with scene else if (matchesCategories(collisionPair, Category::Sensor, Category::Scene)) { auto& sensor = static_cast<Sensor&>(*collisionPair.first); sensor.beginContact(); } // Sensor collides with player else if (matchesCategories(collisionPair, Category::Sensor, Category::PlayerCharacter)) { auto& sensor = static_cast<Sensor&>(*collisionPair.first); auto& player = static_cast<Character&>(*collisionPair.second); sensor.beginContact(player); } // Sensor collides with tile else if (matchesCategories(collisionPair, Category::Sensor, Category::Tile)) { auto& sensor = static_cast<Sensor&>(*collisionPair.first); sensor.beginContact(); } }