Ejemplo n.º 1
0
    public void Regenerate()
    {
        LogText.text = "Initializing World";
        GameObject Temp;
        int        TileDeltaX = 0, TileDeltaY = 0;

        if (Type == GenType.UnityType)
        {
            Noise = new NoiseBase((double)Random.Range(Seed.x, Seed.y));
        }
        else
        {
            Noise = new NoiseBase((long)Random.Range(Seed.x, Seed.y));
        }

        Noise.Frequency       = ChunkSize;
        Noise.heightAddition  = Elevation;
        Noise.heightMultplier = Multiplier;
        Noise.Smooth          = Smooth;
        Noise.Interpolation   = InterpolationType;
        Noise.ClampChunk      = ClampChunk;

        int   h = 0;
        float width  = (float)SpriteSize.x / PixelPerUnit;     //Tile.transform.lossyScale.x;
        float height = (float)SpriteSize.y / PixelPerUnit;     //Tile.transform.lossyScale.y;

        LogText.text = "Generating Chunks...";
        ChunkMap.Add(new GameObject("TempChunk"));
        for (float x = Position.x; x < Size.x; x++)
        {
            TileDeltaX++;

            if (Type == GenType.UnityType)
            {
                h = Noise.GetNoise(x);
            }
            else
            {
                h = Noise.GetNoise((int)(x - Position.x), (int)(Size.y - Position.y));
            }

            for (float y = Position.y; y < Position.y + h; y++)
            {
                TileDeltaY++;
                if (RandomRotation)
                {
                    switch (Random.Range(1, 4))
                    {
                    case 1: Rot = Quaternion.Euler(0, 0, 90); break;

                    case 2: Rot = Quaternion.Euler(0, 0, 180); break;

                    case 3: Rot = Quaternion.Euler(0, 0, 270); break;

                    case 4: Rot = Quaternion.identity; break;
                    }
                }
                else
                {
                    Rot = Quaternion.identity;
                }

                Temp = Instantiate(TilePrefab, new Vector3(x * width, y * height, 0), Rot);
                TileMap.Add(Temp);
                Temp.name = "Tile[" + x + "," + y + "]";

                if (TileDeltaX > ChunkSize && TileDeltaY > ChunkSize)
                {
                    ChunkMap.Add(new GameObject("TempChunk"));
                    TileDeltaX = 0;
                    TileDeltaY = 0;
                }

                Temp.transform.SetParent(ChunkMap.Last().transform);
                TileScript.TileLayer.Add((int)Size.y - h);
            }
        }
        //Fin de generacion de mapa:

        LogText.text = "Loading Chunk Data...";
        for (int i = 0; i < ChunkMap.Count; i++)
        {
            ChunkMap [i].transform.SetParent(gameObject.transform);
            ChunkMap [i].name = "Chunk[" + i + "]";
        }

        LogText.text = "Loading Tiles...";
        TileScript.ComputeLights(TileMap);

        LogText.text = "Done...";
    }
Ejemplo n.º 2
0
    public void GenerateLayer(bool preview = false)
    {
        Init();
        int TileDeltaX = 0;

        switch (Type)
        {
        default: break;

        case GenType.UnityType: Noise = new NoiseBase((double)Random.Range(Seed.x, Seed.y)); break;

        case GenType.CustomType: Noise = new NoiseBase((long)Random.Range(Seed.x, Seed.y)); break;

        case GenType.DynamicMoveType: Rand = new System.Random((int)Random.Range(Seed.x, Seed.y)); break;
        }

        LayerMap[SelectedLayer].gameObject.SetActive(false);

        switch (Type)
        {
        //Custom Type - Unity Type se generan aca
        default:
            int height = 0;

            Noise.Frequency       = Frequency;
            Noise.heightAddition  = Elevation;
            Noise.heightMultplier = Multiplier;
            Noise.Smooth          = Smooth;
            Noise.Interpolation   = InterpolationType;
            Noise.ClampChunk      = ClampChunk;

            for (int x = (int)Transform.x; x < Transform.width; x++)
            {
                TileDeltaX++;
                switch (Type)
                {
                default: break;

                case GenType.UnityType:
                    height = Noise.GetNoise(x);
                    break;

                case GenType.CustomType:
                    height = Noise.GetNoise((int)(x - Transform.x), (int)(Transform.height - Transform.y));
                    break;
                }

                for (int y = (int)Transform.y; y < Transform.y + height; y++)
                {
                    SetTileAt(x, y, preview);
                    if (Copy)
                    {
                        SetTileAt((int)CopyLocation.x + x, (int)CopyLocation.y + y, preview);
                    }

                    /*if (TileDeltaX <= ChunkSize)
                     *  SetTileAt(x - (SelectedLayer * ChunkSize), y, preview);
                     * else
                     * {
                     *  if (SelectedLayer < LayerMap.Length)
                     *      SelectedLayer++;
                     *  TileDeltaX = 0;
                     *  SetTileAt(x - (SelectedLayer * ChunkSize), y, preview);
                     * }*/
                }
            }


            break;

        //Unity Prime se genera aca:
        case GenType.UnityTypePrime:
            float[,] noiseMap;
            Noise = new NoiseBase();
            Noise.OctavesOffset = OctavesOffset;
            Noise.OctavesRange  = OctavesRange;
            Noise.Seed          = (int)Random.Range(Seed.x, Seed.y);
            noiseMap            = Noise.GetNoise((int)Transform.width, (int)Transform.height, Scale, Octaves, Persistance, Lacunarity);
            var w = noiseMap.GetLength(0);
            var h = noiseMap.GetLength(1);

            //Cosntuctor de Textura y material:
            GeneratedTexture = new Texture2D(w, h);
            Color[] colorMap = new Color[w * h];
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    if (ActiveRegions)
                    {
                        float currentHeight = noiseMap[x, y];
                        for (int i = 0; i < Regions.Length; i++)
                        {
                            if (currentHeight <= Regions[i].Height)
                            {
                                colorMap[Utils.ToIndex(x, y, w)] = Regions[i].Colour;
                            }
                        }
                    }
                    else
                    {
                        colorMap[Utils.ToIndex(x, y, w)] = Color.Lerp(Color.black, Color.white, noiseMap[x, y]);
                    }
                }
            }
            GeneratedTexture.filterMode = RegionsFilterMode;
            GeneratedTexture.SetPixels(colorMap);
            GeneratedTexture.Apply();
            if (GeneratedMaterial != null)
            {
                GeneratedMaterial.mainTexture = GeneratedTexture;
            }
            //Generacion de Mapa (inicio):
            for (int x = (int)Transform.x; x < w; x++)
            {
                TileDeltaX++;
                for (int y = (int)Transform.y; y < h; y++)
                {
                    for (int i = 0; i < Regions.Length; i++)
                    {
                        if (colorMap[Utils.ToIndex(x, y, w)] == Regions[i].Colour)
                        {
                            SelectedTile = Regions[i].id;
                        }
                    }

                    SetTileAt(x, y, preview);
                    if (Copy)
                    {
                        SetTileAt((int)CopyLocation.x + x, (int)CopyLocation.y + y, preview);
                    }



                    /*if (TileDeltaX <= ChunkSize)
                     *  SetTileAt(x-(SelectedLayer*ChunkSize), y, preview);
                     * else
                     * {
                     *  if(SelectedLayer < LayerMap.Length)
                     *      SelectedLayer++;
                     *  TileDeltaX = 0;
                     *  SetTileAt(x - (SelectedLayer * ChunkSize), y, preview);
                     * }*/
                }
            }
            //Generacion de Mapa (fin)
            return;

        //Dynamic Move Type se genera aca:
        case GenType.DynamicMoveType:
            if (ResetOnMove)
            {
                OldLocation.x = Transform.x;
                OldLocation.y = Transform.y;
            }

            for (int i = 0; i < Octaves; i++)
            {
                switch (Rand.Next(0, 5))
                {
                case 0: Transform.x--; break;

                case 1: Transform.x++; break;

                case 2: Transform.y--; break;

                case 3: Transform.y++; break;
                }

                switch (MoveMode)
                {
                default: break;

                case 0: SetTileAt((int)Transform.x, (int)Transform.y, preview); break;

                case 1: RemoveTileAt((int)Transform.x, (int)Transform.y, preview); break;

                case 2: Cached = RemoveTileAt((int)Transform.x, (int)Transform.y, preview); break;
                }
            }
            if (ResetOnMove)
            {
                Transform.x = OldLocation.x;
                Transform.y = OldLocation.y;
            }
            break;
        }
        LayerMap[SelectedLayer].gameObject.SetActive(true);

        SelectedLayer = 0;
    }