Beispiel #1
0
    public void PlaceVoxel(int x, int y, int z, int type, bool overwrite = true)
    {
        //GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        //cube.transform.position = new Vector3(x, y, z) + Vector3.one * 0.5f;
        //cube.renderer.material.color = Color.red;

        int cx, cy, cz;

        Utils.CoordVoxelToChunk(x, y, z, out cx, out cy, out cz, ChunkSize);
        int vx, vy, vz;

        Utils.CoordChunkToVoxel(cx, cy, cz, out vx, out vy, out vz);
        VoxelContainer vc = null;

        if (!VoxelContainer.Containers.TryGetValue(Utils.VoxelCoordToLong(cx, cy, cz), out vc))
        {
            GameObject go = new GameObject("Voxel Container");
            go.transform.parent = WorldManager.Active.transform;
            vc               = go.AddComponent <VoxelContainer>();
            vc.SideTexture   = WorldManager.Active.SideTexture;
            vc.TopTexture    = WorldManager.Active.TopTexture;
            vc.BottomTexture = WorldManager.Active.BottomTexture;
            vc.Shader        = WorldManager.Active.VoxelShader;
            vc.LiquidShader  = WorldManager.Active.LiquidShader;
            vc.BreakTexture  = WorldManager.Active.BreakTexture;

            vc.X = cx;
            vc.Y = cy;
            vc.Z = cz;
            vc.Register();
            go.isStatic           = true;
            go.transform.position = new Vector3(vx, vy, vz);
        }
        lock (vc.Voxels)
        {
            var key = Utils.VoxelCoordToLong(x - vx, y - vy, z - vz);
            if (type == 0)
            {
                vc.Voxels.Remove(key);
                if (sygnalize)
                {
                    vc.ProcessingNeeded = true;
                }
            }
            else if (overwrite || !vc.Voxels.ContainsKey(key))
            {
                bool changed = vc.Voxels.AddOrReplace(key, type);
                if (changed && sygnalize)
                {
                    vc.ProcessingNeeded = true;
                }
            }
        }
    }