Example #1
0
        public override void Update(EditorWorldRaycastResult intersection, float deltaTime)
        {
            if (intersection.HitTerrain)
            {
                TerrainRaycastResult terrainIntersection = intersection.TerrainResult;

                if (!TerrainEditor.IsSelecting)
                {
                    lastNonSelectingNormal = terrainIntersection.IntersectionCubeSide.Value;
                }

                if (TerrainEditor.IsSelecting && Input.GetMouseButtonUp(MouseButton.Left))
                {
                    ApplyActionToSelection(TerrainEditor.SelectionBox,
                                           (chunk, blockPos) =>
                    {
                        Block block = Block.CUSTOM;
                        Color color = TerrainEditor.BlockColor;
                        block.R     = color.R;
                        block.G     = color.G;
                        block.B     = color.B;

                        TerrainEditor.SetBlock(chunk, block, blockPos);
                    });
                }
            }
        }
 public override void Update(EditorWorldRaycastResult intersection, float deltaTime)
 {
     if (intersection.HitTerrain && TerrainEditor.IsSelecting && Input.GetMouseButtonUp(MouseButton.Left))
     {
         ApplyActionToSelection(TerrainEditor.SelectionBox,
                                (chunk, blockPos) =>
         {
             TerrainEditor.SetBlock(chunk, Block.AIR, blockPos);
         });
     }
 }
Example #3
0
    private void Update()
    {
        // block removing
        if (Input.GetMouseButtonDown(0))
        {
            mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(mouseRay, out hit, rayDistance))
            {
                TerrainEditor.SetBlock(hit, new BlockAir());
            }
        }

        // block placing
        else if (Input.GetMouseButtonDown(1))
        {
            mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(mouseRay, out hit, rayDistance))
            {
                TerrainEditor.SetBlock(hit, new BlockWood(), true); // test wood!
            }
        }

        // movement
        if (rb != null)
        {
            float horizontalInput = Input.GetAxisRaw("Horizontal");
            float verticalInput   = Input.GetAxisRaw("Vertical");

            forward = Vector3.ProjectOnPlane(cameraTransform.forward, Vector3.up);
            right   = Vector3.ProjectOnPlane(cameraTransform.right, Vector3.up);

            Vector3 targetDir = forward * verticalInput + right * horizontalInput;
            moveDir = Vector3.RotateTowards(moveDir, targetDir, 200 * Mathf.Deg2Rad * Time.deltaTime, 1000);

            if (moveDir != Vector3.zero)
            {
                transform.LookAt(transform.position + moveDir.normalized);
                rb.MovePosition(transform.position + moveDir.normalized * Time.deltaTime * moveSpeed);
            }

            // notify player position
            VSEventManager.Instance.TriggerEvent(new GameEvents.PlayerPositionUpdateEvent(transform.position));

            if (Input.GetButtonDown("Jump"))
            {
                rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
            }
        }
    }
        public override void Update(EditorWorldRaycastResult intersection, float deltaTime)
        {
            if (intersection.HitTerrain && TerrainEditor.IsSelecting && Input.GetMouseButtonUp(MouseButton.Left))
            {
                ApplyActionToSelection(TerrainEditor.SelectionBox,
                                       (chunk, blockPos) =>
                {
                    if (chunk.GetBlockSafe(blockPos.X, blockPos.Y, blockPos.Z) != Block.AIR)
                    {
                        Block block     = Block.CUSTOM;
                        Color color     = TerrainEditor.BlockColor;
                        int grainRadius = grainFactor;
                        block.R         = (byte)MathHelper.ClampToByteRange(color.R + Maths.Random.Next(-grainRadius, grainRadius));
                        block.G         = (byte)MathHelper.ClampToByteRange(color.G + Maths.Random.Next(-grainRadius, grainRadius));
                        block.B         = (byte)MathHelper.ClampToByteRange(color.B + Maths.Random.Next(-grainRadius, grainRadius));

                        TerrainEditor.SetBlock(chunk, block, blockPos);
                    }
                });
            }
        }
Example #5
0
        void MoveSelection()
        {
            int xSize = startSelectionBox.Max.X - startSelectionBox.Min.X + 1;
            int ySize = startSelectionBox.Max.Y - startSelectionBox.Min.Y + 1;
            int zSize = startSelectionBox.Max.Z - startSelectionBox.Min.Z + 1;

            for (int x = 0; x < xSize; x++)
            {
                for (int y = 0; y < ySize; y++)
                {
                    for (int z = 0; z < zSize; z++)
                    {
                        IndexPosition startPos = new IndexPosition(x, y, z) + startSelectionBox.Min;
                        IndexPosition endPos   = new IndexPosition(x, y, z) + TerrainEditor.SelectionBox.Min;

                        IndexPosition cIndex, startBlock, endBlock;
                        GetLocalBlockCoords(startPos, out cIndex, out startBlock);

                        Chunk startChunk, endChunk;
                        if (Terrain.Chunks.TryGetValue(cIndex, out startChunk))
                        {
                            GetLocalBlockCoords(endPos, out cIndex, out endBlock);
                            if (Terrain.Chunks.TryGetValue(cIndex, out endChunk))
                            {
                                Block block = startChunk.GetBlockSafe(startBlock.X, startBlock.Y, startBlock.Z);
                                TerrainEditor.SetBlock(endChunk, block, endBlock);

                                if (!TerrainEditor.SelectionBox.Contains(startPos))
                                {
                                    TerrainEditor.SetBlock(startChunk, Block.AIR, startBlock);
                                }
                            }
                        }
                    }
                }
            }

            startSelectionBox.SetMinMax(TerrainEditor.SelectionBox.Min, TerrainEditor.SelectionBox.Max);
        }
Example #6
0
        void PasteSelection()
        {
            EditorSelectionBox selectionBox = TerrainEditor.SelectionBox;
            IndexPosition      origin       = selectionBox.Min;

            IndexPosition min = origin;
            IndexPosition max = origin + new IndexPosition(
                copy.GetLength(2) - 1,
                copy.GetLength(1) - 1,
                copy.GetLength(0) - 1);

            if (selectionBox.Size() == IndexPosition.Zero && (max - min) != IndexPosition.Zero)
            {
                min += new IndexPosition(0, 1, 0);
                max += new IndexPosition(0, 1, 0);
            }

            selectionBox.SetMinMax(min, max);

            for (int x = selectionBox.Min.X, _x = 0; x <= selectionBox.Max.X; x++, _x++)
            {
                for (int y = selectionBox.Min.Y, _y = 0; y <= selectionBox.Max.Y; y++, _y++)
                {
                    for (int z = selectionBox.Min.Z, _z = 0; z <= selectionBox.Max.Z; z++, _z++)
                    {
                        IndexPosition globalPos = new IndexPosition(x, y, z);
                        IndexPosition cIndex, bIndex;
                        GetLocalBlockCoords(globalPos, out cIndex, out bIndex);

                        Chunk chunk;
                        if (Terrain.Chunks.TryGetValue(cIndex, out chunk))
                        {
                            TerrainEditor.SetBlock(chunk, copy[_z, _y, _x], bIndex);
                        }
                    }
                }
            }
        }
Example #7
0
        void DeleteSelection()
        {
            EditorSelectionBox selectionBox = TerrainEditor.SelectionBox;

            for (int x = selectionBox.Min.X, _x = 0; x <= selectionBox.Max.X; x++, _x++)
            {
                for (int y = selectionBox.Min.Y, _y = 0; y <= selectionBox.Max.Y; y++, _y++)
                {
                    for (int z = selectionBox.Min.Z, _z = 0; z <= selectionBox.Max.Z; z++, _z++)
                    {
                        IndexPosition globalPos = new IndexPosition(x, y, z);
                        IndexPosition cIndex, bIndex;
                        GetLocalBlockCoords(globalPos, out cIndex, out bIndex);

                        Chunk chunk;
                        if (Terrain.Chunks.TryGetValue(cIndex, out chunk))
                        {
                            TerrainEditor.SetBlock(chunk, Block.AIR, bIndex);
                        }
                    }
                }
            }
        }
        public override void Update(EditorWorldRaycastResult worldIntersection, float deltaTime)
        {
            halfBrush = (int)Math.Ceiling(brushSize / 2f);

            if (worldIntersection.HitTerrain && !GUISystem.HandledMouseInput)
            {
                TerrainRaycastResult intersection = worldIntersection.TerrainResult;

                if (Input.GetMouseButtonDown(MouseButton.Left))
                {
                    if (Input.IsControlHeld)
                    {
                        dragging        = true;
                        dragHeight      = intersection.BlockIndex.Value.Y;
                        chunkDragHeight = intersection.Chunk.IndexPosition.Y;
                    }
                }

                if (Input.GetMouseButtonUp(MouseButton.Left) && dragging)
                {
                    dragging = false;
                }
                else if (Input.GetMouseButtonUp(MouseButton.Left) || dragging)
                {
                    IndexPosition cIndex, bIndex;
                    if (dragging)
                    {
                        cIndex = new IndexPosition(intersection.Chunk.IndexPosition.X,
                                                   chunkDragHeight, intersection.Chunk.IndexPosition.Z);
                        bIndex = new IndexPosition(intersection.BlockIndex.Value.X, dragHeight,
                                                   intersection.BlockIndex.Value.Z);
                    }
                    else
                    {
                        cIndex = intersection.Chunk.IndexPosition;
                        bIndex = intersection.BlockIndex.Value;
                    }

                    IndexPosition globalBlock = GetGlobalBlockCoords(cIndex, bIndex);

                    for (int x = -halfBrush; x <= halfBrush; x++)
                    {
                        for (int y = -halfBrush; y <= halfBrush; y++)
                        {
                            for (int z = -halfBrush; z <= halfBrush; z++)
                            {
                                float dist = (new Vector3(x, y, z)).Length;
                                if (dist > halfBrush)
                                {
                                    continue;
                                }

                                float distPercent = (float)Math.Cos((dist / halfBrush) * MathHelper.PiOver2);
                                int   offset      = (int)Math.Round(distPercent * riseHeight);

                                IndexPosition globalPos = globalBlock + new IndexPosition(x, y - offset, z);
                                GetLocalBlockCoords(globalPos, out cIndex, out bIndex);

                                Chunk chunk;
                                if (Terrain.Chunks.TryGetValue(cIndex, out chunk) && chunk.IsBlockCoordInRange(bIndex))
                                {
                                    IndexPosition cIndex2, bIndex2;
                                    IndexPosition globalPos2 = globalBlock + new IndexPosition(x, y, z);
                                    GetLocalBlockCoords(globalPos2, out cIndex2, out bIndex2);

                                    Chunk chunk2;
                                    if (Terrain.Chunks.TryGetValue(cIndex2, out chunk2) && chunk2.IsBlockCoordInRange(bIndex2))
                                    {
                                        TerrainEditor.SetBlock(chunk2, chunk[bIndex], bIndex2);
                                    }
                                }
                            }
                        }
                    }

                    //ApplyActionToBrush(cIndex, bIndex,
                    //    (chunk, blockIndex, x, y, z, _) =>
                    //    {
                    //    //    int ay = 0;// Math.Abs(y);
                    //    //    float dist = (new Vector3(x + ay, ay, z + ay)).Length;
                    //    //    int offset = (int)Math.Round(((brushSize - dist) / brushSize) * riseHeight);

                    //    ////if (x == 0 && z == 0)
                    //    ////    y++;

                    //    //if (y > riseHeight)
                    //    //        return;

                    //    //    IndexPosition newBlockPos;
                    //    //    IndexPosition newChunkIndex;
                    //    //    Chunk.WrapBlockCoords(blockIndex.X, blockIndex.Y + offset, blockIndex.Z, chunk.IndexPosition,
                    //    //        out newBlockPos, out newChunkIndex);

                    //    //    if (Terrain.Chunks.TryGetValue(newChunkIndex, out chunk))
                    //    //    {
                    //    //        IndexPosition blockPos = new IndexPosition(x, y, z);
                    //    //        Vector3 coloroff = (blockPos + (newChunkIndex * Chunk.SIZE)) * Block.CUBE_SIZE;
                    //    //        Nybble2 data;
                    //    //        Color voxelColor;
                    //    //        if (y == riseHeight)
                    //    //        {
                    //    //            data = Block.GRASS.Data;
                    //    //            voxelColor = grassGradient.GetColor(coloroff);
                    //    //        }
                    //    //        else
                    //    //        {
                    //    //            data = Block.STONE.Data;
                    //    //            voxelColor = stoneGradient.GetColor(coloroff);
                    //    //        }

                    //    //        voxelColor = new Color(
                    //    //            (byte)MathHelper.Clamp(voxelColor.R + Maths.Random.Next(-3, 3), 0, 255),
                    //    //            (byte)MathHelper.Clamp(voxelColor.G + Maths.Random.Next(-3, 3), 0, 255),
                    //    //            (byte)MathHelper.Clamp(voxelColor.B + Maths.Random.Next(-3, 3), 0, 255));

                    //    //        if (chunk.GetBlockSafe(newBlockPos.X, newBlockPos.Y, newBlockPos.Z).Data.Value != data.Value)
                    //    //            Editor.SetBlock(chunk, new Block(data, voxelColor.R, voxelColor.G, voxelColor.B), newBlockPos);
                    //    //    }
                    //    });
                }
            }
        }