Ejemplo n.º 1
0
        private bool CollapseGridItems(ref List <Level.Event> gridUpdateEvents)
        {
            var movedItems = new List <GridEventItem>();

            for (int x = 0; x < Width; x++)
            {
                for (int y = 1; y < Height; y++)                        // skip the lowest row
                {
                    // find a filled node
                    var node = Grid[x, y];
                    if (!node.IsFilled())
                    {
                        continue;
                    }

                    // collapse left
                    var nodeLeft = TryGetGridNodeState(x - 1, y - 1);
                    if (nodeLeft?.IsEmpty() == true)
                    {
                        nodeLeft.SetItemId(node.ItemId);
                        node.RemoveItem();

                        // record the event
                        var movedItem = GridEventItem.Create(nodeLeft, 0, node.Index);
                        movedItems.Add(movedItem);
                        continue;
                    }

                    // collapse right
                    var nodeRight = TryGetGridNodeState(x + 1, y - 1);
                    if (nodeRight?.IsEmpty() == true)
                    {
                        nodeRight.SetItemId(node.ItemId);
                        node.RemoveItem();

                        // record the event
                        var movedItem = GridEventItem.Create(nodeRight, 0, node.Index);
                        movedItems.Add(movedItem);
                        continue;
                    }
                }
            }

            if (movedItems.Count > 0)
            {
                var itemsMovedEvent = new Level.ItemsMovedEvent(movedItems);
                gridUpdateEvents.Add(itemsMovedEvent);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        private bool PullDownGridItems(ref List <Level.Event> gridUpdateEvents)
        {
            var movedItems = new List <GridEventItem>();

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    // find an empty node
                    var node = Grid[x, y];
                    if (!node.IsEmpty())
                    {
                        continue;
                    }

                    // find an item above
                    var aboveFilledNode = FindFirstFilledNodeAbove(x, y);
                    if (aboveFilledNode != null)
                    {
                        // pull the item down
                        node.SetItemId(aboveFilledNode.ItemId);
                        aboveFilledNode.RemoveItem();

                        // record the event
                        var movedItem = GridEventItem.Create(node, 0, aboveFilledNode.Index);
                        movedItems.Add(movedItem);
                    }
                }
            }

            if (movedItems.Count > 0)
            {
                var itemsMovedEvent = new Level.ItemsMovedEvent(movedItems);
                gridUpdateEvents.Add(itemsMovedEvent);
                return(true);
            }

            return(false);
        }