void Start()
    {
        values = new Vector4[terrainSize.x * terrainSize.y * terrainSize.z];
        bounds = new Bounds(gameObject.transform.position, (terrainSize - Vector3.one) * gridSize);

        //initializes the positions, sets all the points values to 0, except the ones on the bottom layer, whose value gets set to 1
        for (int x = 0; x < terrainSize.x; x++)
        {
            for (int y = 0; y < terrainSize.y; y++)
            {
                for (int z = 0; z < terrainSize.z; z++)
                {
                    values[Utility.XYZtoIndex(x, y, z, terrainSize)] = Utility.Vector4FromVector3andValue(Utility.CalculatePointPosition(new Vector3Int(x, y, z), gameObject.transform.position, gridSize, terrainSize), 0);
                    if (y == 0)
                    {
                        values[Utility.XYZtoIndex(x, y, z, terrainSize)].w = 1;
                    }
                }
            }
        }

        if (Camera.current != null)
        {
            rayDistance = Mathf.Sqrt(bounds.SqrDistance(Camera.current.transform.position));
        }

        terrainChunkData = ChunkGenerator.Generate(values, terrainSize, surfaceLevel, true);
        CreateChunks();
    }
Beispiel #2
0
 void GenerateChunks()
 {
     for (int y = 0; y < WORLD_SIZE_IN_CHUNKS; y++)
     {
         for (int x = 0; x < WORLD_SIZE_IN_CHUNKS; x++)
         {
             _chunks[x, y] = _chunkGenerator.Generate(new Vector2DInt(x, y));
         }
     }
 }
Beispiel #3
0
    private GameObject GenerateBonusWorldChunk(float positionX, BaseChunkShape shape)
    {
        Debug.Log("new chunk at " + positionX);
        var eligibleChunkShapes = chunkShapes.Where(x => GameManager.Instance.distanceTraveled >= x.Difficulty - 1).ToList();

        var chunk = (GameObject)Instantiate(worldChunkPrefab, new Vector3(positionX, GameManager.Instance.Area == GameManager.Areas.Bonus ? 100 : 0, 0), new Quaternion(0, 0, 0, 0));

        if (shape == null)
        {
            shape = eligibleChunkShapes.RandomElement();
        }

        BaseBiome biome = new BonusBiome();

        chunkGenerator.Generate(chunk, shape, biome, false);
        coinGenerator.Generate(chunk, shape, biome, false);
        powerupGenerator.Generate(chunk, shape, biome, false);
        return(chunk);
    }
Beispiel #4
0
    public override void OnInspectorGUI()
    {
        ChunkGenerator c = (ChunkGenerator)target;

        DrawDefaultInspector();
        if (GUILayout.Button("Generate Terrain"))
        {
            c.Generate();
        }
        if (GUILayout.Button("Erode Terrain"))
        {
            c.ApplyErosion();
        }
        if (GUILayout.Button("Generate and Erode Terrain"))
        {
            c.Generate();
            c.ApplyErosion();
        }
        if (GUILayout.Button("Retexture Terrain"))
        {
            c.TextureAllChunks();
        }
    }
Beispiel #5
0
    private void ReuseHex(ref Hex pos)
    {
        GameObject go = chunksPool.Dequeue();

        go.SetActive(true);

        Vector2 center = Hex.HexToPixel(pos, datas.tVars.WorldSize);

        go.transform.position = new Vector3(center.x, 0, center.y);

        ChunkGenerator map = go.GetComponent <ChunkGenerator>();

        map.Generate(center.x, center.y);

        chunksDictionary.Add(pos, go);
    }
Beispiel #6
0
    private void NewHex(ref Hex pos)
    {
        GameObject go = new GameObject(pos.ToString(),
                                       typeof(ChunkGenerator));

        Vector2 center = Hex.HexToPixel(pos, datas.tVars.WorldSize);

        go.transform.position = new Vector3(center.x, 0, center.y);
        go.transform.SetParent(transform, false);

        ChunkGenerator map = go.GetComponent <ChunkGenerator>();

        map.datas = datas;
        map.Generate(center.x, center.y);

        go.GetComponent <MeshRenderer>().sharedMaterial =
            map.datas.tVars.material;
        chunksDictionary.Add(pos, go);
    }
Beispiel #7
0
    public virtual void GenerateChunks()
    {
        GenerateValues();

        if (showMinAndMaxValues)
        {
            PrintMinAndMaxValues();
        }

        if (usePercentageSurfaceLevel)
        {
            SetIsoLevel();
            terrainChunkData = ChunkGenerator.Generate(values, terrainSize, isoLevel);
        }
        else
        {
            terrainChunkData = ChunkGenerator.Generate(values, terrainSize, surfaceLevel);
        }

        CreateChunks();
    }
    void Update()
    {
        timer += Time.deltaTime;
        if (timer < interval)
        {
            return;
        }

        timer = 0;

        for (int i = 0; i < processes.Count; i++)
        {
            if (processes[i].flag == Flag.Pending)
            {
                for (int j = 0; j < processingThread.Length; j++)
                {
                    if ((processingThread[j] == null || !processingThread[j].IsAlive) && processes[i].flag == Flag.Pending)
                    {
                        Thread.Sleep(1);

                        processingThread[j] = new Thread(new ThreadStart(delegate {
                            ChunkGenerator.Generate(processingThread[j], processes[i]);
                        }
                                                                         )
                                                         );
                        processingThread[j].Start();
                        break;
                    }
                }
            }
            if (processes[i].flag == Flag.Ready)
            {
                processes[i].chunk.Create(processes[i]);
                processes.Remove(processes[i]);
            }
        }
    }
    public override void OnInspectorGUI()
    {
        ChunkGenerator chunkGen = (ChunkGenerator)target;

        DrawDefaultInspector();

        if (GUILayout.Button("Generate"))
        {
            chunkGen.Generate();
        }

        if (GUILayout.Button("Add Chunk: X"))
        {
            chunkGen.CreateChunk(chunkGen.sizeX++, chunkGen.sizeY - 1, chunkGen.sizeZ - 1);
        }
        if (GUILayout.Button("Add Chunk: Y"))
        {
            chunkGen.CreateChunk(chunkGen.sizeX - 1, chunkGen.sizeY++, chunkGen.sizeZ - 1);
        }
        if (GUILayout.Button("Add Chunk: Z"))
        {
            chunkGen.CreateChunk(chunkGen.sizeX - 1, chunkGen.sizeY - 1, chunkGen.sizeZ++);
        }
    }
    // Update is called once per frame
    void Update()
    {
        bool valuesChanged = false;
        Ray  mouseRay      = cam.ScreenPointToRay(Input.mousePosition);

        ChangeBrushSize();

        float distance;

        if (bounds.IntersectRay(mouseRay, out distance))
        {
            if (!brush.activeSelf)
            {
                brush.SetActive(true);
            }

            ChangeBrushDistance();

            MoveBrush(mouseRay, distance);

            if (Input.GetMouseButton(0))
            {
                Vector3 center = mouseRay.GetPoint(rayDistance);
                //left click
                for (int i = 0; i < values.Length; i++)
                {
                    Vector3 diff = new Vector3(values[i].x, values[i].y, values[i].z) - center;
                    if (diff.x * diff.x + diff.y * diff.y + diff.z * diff.z < brushSize * brushSize)
                    {
                        values[i].w  += 0.01f;
                        valuesChanged = true;
                        values[i].w   = Utility.Constrain(0, 1, values[i].w);
                    }
                }
            }
            else if (Input.GetMouseButton(1))
            {
                Vector3 center = mouseRay.GetPoint(rayDistance);
                //right click
                for (int i = 0; i < values.Length; i++)
                {
                    Vector3 diff = new Vector3(values[i].x, values[i].y, values[i].z) - center;
                    if (diff.x * diff.x + diff.y * diff.y + diff.z * diff.z < brushSize * brushSize)
                    {
                        values[i].w  -= 0.01f;
                        valuesChanged = true;
                        values[i].w   = Utility.Constrain(0, 1, values[i].w);
                    }
                }
            }
        }
        else
        {
            if (brush.activeSelf)
            {
                brush.SetActive(false);
            }
        }

        if (valuesChanged)
        {
            terrainChunkData = ChunkGenerator.Generate(values, terrainSize, surfaceLevel);
            CreateChunks();
        }
        else if (valuesChangedLastFrame)
        {
            ChunkGenerator.DestroyBuffers();
        }

        valuesChangedLastFrame = valuesChanged;
    }