Exemplo n.º 1
0
    /// <summary>
    /// Using the currently selected board configuration, build a set of tile options
    /// that are used in the board configuration.
    /// </summary>
    private void BuildTileDropdownOptions()
    {
        List <Dropdown.OptionData> tileTypeOptions = new List <Dropdown.OptionData>();

        tileType.ClearOptions();
        BoardConfigurationSet.BoardConfiguration boardConfiguration = SelectedBoardConfiguration();
        foreach (var tileName in boardConfiguration.allowedTiles)
        {
            GameObject prefab = Resources.Load <GameObject>(tileName);
            Tile       tile   = prefab.GetComponent <Tile>();

            TileRuleSetting optionData = new TileRuleSetting();
            optionData.text         = tile.tileFamilyName;
            optionData.tileResource = tileName;
            tileTypeOptions.Add(optionData);
        }
        tileType.AddOptions(tileTypeOptions);
    }
Exemplo n.º 2
0
    /// <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);
        }
    }