Пример #1
0
    // Method to create the base grid; all needed cells and walls
    private IEnumerator GenerateGrid()
    {
        cells = new Cell[width * height];                      // Definíng array storing all cell objects
        Vector2[] cellPositions = new Vector2[width * height]; // Array to store all cell positions in grid
        float     cellPosX;
        float     cellPosY;

        int addWidth = 0; // Variable to help indexing the cell array correctly

        // Nested loop to create the grid with no walls missing
        for (int j = 0; j < height; j++)
        {
            for (int i = 0; i < width; i++)
            {
                cellPosX = i * w - (w * width / 2) + w / 2;  // x coordinate of current cell to be placed
                cellPosY = j * w - (w * height / 2) + w / 2; // y coordinate of current cell to be placed

                GameObject currBox = Instantiate(blockPrefab, new Vector3(cellPosX, cellPosY, 0), Quaternion.identity);
                cellPositions[i + addWidth] = new Vector2(cellPosX, cellPosY);

                currBox.transform.parent = blockHolder.transform;

                // Instantiating walls for the current cell
                GameObject leftwall  = Instantiate(wall, new Vector3(currBox.transform.position.x - w / 2, currBox.transform.position.y, -0.01f), Quaternion.identity);
                GameObject topwall   = Instantiate(wall, new Vector3(currBox.transform.position.x, currBox.transform.position.y + w / 2, -0.01f), Quaternion.Euler(new Vector3(0, 0, 90)));
                GameObject rightwall = Instantiate(wall, new Vector3(currBox.transform.position.x + w / 2, currBox.transform.position.y, -0.01f), Quaternion.identity);
                GameObject botwall   = Instantiate(wall, new Vector3(currBox.transform.position.x, currBox.transform.position.y - w / 2, -0.01f), Quaternion.Euler(new Vector3(0, 0, 90)));

                // Add new cell to cell array
                cells[i + addWidth] = new Cell(i, j, currBox);

                // Add walls to cell object
                cells[i + addWidth].leftWall  = leftwall;
                cells[i + addWidth].topWall   = topwall;
                cells[i + addWidth].rightWall = rightwall;
                cells[i + addWidth].botWall   = botwall;

                // Parent the cell walls
                cells[i + addWidth].leftWall.transform.parent  = wallHolder.transform;
                cells[i + addWidth].topWall.transform.parent   = wallHolder.transform;
                cells[i + addWidth].rightWall.transform.parent = wallHolder.transform;
                cells[i + addWidth].botWall.transform.parent   = wallHolder.transform;

                nrOfCreatedCells++;

                // Yields after genYieldLimit cells to not have the whole application window "freeze" too long for large mazes
                if (nrOfCreatedCells % genYieldLimitGrid == 0)
                {
                    yield return(null);
                }
            }
            addWidth += width;
        }
        coinCreator.SetSpawnPoints(cellPositions); // Give coinCreator all cell positions (needed to position coins)
        coinCreator.GenerateCoins();
        StartCoroutine(DepthFirstSearch());        // Start main algorithm
    }
Пример #2
0
    // Update is called once per frame
    void Update()
    {
        // If the position of our platform is going to be less then that of the generation point
        // we want to select a random distance between, as well as a platform index.
        if (transform.position.x < generationPoint.position.x)
        {
            distanceBetween = Random.Range(pDistanceMin, pDistanceMax);

            // This is the amount of different platforms we are using.
            List <GameObject> platforms = gp.getDifferentPlatforms();

            platformIndex = Random.Range(0, platforms.Count);

            // We then want to calculate a height differental for our platform.
            heightChange = transform.position.y + Random.Range(maximumHeightChange, -maximumHeightChange);
            // Though we should ignore it if it goes beyond the overall maximum height.
            if (heightChange > maxHeight)
            {
                heightChange = maxHeight;
            }
            // as well as if it goes below the minimum height set.
            else if (heightChange < minHeight)
            {
                heightChange = minHeight;
            }

            // We then want to get a random number between 0 and 100
            // if it is less then the power up percentage, make a new powerup.
            if (Random.Range(0.0f, 100.0f) < powerUpPercentage)
            {
                GameObject newPowerup = gp.getPooledObject("PowerUp");

                // This powerup will be in between 2 platforms, with a random height difference.
                newPowerup.transform.position = transform.position + new Vector3(distanceBetween / 2.0f, Random.Range(2.0f, pickupHeight), 0.0f);
                // Then, set the powerup to be active.
                newPowerup.SetActive(true);
            }
            // We then want to calculate our new position.
            transform.position = new Vector3(transform.position.x + (platformWidths[platformIndex] / 2) + distanceBetween, heightChange, transform.position.z);



            // We then get a pooled platform and set its position and roation to be that of our platform creator.
            GameObject newPlatform = gp.getPooledObject(platforms[platformIndex].tag);

            newPlatform.transform.position = transform.position;
            newPlatform.transform.rotation = transform.rotation;
            // then set it to be active.
            newPlatform.SetActive(true);


            // Based on the percentage, if we generate a number that is less then the percentage number, create the coins.
            if (Random.Range(0.0f, 100.0f) < randomCoinGeneratePercentage)
            {
                // Add the coins to the platform.
                coinCreator.GenerateCoins(new Vector3(transform.position.x, transform.position.y + 1.0f, transform.position.z));
            }

            // Based on the percentage, if we generate a number that is less then the percentage number, create the spikes.
            if (Random.Range(0.0f, 100.0f) < randomSpikeGeneratePercentage)
            {
                // grab the spike from the spike object pool.
                GameObject newSpike = gp.getPooledObject("KillVolume");
                //These spikes will have a random position on the platform. based on this random x coordinate.
                float spikeXPos = Random.Range(-platformWidths[platformIndex] / 2 + 1.0f, platformWidths[platformIndex] / 2 - 1.0f);


                // add our x position differental to our spikes position, with the height being 0.5f to ensure its on top of the platform.
                Vector3 spikePos = new Vector3(spikeXPos, 0.5f, 0f);
                // Set the new spikes position and rotation to be that of the platform creator with the addition of the spikePos.
                newSpike.transform.position = transform.position + spikePos;
                newSpike.transform.rotation = transform.rotation;
                // Then we want to set the spikes to be active.
                newSpike.SetActive(true);
            }

            // After all this, jump the platform generator forward to the next position within the game world.
            transform.position = new Vector3(transform.position.x + (platformWidths[platformIndex] / 2), transform.position.y, transform.position.z);
        }
    }