Example #1
0
    //from project settings the fixed update is set to 0.05 seconds ie. 20 tics per second
    //generating  1 chunk from queue every 0.05 seconds should offload some work when processing giant squares of new chunks at once
    //after trying it seems to not have impact on basic physics in game
    //if weird things wiith physics will occur probably switch to courutine

    private void FixedUpdate()
    {
        currentPlayerPosInChunk = ChunkCalculations.CalculateChunkFromWorldPos(player.position);
        if (currentChunkPos.posX != currentPlayerPosInChunk.posX || currentChunkPos.posZ != currentPlayerPosInChunk.posZ)
        {
            for (int i = -5; i <= 5; i++)
            {
                for (int j = -5; j <= 5; j++)
                {
                    if (!VoxelData.chunkDictionary.ContainsKey(new ChunkCoord((int)(player.position.x / 15 + i), (int)(player.position.z / 15 + j))))
                    {
                        int x = (int)(player.position.x / 15 + i);
                        int z = (int)(player.position.z / 15 + j);
                        if (toGenerateUniques.Add(new ChunkCoord(x, z)))
                        {
                            toGenerate.Enqueue(new ChunkCoord(x, z));
                        }
                    }
                }
            }
            currentChunkPos = currentPlayerPosInChunk;
        }
        if (toGenerate.Count > 0)
        {
            toGenerateUniques.Remove(toGenerate.Peek());
            SpawnChunk(toGenerate.Dequeue());
        }
    }
Example #2
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 #3
0
 // Update is called once per frame
 void Update()
 {
     currentPlayerPosInChunk = ChunkCalculations.CalculateChunkFromWorldPos(player.position);
     if (currentChunkPos.posX != currentPlayerPosInChunk.posX || currentChunkPos.posZ != currentPlayerPosInChunk.posZ)
     {
         currentChunkPos = currentPlayerPosInChunk;
         foreach (var chunkCoorditem in editedBlock)
         {
             if (Mathf.Abs(chunkCoorditem.Key.posX - currentPlayerPosInChunk.posX) > 2 || Mathf.Abs(chunkCoorditem.Key.posZ - currentPlayerPosInChunk.posZ) > 2)
             {
                 preparedToSave.Add(chunkCoorditem.Key, chunkCoorditem.Value);
             }
         }
     }
     if (editedBlock.Count > 30)
     {
         preparedToSave = editedBlock;
     }
     if (preparedToSave.Count > 0)
     {
         SaveDataToChunkSave();
     }
 }
Example #4
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 #5
0
 void Start()
 {
     player                  = GameObject.FindObjectOfType <PlayerMove>().transform;
     currentChunkPos         = ChunkCalculations.CalculateChunkFromWorldPos(player.position);
     currentPlayerPosInChunk = currentChunkPos;
 }
Example #6
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);
        }
    }