Exemple #1
0
        void Update()
        {
            for (var i = 0; i < keyCodes.Length; i++)
            {
                if (Input.GetKeyDown(keyCodes[i]) && i < buildableBlocks.Length)
                {
                    blockTypeToBuild = buildableBlocks[i];
                    blockTypeSelected();
                }
            }
            if (Input.GetMouseButtonDown(1))
            {
                if (!interactor.isBlockSelected)
                {
                    return;
                }
                // Prevent building on blocks where player stays
                var playerPosition   = Vector3i.FloorToInt(playerTransform.localPosition - new Vector3(0, 0.5f, 0));
                var upperPosition    = playerPosition + new Vector3i(0, 1, 0);
                var placeblePosition = interactor.placeblePosition + interactor.selectedChunk.chunkPosition;

                if (playerPosition != placeblePosition && upperPosition != placeblePosition)
                {
                    interactor.selectedChunk.CreateBlock(blockTypeToBuild, interactor.placeblePosition);
                }
            }
        }
Exemple #2
0
        bool SelectBlock()
        {
            var hitPosition   = hit.collider.transform.position;
            var chunkPosition = World.GetChunkPosition(hitPosition);

            selectedChunk = world.GetChunk(chunkPosition);
            if (!selectedChunk)
            {
                return(false);
            }

            var outsideHit = Vector3i.FloorToInt(hit.point + hit.normal / 2);
            var insideHit  = Vector3i.FloorToInt(hit.point - hit.normal / 2);

            placeblePosition = outsideHit - chunkPosition;
            var insidePosition = insideHit - chunkPosition;

            if (selectedPosition != insidePosition)
            {
                selectedPosition = insidePosition;
                selectedBlockChanged();
            }
            return(true);
        }
Exemple #3
0
 public static Vector3i GetChunkPosition(Vector3 position)
 {
     return(Vector3i.FloorToInt(position / Chunk.size) * Chunk.size);
 }