Exemplo n.º 1
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;
            }
        }