Example #1
0
    void PlaceBlock()
    {
        //int blockPosX = Mathf.Abs((int)(ghostBlock.transform.position.x % 16));
        //int blockPosY = (int)(ghostBlock.transform.position.y);
        //int blockPosZ = Mathf.Abs((int)(ghostBlock.transform.position.z % 16));

        Vector3Int blockInChunkPos = ChunkCalculations.CalculatePosInBlock(ghostBlock.transform.position);

        ChunkCoord ghostChunkCoord = ChunkCalculations.CalculateChunkFromWorldPos(ghostBlock.transform.position);
        Chunk      c = VoxelData.chunkDictionary[ghostChunkCoord];

        c.voxelMap[blockInChunkPos.x, blockInChunkPos.y, blockInChunkPos.z] = (byte)Inventory.currentBlock;
        c.RegenerateChunk();
        saveLoadHandler.PrepareToSave(blockInChunkPos.x, blockInChunkPos.y, blockInChunkPos.z, ghostChunkCoord, Inventory.currentBlock);
        //Debug.Log($"ghost block pos: {ghostBlock.transform.position.x} {ghostBlock.transform.position.y} {ghostBlock.transform.position.z}");
        //Debug.Log($"Block placement = {blockInChunkPos.x} , {blockInChunkPos.y} , {blockInChunkPos.z}");
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        tmpWorldPos.text = $"X:{player.transform.position.x:0.00} Y:{player.transform.position.y:0.00} Z:{player.transform.position.z:0.00}";

        tmpChunk.text = $"({Mathf.FloorToInt(player.transform.position.x / 16)} , {Mathf.FloorToInt(player.transform.position.z / 16)})";

        Vector3Int worldToChunkblock = ChunkCalculations.CalculatePosInBlock(player.transform.position);

        tmpBlockInChunk.text = $"{worldToChunkblock.x} , {worldToChunkblock.z}";

        tmpInstantMining.text = PlayerInput.instantMining.ToString();

        tmpSeed.text = StartInitializer.seed.ToString();

        if (Physics.Raycast(mainCam.transform.position, mainCam.transform.forward, out hit, rayDistance))
        {
            tmpWorldPositionLook.text = $"X:{hit.point.x:0.00} Y:{hit.point.y:0.00} Z:{hit.point.z:0.00}";

            tmpChunkLook.text = $"({Mathf.FloorToInt(hit.point.x / 16)} , {Mathf.FloorToInt(hit.point.z / 16)})";

            Vector3Int hitToChunkkBlock = ChunkCalculations.CalculatePosInBlock(hit.point);
            tmpBlockInChunkLook.text = $"{hitToChunkkBlock.x} , {hitToChunkkBlock.z}";
        }
    }
Example #3
0
    void DestroyBlock(Vector3 destroyPoint)
    {
        //int blockPosX = Mathf.Abs((int)(destroyPoint.x % 16));
        //int blockPosY = (int)(destroyPoint.y);
        //int blockPosZ = Mathf.Abs((int)(destroyPoint.z % 16));

        Vector3Int blockInChunkPos = ChunkCalculations.CalculatePosInBlock(destroyPoint);

        //Debug.Log($"{blockInChunkPos.x} , {blockInChunkPos.y} , {blockInChunkPos.z} mining this block");
        float x = destroyPoint.x >= 0 ? (int)destroyPoint.x + .5f : (int)destroyPoint.x - .5f;
        float z = destroyPoint.z >= 0 ? (int)destroyPoint.z + .5f : (int)destroyPoint.z - .5f;

        destructionGhost.transform.position = new Vector3(x, (int)destroyPoint.y + .5f, z);
        destructionGhost.SetActive(true);

        //to prevent useless initializing every frame
        if (blockInChunkPos.x != pomX || blockInChunkPos.y != pomY || blockInChunkPos.z != pomZ)
        {
            destroyChunkCoord = ChunkCalculations.CalculateChunkFromWorldPos(destroyPoint);
            c           = VoxelData.chunkDictionary[destroyChunkCoord];
            pomX        = blockInChunkPos.x;
            pomY        = blockInChunkPos.y;
            pomZ        = blockInChunkPos.z;
            timeElapsed = 0;
        }

        timeElapsed += Time.deltaTime;
        if (!PlayerInput.instantMining)
        {
            if (timeElapsed > world.blocks[c.voxelMap[blockInChunkPos.x, blockInChunkPos.y, blockInChunkPos.z]].hardeness)
            {
                destroyed = true;
            }
            ShowDestructionProgress(world.blocks[c.voxelMap[blockInChunkPos.x, blockInChunkPos.y, blockInChunkPos.z]].hardeness);
        }
        else
        {
            if (c.voxelMap[blockInChunkPos.x, blockInChunkPos.y, blockInChunkPos.z] != (byte)BlockType.BEDROCK)
            {
                destroyed = true;
            }
        }

        if (destroyed)
        {
            c.voxelMap[blockInChunkPos.x, blockInChunkPos.y, blockInChunkPos.z] = (byte)BlockType.AIR;
            c.RegenerateChunk();
            //Debug.Log($"destroy block pos: {(int)destroyPoint.x} {(int)destroyPoint.y} {(int)destroyPoint.z}");
            //Debug.Log($"Block destroyed = {blockInChunkPos.x} , {blockInChunkPos.y} , {blockInChunkPos.z}");
            ChunkCoord nextChunk;
            if (blockInChunkPos.x == 0)
            {
                nextChunk = new ChunkCoord(destroyChunkCoord.posX - 1, destroyChunkCoord.posZ);
                VoxelData.chunkDictionary[nextChunk].RegenerateChunk();
            }
            if (blockInChunkPos.x == 15)
            {
                nextChunk = new ChunkCoord(destroyChunkCoord.posX + 1, destroyChunkCoord.posZ);
                VoxelData.chunkDictionary[nextChunk].RegenerateChunk();
            }
            if (blockInChunkPos.z == 0)
            {
                nextChunk = new ChunkCoord(destroyChunkCoord.posX, destroyChunkCoord.posZ - 1);
                VoxelData.chunkDictionary[nextChunk].RegenerateChunk();
            }
            if (blockInChunkPos.z == 15)
            {
                nextChunk = new ChunkCoord(destroyChunkCoord.posX, destroyChunkCoord.posZ + 1);
                VoxelData.chunkDictionary[nextChunk].RegenerateChunk();
            }
            ResetPoms();
            timeElapsed = 0;
            destroyed   = false;
            destructionGhost.SetActive(false);
            cooldownTimer = 0;
            saveLoadHandler.PrepareToSave(blockInChunkPos.x, blockInChunkPos.y, blockInChunkPos.z, destroyChunkCoord, BlockType.AIR);
        }
    }