Example #1
0
    public void GenerateOres(bool newOres)
    {
        DevController.MeasureElapsedTimeStart();

        _emptyPoints      = GenerateLevel.GetEmptyPoints();
        _emptyPointsIndex = 0;
        values            = chancesGenerator.GetChance();

        if (values.Sum() != 100)
        {
            Debug.LogError("ERROR");
        }

        if (newOres) // generate new ores
        {
            randNumbers = new float[GridBehavior.WIDTH, GridBehavior.HEIGHT];

            var spawnPointIndex = _gridBehaviour.GetArrayIndex(player.transform.position);

            for (int x = 0; x < GridBehavior.WIDTH; x++)
            {
                for (int y = 0; y < GridBehavior.HEIGHT; y++)
                {
                    if (Math.Abs(spawnPointIndex.x - x) > 9 || Math.Abs(spawnPointIndex.y - y) > 6)
                    {
                        randNumbers[x, y] = -1;
                        continue;
                    }

                    if (new Vector3Int(x, y, 0) == _gridBehaviour.GetArrayIndex(_emptyPoints[_emptyPointsIndex]))
                    {
                        _gridBehaviour.InstantiateNewGridPrefab(_emptyPoints[_emptyPointsIndex]);
                        randNumbers[x, y] = -1;
                        if (_emptyPointsIndex < _emptyPoints.Length - 1)
                        {
                            _emptyPointsIndex++;
                        }
                    }
                    else
                    {
                        GenerateOre(x, y);
                    }
                }
            }
        }
        else // load old ores
        {
            Debug.Log("load ores");

            for (int x = 0; x < GridBehavior.WIDTH; x++)
            {
                for (int y = 0; y < GridBehavior.HEIGHT; y++)
                {
                    if (_gridBehaviour.gridArray[x, y] == null &&
                        _gridBehaviour.descentPos != new Vector2(x, y) && randNumbers[x, y] != -1)
                    {
                        GenerateOre(x, y, randNumbers[x, y]);
                    }
                }
            }

            NextLevelLoadController.needsToLoadNextLevel = false;
        }

        DevController.MeasureElapsedTimeEnd("Ore generation");
    }