Ejemplo n.º 1
0
 void Start()
 {
     // Cache references
     spawner     = GetComponent <GemSpawner>();
     destroyer   = GetComponent <GemDestroyer>();
     myCollider  = GetComponent <Collider2D>();
     container   = GameObject.Find("Gem Container").transform;
     audioSource = GetComponent <AudioSource>();
     // Initialize lists and arrays
     gemTable   = new int[COL_NUM, ROW_NUM];
     gemColumns = new List <Gem> [COL_NUM];
     for (int x = 0; x < COL_NUM; x++)
     {
         gemColumns[x] = new List <Gem>();
     }
     // Fill board with gems
     spawner.SpawnGems(true);
     // Break possible matches
     gemsToDestroy = MatchLib.CheckMatches(gemTable);
     // And keep doing so until there are no matches left
     while (gemsToDestroy.Count > 0)
     {
         destroyer.DestroyGems(true);
         spawner.SpawnGems(true);
         gemsToDestroy = MatchLib.CheckMatches(gemTable);
     }
     // Make sure there are possible moves for the player and no unbroken matches
     if (!MatchLib.MovementIsPossible(gemTable))
     {
         MatchLib.Shuffle(gemColumns, gemTable);
     }
     // Disable collider
     myCollider.enabled = false;
 }
Ejemplo n.º 2
0
 // Hierarchy of actions: only one action per update: one gem breaking or one gem spawning
 void Update()
 {
     // Check if can update something in this frame
     if (dontUpdate)
     {
         // Reset flag
         dontUpdate = false;
         // Don't allow players to input
         myCollider.enabled = true;
         return;
     }
     // Needs to break gems
     if (gemsToDestroy.Count > 0)
     {
         // Check if a new move was made
         if (moveMade)
         {
             // Clear flag
             moveMade = false;
             // Disable player input by putting a collider in front of the gems
             myCollider.enabled = true;
             // Reset combo counter
             comboCounter = 0;
         }
         // Update combo counter
         comboCounter++;
         // Destroy gems and get the points
         int score = destroyer.DestroyGems() * basePoints * comboCounter;
         gemsToDestroy.Clear();
         // Make score effects
         audioSource.PlayOneShot(destroySound);
         if (scorePF)
         {
             ScoreEffect effect = Instantiate(
                 scorePF,
                 transform.position + Vector3.up * ROW_NUM * .6f,
                 Quaternion.identity
                 ).GetComponent <ScoreEffect>();
             effect.SetScore(score);
             effect.transform.SetParent(transform);
         }
         // Add points to total
         totalScore += score;
         // Flag to move and spawn gems
         needToSpawnGems = true;
     }
     // Needs to move and spawn gems
     else if (needToSpawnGems)
     {
         // Clear flag
         needToSpawnGems = false;
         spawner.SpawnGems();
         // Check if more matches were made
         gemsToDestroy = MatchLib.CheckMatches(gemTable);
         // If not, check if new moves are possible
         if (gemsToDestroy.Count == 0 && !MatchLib.MovementIsPossible(gemTable))
         {
             // Shuffle until a new board with possible moves and no matches is made
             MatchLib.Shuffle(gemColumns, gemTable);
         }
     }
     // Else, nothing is happening. Allow player Input
     else
     {
         myCollider.enabled = false;
     }
 }
Ejemplo n.º 3
0
    public GemSelect Select(Gem gem)
    {
        // Is this the first gem selected?
        if (!gem1)
        {
            // Mark this gem
            gem1 = gem;
            // Play selection sound
            audioSource.PlayOneShot(selectSound);
            return(GemSelect.Select);
        }
        // Is it the same as the first one?
        if (gem1 == gem)
        {
            // De-select the gem
            gem1 = null;
            // Play selection sound
            audioSource.PlayOneShot(selectSound);
            return(GemSelect.Deselect);
        }
        // Are the gems adjacent? (i.e. distance = 1 < sqrt(2))
        if (Vector2.Distance(gem1.transform.localPosition, gem.transform.localPosition) > 1.1f)
        {
            // Unmark gem 1
            gem1.SetNotSelected();
            gem1 = null;
            // Play selection sound
            audioSource.PlayOneShot(selectSound);
            return(GemSelect.NotValid);
        }
        // Check validity of second gem selected
        gem2 = gem;
        // Get a copy of the table of gem types
        int[,] table = board.GetTableCopy();
        // Swap gems and check validity
        int x1, y1, x2, y2;

        x1            = gem1.GetCol();
        y1            = gem1.GetRow();
        x2            = gem2.GetCol();
        y2            = gem2.GetRow();
        table[x1, y1] = gem2.GetGemType();
        table[x2, y2] = gem1.GetGemType();
        // Check if this move makes a match
        List <int[]> matches = MatchLib.CheckMatches(table);

        if (matches.Count > 0)
        {
            // Swap gem positions
            gem1.MoveToCoordinate(x2, y2);
            gem2.MoveToCoordinate(x1, y1);
            // Swap gem positions in the board manager's tables
            board.SwapGems(x1, y1, x2, y2);
            // Pass on the list of matched gems
            board.SetGemsToDestroy(matches);
            // Notify gem1 to set the color back to normal
            gem1.SetNotSelected();
            // Unmark gems
            gem1 = null;
            gem2 = null;
            // Play no sound, the board will play the destroy sound next
            return(GemSelect.Valid);
        }
        // else
        // Just change the first gem's color and unmark them
        gem1.SetNotSelected();
        // Unmark gems
        gem1 = null;
        gem2 = null;
        // Play selection sound
        audioSource.PlayOneShot(selectSound);
        return(GemSelect.NotValid);
    }