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;
     }
 }