Game Development Reference
In-Depth Information
ing them that the game is over, along with the score, and an invitation to tap the
screen to continue playing.
We need to set the text each time to ensure the score is up-to-date, and once that is
done we need to reposition the object so that it is in the center of the screen. To do
this we will add a helper method to the Renderer class to help us get the size of the
text block using the current font.
XMFLOAT2 Renderer::MeasureText(TextBlock *block)
{
XMVECTOR sizeVec =
_font->MeasureString(block->Text.data());
XMFLOAT2 sizeResult = XMFLOAT2(0, 0);
XMStoreFloat2(&sizeResult, sizeVec);
return sizeResult;
}
MeasureString uses the SpriteFont::MeasureString command and ex-
tracts the XMFLOAT2 object from the provided XMVECTOR to get the width and height
of the string in the text block. Once we have that we can apply a simple bit of math to
determine where the top-left of the text block should be so that the block is centered
on the screen.
The last step is to call EndGame when the player dies. This can be done with a quick
check of _ player->GetIsAlive() after your call to _ player->Damage() inside
Game::ProcessCollisions , as shown:
if (enemyCollider->CollidesWith(playerCollider))
{
enemy->SetIsAlive(false);
_player->Damage(25);
{
EndGame();
return;
}
}
Search WWH ::




Custom Search