Exemplo n.º 1
0
    public BuildingSubworldBuilder(Building building, BuildingVoxels vox, BuildingGenerationPlan plan)
    {
        Building = building;
        BuildVox = vox;
        Plan     = plan;
        Size     = new Vec2i(building.Width, building.Height);
        int cWidth  = Mathf.CeilToInt(((float)Size.x) / World.ChunkSize);
        int cHeight = Mathf.CeilToInt(((float)Size.z) / World.ChunkSize);

        if (cWidth < 1)
        {
            cWidth = 1;
        }
        if (cHeight < 1)
        {
            cHeight = 1;
        }
        ChunkSize = new Vec2i(cWidth, cHeight);

        Tiles   = new int[cWidth, cHeight][, ];
        Voxels  = new ChunkVoxelData[cWidth, cHeight];
        Objects = new List <WorldObjectData> [cWidth, cHeight];
        for (int x = 0; x < cWidth; x++)
        {
            for (int z = 0; z < cHeight; z++)
            {
                Tiles[x, z]  = new int[World.ChunkSize, World.ChunkSize];
                Voxels[x, z] = new ChunkVoxelData();
            }
        }
    }
Exemplo n.º 2
0
    void Start()
    {
        int[,] tiles     = new int[World.ChunkSize, World.ChunkSize];
        float[,] heights = new float[World.ChunkSize, World.ChunkSize];
        ChunkVoxelData cvd = new ChunkVoxelData();

        for (int x = 0; x < World.ChunkSize; x++)
        {
            for (int z = 0; z < World.ChunkSize; z++)
            {
                heights[x, z] = x + z;
                tiles[x, z]   = Tile.GRASS.ID;
                for (int y = 0; y < World.ChunkHeight; y++)
                {
                    if (x + y + z < 20)
                    {
                        cvd.SetVoxel(x, y, z, Voxel.stone);
                    }
                    else if ((x - 8) * (x - 8) + (y - 8) * (y - 8) + (z - 8) * (z - 8) < 5 * 5)
                    {
                        cvd.SetVoxel(x, y, z, Voxel.wood);
                    }
                }
            }
        }
        ChunkData cData = new ChunkData(0, 0, tiles, true, 5, heights);

        cData.SetVoxelData(cvd);
        ChunkLoader.LoadChunk(cData);
    }
Exemplo n.º 3
0
    public BuilderBase(Vec2i baseChunk, Vec2i chunkSize, HeightFunction heightFunc, ChunkBase2[,] chunkBases)
    {
        BaseChunk = baseChunk;
        ChunkSize = chunkSize;
        BaseTile  = BaseChunk * World.ChunkSize;
        TileSize  = ChunkSize * World.ChunkSize;

        Subworlds = new List <Subworld>();

        ChunkVoxels = new ChunkVoxelData[ChunkSize.x, ChunkSize.z];
        HeightMaps  = new float[ChunkSize.x, ChunkSize.z][, ];
        TileMaps    = new int[ChunkSize.x, ChunkSize.z][, ];
        ObjectMaps  = new List <WorldObjectData> [ChunkSize.x, ChunkSize.z];
        ChunkBases  = chunkBases;

        for (int x = 0; x < ChunkSize.x; x++)
        {
            for (int z = 0; z < ChunkSize.z; z++)
            {
                HeightMaps[x, z] = new float[World.ChunkSize + 1, World.ChunkSize + 1];

                // ChunkBaseHeights[x, z] = baseHeight;
                TileMaps[x, z] = new int[World.ChunkSize, World.ChunkSize];


                ChunkVoxels[x, z] = new ChunkVoxelData();
            }
        }
        //if no height function is specified, we define the height to be 0
        if (heightFunc == null)
        {
            heightFunc = (float x, float y) => { return(0); }
        }
        ;

        for (int x = 0; x < ChunkSize.x; x++)
        {
            for (int z = 0; z < ChunkSize.z; z++)
            {
                float baseHeight = ChunkBases == null ? 0 : ChunkBases[x, z].Height;
                int   baseTile   = (ChunkBases == null || ChunkBases[x, z] == null) ? Tile.NULL.ID : Tile.GetFromBiome(ChunkBases[x, z].Biome).ID;

                for (int x_ = 0; x_ < World.ChunkSize; x_++)
                {
                    for (int z_ = 0; z_ < World.ChunkSize; z_++)
                    {
                        baseHeight = heightFunc((x + BaseChunk.x) * World.ChunkSize + x_, (z + BaseChunk.z) * World.ChunkSize + z_);

                        SetHeight(x * World.ChunkSize + x_, z * World.ChunkSize + z_, baseHeight);
                        //SetTile(x * World.ChunkSize + x_, z * World.ChunkSize + z_, Tile.FromID(baseTile));
                    }
                }
            }
        }
    }
Exemplo n.º 4
0
 public void SetVoxelData(ChunkVoxelData cvd)
 {
     VoxelData = cvd;
 }
Exemplo n.º 5
0
    private ChunkData GenerateRiverChunk(int x, int z, ChunkBase2 cb)
    {
        GenerationRandom genRan = new GenerationRandom(new Vec2i(x, z).GetHashCode() + Seed);

        int[,] tiles            = new int[World.ChunkSize, World.ChunkSize];
        WorldObjectData[,] data = new WorldObjectData[World.ChunkSize, World.ChunkSize];
        float[,] heights        = new float[World.ChunkSize, World.ChunkSize];
        Vec2i exitDelta = new Vec2i(1, 0);
        Vec2i entrDelta = new Vec2i(-1, 0);

        if (exitDelta == null)
        {
            exitDelta = new Vec2i(0, 0);
        }
        if (entrDelta == null)
        {
            entrDelta = new Vec2i(0, 0);
        }

        //Calculatee the tile position of the entrance and exit point of the river
        int entrX = (entrDelta.x == 1) ? 16 : ((entrDelta.x == 0) ? 8 : 0);
        int entrZ = (entrDelta.z == 1) ? 16 : ((entrDelta.z == 0) ? 8 : 0);

        int exitX = (exitDelta.x == 1) ? 16 : ((exitDelta.x == 0) ? 8 : 0);
        int exitZ = (exitDelta.z == 1) ? 16 : ((exitDelta.z == 0) ? 8 : 0);



        float dx = entrX - exitX;
        float dz = entrZ - exitZ;
        //If dx or dz is 0, then
        float a, b, c;
        bool  angle = (dx != 0 && dz != 0);
        float divBy = angle ? 2 : 1;

        if (dx == 0)
        {
            a = 0;
            b = 1;
            c = -entrX;
        }
        else if (dz == 0)
        {
            a = 1;
            b = 0;
            c = -entrZ;
        }
        else
        {
            float m = dz / dx;
            c = -(entrZ - m * entrX);

            a = 1;
            b = -m;
        }

        int inWidth  = 8;
        int outWidth = 8;


        float          dem_sqr = (a * a + b * b);
        ChunkVoxelData vox     = new ChunkVoxelData();

        for (int tx = 0; tx < World.ChunkSize; tx++)
        {
            for (int tz = 0; tz < World.ChunkSize; tz++)
            {
                float baseheight = GameGen.TerGen.GetWorldHeightAt(x * World.ChunkSize + tx, z * World.ChunkSize + tz);

                float dist_sqr = ((a * tz + b * tx + c) * (a * tz + b * tx + c)) / dem_sqr;
                if (dist_sqr < (inWidth * inWidth) / divBy)
                {
                    Vector2 off = new Vector2(x * World.ChunkSize + tx, z * World.ChunkSize + tz);
                    //Debug.Log("here");
                    tiles[tx, tz] = Tile.WATER.ID;
                    //float baseheight = GameGen.TerrainGenerator.WorldHeight(x * World.ChunkSize + tx, z * World.ChunkSize + tz);
                    float lerp = dist_sqr / ((inWidth * inWidth) / divBy);


                    heights[tx, tz] = Mathf.Lerp(baseheight, baseheight - 5, 1 / lerp);
                    heights[tx, tz] = Mathf.Clamp(cb.Height - 5, 1, 16);
                    for (int y = Mathf.FloorToInt(heights[tx, tz]); y < baseheight; y++)
                    {
                        vox.SetVoxelNode(tx, y, tz, new VoxelNode(Voxel.glass));
                    }

                    /*
                     * if (!(data[tx, tz] is Water))
                     * {
                     *  data[tx, tz] = new Water(new Vec2i(x * World.ChunkSize + tx, z * World.ChunkSize + tz));
                     *  (data[tx, tz] as Water).SetUVOffset(off);
                     * }
                     *
                     * if (tx < World.ChunkSize - 1 && !(data[tx + 1, tz] is Water))
                     * {
                     *  data[tx + 1, tz] = new Water(new Vec2i(x * World.ChunkSize + tx + 1, z * World.ChunkSize + tz));
                     *  (data[tx + 1, tz] as Water).SetUVOffset(off + new Vector2(1, 0));
                     * }
                     * if (tz < World.ChunkSize - 1 && !(data[tx, tz + 1] is Water))
                     * {
                     *  data[tx, tz + 1] = new Water(new Vec2i(x * World.ChunkSize + tx, z * World.ChunkSize + tz + 1));
                     *  (data[tx, tz + 1] as Water).SetUVOffset(off + new Vector2(0, 1));
                     * }
                     * if (tx < World.ChunkSize - 1 && tz < World.ChunkSize - 1 && !(data[tx + 1, tz + 1] is Water))
                     * {
                     *  data[tx + 1, tz + 1] = new Water(new Vec2i(x * World.ChunkSize + tx + 1, z * World.ChunkSize + tz + 1));
                     *  (data[tx + 1, tz + 1] as Water).SetUVOffset(off + new Vector2(1, 1));
                     * }
                     *
                     * if (tx > 0 && !(data[tx - 1, tz] is Water))
                     * {
                     *  data[tx - 1, tz] = new Water(new Vec2i(x * World.ChunkSize + tx - 1, z * World.ChunkSize + tz));
                     *  (data[tx - 1, tz] as Water).SetUVOffset(off + new Vector2(-1, 0));
                     * }
                     * if (tz > 0 && !(data[tx, tz - 1] is Water))
                     * {
                     *  data[tx, tz - 1] = new Water(new Vec2i(x * World.ChunkSize + tx, z * World.ChunkSize + tz - 1));
                     *  (data[tx, tz - 1] as Water).SetUVOffset(off + new Vector2(0, -1));
                     * }
                     * if (tx > 0 && tz > 0 && !(data[tx - 1, tz - 1] is Water))
                     * {
                     *  data[tx - 1, tz - 1] = new Water(new Vec2i(x * World.ChunkSize + tx - 1, z * World.ChunkSize + tz - 1));
                     *  (data[tx - 1, tz - 1] as Water).SetUVOffset(off + new Vector2(-1, -1));
                     * }
                     *
                     * if (tx > 0 && tz < World.ChunkSize - 1 && !(data[tx - 1, tz + 1] is Water))
                     * {
                     *  data[tx - 1, tz + 1] = new Water(new Vec2i(x * World.ChunkSize + tx - 1, z * World.ChunkSize + tz + 1));
                     *  (data[tx - 1, tz + 1] as Water).SetUVOffset(off + new Vector2(-1, +1));
                     * }
                     * if (tz > 0 && tx < World.ChunkSize - 1 && !(data[tx + 1, tz - 1] is Water))
                     * {
                     *  data[tx + 1, tz - 1] = new Water(new Vec2i(x * World.ChunkSize + tx + 1, z * World.ChunkSize + tz - 1));
                     *  (data[tx + 1, tz - 1] as Water).SetUVOffset(off + new Vector2(1, -1));
                     * }*/
                }
                else if (dist_sqr < (inWidth * inWidth) * 2 / divBy)
                {
                    tiles[tx, tz]   = Tile.SAND.ID;
                    heights[tx, tz] = baseheight;
                    for (int y = Mathf.FloorToInt(heights[tx, tz]); y < baseheight; y++)
                    {
                        vox.SetVoxelNode(tx, y, tz, new VoxelNode(Voxel.glass));
                    }
                }
                else
                {
                    tiles[tx, tz]   = Tile.GRASS.ID;
                    heights[tx, tz] = baseheight;

                    // heights[tx, tz] = cb.BaseHeight;

                    //if (genRan.Random() < 0.25f)
                    //    data[tx, tz] = new Grass(new Vec2i(x * World.ChunkSize + tx + 1, z * World.ChunkSize + tz - 1));
                }
            }
        }

        //data[0, 0] = new Tree(new Vec2i(x * World.ChunkSize, z * World.ChunkSize));

        //data[2, 2] = new RockFormation(new Vec2i(x * World.ChunkSize + 2, z * World.ChunkSize + 2));


        Dictionary <int, WorldObjectData> data_ = new Dictionary <int, WorldObjectData>();

        for (int i = 0; i < World.ChunkSize; i++)
        {
            for (int j = 0; j < World.ChunkSize; j++)
            {
                if (data[i, j] != null)
                {
                    data_.Add(WorldObject.ObjectPositionHash(i, j), data[i, j]);
                }
            }
        }
        ChunkData cd = new ChunkData(x, z, tiles, true, baseHeight: cb.Height, heightMap: heights);

        cd.SetVoxelData(vox);
        return(cd);
    }