예제 #1
0
        /// <summary>
        /// Check if both corners are touching in another group, if so, combine groups
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public bool CanCombine(Direction dir)
        {
            Block other1 = null;
            Block other2 = null;

            Group otherGroup = null;

            switch (dir)
            {
            case Direction.up:
                other1 = data.topLeft.SameAdjacent(Direction.up);
                other2 = data.topRight.SameAdjacent(Direction.up);
                break;

            case Direction.right:
                other1 = data.topRight.SameAdjacent(Direction.right);
                other2 = data.bottomRight.SameAdjacent(Direction.right);
                break;

            case Direction.down:
                other1 = data.bottomLeft.SameAdjacent(Direction.down);
                other2 = data.bottomRight.SameAdjacent(Direction.down);
                break;

            case Direction.left:
                other1 = data.topLeft.SameAdjacent(Direction.left);
                other2 = data.bottomLeft.SameAdjacent(Direction.left);
                break;

            default:
                return(false);
            }

            if (other1 == null || other2 == null)
            {
                return(false);
            }

            //If other1 and other 2 are grouped and if they're part of the same group
            if (other1.IsGrouped() == true && other2.IsGrouped() == true && other1.GetGroup().Equals(other2.GetGroup()))
            {
                //If other blocks are corner blocks, then it can expand
                otherGroup = other1.GetGroup();

                switch (dir)
                {
                case Direction.up:
                    if (other1.Equals(otherGroup.data.bottomLeft) && other2.Equals(otherGroup.data.bottomRight))
                    {
                        return(true);
                    }
                    break;

                case Direction.right:
                    if (other1.Equals(otherGroup.data.topLeft) && other2.Equals(otherGroup.data.bottomLeft))
                    {
                        return(true);
                    }
                    break;

                case Direction.down:
                    if (other1.Equals(otherGroup.data.topLeft) && other2.Equals(otherGroup.data.topRight))
                    {
                        return(true);
                    }
                    break;

                case Direction.left:
                    if (other1.Equals(otherGroup.data.topRight) && other2.Equals(otherGroup.data.bottomRight))

                    {
                        return(true);
                    }
                    break;

                default:

                    break;
                }
            }

            return(false);
        }
예제 #2
0
        public void Expand(Direction dir)
        {
            Block other1 = null;
            Block other2 = null;

            switch (dir)
            {
            case Direction.up:
                other1 = data.topLeft.SameAdjacent(Direction.up);
                other2 = data.topRight.SameAdjacent(Direction.up);

                if (other1 == null || other2 == null)
                {
                    return;
                }

                data.topLeft  = other1;
                data.topRight = other2;

                //Iterate from topLeft to topRight (right)
                OnIterate = IterateRight;
                OnExpand  = IncrementHeight;
                break;

            case Direction.right:
                other1 = data.topRight.SameAdjacent(Direction.right);
                other2 = data.bottomRight.SameAdjacent(Direction.right);

                if (other1 == null || other2 == null)
                {
                    return;
                }

                data.topRight    = other1;
                data.bottomRight = other2;

                //Iterate from topRight to bottomRight (down)
                OnIterate = IterateDown;
                OnExpand  = IncrementWidth;
                break;

            case Direction.down:
                other1 = data.bottomLeft.SameAdjacent(Direction.down);
                other2 = data.bottomRight.SameAdjacent(Direction.down);

                if (other1 == null || other2 == null)
                {
                    return;
                }

                data.bottomLeft  = other1;
                data.bottomRight = other2;

                //Iterate from bottomLeft to bottomRight (right)
                OnIterate = IterateRight;
                OnExpand  = IncrementHeight;
                break;

            case Direction.left:
                other1 = data.topLeft.SameAdjacent(Direction.left);
                other2 = data.bottomLeft.SameAdjacent(Direction.left);

                if (other1 == null || other2 == null)
                {
                    return;
                }

                data.topLeft    = other1;
                data.bottomLeft = other2;

                //Iterate from topLeft to bottomLeft (down)
                OnIterate = IterateDown;
                OnExpand  = IncrementWidth;
                break;

            default:
                return;
            }

            //Get blocks adjacent to the corners
            other2.groupable.SetGroup(this);

            blocks.Add(other2);
            while (!other1.Equals(other2) && other1 != null)
            {
                blocks.Add(other1);
                other1.groupable.SetGroup(this);
                other1 = OnIterate(other1);
            }

            OnExpand();

            OnIterate = null;
            OnExpand  = null;
        }