I added in a few additional modules into the GameSession code so the game now has 3 balls per play and it displays the total in the top left. I also made a “tilt” feature that is triggered by the space bar on hte Ball object that may help in the event the ball does get stuck.
Added to GameSession:
[SerializeField] TextMeshProUGUI ballText;
[SerializeField] int ballCount = 3;
Added to GameSession Start:
ballText.text = "Balls: " + ballCount.ToString();
Added to GameSession:
public int AdjustBallCount(int Caller)
{
if (Caller == 1)
{
ballCount--;
}
else if (Caller == 2)
{
ballCount++;
}
ballText.text = "Balls: " + ballCount.ToString();
return ballCount;
}
Added to LoseCollider:
int ballCount = FindObjectOfType<GameSession>().AdjustBallCount(1);
if (ballCount == 0)
{
SceneManager.LoadScene("Game Over");
}
else
{
gameBall.hasStarted = false;
gameBall.Update();
}
Added to Ball Update:
if(Input.GetKeyDown(KeyCode.Space))
{
TiltBall();
}
Added to Ball:
public void TiltBall()
{
tiltCount--;
if (tiltCount > 0)
{
Vector2 originalVelocity = new Vector2
(myRigidBody2d.transform.position.x,
myRigidBody2d.transform.position.y);
Vector2 velocityBump = new Vector2
(myRigidBody2d.transform.position.x,
Random.Range(0f, randomFactor));
myRigidBody2d.velocity += velocityBump;
myRigidBody2d.velocity = originalVelocity;
}
}
I think the tilt count be better executed, but it works for the time being. Would like to add to the tiltCount after a certain period of time.
I also altered where the pointsPerBlockDestroyed was housed. Given that larger blocks that take more damage to destroy should be worth more, I pushed that field over to the Block script. I added a call to the AddToScore module of the GameSession type, passing the destroyed block’s value.
Added to Block :
[SerializeField] int pointsPerBlockDestroyed = 5;
Added to Block DestroyBlock:
FindObjectOfType<GameSession>().AddToScore(pointsPerBlockDestroyed);
Updated GameSession AddToScore:
public void AddToScore(int pointsPerBlockDestroyed)
{
currentScore += pointsPerBlockDestroyed;
scoreText.text = currentScore.ToString();
}