Inheritance: MonoBehaviour
Exemplo n.º 1
0
    public void RefreshByBounds(Bounds bounds)
    {
        GetCoordinateByBounds(bounds);
        removeList.Clear();
        foreach (var c in grid.chunks)
        {
            if (!tempCoordinate.ContainsKey(c.Key))
            {
                removeList.Add(c.Key);
            }
        }
        for (int i = 0; i < removeList.Count; i++)
        {
            CubeChunk chunk = grid.chunks[removeList[i]];

            if (grid.chunkDatas.ContainsKey(removeList[i]))
            {
                grid.chunkDatas[removeList[i]] = chunk.GetAllCubeData();
            }
            else
            {
                grid.chunkDatas.Add(removeList[i], chunk.GetAllCubeData());
            }
            grid.chunks.Remove(removeList[i]);
            ChunkPool.Instance.Set(chunk);
        }

        ProcessByCoordinate();
    }
Exemplo n.º 2
0
    void CreateChunks_test()
    {
        for (int y = -2, i = 0; y < chunkCountY; y++)
        {
            for (int z = -2; z < chunkCountZ; z++)
            {
                for (int x = -2; x < chunkCountX; x++)
                {
                    CubeChunk chunk = ChunkPool.Instance.Get();
                    chunk.grid = this;

                    chunk.transform.SetParent(transform);
                    chunk.transform.name = "Chunk" + x + "_" + y + "_" + z + "_index" + (i - 1) + "C" + (x + y * chunkCountY * chunkCountY + z * chunkCountZ);

                    chunk.ChunkCoordinate = new CubeCoordinate(x, y, z);
                    chunks[chunk.ChunkCoordinate.ToString()] = chunk;
                }
            }
        }

        foreach (var c in chunks)
        {
            manager.AddChunk(c.Value);
        }
    }
Exemplo n.º 3
0
 public void Set(CubeChunk chunk)
 {
     chunk.Init();
     chunk.SetVisible(false);
     //chunk.GetComponent<Renderer>().enabled = false;
     chunkPool.Add(chunk);
 }
Exemplo n.º 4
0
    public void SetCubeData(Vector3 position, byte data)
    {
        Vector3        cubePosition    = CubeMetrics.WorldPosition2CubePosition(position);
        CubeCoordinate chunkCoordinate = new CubeCoordinate(position, CubeCoordinate.CoordinateType.chunk);
        CubeCoordinate cubeCoordinate  = new CubeCoordinate(position, CubeCoordinate.CoordinateType.cubeWorld);
        CubeChunk      chunk           = chunks[chunkCoordinate.ToString()];

        chunk.SetCubeData(cubePosition, data, true);
    }
Exemplo n.º 5
0
    public CubeChunk GetAdjacentChunk(CubeChunk chunk, AdjacentDirection direction)
    {
        CubeCoordinate adjChunkCoordinate = chunk.ChunkCoordinate.GetAdjacentCoordinate(direction);

        if (chunks.ContainsKey(adjChunkCoordinate.ToString()))
        {
            return(chunks[adjChunkCoordinate.ToString()]);
        }
        else
        {
            return(null);
        }
    }
Exemplo n.º 6
0
    public CubeChunk Get()
    {
        if (chunkPool.Count > 0)
        {
            CubeChunk cubeChunk = chunkPool[chunkPool.Count - 1];
            chunkPool.RemoveAt(chunkPool.Count - 1);

            return(cubeChunk);
        }
        else
        {
            return(Instantiate(chunkPrefab));
        }
    }
Exemplo n.º 7
0
 public void ConnectNeighbors()
 {
     for (int i = 0; i < 6; i++)
     {
         if (!NeighborChunks[i])
         {
             AdjacentDirection chunkDir = (AdjacentDirection)i;
             CubeChunk         neighbor = grid.GetAdjacentChunk(this, chunkDir);
             if (neighbor)
             {
                 NeighborChunks[i] = neighbor;
                 neighbor.NeighborChunks[(int)chunkDir.ChunkOpposite()] = this;
             }
         }
     }
 }
Exemplo n.º 8
0
    IEnumerator ChunkUpdate()
    {
        while (true)
        {
            if (pendingChunk.Count > 0)
            {
                CubeChunk chunk = pendingChunk.Pop();
                if (chunk.NeedRefresh)
                {
                    chunk.RefreshSelf();
                    chunk.GetComponent <Renderer>().enabled = true;
                }
                else
                {
                    continue;
                }
            }

            yield return(null);
        }
    }
Exemplo n.º 9
0
 public void ProcessMark(CubeChunk chunk)
 {
     pendingChunk.Push(chunk);
 }
Exemplo n.º 10
0
 public void AddChunk(CubeChunk chunk)
 {
     pendingChunk.Push(chunk);
 }
Exemplo n.º 11
0
    public static byte[] GetTerrainData(CubeChunk chunk)
    {
        byte[] data = new byte[CHUNK_WIDTH * CHUNK_WIDTH * CHUNK_WIDTH];



        for (int y = 0; y < CHUNK_WIDTH; y++)
        {
            for (int z = 0; z < CHUNK_WIDTH; z++)
            {
                for (int x = 0; x < CHUNK_WIDTH; x++)
                {
                    float cubeX = chunk.transform.position.x + x * CUBE_SIDE_LENGTH + CUBE_SIDE_LENGTH / 2f;
                    float cubeY = chunk.transform.position.y + y * CUBE_SIDE_LENGTH + CUBE_SIDE_LENGTH / 2f;
                    float cubeZ = chunk.transform.position.z + z * CUBE_SIDE_LENGTH + CUBE_SIDE_LENGTH / 2f;

                    Vector3 p = new Vector3(cubeX, cubeY, cubeZ);

                    float perlin        = Mathf.PerlinNoise((p.x + TerrainSeed) * 0.010f, (p.z + TerrainSeed) * 0.010f) * 37f;
                    float currentHeight = p.y;
                    float heightDiff    = Mathf.Abs(perlin - currentHeight);

                    if (perlin > currentHeight)
                    {
                        CubeType temp;
                        if (heightDiff < CUBE_SIDE_LENGTH)
                        {
                            temp = CubeType.grass;
                        }
                        else
                        {
                            if (currentHeight > 0)
                            {
                                temp = GetProbability(0.8f) ? CubeType.clay : CubeType.stone;
                            }
                            else if (currentHeight > -5 * CUBE_SIDE_LENGTH)
                            {
                                temp = GetProbability(0.5f) ? CubeType.stone : CubeType.clay;
                            }
                            else if (currentHeight > -10 * CUBE_SIDE_LENGTH)
                            {
                                temp = GetProbability(0.4f) ? CubeType.coal : CubeType.clay;
                            }
                            else if (currentHeight > -20 * CUBE_SIDE_LENGTH)
                            {
                                temp = GetProbability(0.2f) ? CubeType.copper : CubeType.stone;
                            }
                            else if (currentHeight > -30 * CUBE_SIDE_LENGTH)
                            {
                                temp = GetProbability(0.05f) ? CubeType.gold : CubeType.stone;
                            }
                            else
                            {
                                temp = GetProbability(0.01f) ? CubeType.diamond : CubeType.magma;
                            }
                        }
                        CubeData d = new CubeData(true, false, CubeOrientate.front, temp);
                        data[x + y * CHUNK_WIDTH * CHUNK_WIDTH + z * CHUNK_WIDTH] = d.ToByte();
                    }
                    else
                    {
                        data[x + y * CHUNK_WIDTH * CHUNK_WIDTH + z * CHUNK_WIDTH] = byte.MinValue;
                    }
                }
            }
        }

        return(data);
    }