Example #1
0
    private GemController SpawnGem(int row, int column, bool shouldPreventRepeating)
    {
        var candidateGemTypes = new List <GemType>();

        for (int i = 0; i < (int)GemType.Gem_Count; i++)
        {
            candidateGemTypes.Add((GemType)i);
        }

        if (shouldPreventRepeating)
        {
            var leftRow = row - 1;
            if (leftRow >= 0 && _currentGemControllerMatrix[leftRow, column] != null)
            {
                candidateGemTypes.Remove(_currentGemControllerMatrix[leftRow, column].Type);
            }
            var topColumn = column - 1;
            if (topColumn >= 0 && _currentGemControllerMatrix[row, topColumn] != null)
            {
                candidateGemTypes.Remove(_currentGemControllerMatrix[row, topColumn].Type);
            }
        }

        var     selectedGemType = candidateGemTypes[Random.Range(0, candidateGemTypes.Count)];
        var     gemController   = GemController.CreateGemObject(row, column, selectedGemType, gemSprites[(int)selectedGemType]);
        var     startX          = GameConfig.Instance.gemStartX;
        var     startY          = GameConfig.Instance.gemStartY;
        var     gemWidth        = GameConfig.Instance.gemWidth;
        var     gemHeight       = GameConfig.Instance.gemHeight;
        Vector3 pos             = new Vector3(0, 0, board.transform.position.z);

        pos.x = startX + column * gemWidth;
        pos.y = startY - row * gemHeight;
        gemController.transform.position = pos;

        _currentGemControllerMatrix[row, column] = gemController;
        gemController.GemOperationEvent         += OnGemOperation;

        return(gemController);
    }