const float MaxFloodCounter = 100.0f; float floodCount = 0.0f; float timeSinceLastFloodIncrease = 0.0f; float timeBetweenFloodIncreases = 1.0f; float floodIncreaseAmount = 0.5f;
Update()
method of Game1.cs
, add the following code to keep track of the increasing flood waters right after the timeSinceLastInput
variable is updated in the GameState.Playing
case section:timeSinceLastFloodIncrease += (float)gameTime.ElapsedGameTime.TotalSeconds; if (timeSinceLastFloodIncrease >= timeBetweenFloodIncreases) { floodCount += floodIncreaseAmount; timeSinceLastFloodIncrease = 0.0f; if (floodCount >= MaxFloodCounter) { gameOverTimer = 8.0f; gameState = GameStates.GameOver; } }
CheckScoringChain()
method of the Game1 class by adding the following to decrease the flood counter when the player scores. Place this code right after playerScore += DetermineScore(WaterChain.Count)
;floodCount = MathHelper.Clamp(floodCount - (DetermineScore(WaterChain.Count)/10), 0.0f, 100.0f);
The flood itself is represented as a percentage. When the floodCount
reaches 100
(MaxFloodCounter), the laboratory has completely flooded and the game is over. In addition to these two declarations, we also need to track how rapidly the flood increases (timeSinceLastFloodIncrease and timeBetweenFloodIncreases)
, and the rate at which the water rises (floodIncreaseAmount).
The timing on the flood increases is handled the same way input pacing is handled: a timer is incremented based on the elapsed game time until it reaches a threshold value. When it does, the timer is reset, and the floodCount
variable is increased by the floodIncreaseAmount
value.
When this increase takes place, we check to see if the floodCount
has reached MaxFloodCount
, indicating that the facility is flooded. If it has, an eight second timer is set for gameOverTimer
and the game state is set to GameOver
. Recall that in the GameOver
handler, the gameOverTimer
determines how long the G A M E O V E R!
text will be displayed before the game switches back to the title screen.
Finally, in step three, the floodCount
variable needs to be decreased each time the player completes a scoring chain. MathHelper.Clamp()
is used to subtract the score value (divided by 10)
from the floodCount
, while keeping the value between 0.0f
and 100.0f
.
If you open the Background.png
file in an image viewer, you will see that there is a full water tank floating inside the space on the playfield where game pieces get displayed. Since we always draw opaque game piece backgrounds over this area, so far we have not seen this portion of the image during game play.
We can use SpriteBatch.Draw()
to cut out pieces of this full water tank and superimpose it over the empty tank on the right side of the game screen as the facility fills with water. The deeper the water gets, the more of the hidden water tank image we transfer to the visible tank on the screen, working our way up from the bottom:
3.17.128.129