Time for action - updating GameBoard to support animated pieces

  1. In the declarations section of the GameBoard class, add three dictionaries:
    public Dictionary<string, FallingPiece> fallingPieces =
    new Dictionary<string, FallingPiece>();
    public Dictionary<string, RotatingPiece> rotatingPieces =
    new Dictionary<string, RotatingPiece>();
    public Dictionary<string, FadingPiece> fadingPieces =
    new Dictionary<string, FadingPiece>();
    
  2. Add methods to the GameBoard class to create new falling piece entries in the dictionaries:
    public void AddFallingPiece(int X, int Y,
    string PieceName, int VerticalOffset)
    {
    fallingPieces[X.ToString() + "_" + Y.ToString()] = new
    FallingPiece(PieceName, VerticalOffset);
    }
    public void AddRotatingPiece(int X, int Y,
    string PieceName, bool Clockwise)
    {
    rotatingPieces[X.ToString() + "_" + Y.ToString()] = new
    RotatingPiece(PieceName, Clockwise);
    }
    public void AddFadingPiece(int X, int Y, string PieceName)
    {
    fadingPieces[X.ToString() + "_" + Y.ToString()] = new
    FadingPiece(PieceName,"W");
    }
    
  3. Add the ArePiecesAnimating() method to the GameBoard class:
    public bool ArePiecesAnimating()
    {
    if ((fallingPieces.Count == 0) &&
    (rotatingPieces.Count == 0) &&
    (fadingPieces.Count == 0))
    {
    return false;
    }
    else
    {
    return true;
    }
    }
    
  4. Add the UpdateFadingPieces() method to the GameBoard class:
    private void UpdateFadingPieces()
    {
    Queue<string> RemoveKeys = new Queue<string>();
    foreach (string thisKey in fadingPieces.Keys)
    {
    fadingPieces[thisKey].UpdatePiece();
    if (fadingPieces[thisKey].alphaLevel == 0.0f)
    RemoveKeys.Enqueue(thisKey.ToString());
    }
    while (RemoveKeys.Count > 0)
    fadingPieces.Remove(RemoveKeys.Dequeue());
    }
    
  5. Add the UpdateFallingPieces() method to the GameBoard class:
    private void UpdateFallingPieces()
    {
    Queue<string> RemoveKeys = new Queue<string>();
    foreach (string thisKey in fallingPieces.Keys)
    {
    fallingPieces[thisKey].UpdatePiece();
    if (fallingPieces[thisKey].VerticalOffset == 0)
    RemoveKeys.Enqueue(thisKey.ToString());
    }
    while (RemoveKeys.Count > 0)
    fallingPieces.Remove(RemoveKeys.Dequeue());
    }
    
  6. Add the UpdateRotatingPieces() method to the GameBoard class:
    private void UpdateRotatingPieces()
    {
    Queue<string> RemoveKeys = new Queue<string>();
    foreach (string thisKey in rotatingPieces.Keys)
    {
    rotatingPieces[thisKey].UpdatePiece();
    if (rotatingPieces[thisKey].rotationTicksRemaining == 0)
    RemoveKeys.Enqueue(thisKey.ToString());
    }
    while (RemoveKeys.Count > 0)
    rotatingPieces.Remove(RemoveKeys.Dequeue());
    }
    
  7. Add the UpdateAnimatedPieces() method to the GameBoard class:
    public void UpdateAnimatedPieces()
    {
    if (fadingPieces.Count == 0)
    {
    UpdateFallingPieces();
    UpdateRotatingPieces();
    }
    else
    {
    UpdateFadingPieces();
    }
    }
    

What just happened?

After declaring the three Dictionary objects, we have three methods used by the GameBoard class to create them when necessary. In each case, the key is built in the form "X_Y", so an animated piece in column 5 on row 4 will have a key of "5_4". Each of the three Add... methods simply pass the parameters along to the constructor for the appropriate piece types after determining the key to use.

When we begin drawing the animated pieces, we want to be sure that animations finish playing before responding to other input or taking other game actions (like creating new pieces). The ArePiecesAnimating() method returns "true" if any of the Dictionary objects contain entries. If they do, we will not process any more input or fill empty holes on the game board until they have completed.

The UpdateAnimatedPieces() method will be called from the game's Update() method and is responsible for calling the three different update methods above (UpdateFadingPiece(), UpdateFallingPiece(), and UpdateRotatingPiece()) for any animated pieces currently on the board. The first line in each of these methods declares a Queue object called RemoveKeys. We will need this because C# does not allow you to modify a Dictionary (or List, or any of the similar "generic collection" objects) while a foreach loop is processing them.

A Queue is yet another generic collection object that works like a line at the bank. People stand in a line and await their turn to be served. When a bank teller is available, the first person in the line transacts his/her business and leaves. The next person then steps forward. This type of processing is known as FIFO, or First In, First Out.

Using the Enqueue() and Dequeue() methods of the Queue class, objects can be added to the Queue (Enqueue()) where they await processing. When we want to deal with an object, we Dequeue() the oldest object in the Queue and handle it. Dequeue() returns the first object waiting to be processed, which is the oldest object added to the Queue.

Tip

Collection classes

C# provides a number of different "collection" classes, such as the Dictionary, Queue, List, and Stack objects. Each of these objects provides different ways to organize and reference the data in them. For information on the various collection classes and when to use each type, see the following MSDN entry: http://msdn.microsoft.com/en-us/library/6tc79sx1(VS.80).aspx

Each of the update methods loops through all of the keys in its own Dictionary and in turn calls the UpdatePiece() method for each key. Each piece is then checked to see if its animation has completed. If it has, its key is added to the RemoveKeys queue. After all of the pieces in the Dictionary have been processed, any keys that were added to RemoveKeys are then removed from the Dictionary, eliminating those animated pieces.

If there are any FadingPieces currently active, those are the only animated pieces that UpdateAnimatedPieces() will update. When a row is completed, the scoring tiles fade out, the tiles above them fall into place, and new tiles fall in from above. We want all of the fading to finish before the other tiles start falling (or it would look strange as the new tiles pass through the fading old tiles).

Fading pieces

In the discussion of UpdateAnimatedPieces(), we stated that fading pieces are added to the board whenever the player completes a scoring chain. Each piece in the chain is replaced with a fading piece.

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

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