Beispiel #1
0
        /// <summary>
        /// Adds the selected block to the dictionary.
        /// </summary>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <param name="z">The z coordinate.</param>
        private void AddSelectedBlock(float x, float y, float z)
        {
            // Convert postion to a Vector3
            Vector3 position = new Vector3(x, y, z);
            // Find the blocktype
            BlockType blockType = _world.BlockTypeAtPoint(position);

            // If the blocktype isn't none
            if (blockType != BlockType.None)
            {
                // Create positioned block
                PositionedBlock posBlock = new PositionedBlock(new Vector3i(position), blockType);

                // TODO: JUST FOR TESTING, later on more block types needs to be checked if it's possible to build there
                // TODO: Check if building is possible
                if (blockType == BlockType.Grass)
                {
                    // Possible to build here so add block and the white texture to dictionary
                    _selectedBlocks.Add(posBlock, _aimedBlockWhiteTexture);
                }
                else
                {
                    // Not possible to build here, add block and the red texture to the dictionary
                    _selectedBlocks.Add(posBlock, _aimedBlockRedTexture);
                }
            }
        }
Beispiel #2
0
 public override bool Equals(object obj)
 {
     if (obj is PositionedBlock)
     {
         PositionedBlock other = (PositionedBlock)obj;
         return(Position == other.Position && Type == other.Type);
     }
     return(base.Equals(obj));
 }
Beispiel #3
0
 public void SetAimedBlock(Vector3i position, BlockType blockType, bool isSolid)
 {
     if (isSolid)
     {
         AimedSolidBlock = new PositionedBlock(position, blockType);
     }
     else
     {
         AimedEmptyBlock = new PositionedBlock(position, blockType);
     }
 }
Beispiel #4
0
        /// <summary>
        /// Calculates the grid.
        /// </summary>
        /// <param name="currentBlock">The current block.</param>
        private void CalculateGrid(PositionedBlock currentBlock)
        {
            // Clear the dictionary so it will start all over
            _selectedBlocks.Clear();

            // Get the starting position of the first block
            Vector3 startPos = _startBlock.Value.Position.AsVector3();

            // Get the position of the block that is currently selected
            Vector3 endPos = currentBlock.Position.AsVector3();

            // Fills up a rectangle with blocks depending the position of the current block (endPos)
            if (startPos.X <= endPos.X && startPos.Z <= endPos.Z)
            {
                for (float x = startPos.X; x <= endPos.X; x++)
                {
                    for (float z = startPos.Z; z <= endPos.Z; z++)
                    {
                        AddSelectedBlock(x, startPos.Y, z);
                    }
                }
            }
            else if (startPos.X <= endPos.X && startPos.Z >= endPos.Z)
            {
                for (float x = startPos.X; x <= endPos.X; x++)
                {
                    for (float z = startPos.Z; z >= endPos.Z; z--)
                    {
                        AddSelectedBlock(x, startPos.Y, z);
                    }
                }
            }
            else if (startPos.X >= endPos.X && startPos.Z >= endPos.Z)
            {
                for (float x = startPos.X; x >= endPos.X; x--)
                {
                    for (float z = startPos.Z; z >= endPos.Z; z--)
                    {
                        AddSelectedBlock(x, startPos.Y, z);
                    }
                }
            }
            else if (startPos.X >= endPos.X && startPos.Z <= endPos.Z)
            {
                for (float x = startPos.X; x >= endPos.X; x--)
                {
                    for (float z = startPos.Z; z <= endPos.Z; z++)
                    {
                        AddSelectedBlock(x, startPos.Y, z);
                    }
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Finds the aimed (targeted) block.
        /// </summary>
        public void FindAimedBlock()
        {
            BlockIndex index;
            float      distance = 0.0f;
            //float? intersect;

            // Get the current mouse state
            MouseState mouseState = Mouse.GetState();

            // Get mouse positions
            Vector2 mousePos = new Vector2(mouseState.X, mouseState.Y);

            // Calculate where the mouse points to
            Ray ray = Camera.GetMouseRay(mousePos, _game.GraphicsDevice.Viewport);

            // While within reach
            while (distance <= WorldSettings.BlockEditing.PLAYERREACH)
            {
                // Create a box at calculated position
                index = new BlockIndex(ray.Direction * distance + ray.Position);

                //intersect = index.GetBoundingBox().Intersects(ray);

                // Get the block from the previous calculated index
                Block block = _world.BlockAt((int)index.Position.X, (int)index.Position.Y, (int)index.Position.Z);

                // If there isn't a block
                if (block.BlockType == BlockType.None)
                {
                    // Set empty aimed block
                    // TODO: does this even have a point? REMOVE?
                    SetAimedBlock(new Vector3i(index.Position), block.BlockType, false);
                }
                // If there's a block and it is active
                else if (block.BlockType != BlockType.None && block.IsActive)
                {
                    // If there's a starting block selected for editing
                    if (_startingBlockSelected)
                    {
                        // Create a positioned block from the current selected block
                        PositionedBlock currentSelectedBlock = new PositionedBlock(new Vector3i(index.Position), block.BlockType);

                        // Check if the starting block is set
                        if (_startBlock != null)
                        {
                            // Calculate the grid
                            CalculateGrid(currentSelectedBlock);
                        }
                    }
                    // If not in editing mode
                    else
                    {
                        // Set aimed block
                        SetAimedBlock(new Vector3i(index.Position), block.BlockType, true);
                    }
                    return;
                }

                // Increaase the distance
                distance += 0.2f;
            }

            // Reset aimed block
            AimedSolidBlock = null;
        }