Game Development Reference
In-Depth Information
Now we need to perform the collision checks. The best way to do this right now is to
add this to the Game class during our update loop. We'll add a new method called
ProcessCollisions that will be called at the end of the Update method. We also
need to determine if the enemies are still on screen, and to do this we will test if
an enemy is inside a collider that represents the screen. Create a RectangleCol-
lider and create it inside Game::LoadContent by setting the size of the collider
to the screen size - but make sure to add one to the width so that the enemies aren't
destroyed as soon as they spawn outside of the screen.
void Game::ProcessCollisions()
{
auto playerCollider = _player->GetCollider();
for (auto enemy : _enemies)
{
if (enemy->GetIsAlive())
{
auto enemyCollider = enemy->GetCollider();
// Check if collided against the player
if
(enemyCollider->CollidesWith(playerCollider))
enemy->SetIsAlive(false);
// Check if the enemy has exited the play
area
if (!enemyCollider->CollidesWith(_bounds))
enemy->SetIsAlive(false);
}
}
}
In here we simply go through each enemy and if it is active we do two things. First
we check if the enemy has collided with the player and, if it has, we destroy the en-
emy. Later on we'll add in a gameplay consequence for this.
The other thing we need to do is to check if the enemy is still inside the screen. This
is done by checking if the enemy no longer collides with the screen collider and des-
troying it if it is outside.
Search WWH ::




Custom Search