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); }
public void OnClick_EditMode_Save() { if (!GameConfig.AllMatchRules.Any(rule => rule.ID == _editModeTextMatchRules.text)) { MessagePopup.ShowMessage($"Invalid Match Rules: '{_editModeTextMatchRules.text}'"); return; } var allCategories = GameConfig.GetAllGridItemCategories(); if (!allCategories.Contains(_editModeTextCategory.text)) { MessagePopup.ShowMessage($"Invalid Category: '{_editModeTextCategory.text}'"); return; } if (!int.TryParse(_editModeInputMaxItemTypes.text, out var maxItemTypes)) { MessagePopup.ShowMessage($"Invalid Max Item Types: '{_editModeInputMaxItemTypes.text}'"); return; } if (!Enum.TryParse(_editModeTextGoalType.text, out LevelGoalType goalType)) { MessagePopup.ShowMessage($"Invalid Goal Type: '{_editModeTextGoalType.text}'"); return; } string goalItemId = null; if (!string.IsNullOrEmpty(_editModeInputGoalItem.text)) { goalItemId = _editModeInputGoalItem.text; if (GameConfig.GetGridItem(goalItemId).ID == null) { MessagePopup.ShowMessage($"Invalid Goal Item ID: '{_editModeInputGoalItem.text}'"); return; } } if (!int.TryParse(_editModeInputGoal1.text, out var goal1)) { MessagePopup.ShowMessage($"Invalid Goal #1: '{_editModeInputGoal1.text}'"); return; } if (!int.TryParse(_editModeInputGoal2.text, out var goal2)) { MessagePopup.ShowMessage($"Invalid Goal #2: '{_editModeInputGoal2.text}'"); return; } if (!int.TryParse(_editModeInputGoal3.text, out var goal3)) { MessagePopup.ShowMessage($"Invalid Goal #3: '{_editModeInputGoal3.text}'"); return; } if (!Enum.TryParse(_editModeTextChallengeType.text, out LevelChallengeType challengeType)) { MessagePopup.ShowMessage($"Invalid Challenge Type: '{_editModeTextChallengeType.text}'"); return; } if (!int.TryParse(_editModeInputChallengeValue.text, out var moves) || moves < 1) { MessagePopup.ShowMessage($"Invalid Max Moves: '{_editModeInputChallengeValue.text}'"); return; } _levelConfig.category = _editModeTextCategory.text; _levelConfig.excludeItemIds = EditMode_ConvertMaxItemTypesToExcludedItemIds(_editModeTextCategory.text, Mathf.Max(0, maxItemTypes)); _levelConfig.matchRules = _editModeTextMatchRules.text; _levelConfig.goalType = goalType; _levelConfig.goalItemId = goalItemId; _levelConfig.goal1 = Mathf.Max(0, goal1); _levelConfig.goal2 = Mathf.Max(0, goal2); _levelConfig.goal3 = Mathf.Max(0, goal3); _levelConfig.challengeType = challengeType; _levelConfig.challengeValue = moves; foreach (var gridNode in _gridNodeInstances) { var gridItem = TryGetGridItem(gridNode.Index); var nodeLayout = _levelConfig.GetNodeLayout(gridNode.Index); nodeLayout.nodeId = gridNode.ID; nodeLayout.itemId = (gridItem != null && gridNode.GridNodeConfig.IsOpen) ? gridItem.ID : null; _levelConfig.SetNodeLayout(gridNode.Index, nodeLayout); } LevelConfig.ExportLevelFile(_levelIndex, _levelConfig); }
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); }