Example #1
0
        private void OnCollisionEnter2D(Collision2D collision)
        {
            Bubble bubble     = collision.gameObject.GetComponent <Bubble>();
            bool   hitTopWall = collision.gameObject.tag.Equals("TopWall");

            if (bubble == null && hitTopWall == false)
            {
                return;
            }
            _rigidbody.bodyType = RigidbodyType2D.Static;
            if (_hasHitSomething == false)
            {
                HitSomething.Invoke(this, transform.position);
                _hasHitSomething = true;
            }
        }
        private Vector2Int FindPositionOfBubble(Bubble bubble)
        {
            Vector2Int result = Vector2Int.zero;

            for (int rowIndex = 0; rowIndex < _height; rowIndex++)
            {
                for (int columnIndex = 0; columnIndex < _width; columnIndex++)
                {
                    if (_grid[rowIndex, columnIndex] == bubble)
                    {
                        result = new Vector2Int(rowIndex, columnIndex);
                    }
                }
            }

            return(result);
        }
 public void ShiftDown(int amount)
 {
     // Searching from bottom up.
     for (int rowIndex = _height - 1; rowIndex >= 0; rowIndex--)
     {
         for (int columnIndex = 0; columnIndex < _width; columnIndex++)
         {
             Bubble currentBubble = _grid[rowIndex, columnIndex];
             if (currentBubble == null)
             {
                 continue;
             }
             if (rowIndex == _height - 1)
             {
                 GameManager.Instance.GameState = StateType.GameOver; break;
             }
             _grid[rowIndex + 1, columnIndex]  = currentBubble;
             _grid[rowIndex, columnIndex]      = null;
             currentBubble.transform.position += Vector3.down * amount;
         }
     }
 }
Example #4
0
 public bool CompareTypes(Bubble other)
 {
     return(_type == other.Type);
 }
 public void ListenForCollision(Bubble bubble)
 {
     bubble.HitSomething.AddListener(AddAndSnap);
 }
 public void AddToGrid(Vector2Int position, Bubble bubble)
 {
     _grid[position.x, position.y] = bubble;
 }
        private List <Bubble> FindNeighbors(Vector2Int location, bool isCheckingColor = true)
        {
            List <Bubble> results = new List <Bubble>();

            Bubble thisBubble = _grid[location.x, location.y];

            if (thisBubble == null)
            {
                return(null);
            }

            // Check up.
            if (location.x > 0)
            {
                Bubble up = _grid[location.x - 1, location.y];
                if (up != null)
                {
                    bool isMatch = thisBubble.CompareTypes(up);
                    if (isMatch || !isCheckingColor)
                    {
                        results.Add(up);
                    }
                }
            }

            // Check down.
            if (location.x < _height - 1)
            {
                Bubble down = _grid[location.x + 1, location.y];
                if (down != null)
                {
                    bool isMatch = thisBubble.CompareTypes(down);
                    if (isMatch || !isCheckingColor)
                    {
                        results.Add(down);
                    }
                }
            }

            // Check left.
            if (location.y > 0)
            {
                Bubble left = _grid[location.x, location.y - 1];
                if (left != null)
                {
                    bool isMatch = thisBubble.CompareTypes(left);
                    if (isMatch || !isCheckingColor)
                    {
                        results.Add(left);
                    }
                }
            }

            // Check right.
            if (location.y < _width - 1)
            {
                Bubble right = _grid[location.x, location.y + 1];
                if (right != null)
                {
                    bool isMatch = thisBubble.CompareTypes(right);
                    if (isMatch || !isCheckingColor)
                    {
                        results.Add(right);
                    }
                }
            }

            // Check diagonal up right.
            if (location.x > 0 && location.y < _width - 1)
            {
                Bubble upRight = _grid[location.x - 1, location.y + 1];
                if (upRight != null)
                {
                    bool isMatch = thisBubble.CompareTypes(upRight);
                    if (isMatch)
                    {
                        results.Add(upRight);
                    }
                }
            }

            // Check diagonal down right.
            if (location.x < _height - 1 && location.y < _width - 1)
            {
                Bubble downRight = _grid[location.x + 1, location.y + 1];
                if (downRight != null)
                {
                    bool isMatch = thisBubble.CompareTypes(downRight);
                    if (isMatch)
                    {
                        results.Add(downRight);
                    }
                }
            }

            // Check diagonal down left.
            if (location.x < _height - 1 && location.y > 0)
            {
                Bubble downLeft = _grid[location.x + 1, location.y - 1];
                if (downLeft != null)
                {
                    bool isMatch = thisBubble.CompareTypes(downLeft);
                    if (isMatch)
                    {
                        results.Add(downLeft);
                    }
                }
            }

            // Check diagonal up left.
            if (location.x > 0 && location.y > 0)
            {
                Bubble upLeft = _grid[location.x - 1, location.y - 1];
                if (upLeft != null)
                {
                    bool isMatch = thisBubble.CompareTypes(upLeft);
                    if (isMatch)
                    {
                        results.Add(upLeft);
                    }
                }
            }

            return(results);
        }
        private void AddAndSnap(Bubble bubble, Vector3 positionOnCollision)
        {
            Vector2Int newPos = new Vector2Int(Mathf.RoundToInt(positionOnCollision.x), Mathf.RoundToInt(positionOnCollision.y));

            StartCoroutine(SnapToGrid(bubble.transform, newPos));
        }