Exemple #1
0
 public GridNodeState(int x, int y, string nodeId, string itemId)
 {
     Index = new GridIndex()
     {
         x = x, y = y
     };
     NodeId = nodeId;
     ItemId = itemId;
 }
Exemple #2
0
        public static bool IsNeighbor(GridIndex index1, GridIndex index2)
        {
            if (Mathf.Abs(index1.x - index2.x) > 1 || Mathf.Abs(index1.y - index2.y) > 1)
            {
                return(false);
            }

            return(true);
        }
Exemple #3
0
        private bool CanSelectGridItem(GridItemView gridItem)
        {
            if (gridItem == null || gridItem.IsEmpty)
            {
                return(false);                  // invalid
            }
            if (_selectedGridItems.Count == 0)
            {
                return(true);                   // first selection
            }
            var itemConfig = GameConfig.GetGridItem(gridItem.ID);

            if (itemConfig.MatchType == GridItemMatchType.None)
            {
                return(false);
            }

            var previousItemConfig = _selectedGridItems[_selectedGridItems.Count - 1].GridItemConfig;

            if (itemConfig.MatchType == GridItemMatchType.Category || previousItemConfig.MatchType == GridItemMatchType.Category)
            {
                if (itemConfig.Category != previousItemConfig.Category)
                {
                    return(false);
                }
            }
            else if (itemConfig.MatchType == GridItemMatchType.Exact)
            {
                if (itemConfig.ID != previousItemConfig.ID)
                {
                    return(false);
                }
            }

            if (_selectedGridItems.Contains(gridItem))
            {
                return(false);                  // already selected
            }
            var matchRules = GameConfig.GetMatchRules(_levelConfig.matchRules);

            if (matchRules.MaxSelection > matchRules.MinSelection && _selectedGridItems.Count > matchRules.MaxSelection)
            {
                return(false);
            }

            if (GridIndex.IsNeighbor(gridItem.Index, _selectedGridItems[_selectedGridItems.Count - 1].Index))
            {
                return(true);                   // must neighbor the last selected item
            }
            return(false);
        }
Exemple #4
0
 private bool CanDeselectGridItems(GridItemView gridItem)
 {
     if (gridItem == null)
     {
         return(false);                  // invalid
     }
     if (_selectedGridItems.Count < 2)
     {
         return(false);                  // not enough items selected
     }
     if (!_selectedGridItems.Contains(gridItem))
     {
         return(false);                  // not selected
     }
     if (gridItem == _selectedGridItems[_selectedGridItems.Count - 2])
     {
         return(true);                   // take one step back
     }
     if (!GridIndex.IsNeighbor(gridItem.Index, _selectedGridItems[_selectedGridItems.Count - 1].Index))
     {
         return(true);                   // must NOT neighbor the last selected item to trace back further (else it's annoying)
     }
     return(false);
 }
Exemple #5
0
 public void SetGridIndex(GridIndex index)
 {
     SetGridIndex(index.x, index.y);
 }
Exemple #6
0
 public void SetNodeLayout(GridIndex index, GridNodeLayout nodeLayout)
 {
     SetNodeLayout(index.x, index.y, nodeLayout);
 }
Exemple #7
0
 public GridNodeLayout GetNodeLayout(GridIndex index)
 {
     return(GetNodeLayout(index.x, index.y));
 }
Exemple #8
0
 private GridItemView TryGetGridItem(GridIndex index)
 {
     return(TryGetGridItem(index.x, index.y));
 }
Exemple #9
0
 private Vector3 CalculateGridNodePosition(GridIndex index)
 {
     return(CalculateGridNodePosition(index.x, index.y));
 }
Exemple #10
0
 private GridItemView CreateGridItemView(GridIndex index, string itemId)
 {
     return(CreateGridItemView(index.x, index.y, itemId, CalculateGridNodePosition(index.x, index.y)));
 }
Exemple #11
0
 public GridNodeState TryGetGridNodeState(GridIndex index)
 {
     return(TryGetGridNodeState(index.x, index.y));
 }
Exemple #12
0
 public bool IsInBounds(GridIndex index)
 {
     return(IsInBounds(index.x, index.y));
 }
Exemple #13
0
        private bool IsValidMatch(List <GridIndex> indices, out List <Tuple <int, GridNodeState> > matchedItems)
        {
            var matchRules = GameConfig.GetMatchRules(Config.matchRules);

            matchedItems = null;

            if (indices == null)
            {
                return(false);
            }

            if (indices.Count < matchRules.MinSelection)
            {
                return(false);
            }

            if (matchRules.MaxSelection > matchRules.MinSelection && indices.Count > matchRules.MaxSelection)
            {
                return(false);
            }

            int pointsMultiplier = 1;

            var matchedNodes = new List <GridNodeState>(indices.Count);

            for (int i = 0; i < indices.Count; i++)
            {
                var matchedNode = TryGetGridNodeState(indices[i]);
                if (matchedNode == null || !matchedNode.IsFilled())
                {
                    return(false);                    // must select valid items
                }
                matchedNodes.Add(matchedNode);

                pointsMultiplier *= matchedNode.GridNodeConfig.MatchPointsMatchMultiplier;
            }

            matchedItems = new List <Tuple <int, GridNodeState> >(indices.Count)
            {
                new Tuple <int, GridNodeState> (
                    (matchRules.MatchPointsBase + matchedNodes[0].GridItemConfig.MatchPoints) * matchedNodes[0].GridNodeConfig.MatchPointsItemMultiplier * pointsMultiplier,
                    matchedNodes[0]
                    )
            };

            var previousItem = matchedNodes[0];

            for (int i = 1; i < matchedNodes.Count; i++)
            {
                var item       = TryGetGridNodeState(indices[i]);
                var itemConfig = GameConfig.GetGridItem(item.ItemId);

                if (itemConfig.MatchType == GridItemMatchType.None)
                {
                    return(false);
                }

                if (itemConfig.MatchType == GridItemMatchType.Category || previousItem.GridItemConfig.MatchType == GridItemMatchType.Category)
                {
                    if (itemConfig.Category != previousItem.GridItemConfig.Category)
                    {
                        return(false);
                    }
                }
                else if (itemConfig.MatchType == GridItemMatchType.Exact)
                {
                    if (item.ItemId != previousItem.GridItemConfig.ID)
                    {
                        return(false);
                    }
                }

                if (!GridIndex.IsNeighbor(item.Index, previousItem.Index))
                {
                    return(false);                      // must select neighbors
                }
                matchedItems.Add(new Tuple <int, GridNodeState> (
                                     (matchRules.MatchPointsBase + matchedNodes[i].GridItemConfig.MatchPoints + (matchRules.MatchPointsIncrement * i)) * matchedNodes[i].GridNodeConfig.MatchPointsItemMultiplier * pointsMultiplier,
                                     matchedNodes[i]
                                     ));

                previousItem = item;
            }

            if (matchRules.WordCheck)
            {
                var word = new StringBuilder();
                foreach (var item in matchedItems)
                {
                    word.Append((char)GameConfig.GetGridItem(item.Item2.ItemId).MatchIndex);
                }

                if (!WordMap.Words.FindWord(word.ToString().ToLower(), out var wordTypes))
                {
                    return(false);
                }
            }

            return(true);
        }