Exemplo n.º 1
0
    //Generate player, enemies and structures spawn points
    void GenerateSpawnPositions()
    {
        List <Vector2> points      = Poisson.GeneratePoints(30, new Vector2(mapSize, mapSize), 15);
        List <Vector2> spawnPoints = new List <Vector2>();

        //check all points from poisson, save the point when it is bigger than the water level
        for (int i = 0; i < points.Count; i++)
        {
            if (heightMap[(int)points[i].x, (int)points[i].y] > waterLevel)
            {
                spawnPoints.Add(points[i]);
            }
        }

        int p = 1;

        //choose one random pont to spawn the player
        while (p > 0)
        {
            int     index    = Random.Range(0, spawnPoints.Count);
            Vector2 position = spawnPoints[index];

            int x = Mathf.FloorToInt(position.x);
            int z = Mathf.FloorToInt(position.y);

            spawnPosition = new Vector3(x, heightMap[x, z] + 1, z);
            spawnPoints.RemoveAt(index);
            p--;
        }

        //remaining points is for structures
        foreach (Vector2 point in spawnPoints)
        {
            ChunkCoord c = GetChunkCoord(new Vector3(point.x, 0, point.y));

            if (!chunks[c.x, c.z].IsStructureAdded)
            {
                int x = Mathf.FloorToInt(point.x);
                int z = Mathf.FloorToInt(point.y);

                int randomIndex = Random.Range(0, data.structureZones[data.selectedSurface].structures.Length);

                //calculate new position to skip structure overflow
                int        rCoord      = Random.Range(0, 6);
                int        xCoord      = c.x * ChunkData.chunkWidth + 5 + rCoord;
                int        zCoord      = c.z * ChunkData.chunkWidth + 5 + rCoord;
                Vector3Int newPosition = new Vector3Int(xCoord, heightMap[xCoord, zCoord], zCoord);

                ChunkModification modification = new ChunkModification
                {
                    position = newPosition,
                    zone     = data.selectedSurface,
                    index    = randomIndex
                };

                chunks[c.x, c.z].modification     = modification;
                chunks[c.x, c.z].IsStructureAdded = true;
            }
        }

        playerLastChunkCoord      = GetChunkCoord(spawnPosition);
        playerChunkCoord          = GetChunkCoord(spawnPosition);
        player.transform.position = spawnPosition;
    }