示例#1
0
 public Player(GenericCamera _camera)
 {
     Direction               = new vec3(1, 0, 0);
     camera                  = _camera;
     CurrentDiggingShape     = DiggingShape.Sphere;
     CurrentDiggingAlignment = DiggingAlignment.AxisAligned;
     Input.OnPressInput     += HandlePressInput;
     Input.OnAxisInput      += HandleAxisInput;
     Inventory               = new Inventory(this);
 }
示例#2
0
        void HandlePressInput(object sender, InputPressArgs e)
        {
            if (!Rendering.MainViewport.HasFocus)
            {
                return;
            }

            // Scale the area using + and - keys.
            // Translate it using up down left right (x, z)
            // and PageUp PageDown (y).
            if (e.PressType == InputPressArgs.KeyPressType.Down)
            {
                switch (e.Key)
                {
                case InputKey.Shift:
                    keyModifierShift = true;
                    break;

                case InputKey.Control:
                    keyModifierControl = true;
                    break;

                case InputKey.Alt:
                    keyModifierAlt = true;
                    break;

                case InputKey.F8:
                    Renderer.Opaque.Mesh.DebugWireframe = !Renderer.Opaque.Mesh.DebugWireframe;
                    break;

                case InputKey.Q:
                    if (Inventory.Selection != null)
                    {
                        DropItem(Inventory.Selection);
                    }
                    break;

                // F1 resets the player position
                case InputKey.F1:
                    character.Body.SetTransformation(mat4.Translate(new vec3(0, 10f, 0)));
                    break;

                // Tab and shift-Tab cycle between digging shapes
                case InputKey.Tab:

                    if (!keyModifierControl)
                    {
                        int vals   = Enum.GetValues(typeof(DiggingShape)).Length;
                        int offset = keyModifierShift ? vals - 1 : 1;
                        CurrentDiggingShape = (DiggingShape)(((uint)CurrentDiggingShape + offset) % vals);
                    }
                    else
                    {
                        int vals   = Enum.GetValues(typeof(DiggingAlignment)).Length;
                        int offset = keyModifierShift ? vals - 1 : 1;
                        CurrentDiggingAlignment = (DiggingAlignment)(((uint)CurrentDiggingAlignment + offset) % vals);
                    }

                    // Reselect to refresh shape
                    if (Inventory.Selection != null)
                    {
                        Inventory.Selection.OnDeselect(this);
                        Inventory.Selection.OnSelect(this);
                    }

                    break;

                default:
                    break;
                }

                // Quickaccess items.
                if (InputKey.Key1 <= e.Key && e.Key <= InputKey.Key9)
                {
                    Inventory.Select((int)e.Key - (int)InputKey.Key1);
                }
                if (e.Key == InputKey.Key0)
                {
                    Inventory.Select(9); // Special '0'.
                }
            }
            else if (e.PressType == InputPressArgs.KeyPressType.Up)
            {
                switch (e.Key)
                {
                case InputKey.Shift:
                    keyModifierShift = false;
                    break;

                case InputKey.Control:
                    keyModifierControl = false;
                    break;

                case InputKey.Alt:
                    keyModifierAlt = false;
                    break;
                }
            }

            // Following interactions are only possible if UI is not open.
            if (!Gui.IsInventoryOpen)
            {
                float minRayQueryRange;
                float maxRayQueryRange;
                if (!LocalScript.NoclipEnabled)
                {
                    minRayQueryRange = minRayQueryDistancePlayer;
                    maxRayQueryRange = maxRayQueryDistancePlayer;
                }
                else
                {
                    minRayQueryRange = minRayQueryDistanceNoClip;
                    maxRayQueryRange = maxRayQueryDistanceNoClip;
                }

                // If left mouse click is detected, we want to execute a rayquery and report a "OnUse" to the selected item.
                if (Inventory.Selection != null && e.Key == InputKey.MouseLeft && e.PressType == InputPressArgs.KeyPressType.Down)
                {
                    // Send a ray query to find the position on the terrain we are looking at.
                    ContainingWorld.Physics.RayQuery(camera.Position + camera.ForwardDirection * minRayQueryRange, camera.Position + camera.ForwardDirection * maxRayQueryRange, delegate(bool _hit, vec3 _position, vec3 _normal, RigidBody _body, bool _hasTerrainCollision)
                    {
                        // Receiving the async ray query result here
                        if (_hit)
                        {
                            Entity _hitEntity = null;
                            if (_body != null && _body.RefComponent != null)
                            {
                                _hitEntity = _body.RefComponent.Entity;
                            }

                            /// Subtract a few cm toward camera to increase stability near constraints.
                            _position -= camera.ForwardDirection * .04f;

                            // Use currently selected item.
                            if (e.Key == InputKey.MouseLeft)
                            {
                                Item selection = Inventory.Selection;
                                if (selection != null)
                                {
                                    selection.OnUse(this, _position, _normal, _hitEntity);
                                }
                            }
                        }
                    });
                }

                if (e.Key == InputKey.E && e.PressType == InputPressArgs.KeyPressType.Down)
                {
                    ContainingWorld.Physics.RayQuery(camera.Position + camera.ForwardDirection * minRayQueryRange, camera.Position + camera.ForwardDirection * maxRayQueryRange, delegate(bool _hit, vec3 _position, vec3 _normal, RigidBody _body, bool _hasTerrainCollision)
                    {
                        // Receiving the async ray query result here
                        if (_body != null && _body.RefComponent != null)
                        {
                            Entity entity = _body.RefComponent.Entity;
                            if (entity != null)
                            {
                                TriggerId trigger = TriggerId.getIdByName("Interaction");
                                entity[trigger]  |= new InteractionMessage(thisEntity);
                            }
                        }
                    });
                }
            }
        }