Ejemplo n.º 1
0
    public void GenerateChunkMeshData(MCS_Chunk chunk)
    {
        int index = 0;

        chunk.Vertices = new List <Vector3>();
        chunk.Indices  = new List <int>();
        chunk.Uvs      = new List <Vector2>();
        chunk.Colors   = new List <Color>();


        int chunkX = chunk.WorldPos.x;
        int chunkY = chunk.WorldPos.y;
        int chunkZ = chunk.WorldPos.z;

        for (int x = 0; x < myWorldData.MyChunkSizeX; x++)
        {
            int blockX = chunkX + x;
            for (int y = 0; y < myWorldData.MyChunkSizeY; y++)
            {
                int blockY = chunkY + y;
                for (int z = 0; z < myWorldData.MyChunkSizeZ; z++)
                {
                    int        blockZ = chunkZ + z;
                    Vector3Int pos    = new Vector3Int(blockX, blockY, blockZ);
                    MCS_Block  block  = myWorldData.GetBlock(pos);
                    block.myIndex = index;
                    index         = CreateDataMeshForBlock(block);
                }
            }
        }
    }
    public void SpawnDecoration(MCS_Chunk aChunk, Entity aEntityType, Vector3Int aWorldPos)
    {
        MCS_VoxelEntity entity = new MCS_VoxelEntity(aWorldPos, aEntityType);

        for (int i = 0; i <  entity.Pattern.Length; i++)
        {
            Vector3Int pos   = entity.WorldPos + entity.Pattern[i];
            MCS_Block  block = myWorldData.GetBlock(pos);

            block.Type = BlockType.Stone;
        }
    }
Ejemplo n.º 3
0
    private void Hit()
    {
        if (Input.GetButtonDown("Fire"))
        {
            RaycastHit hit;

            if (Physics.Raycast(myCameraTransform.position, myCameraTransform.forward, out hit, Mathf.Infinity, myHitLayer))
            {
                Item selectedItem = playerManager.myInventory.Get(playerManager.mySelectedSlot);

                if (selectedItem == Item.Hand)
                {
                    // Dig
                    Vector3    pos       = hit.point + myCameraTransform.forward.normalized * 0.1f;
                    float      voxelSize = myWorldObj.myWorldData.MyVoxelSize;
                    Vector3Int blockPos  = new Vector3Int((int)(pos.x / voxelSize), (int)(pos.y / voxelSize), (int)(pos.z / voxelSize));
                    Debug.Log(blockPos);
                    MCS_Block block = myWorldObj.myWorldData.GetBlock(blockPos);

                    playerManager.myInventory.Add(block);
                    block.Dig();
                }
                else
                {
                    // Dig
                    Vector3    pos       = hit.point + myCameraTransform.forward.normalized * -0.1f;
                    float      voxelSize = myWorldObj.myWorldData.MyVoxelSize;
                    Vector3Int blockPos  = new Vector3Int((int)(pos.x / voxelSize), (int)(pos.y / voxelSize), (int)(pos.z / voxelSize));
                    MCS_Block  block     = myWorldObj.myWorldData.GetBlock(blockPos);

                    playerManager.myInventory.Remove(playerManager.mySelectedSlot);

                    BlockType type = BlockType.Air;

                    switch (selectedItem)
                    {
                    case Item.Dirt:
                        type = BlockType.Dirt;
                        break;

                    case Item.Stone:
                        type = BlockType.Stone;
                        break;
                    }

                    block.Place(type);
                }
            }
        }
    }
Ejemplo n.º 4
0
    public void GenerateBlocks()
    {
        myBlocks = new MCS_Block[myWorldData.MyChunkSizeX, myWorldData.MyChunkSizeY, myWorldData.MyChunkSizeZ];

        for (int x = 0; x < myWorldData.MyChunkSizeX; x++)
        {
            for (int y = 0; y < myWorldData.MyChunkSizeY; y++)
            {
                for (int z = 0; z < myWorldData.MyChunkSizeZ; z++)
                {
                    Vector3Int pos = new Vector3Int(x, y, z);
                    myBlocks[x, y, z]             = new MCS_Block(BlockType.Air, pos + WorldPos, this);
                    myBlocks[x, y, z].myWorldData = myWorldData;
                }
            }
        }
    }
Ejemplo n.º 5
0
    public int CreateDataMeshForBlock(MCS_Block aBlock)
    {
        int       index = aBlock.myIndex;
        MCS_Chunk chunk = aBlock.myChunk;

        int blockX = aBlock.myWorldPos.x;
        int blockY = aBlock.myWorldPos.y;
        int blockZ = aBlock.myWorldPos.z;

        int x = blockX - aBlock.myChunk.WorldPos.x;
        int y = blockY - aBlock.myChunk.WorldPos.y;
        int z = blockZ - aBlock.myChunk.WorldPos.z;

        MCS_Block currentBlock = aBlock;

        byte lightAmount = currentBlock.LightAmount;

        if (currentBlock.Type != BlockType.Air)
        {
            return(index);
        }

        MCS_Block blockBelow = myWorldData.GetBlock(blockX, blockY - 1, blockZ);

        if (blockBelow != null && blockBelow.Type != BlockType.Air)
        {
            // If bloc below is solid, we add a below face facing up
            AddBlockSide(x + 1, y, z,
                         x, y, z,
                         x, y, z + 1,
                         x + 1, y, z + 1,
                         0.5f, chunk, index, blockBelow.Type, Faces.BOTTOM, lightAmount);
            index += 4;
        }

        MCS_Block blockWest = myWorldData.GetBlock(blockX - 1, blockY, blockZ);

        if (blockWest != null && blockWest.Type != BlockType.Air)
        {
            // if bloc west (left) is solid, then render its west side facing east
            AddBlockSide(x, y, z,
                         x, y + 1, z,
                         x, y + 1, z + 1,
                         x, y, z + 1,
                         0.8f, chunk, index, blockWest.Type, Faces.SIDE, lightAmount);
            index += 4;
        }

        MCS_Block blockTop = myWorldData.GetBlock(blockX, blockY + 1, blockZ);

        if (blockTop != null && blockTop.Type != BlockType.Air)
        {
            AddBlockSide(x + 1, y + 1, z,
                         x + 1, y + 1, z + 1,
                         x, y + 1, z + 1,
                         x, y + 1, z,
                         0.9f, chunk, index, blockTop.Type, Faces.TOP, lightAmount);

            index += 4;
        }

        MCS_Block blockEast = myWorldData.GetBlock(blockX + 1, blockY, blockZ);

        if (blockEast != null && blockEast.Type != BlockType.Air)
        {
            AddBlockSide(x + 1, y, z,
                         x + 1, y, z + 1,
                         x + 1, y + 1, z + 1,
                         x + 1, y + 1, z,
                         0.7f, chunk, index, blockEast.Type, Faces.SIDE, lightAmount);

            index += 4;
        }

        MCS_Block blockNorth = myWorldData.GetBlock(blockX, blockY, blockZ + 1);

        if (blockNorth != null && blockNorth.Type != BlockType.Air)
        {
            AddBlockSide(x, y, z + 1,
                         x, y + 1, z + 1,
                         x + 1, y + 1, z + 1,
                         x + 1, y, z + 1,
                         0.4f, chunk, index, blockNorth.Type, Faces.SIDE, lightAmount);

            index += 4;
        }

        MCS_Block blockSouth = myWorldData.GetBlock(blockX, blockY, blockZ - 1);

        if (blockSouth != null && blockSouth.Type != BlockType.Air)
        {
            AddBlockSide(x, y, z,
                         x + 1, y, z,
                         x + 1, y + 1, z,
                         x, y + 1, z,
                         1.0f, chunk, index, blockSouth.Type, Faces.SIDE, lightAmount);

            index += 4;
        }

        return(index);
    }