/// <summary> /// Taking the currently selected movement rule, build a set of compatible boards and /// add their player-facing names to the boardType dropdown /// </summary> private void BuildBoardDropdownOptions() { List <Dropdown.OptionData> boardTypeOptions = new List <Dropdown.OptionData>(); boardType.ClearOptions(); MovementRuleSetting selectedRuleSetting = movementRules.Find(setting => { return(setting.GetComponent <Toggle>().isOn); }); foreach (var boardConfiguration in boardConfigurations.configurations) { bool validMove = boardConfiguration.allowedMoves.Any(move => { return(move == selectedRuleSetting.movement); }); if (validMove) { foreach (var allowedBoard in boardConfiguration.allowedBoards) { GameObject boardObject = Resources.Load <GameObject>(allowedBoard); Board b = boardObject.GetComponent <Board>(); BoardRuleSetting optionData = new BoardRuleSetting(); optionData.text = b.boardName; optionData.boardResource = allowedBoard; boardTypeOptions.Add(optionData); } } } boardType.AddOptions(boardTypeOptions); boardType.value = 0; }
/// <summary> /// Calculate the currently selected board configuration /// </summary> /// <returns>The currently selected board configuration</returns> private BoardConfigurationSet.BoardConfiguration SelectedBoardConfiguration() { BoardRuleSetting boardRuleSetting = (BoardRuleSetting)boardType.options[boardType.value]; string selectedBoardResource = boardRuleSetting.boardResource; MovementRuleSetting selectedRuleSetting = movementRules.Find(setting => { return(setting.GetComponent <Toggle>().isOn); }); foreach (var boardConfiguration in boardConfigurations.configurations) { bool validMove = boardConfiguration.allowedMoves.Any( (movementRule) => { return(movementRule == selectedRuleSetting.movement); }); bool validResource = boardConfiguration.allowedBoards.Any( (resourcePath) => { return(resourcePath == selectedBoardResource); }); if (validMove && validResource) { return(boardConfiguration); } } return(null); }
/// <summary> /// The Unity initialization hook. /// Populates playerSettings, winConditions and movementRules /// Sets up the validators for the text input fields /// </summary> void Start() { playerSettings.AddRange(GetComponentsInChildren <PlayerSettings>()); winConditions.AddRange(GetComponentsInChildren <WinConditionSetting>()); movementRules.AddRange(GetComponentsInChildren <MovementRuleSetting>()); foreach (var movementRuleSetting in movementRules) { Toggle toggle = movementRuleSetting.GetComponent <Toggle>(); toggle.onValueChanged.AddListener((bool newValue) => { if (newValue) { boardType.onValueChanged?.Invoke(boardType.value); } }); } boardConfigurations = Resources.Load <BoardConfigurationSet>("Default Board Configurations"); BuildBoardDropdownOptions(); boardType.onValueChanged.AddListener(newIndex => { BoardRuleSetting boardRuleSetting = boardType.options[newIndex] as BoardRuleSetting; if (r != null) { r.boardResource = boardRuleSetting.boardResource; } BuildTileDropdownOptions(); tileType.onValueChanged?.Invoke(tileType.value); var selectedBoardConfiguration = SelectedBoardConfiguration(); foreach (var playerSetting in playerSettings) { playerSetting.OnBoardConfigurationChanged(selectedBoardConfiguration); } }); tileType.onValueChanged.AddListener(newIndex => { if (r != null) { TileRuleSetting tileRuleSetting = tileType.options[newIndex] as TileRuleSetting; r.tileResource = tileRuleSetting.tileResource; } }); // Randomize the sides icons and colors and add callbacks. Also set the first two // player settings toggles to enabled and read-only List <int> colorsNotChosen = new List <int>(); for (int i = 0; i < playerSettings.Count; i++) { var playerSetting = playerSettings[i]; if (i < 2) { playerSetting.sideEnabled.isOn = true; playerSetting.sideEnabled.interactable = false; } playerSetting.colorDropdown.onValueChanged.AddListener((int value) => { // Build a list of all the options List <int> valuesNotChosen = new List <int>(); for (int j = 0; j < playerSetting.colorDropdown.options.Count; j++) { valuesNotChosen.Add(j); } // Remove all options that are taken already by some other player foreach (var pSetting in playerSettings) { if (pSetting == playerSetting) { continue; } valuesNotChosen.Remove(pSetting.colorDropdown.value); } if (valuesNotChosen.Count < 1) { throw new System.ArgumentOutOfRangeException("colors", "Not enough side colors for each side"); } // If the requested values is not available, choose the first available if (valuesNotChosen.IndexOf(value) < 0) { playerSetting.colorDropdown.value = valuesNotChosen[0]; } }); playerSetting.iconDropdown.value = Random.Range(0, playerSetting.iconDropdown.options.Count); playerSetting.pieceDropdown.value = Random.Range(0, playerSetting.pieceDropdown.options.Count); playerSetting.pieceDropdown.onValueChanged?.Invoke(playerSetting.pieceDropdown.value); // The first playersetting gets all the color choices if (i == 0) { colorsNotChosen.AddRange(System.Linq.Enumerable.Range(0, playerSetting.colorDropdown.options.Count)); } int chosen = colorsNotChosen[Random.Range(0, colorsNotChosen.Count)]; if (playerSetting.colorDropdown.value != chosen) { playerSetting.colorDropdown.value = chosen; } else { playerSetting.colorDropdown.onValueChanged?.Invoke(chosen); } colorsNotChosen.Remove(playerSetting.colorDropdown.value); } rows.onValidateInput += ValidateRowsAndColumns; rows.onValueChanged.AddListener((string newValue) => { if (newValue.Length > 0) { r.rows = int.Parse(newValue); } }); columns.onValidateInput += ValidateRowsAndColumns; columns.onValueChanged.AddListener((string newValue) => { if (newValue.Length > 0) { r.cols = int.Parse(newValue); } }); matchN.onValidateInput += ValidateMatchN; matchN.onValueChanged.AddListener((string newValue) => { if (newValue.Length > 0) { r.consecutiveTiles = int.Parse(newValue); } }); maxGames.onValidateInput += ValidateMaxGames; maxGames.onValueChanged.AddListener((string newValue) => { if (newValue.Length > 0) { r.maxGames = int.Parse(newValue); } }); gamesToWin.onValidateInput += ValidateGamesToWin; gamesToWin.onValueChanged.AddListener((string newValue) => { if (newValue.Length > 0) { r.gamesToWin = int.Parse(newValue); } }); var movementRule = movementRules.FirstOrDefault((ruleSetting) => { return(ruleSetting.GetComponent <Toggle>().isOn); }); if (movementRule != null) { movementRule.GetComponent <Toggle>().onValueChanged?.Invoke(true); } }