Ejemplo n.º 1
0
 private void Unselect()
 {
     // Hide the selection ui
     this.isSelected = false;
     Match3Block.currentSelection = null;
     this.Selection.gameObject.SetActive(false);
 }
Ejemplo n.º 2
0
 private void Select()
 {
     // Show the selection ui
     this.isSelected = true;
     Match3Block.currentSelection = this;
     this.Selection.gameObject.SetActive(true);
 }
Ejemplo n.º 3
0
        public static Match3Block Create(Match3Block prefab, int x, int y, Vector3 position)
        {
            // Create a new block and setup the params
            Match3Block block = GameObject.Instantiate <Match3Block>(prefab, position, prefab.transform.rotation);

            block.BoardX = x;
            block.BoardY = y;
            return(block);
        }
Ejemplo n.º 4
0
        public bool IsNextTo(Match3Block block)
        {
            // Check up, down, left, and right
            if ((this.BoardX == block.BoardX && (this.BoardY == block.BoardY - 1 || this.BoardY == block.BoardY + 1)) ||
                (this.BoardY == block.BoardY && (this.BoardX == block.BoardX - 1 || this.BoardX == block.BoardX + 1)))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 5
0
        private bool CheckPositionMatch(Match3Block toCheck, int x, int y, ref int count, ref List <Match3Block> found)
        {
            // If they match add it to the list and return true
            var other = DemoMatch3.Instance.GetBlockAt(x, y);

            if (toCheck.IsMatchTo(other))
            {
                found.Add(other);
                count++;
                return(true);
            }

            // Not a match
            return(false);
        }
Ejemplo n.º 6
0
        private void BeginSwap(Match3Block other)
        {
            // Ignore matching blocks since a swap would do nothing
            if (this.IsMatchTo(other))
            {
                return;
            }

            Match3Block.isPerformingSwap = true;

            this.otherSwappingBlock = other;
            this.swapTime           = 0;
            this.swapPosition1      = this.transform.position;
            this.swapPosition2      = other.transform.position;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Begin the mechanism for find matches and updating the board
        /// </summary>
        /// <param name="start">One block that was just moved</param>
        /// <param name="other">The other block that was moved</param>
        public void StartFindingMatchesForSwap(Match3Block start, Match3Block other)
        {
            // Set the flag to prevent any other swaps from taking place until we are done
            this.IsRunningBoardChange = true;

            // Find matches with the first block
            List <Match3Block> found = new List <Match3Block>();
            int xCount;
            int yCount;

            this.FindMatchesFrom(start, ref found, out xCount, out yCount);
            if (xCount >= 2 || yCount >= 2)
            {
                found.Add(start);
                this.ClearMatches(found);
            }

            // Find matches with the second block
            found.Clear();
            this.FindMatchesFrom(other, ref found, out xCount, out yCount);
            if (xCount >= 2 || yCount >= 2)
            {
                found.Add(other);
                this.ClearMatches(found);
            }

            // Fill up the board by dropping blocks from the top
            int count = this.RefillBoard();

            if (count > 0)
            {
                this.Animate(this.player);
            }
            else
            {
                turnCount++;
                if (turnCount > 1)
                {
                    turnCount = 0;
                    this.Animate(this.enemy);
                }
            }

            // Clear the flag to allow swaps again
            this.IsRunningBoardChange = false;
        }
Ejemplo n.º 8
0
        void Start()
        {
            // Create a new board to store the blocks
            this.board = new Match3Block[this.Width, this.Height];

            // Calculate the start position to center the board
            Vector2 offset = BlockPrefab.GetComponent <SpriteRenderer>().bounds.size * 0.9f;
            float   startX = this.BoardPosition.position.x - offset.x * this.Width * 0.5f + offset.x * 0.5f;
            float   startY = this.BoardPosition.position.y - offset.y * this.Height * 0.5f + offset.y * 0.5f;

            Sprite[] previousLeft  = new Sprite[this.Height];
            Sprite   previousBelow = null;

            // Add each block making sure they don't immediately match
            for (int x = 0; x < this.Width; x++)
            {
                for (int y = 0; y < this.Height; y++)
                {
                    // Create the block
                    Vector3     position = new Vector3(startX + (offset.x * x), startY + (offset.y * y), 0);
                    Match3Block block    = Match3Block.Create(this.BlockPrefab, x, y, position);

                    // Start out with all sprites available and remove matching ones
                    List <Sprite> possibleBlocks = new List <Sprite>();
                    possibleBlocks.AddRange(this.BlockSprites);
                    possibleBlocks.Remove(previousLeft[y]);
                    possibleBlocks.Remove(previousBelow);

                    // Set the new sprite
                    Sprite picked = possibleBlocks[Random.Range(0, possibleBlocks.Count)];
                    block.SetSprite(picked);

                    previousLeft[y] = picked;
                    previousBelow   = picked;

                    // Store it in our board
                    this.board[x, y] = block;
                }
            }

            // Add an enemy
            this.SpawnEnemy();
        }
Ejemplo n.º 9
0
        private void FindMatchesFrom(Match3Block startBlock, ref List <Match3Block> found, out int xCount, out int yCount)
        {
            xCount = 0;
            yCount = 0;

            // Ignore blocks with no sprite
            if (startBlock.IsEmpty)
            {
                return;
            }

            // Loop through left and right looking for matches, quit when we reach the edge or a non-matching block
            for (int x = startBlock.BoardX - 1; x >= 0; x--)
            {
                if (!CheckPositionMatch(startBlock, x, startBlock.BoardY, ref xCount, ref found))
                {
                    break;
                }
            }
            for (int x = startBlock.BoardX + 1; x < this.Width; x++)
            {
                if (!CheckPositionMatch(startBlock, x, startBlock.BoardY, ref xCount, ref found))
                {
                    break;
                }
            }

            // Loop through up and down looking for matches, quit when we reach the edge or a non-matching block
            for (int y = startBlock.BoardY - 1; y >= 0; y--)
            {
                if (!CheckPositionMatch(startBlock, startBlock.BoardX, y, ref yCount, ref found))
                {
                    break;
                }
            }
            for (int y = startBlock.BoardY + 1; y < this.Height; y++)
            {
                if (!CheckPositionMatch(startBlock, startBlock.BoardX, y, ref yCount, ref found))
                {
                    break;
                }
            }
        }
Ejemplo n.º 10
0
        private void EndSwap()
        {
            Match3Block.isPerformingSwap = false;

            Match3Block other = this.otherSwappingBlock;

            this.otherSwappingBlock = null;

            // Swap the sprites
            Sprite temp = other.spriteRenderer.sprite;

            other.spriteRenderer.sprite = this.spriteRenderer.sprite;
            this.spriteRenderer.sprite  = temp;

            this.transform.position  = this.swapPosition1;
            other.transform.position = this.swapPosition2;

            // Look for 3's in a row
            DemoMatch3.Instance.StartFindingMatchesForSwap(this, other);
        }
Ejemplo n.º 11
0
 public void MoveSpriteFrom(Match3Block source)
 {
     // Cut and paste the sprite
     this.spriteRenderer.sprite   = source.spriteRenderer.sprite;
     source.spriteRenderer.sprite = null;
 }
Ejemplo n.º 12
0
 public bool IsMatchTo(Match3Block other)
 {
     // Do the blocks have the same sprite?
     return(!this.IsEmpty && other.spriteRenderer.sprite == this.spriteRenderer.sprite);
 }