public void SetLevelSO(LevelSO levelSO)
    {
        this.levelSO = levelSO;

        gridWidth  = levelSO.width;
        gridHeight = levelSO.height;
        grid       = new Grid <GemGridPosition>(gridWidth, gridHeight, 1f, Vector3.zero, (Grid <GemGridPosition> g, int x, int y) => new GemGridPosition(g, x, y));

        // Initialize Grid
        for (int x = 0; x < gridWidth; x++)
        {
            for (int y = 0; y < gridHeight; y++)
            {
                // Get Saved LevelGridPosition
                LevelSO.LevelGridPosition levelGridPosition = null;

                foreach (LevelSO.LevelGridPosition tmpLevelGridPosition in levelSO.levelGridPositionList)
                {
                    if (tmpLevelGridPosition.x == x && tmpLevelGridPosition.y == y)
                    {
                        levelGridPosition = tmpLevelGridPosition;
                        break;
                    }
                }

                if (levelGridPosition == null)
                {
                    // Couldn't find LevelGridPosition with this x, y!
                    Debug.LogError("Error! Null!");
                }

                GemSO   gem     = levelGridPosition.gemSO;
                GemGrid gemGrid = new GemGrid(gem, x, y);
                grid.GetGridObject(x, y).SetGemGrid(gemGrid);
                grid.GetGridObject(x, y).SetHasGlass(levelGridPosition.hasGlass);
            }
        }

        score     = 0;
        moveCount = levelSO.moveAmount;

        OnLevelSet?.Invoke(this, new OnLevelSetEventArgs {
            levelSO = levelSO, grid = grid
        });
    }
예제 #2
0
    public void SetLevelSO(LevelSO levelSO)
    {
        this.levelSO = levelSO;

        gridWidth  = levelSO.width;
        gridHeight = levelSO.height;
        grid       = new Grid <GemGridPosition>(gridWidth, gridHeight, 1f, Vector3.zero, (Grid <GemGridPosition> g, int x, int y) => new GemGridPosition(g, x, y));

        // Initialize Grid
        for (int x = 0; x < gridWidth; x++)
        {
            for (int y = 0; y < gridHeight; y++)
            {
                // Get Saved LevelGridPosition
                LevelSO.LevelGridPosition levelGridPosition = null;

                foreach (LevelSO.LevelGridPosition tmpLevelGridPosition in levelSO.levelGridPositionList)
                {
                    if (tmpLevelGridPosition.x == x && tmpLevelGridPosition.y == y)
                    {
                        levelGridPosition = tmpLevelGridPosition;
                        break;
                    }
                }

                if (levelGridPosition == null)
                {
                    // Couldn't find LevelGridPosition with this x, y!
                    Debug.LogError("Error! Null!");
                }

                GemSO   gem     = levelGridPosition.gemSO;
                GemGrid gemGrid = new GemGrid(gem, x, y);
                grid.GetGridObject(x, y).SetGemGrid(gemGrid);
                grid.GetGridObject(x, y).SetHasGlass(levelGridPosition.hasGlass);
            }
        }

        if (randomGlass.enabled)
        {
            // Remove all glass
            for (int x = 0; x < gridWidth; x++)
            {
                for (int y = 0; y < gridHeight; y++)
                {
                    grid.GetGridObject(x, y).SetHasGlass(false);
                }
            }
            // Add Random Glass
            while (GetGlassAmount() < randomGlass.glassAmount)
            {
                int glassX = UnityEngine.Random.Range(0, gridWidth);
                int glassY = UnityEngine.Random.Range(0, gridHeight);
                grid.GetGridObject(glassX, glassY).SetHasGlass(true);
            }
        }

        score     = 0;
        moveCount = levelSO.moveAmount;

        OnLevelSet?.Invoke(this, new OnLevelSetEventArgs {
            levelSO = levelSO, grid = grid
        });
    }