public void CheckBuild(GameTime gameTime)
        {
            PlayerIndex controlIndex;

            MouseState mouseState = Mouse.GetState();

            if (_game.InputState.IsButtonPressed(Buttons.RightTrigger, _game.ActivePlayerIndex, out controlIndex) ||
                _game.InputState.IsKeyPressed(Keys.Q, _game.ActivePlayerIndex, out controlIndex) ||
                (mouseState.LeftButton == ButtonState.Pressed && _previousMouseState.LeftButton != ButtonState.Pressed))
            {
                for (float x = 0.5f; x < 5f; x += 0.2f)
                {
                    Vector3   targetPoint = Camera.Position + (_lookVector * x);
                    BlockType blockType   = _world.BlockAtPoint(targetPoint);
                    if (blockType != BlockType.None && blockType != BlockType.Water)
                    {
                        if (targetPoint.Y > 2)
                        {
                            // Can't dig water or lava
                            BlockType targetType = _world.BlockAtPoint(targetPoint);
                            if (BlockInformation.IsDiggable(targetType))
                            {
                                _world.RemoveBlock((ushort)targetPoint.X, (ushort)targetPoint.Y, (ushort)targetPoint.Z);
                            }
                        }
                        break;
                    }
                }
            }
            if (_game.InputState.IsKeyPressed(Keys.V, _game.ActivePlayerIndex, out controlIndex))
            {
                for (float x = 0.5f; x < 5f; x += 0.2f)
                {
                    Vector3 targetPoint = Camera.Position + (_lookVector * x);
                    if (_world.BlockAtPoint(targetPoint) != BlockType.None)
                    {
                        Random r = new Random();
                        for (ushort dy = (ushort)(targetPoint.Y - 3); dy < (ushort)(targetPoint.Y + 3); dy++)
                        {
                            for (ushort dx = (ushort)(targetPoint.X - 3); dx < (ushort)(targetPoint.X + 3); dx++)
                            {
                                for (ushort dz = (ushort)(targetPoint.Z - 3); dz < (ushort)(targetPoint.Z + 3); dz++)
                                {
                                    if (r.Next(3) == 0)
                                    {
                                        _world.RemoveBlock(dx, dy, dz);
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            }
            if (_game.InputState.IsButtonPressed(Buttons.LeftTrigger, _game.ActivePlayerIndex, out controlIndex) ||
                _game.InputState.IsKeyPressed(Keys.E, _game.ActivePlayerIndex, out controlIndex) ||
                (mouseState.RightButton == ButtonState.Pressed &&
                 _previousMouseState.RightButton != ButtonState.Pressed))
            {
                float hit = 0;
                for (float x = 0.8f; x < 5f; x += 0.1f)
                {
                    Vector3 targetPoint = Camera.Position + (_lookVector * x);
                    if (_world.BlockAtPoint(targetPoint) != BlockType.None)
                    {
                        hit = x;
                        break;
                    }
                }
                if (hit != 0)
                {
                    for (float x = hit; x > 0.7f; x -= 0.1f)
                    {
                        Vector3 targetPoint = Camera.Position + (_lookVector * x);
                        if (_world.BlockAtPoint(targetPoint) == BlockType.None)
                        {
                            _world.AddBlock((ushort)targetPoint.X, (ushort)targetPoint.Y, (ushort)targetPoint.Z, _state.BlockPicker.SelectedBlockType, true, true);
                            break;
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public void CheckBuild(GameTime gameTime)
        {
            PlayerIndex controlIndex;
            float       distance = 0.0f;

            MouseState mouseState = Mouse.GetState();
            Vector2    mousePos   = new Vector2(mouseState.X, mouseState.Y);

            Ray ray = Camera.GetMouseRay(mousePos, _game.GraphicsDevice.Viewport);

            BlockIndex index = new BlockIndex(ray.Direction * distance + ray.Position);

            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                // TODO: Add mouse startpostion
                _elapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            if (mouseState.LeftButton == ButtonState.Released && _previousMouseState.LeftButton != ButtonState.Released)
            {
                // TODO: Compare mouse start position and current position
                for (float x = 0.4f; x < WorldSettings.BlockEditing.PLAYERREACH; x += 0.2f)
                {
                    index = new BlockIndex(ray.Direction * distance + ray.Position);

                    BlockType blockType = _world.BlockTypeAtPoint(index.Position);
                    if (blockType != BlockType.None && blockType != BlockType.Water)
                    {
                        if (index.Position.Y > 2)
                        {
                            // Can't dig water or lava
                            BlockType targetType = _world.BlockTypeAtPoint(index.Position);
                            if (BlockInformation.IsDiggable(targetType))
                            {
                                // If in selection mode
                                if (_onBlockSlection)
                                {
                                    OnBlockSelection(index.Position);
                                }
                                // Else remove the block
                                else
                                {
                                    _world.RemoveBlock((ushort)index.Position.X, (ushort)index.Position.Y, (ushort)index.Position.Z);
                                }
                            }
                        }
                        break;
                    }

                    distance += 0.2f;
                }
                distance     = 0.0f;
                _elapsedTime = 0.0f;
            }
            if (mouseState.RightButton == ButtonState.Pressed && _previousMouseState.RightButton != ButtonState.Pressed)
            {
                if (_onBlockSlection)
                {
                    _blockSelection.CancelSelection();
                    _onBlockSlection = false;
                }
                else
                {
                    float hit = 0;
                    //for (float x = 0.8f; x < 5f; x += 0.1f)
                    for (float x = 0.4f; x < WorldSettings.BlockEditing.PLAYERREACH; x += 0.2f)
                    {
                        //Vector3 targetPoint = Camera.Position + (_lookVector * x);
                        index = new BlockIndex(ray.Direction * distance + ray.Position);
                        if (_world.BlockTypeAtPoint(index.Position) != BlockType.None)
                        {
                            hit = x;
                            break;
                        }
                        distance += 0.2f;
                    }
                    if (hit != 0)
                    {
                        for (float x = hit; x > 0.7f; x -= 0.1f)
                        {
                            //Vector3 targetPoint = Camera.Position + (_lookVector * x);
                            index = new BlockIndex(ray.Direction * distance + ray.Position);
                            if (_world.BlockTypeAtPoint(index.Position) == BlockType.None)
                            {
                                _world.AddBlock((ushort)index.Position.X, (ushort)index.Position.Y, (ushort)index.Position.Z, _state.BlockPicker.SelectedBlockType, true, true);
                                break;
                            }
                            distance -= 0.1f;
                        }
                    }
                    distance = 0.0f;
                }
            }
            if (_game.InputState.IsKeyDown(Keys.V, _game.ActivePlayerIndex, out controlIndex))
            {
                _onBlockSlection = true;
            }
        }