Game components

The ball game is created with the following primary components:

  • GameController manages the overall game, including the current score. It subscribes to the BallGame's won and lost events, manages the score, and displays game data. As the game supports switching between ball game types, the GameController chooses and starts a random game.
  • GameDataManager saves and restores data for the game. It manages persistent storage of the player progress and high scores.
  • The BallGame component manages individual ball games. One is attached to each separate ball game. It uses the ThrowControl component to determine when the show is done and if the ball hit the goal, and then it invokes a won or lost event accordingly.
  • The ThrowControl component is attached to the ball. It reads user input and sets the ball in motion. ThrowControl sends events when the ball's starting position is reset and when the ball is thrown.
  • CollisionBehavior detects when the ball hits the goal collider and sends an event. This component is on the game's goal collider object.
  • AppStateManger handles the app phases required for Vuforia Smart Terrain, including the initial target recognition, scanning the table surface and props, and running and resetting the game.

The relationship between components and the events passed between them is shown in the following diagram:

Roughly, the app's messaging follows this flow:

  1. To begin a new game, the ball is moved to its starting position by ThrowControl's ReadyBall.
  2. The ball's ThrowControl detects user input and tosses the ball towards the goal (Throw).
  3. If the ball collides with the goal, the CollisionBehavior component signals the BallGame to validate the goal object was actually hit (CheckGoal). And BallGame remembers that state.
  4. We give the ball a chance to bounce around the court, whether or not it hit the goal.
  5. After a delay, ThrowControl resets the ball via ReadyBall.
  6. ReadyBall also tells the BallGame that the ball has reset (OnBallReset).
  7. At that point, OnBallReset notifies the GameController whether the previous throw resulted in a won or lost goal, which in turn updates the scoreboard.

An advantage of this architecture is we can have multiple BallGames in our app, for example, football, basketball, and so on, but just one GameController. ThrowControl and CollisionBehavior are reusable components that have no dependencies, signally the BallGame via events.

We will implement each of these script components as we build up the project. The app state phases for scanning the Smart Terrain is explained in more detail in the augmenting real world objects section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset