Example #1
0
    private void SetupStencil(bool isBreaking)
    {
        activeStencil = stencils[stencilIndex];

        if (worldManager.creativeMode)
        {
            activeStencil.Initialize(fillTypeIndex, radiusIndex);
        }
        else
        {
            var fillType = 0;
            var blocks   = BlockManager.Read().blocks;

            for (int i = 0; i < blocks.Count; i++)
            {
                if (isBreaking)
                {
                    fillType = 0;
                    break;
                }
                if (uiHotBar.currentItem != null && blocks[i].blockType.ToString() == uiHotBar.currentItem.itemType.ToString())
                {
                    fillType = i;
                    break;
                }
            }

            activeStencil.Initialize(fillType, radiusIndex);
        }
    }
    public void Apply(VoxelStencil stencil)
    {
        int xStart = stencil.XStart;
        int xEnd = stencil.XEnd;
        int yStart = stencil.YStart;
        int yEnd = stencil.YEnd;

        if (xStart < 0)
        {
            xStart = 0;
        }
        if (xEnd >= m_resolution)
        {
            xEnd = m_resolution - 1;
        }
        if (yStart < 0)
        {
            yStart = 0;
        }
        if (yEnd >= m_resolution)
        {
            yEnd = m_resolution - 1;
        }

        for (int y = yStart; y <= yEnd; y++)
        {
            int i = y * m_resolution + xStart;
            for (int x = xStart; x <= xEnd; x++, i++)
            {
                m_voxels[i].m_state = stencil.Apply(x, y, m_voxels[i].m_state);
            }
        }

        Refresh();
    }
Example #3
0
    void MobileControls()
    {
        if (clickState != ClickState.NONE || clickState != ClickState.UP)
        {
            Touch touch = Input.GetTouch(0);
            Ray   ray   = cam.ScreenPointToRay(touch.position);
            scale = Vector3.one * ((radiusIndex + 0.5f) * voxelSize * 2f);
            RaycastHit singleRayHitInfo;

            if (Physics.Raycast(ray, out singleRayHitInfo))
            {
                Vector3 singleRayCenter = transform.InverseTransformPoint(singleRayHitInfo.point);
                gizmoCenter = singleRayCenter;
                Vector3 center = singleRayCenter;
                //   Debug.DrawRay(ray.origin, ray.direction * 20, Color.red);

                center.x += halfSize;
                center.y += halfSize;

                if (clickState == ClickState.HOLD)
                {
                    CylinderCast(touch.position);

                    VoxelStencil activeStencil = stencils[stencilIndex];
                    activeStencil.Initialize(fillTypeIndex == 0, (radiusIndex + 0.5f) * voxelSize);
                    activeStencil.SetCenter(center.x, center.y);

                    activeLayer.EditVoxels(center, activeStencil);
                }
            }
        }
    }
Example #4
0
    private void Update()
    {
        Transform  visualization = stencilVisualizations[stencilIndex];
        RaycastHit hitInfo;

        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo) &&
            hitInfo.collider.gameObject == gameObject)
        {
            //used for stencil visualization calculations
            Vector2 center = transform.InverseTransformPoint(hitInfo.point);
            center.x += halfSize;
            center.y += halfSize;
            //----------------
            if (Input.GetMouseButton(0))
            {
                VoxelStencil activeStencil = stencils[stencilIndex];
                activeStencil.Initialize(fillTypeIndex == 0, (radiusIndex + 0.5f) * voxelSize);
                activeStencil.SetCenter(center.x, center.y);

                EditVoxels(center, activeStencil);
            }
            //used for stencil visualization calculations
            center.x -= halfSize;
            center.y -= halfSize;
            visualization.localPosition = center;
            Vector3 tempScale = Vector3.one * ((radiusIndex + 0.5f) * voxelSize * 2f);
            visualization.localScale = new Vector3(tempScale.x, 0.01f, tempScale.z);
            //----------------
            visualization.gameObject.SetActive(true);
        }
        else
        {
            visualization.gameObject.SetActive(false);
        }
    }
 public void Apply(VoxelStencil stencil)
 {
     int xStart = stencil.XStart;
     if (xStart < 0)
     {
         xStart = 0;
     }
     int xEnd = stencil.XEnd;
     if (xEnd >= Resoultion)
     {
         xEnd = Resoultion - 1;
     }
     int yStart = stencil.YStart;
     if (yStart < 0)
     {
         yStart = 0;
     }
     int yEnd = stencil.YEnd;
     if (yEnd >= Resoultion)
     {
         yEnd = Resoultion - 1;
     }
     for (int y = yStart; y <= yEnd; y++)
     {
         int i = y*Resoultion + xStart;
         for (int x = xStart; x <= xEnd; x++, i++)
         {
             _voxels[i] = stencil.Apply(x, y);
         }
     }
     SetVoxelColor();
 }
Example #6
0
    private void EditVoxels(Vector2 center)
    {
        VoxelStencil activeStencil = stencils[stencilIndex];

        activeStencil.Initialize(
            fillTypeIndex, (radiusIndex + 0.5f) * voxelSize);
        activeStencil.SetCenter(center.x, center.y);

        EditVoxels(center, activeStencil);
    }
Example #7
0
    void CutStart()
    {
        Vector2 center = new Vector2(chunkResolutionX * chunkSize / 2, chunkResolutionY * chunkSize);

        VoxelStencil activeStencil = stencils[1];

        activeStencil.Initialize(false, (4 + 0.5f) * voxelSize);
        activeStencil.SetCenter(center.x, center.y);

        EditVoxels(center, activeStencil);
    }
Example #8
0
    public bool Apply(VoxelStencil stencil)
    {
        int xStart = stencil.XStart;

        if (xStart < 0)
        {
            xStart = 0;
        }

        int xEnd = stencil.XEnd;

        if (xEnd >= resolution)
        {
            xEnd = resolution - 1;
        }

        int yStart = stencil.YStart;

        if (yStart < 0)
        {
            yStart = 0;
        }

        int yEnd = stencil.YEnd;

        if (yEnd >= resolution)
        {
            yEnd = resolution - 1;
        }

        bool didUpdate = false;

        for (int y = yStart; y <= yEnd; y++)
        {
            int i = y * resolution + xStart;
            for (int x = xStart; x <= xEnd; x++, i++)
            {
                if (voxels[i].state != stencil.fillType)
                {
                    voxels[i].state = stencil.Apply(x, y, voxels[i].state);
                    didUpdate       = true;
                }
            }
        }

        Refresh();
        if (didUpdate)
        {
            shouldUpdateCollider = true;
        }

        return(didUpdate);
    }
    private void EditVoxels(Vector3 point)
    {
        int centerX = (int)((point.x + halfSize) / voxelSize);
        int centerY = (int)((point.y + halfSize) / voxelSize);

        int xStart = (centerX - radiusIndex - 1) / voxelResolution;

        if (xStart < 0)
        {
            xStart = 0;
        }
        int xEnd = (centerX + radiusIndex) / voxelResolution;

        if (xEnd >= chunkResolution)
        {
            xEnd = chunkResolution - 1;
        }
        int yStart = (centerY - radiusIndex - 1) / voxelResolution;

        if (yStart < 0)
        {
            yStart = 0;
        }
        int yEnd = (centerY + radiusIndex) / voxelResolution;

        if (yEnd >= chunkResolution)
        {
            yEnd = chunkResolution - 1;
        }


        VoxelStencil activeStencil = stencils[stencilIndex];

        activeStencil.Initialize(fillTypeIndex == 0, radiusIndex);

        int voxelYOffset = yEnd * voxelResolution;

        for (int y = yEnd; y >= yStart; y--)
        {
            int i            = y * chunkResolution + xEnd;
            int voxelXOffset = xEnd * voxelResolution;
            for (int x = xEnd; x >= xStart; x--, i--)
            {
                activeStencil.SetCenter(centerX - voxelXOffset, centerY - voxelYOffset);
                chunks[i].Apply(activeStencil);
                voxelXOffset -= voxelResolution;
            }
            voxelYOffset -= voxelResolution;
        }
    }
Example #10
0
    private void EditVoxels(Vector3 point)
    {
        int centerX = (int)((point.x + _halfSize) / _voxelSize);
        int centerY = (int)((point.y + _halfSize) / _voxelSize);
        int xStart = (centerX - _radiusIndex) / VoxelResolution;
        if (xStart < 0)
        {
            xStart = 0;
        }
        int xEnd = (centerX + _radiusIndex) / VoxelResolution;
        if (xEnd >= ChunkResolution)
        {
            xEnd = ChunkResolution - 1;
        }
        int yStart = (centerY - _radiusIndex) / VoxelResolution;
        if (yStart < 0)
        {
            yStart = 0;
        }
        int yEnd = (centerY + _radiusIndex) / VoxelResolution;
        if (yEnd >= ChunkResolution)
        {
            yEnd = ChunkResolution - 1;
        }
        int chunkX = centerX / VoxelResolution;
        int chunkY = centerY / VoxelResolution;
        centerX -= chunkX * VoxelResolution;
        centerY -= chunkY * VoxelResolution;
        VoxelStencil activeStencil = new VoxelStencil();
        activeStencil.Initialize(_fillTypeIndex == 0, _radiusIndex);

        activeStencil.SetCenter(centerX,centerY);
        _chunks[chunkY * ChunkResolution + chunkX].Apply(activeStencil);

        int voxelYOffset = yStart * VoxelResolution;
        for (int y = yStart; y <= yEnd; y++)
        {
            int i = y * ChunkResolution + xStart;
            int voxelXOffset = xStart * VoxelResolution;
            for (int x = xStart; x <= xEnd; x++, i++)
            {
                activeStencil.SetCenter(centerX - voxelXOffset, centerY - voxelYOffset);
                _chunks[i].Apply(activeStencil);
                voxelXOffset += VoxelResolution;
            }
            voxelYOffset += VoxelResolution;
        }
        Debug.Log(centerX + ", " + centerY + " in chunk " + chunkX + ", " + chunkY);
    }
Example #11
0
    void PCcontrolsCylinder()
    {
        Transform visualization = stencilVisualizations[stencilIndex];
        Ray       ray           = cam.ScreenPointToRay(Input.mousePosition);

        scale = Vector3.one * 2;// * ((radiusIndex + 0.5f) * voxelSize * 2f);
        RaycastHit singleRayHitInfo;

        if (Physics.Raycast(ray, out singleRayHitInfo))
        {
            Vector3 singleRayCenter = transform.InverseTransformPoint(singleRayHitInfo.point);
            gizmoCenter = singleRayCenter;
            Vector3 center = singleRayCenter;
            Debug.DrawRay(ray.origin, ray.direction * 20, Color.red);

            #region used for stencil visualization calculations
            center.x += halfSize;
            center.y += halfSize;
            #endregion

            if (clickState == ClickState.HOLD)
            {
                CylinderCast(Input.mousePosition);

                VoxelStencil activeStencil = stencils[stencilIndex];
                activeStencil.Initialize(fillTypeIndex == 0, 1);
                activeStencil.SetCenter(center.x, center.y);

                activeLayer.EditVoxels(center, activeStencil);
            }

            #region used for stencil visualization calculations

            center.x -= halfSize;
            center.y -= halfSize;
            center.z -= 0.30f;
            visualization.localPosition = center;
            visualization.localScale    = new Vector3(scale.x, 0.05f, scale.z);

            #endregion

            visualization.gameObject.SetActive(true);
        }
        else
        {
            visualization.gameObject.SetActive(false);
            //Debug.Log(hitInfo.collider.gameObject.name);
        }
    }
Example #12
0
        private void PaintVoxels(Vector3 point, VoxelStencil stencil)
        {
            int centerX = (int)((point.x + voxelMap.halfSize) / voxelMap.voxelSize);
            int centerY = (int)((point.y + voxelMap.halfSize) / voxelMap.voxelSize);

            Debug.Log(centerX + " - " + centerY + " Chunk(" + ")");
            stencil.Initialize(fillTypeIndex == 0, radiusIndex);

            int xStart = (centerX - radiusIndex - 1) / voxelMap.gridResolution;

            if (xStart < 0)
            {
                xStart = 0;
            }
            int xEnd = (centerX + radiusIndex) / voxelMap.gridResolution;

            if (xEnd >= voxelMap.resolution)
            {
                xEnd = voxelMap.resolution - 1;
            }
            int yStart = (centerY - radiusIndex - 1) / voxelMap.gridResolution;

            if (yStart < 0)
            {
                yStart = 0;
            }
            int yEnd = (centerY + radiusIndex) / voxelMap.gridResolution;

            if (yEnd >= voxelMap.resolution)
            {
                yEnd = voxelMap.resolution - 1;
            }

            int voxelYOffset = yEnd * voxelMap.gridResolution;

            for (int y = yEnd; y >= yStart; y--)
            {
                int i            = y * voxelMap.resolution + xEnd;
                int voxelXOffset = xEnd * voxelMap.gridResolution;
                for (int x = xEnd; x >= xStart; x--, i--)
                {
                    stencil.SetCenter(centerX - voxelXOffset, centerY - voxelYOffset);
                    ApplyChunkStencil(voxelMap.chunks[i], stencil);
                    voxelXOffset -= voxelMap.gridResolution;
                }
                voxelYOffset -= voxelMap.gridResolution;
            }
        }
Example #13
0
    private void EditVoxels(Vector3 point)
    {
        int voxelX = (int)((point.x + halfSize) / voxelSize);
        int voxelY = (int)((point.y + halfSize) / voxelSize);
        int chunkX = voxelX / voxelResolution;
        int chunkY = voxelY / voxelResolution;

        Debug.Log(voxelX + ", " + voxelY + " in chunk " + chunkX + ", " + chunkY);

        voxelX -= chunkX * voxelResolution;
        voxelY -= chunkY * voxelResolution;

        VoxelStencil activeStencil = new VoxelStencil();

        chunks[chunkY * chunkResolution + chunkX].Apply(voxelX, voxelY, activeStencil);
    }
Example #14
0
    private void EditVoxels(Vector2 center)
    {
        VoxelStencil activeStencil = stencils[stencilIndex];

        activeStencil.Initialize(fillTypeIndex == 0, (radiusIndex + 0.5f) * voxelSize);
        activeStencil.SetCenter(center.x, center.y);

        int xStart = (int)((activeStencil.XStart - voxelSize) / chunkSize);

        if (xStart < 0)
        {
            xStart = 0;
        }
        int xEnd = (int)((activeStencil.XEnd + voxelSize) / chunkSize);

        if (xEnd >= chunkResolution)
        {
            xEnd = chunkResolution - 1;
        }
        int yStart = (int)((activeStencil.YStart - voxelSize) / chunkSize);

        if (yStart < 0)
        {
            yStart = 0;
        }
        int yEnd = (int)((activeStencil.YEnd + voxelSize) / chunkSize);

        if (yEnd >= chunkResolution)
        {
            yEnd = chunkResolution - 1;
        }

        for (int y = yEnd; y >= yStart; y--)
        {
            int i = y * chunkResolution + xEnd;
            for (int x = xEnd; x >= xStart; x--, i--)
            {
                activeStencil.SetCenter(center.x - x * chunkSize, center.y - y * chunkSize);
                chunks[i].Apply(activeStencil);
            }
        }
    }
Example #15
0
    public void Apply(VoxelStencil stencil)
    {
        int xStart = stencil.XStart;

        if (xStart < 0)
        {
            xStart = 0;
        }

        int xEnd = stencil.XEnd;

        if (xEnd >= resolution)
        {
            xEnd = resolution - 1;
        }

        int yStart = stencil.YStart;

        if (yStart < 0)
        {
            yStart = 0;
        }

        int yEnd = stencil.YEnd;

        if (yEnd >= resolution)
        {
            yEnd = resolution - 1;
        }

        for (int y = yStart; y <= yEnd; y++)
        {
            int i = y * resolution + xStart;
            for (int x = xStart; x <= xEnd; x++, i++)
            {
                voxels[i].state = stencil.Apply(x, y, voxels[i].state);
            }
        }
        Refresh();
    }
Example #16
0
    public void Apply(VoxelStencil stencil)
    {
        int xStart = (int)(stencil.XStart / voxelSize);

        if (xStart < 0)
        {
            xStart = 0;
        }
        int xEnd = (int)(stencil.XEnd / voxelSize);

        if (xEnd >= resolution)
        {
            xEnd = resolution - 1;
        }
        int yStart = (int)(stencil.YStart / voxelSize);

        if (yStart < 0)
        {
            yStart = 0;
        }
        int yEnd = (int)(stencil.YEnd / voxelSize);

        if (yEnd >= resolution)
        {
            yEnd = resolution - 1;
        }

        for (int y = yStart; y <= yEnd; y++)
        {
            int i = y * resolution + xStart;
            for (int x = xStart; x <= xEnd; x++, i++)
            {
                stencil.Apply(voxels[i]);
            }
        }
        SetCrossings(stencil, xStart, xEnd, yStart, yEnd);
        Refresh();
    }
Example #17
0
    private void EditVoxels(Vector2 center, VoxelStencil activeStencil)
    {
        int xStart = (int)((activeStencil.XStart - voxelSize) / chunkSize);

        if (xStart < 0)
        {
            xStart = 0;
        }
        int xEnd = (int)((activeStencil.XEnd + voxelSize) / chunkSize);

        if (xEnd >= chunkResolution)
        {
            xEnd = chunkResolution - 1;
        }
        int yStart = (int)((activeStencil.YStart - voxelSize) / chunkSize);

        if (yStart < 0)
        {
            yStart = 0;
        }
        int yEnd = (int)((activeStencil.YEnd + voxelSize) / chunkSize);

        if (yEnd >= chunkResolution)
        {
            yEnd = chunkResolution - 1;
        }

        for (int y = yEnd; y >= yStart; y--)
        {
            int i = y * chunkResolution + xEnd;
            for (int x = xEnd; x >= xStart; x--, i--)
            {
                activeStencil.SetCenter(
                    center.x - x * chunkSize, center.y - y * chunkSize);
                chunks[i].Apply(activeStencil);
            }
        }
    }
Example #18
0
        private void ApplyChunkStencil(VoxelGrid grid, VoxelStencil stencil)
        {
            int xStart = stencil.XStart;

            if (xStart < 0)
            {
                xStart = 0;
            }
            int xEnd = stencil.XEnd;

            if (xEnd >= grid.resolution)
            {
                xEnd = grid.resolution - 1;
            }
            int yStart = stencil.YStart;

            if (yStart < 0)
            {
                yStart = 0;
            }
            int yEnd = stencil.YEnd;

            if (yEnd >= grid.resolution)
            {
                yEnd = grid.resolution - 1;
            }

            for (int y = yStart; y <= yEnd; y++)
            {
                int i = y * grid.resolution + xStart;
                for (int x = xStart; x <= xEnd; x++, i++)
                {
                    grid.voxels[i].state = stencil.Apply(x, y, grid.voxels[i].state);
                }
            }
            grid.UpdateGrid();
        }
Example #19
0
    public void EditVoxels(Vector2 center, VoxelStencil stencilToUse)
    {
        int xStart = (int)((stencilToUse.XStart - voxelSize) / chunkSize);

        if (xStart < 0)
        {
            xStart = 0;
        }
        int xEnd = (int)((stencilToUse.XEnd + voxelSize) / chunkSize);

        if (xEnd >= chunkResolutionX)
        {
            xEnd = chunkResolutionX - 1;
        }
        int yStart = (int)((stencilToUse.YStart - voxelSize) / chunkSize);

        if (yStart < 0)
        {
            yStart = 0;
        }
        int yEnd = (int)((stencilToUse.YEnd + voxelSize) / chunkSize);

        if (yEnd >= chunkResolutionY)
        {
            yEnd = chunkResolutionY - 1;
        }

        for (int y = yEnd; y >= yStart; y--)
        {
            int i = y * chunkResolutionX + xEnd;
            for (int x = xEnd; x >= xStart; x--, i--)
            {
                stencilToUse.SetCenter(center.x - x * chunkSize, center.y - y * chunkSize);
                chunks[i].Apply(stencilToUse, ref activeVoxelsCount);
            }
        }
    }
Example #20
0
    public void Apply(VoxelStencil stencil)
    {
        int xStart = Mathf.Max(stencil.XStart, 0);
        int xEnd = Mathf.Min(stencil.XEnd, resolution - 1);
        int yStart = Mathf.Max(stencil.YStart, 0);
        int yEnd = Mathf.Min(stencil.YEnd, resolution - 1);

        for (int y = yStart; y <= yEnd; y++)
        {
            int i = y * resolution + xStart;
            for (int x = xStart; x <= xEnd; x++, i++)
            {
                voxels[i].state = stencil.Apply(x, y, voxels[i].state);
            }
        }
        Refresh();
    }
Example #21
0
 public void Apply(int x, int y, VoxelStencil stencil)
 {
     voxels[y * resolution + x] = stencil.Apply(x, y);
     SetVoxelColors();
 }
Example #22
0
    private void SetCrossings(VoxelStencil stencil, int xStart, int xEnd, int yStart, int yEnd)
    {
        bool crossHorizontalGap = false;
        bool lastVerticalRow    = false;
        bool crossVerticalGap   = false;

        if (xStart > 0)
        {
            xStart -= 1;
        }
        if (xEnd == resolution - 1)
        {
            xEnd -= 1;
            crossHorizontalGap = xNeighbor != null;
        }
        if (yStart > 0)
        {
            yStart -= 1;
        }
        if (yEnd == resolution - 1)
        {
            yEnd            -= 1;
            lastVerticalRow  = true;
            crossVerticalGap = yNeighbor != null;
        }

        Voxel a, b;

        for (int y = yStart; y <= yEnd; y++)
        {
            int i = y * resolution + xStart;
            b = voxels[i];
            for (int x = xStart; x <= xEnd; x++, i++)
            {
                a = b;
                b = voxels[i + 1];
                stencil.SetHorizontalCrossing(a, b);
                stencil.SetVerticalCrossing(a, voxels[i + resolution]);
            }
            stencil.SetVerticalCrossing(b, voxels[i + resolution]);
            if (crossHorizontalGap)
            {
                dummyX.BecomeXDummyOf(xNeighbor.voxels[y * resolution], gridSize);
                stencil.SetHorizontalCrossing(b, dummyX);
            }
        }
        if (lastVerticalRow)
        {
            int i = voxels.Length - resolution + xStart;
            b = voxels[i];
            for (int x = xStart; x <= xEnd; x++, i++)
            {
                a = b;
                b = voxels[i + 1];
                stencil.SetHorizontalCrossing(a, b);
                if (crossVerticalGap)
                {
                    dummyY.BecomeYDummyOf(yNeighbor.voxels[x], gridSize);
                    stencil.SetVerticalCrossing(a, dummyY);
                }
            }
            if (crossVerticalGap)
            {
                dummyY.BecomeYDummyOf(yNeighbor.voxels[xEnd + 1], gridSize);
                stencil.SetVerticalCrossing(b, dummyY);
            }
            if (crossHorizontalGap)
            {
                dummyX.BecomeXDummyOf(xNeighbor.voxels[voxels.Length - resolution], gridSize);
                stencil.SetHorizontalCrossing(b, dummyX);
            }
        }
    }
Example #23
0
    private void EditVoxels(Vector3 point)
    {
        chunkPos = new Vector3(Mathf.Floor(point.x / voxelResolution), Mathf.Floor(point.y / voxelResolution));
        diff     = new Vector2Int((int)Mathf.Abs(point.x - (chunkPos * voxelResolution).x),
                                  (int)Mathf.Abs(point.y - (chunkPos * voxelResolution).y));

        xStart = (diff.x - radiusIndex - 1) / voxelResolution;
        if (xStart <= -chunkResolution)
        {
            xStart = -chunkResolution + 1;
        }

        xEnd = (diff.x + radiusIndex) / voxelResolution;
        if (xEnd >= chunkResolution)
        {
            xEnd = chunkResolution - 1;
        }

        yStart = (diff.y - radiusIndex - 1) / voxelResolution;
        if (yStart <= -chunkResolution)
        {
            yStart = -chunkResolution + 1;
        }

        yEnd = (diff.y + radiusIndex) / voxelResolution;
        if (yEnd >= chunkResolution)
        {
            yEnd = chunkResolution - 1;
        }

        activeStencil = stencils[stencilIndex];
        activeStencil.Initialize(fillTypeIndex, radiusIndex);

        var        voxelYOffset = yEnd * voxelResolution;
        Vector2Int checkChunk;

        updateChunkPositions.Clear();

        var result = false;

        for (var y = yStart - 1; y < yEnd + 1; y++)
        {
            var voxelXOffset = xEnd * voxelResolution;
            for (var x = xStart - 1; x < xEnd + 1; x++)
            {
                activeStencil.SetCenter(diff.x - voxelXOffset, diff.y - voxelYOffset);

                checkChunk = new Vector2Int((int)Mathf.Floor((point.x + voxelXOffset) / voxelResolution), (int)Mathf.Floor((point.y + voxelYOffset) / voxelResolution));

                if (existingChunks.ContainsKey(checkChunk))
                {
                    var currentChunk = existingChunks[checkChunk];
                    var tempRes      = currentChunk.Apply(activeStencil);
                    if (!result && tempRes)
                    {
                        result = true;
                    }
                }
                voxelXOffset -= voxelResolution;
            }
            voxelYOffset -= voxelResolution;
        }

        if (result)
        {
            checkChunk = new Vector2Int((int)Mathf.Floor((point.x) / voxelResolution), (int)Mathf.Floor((point.y) / voxelResolution));

            EditChunkAndNeighbors(checkChunk, new Vector3(Mathf.Floor(point.x), Mathf.Floor(point.y)));

            updateChunkPositions.Sort(SortByPosition);
            foreach (var chunk in from pos in updateChunkPositions where existingChunks.ContainsKey(pos) select existingChunks[pos])
            {
                voxelMesh.TriangulateChunkMesh(chunk);
                chunkCollider.Generate2DCollider(chunk, chunkResolution);
            }
        }
    }
Example #24
0
    public bool Apply(VoxelStencil stencil)
    {
        var xStart = stencil.XStart;

        if (xStart < 0)
        {
            xStart = 0;
        }

        var xEnd = stencil.XEnd;

        if (xEnd >= resolution)
        {
            xEnd = resolution - 1;
        }

        var yStart = stencil.YStart;

        if (yStart < 0)
        {
            yStart = 0;
        }

        var yEnd = stencil.YEnd;

        if (yEnd >= resolution)
        {
            yEnd = resolution - 1;
        }

        var didUpdate = false;

        for (var y = yStart; y <= yEnd; y++)
        {
            var i = VoxelIndexFromLocalPos(xStart, y);
            for (var x = xStart; x <= xEnd; x++, i++)
            {
                if (voxels[i].state != stencil.fillType)
                {
                    // Deleting
                    if (stencil.fillType == 0)
                    {
                        var deletingBlock = blockCollection.blocks[voxels[i].state];
                        var tempState     = stencil.Apply(x, y, voxels[i].state);

                        if (tempState == stencil.fillType)
                        {
                            voxels[i].state = tempState;

                            var itemType = deletingBlock.itemType;
                            var amount   = deletingBlock.amount;
                            var item     = new Item {
                                itemType = itemType, amount = amount
                            };

                            var worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                            WorldItem.SpawnWorldItem(new Vector2(worldMousePos.x, worldMousePos.y), item);
                            didUpdate = true;
                        }
                    }
                    // Placing
                    if (stencil.fillType != 0 && voxels[i].state == 0)
                    {
                        var placingItemType = BlockManager.Read().blocks[stencil.fillType].itemType;
                        voxels[i].state = stencil.Apply(x, y, voxels[i].state);

                        uiHotBar.Place();

                        didUpdate = true;
                    }
                }
            }
        }

        Refresh();
        if (didUpdate)
        {
            shouldUpdateCollider = true;
        }

        return(didUpdate);
    }