Beispiel #1
0
        /// <summary>
        /// Performs some basic input handling.
        /// </summary>
        private static void HandlePressInput(object sender, InputPressArgs e)
        {
            if (!Rendering.MainViewport.HasFocus)
            {
                return;
            }
            // For now, gameplay and debug actions are bound to static keys.

            // N toggles noclip.
            if (e.PressType == InputPressArgs.KeyPressType.Up && e.Key == InputKey.N)
            {
                NoclipEnabled = !NoclipEnabled;

                if (!NoclipEnabled)                // just switched to first-person mode again
                {
                    if (player != null)
                    {
                        player.Character.Body.SetTransformation(mat4.Translate(camera.Position));
                    }
                }
            }

            // Cam Paths
            if (NoclipEnabled && e.PressType == InputPressArgs.KeyPressType.Down)
            {
                switch (e.Key)
                {
                case InputKey.F9:
                    PathPositions.Add(camera.Position);
                    PathDirections.Add(camera.ForwardDirection);
                    PathTmps.Add(vec3.Zero);
                    Console.WriteLine("Point recorded");
                    break;

                case InputKey.F12:
                    PathPositions.Clear();
                    PathTmps.Clear();
                    PathDirections.Clear();
                    Console.WriteLine("Path deleted");
                    break;

                case InputKey.F10:
                    PathPlaying = !PathPlaying;
                    break;
                }
            }
        }
        /// <summary>
        /// Called on keyboard input. Updates the walking directions of the character.
        /// </summary>
        protected void HandleInput(object sender, InputPressArgs e)
        {
            if (!Rendering.MainViewport.HasFocus)
            {
                return;
            }
            if (LocalScript.NoclipEnabled)
            {
                return;
            }

            if (e.Key == InputKey.F && e.PressType == InputPressArgs.KeyPressType.Down)
            {
                Body.SetVelocity(vec3.Zero);
                mat4 transformation = Body.GetTransformation();
                vec3 pos            = new vec3(transformation.col3);
                pos.y += 20f;
                Body.SetTransformation(mat4.Translate(pos));
            }

            // Let the default WASD-keys control the walking directions.
            if (e.Key == InputKey.W)
            {
                if (e.PressType == InputPressArgs.KeyPressType.Down)
                {
                    walkDirForward++;
                }
                else
                {
                    walkDirForward--;
                }
            }
            else if (e.Key == InputKey.S)
            {
                if (e.PressType == InputPressArgs.KeyPressType.Down)
                {
                    walkDirForward--;
                }
                else
                {
                    walkDirForward++;
                }
            }
            else if (e.Key == InputKey.D)
            {
                if (e.PressType == InputPressArgs.KeyPressType.Down)
                {
                    walkDirRight++;
                }
                else
                {
                    walkDirRight--;
                }
            }
            else if (e.Key == InputKey.A)
            {
                if (e.PressType == InputPressArgs.KeyPressType.Down)
                {
                    walkDirRight--;
                }
                else
                {
                    walkDirRight++;
                }
            }
            else if (e.Key == InputKey.Space)    //Space lets the player jump
            {
                if (TouchesGround && jumpCoolDown == 0f)
                {
                    Body.ApplyImpulse(new vec3(0, 5f * CharacterMass, 0), vec3.Zero);
                    jumpCoolDown = 1f;
                }
            }
            else if (e.Key == InputKey.Shift)    // Shift controls running
            {
                if (e.PressType == InputPressArgs.KeyPressType.Down)
                {
                    IsRunning = true;
                }
                else
                {
                    IsRunning = false;
                }
            }
            else if (e.Key == InputKey.O)
            {
                Body.SetTransformation(mat4.Translate(new vec3(0, 50f, 0)) * Body.GetTransformation());
                Body.SetVelocity(vec3.Zero);
            }

            // Clamp the walking directions to [-1, 1]. The values could get out of bound, for example, when we receive two down events without an up event in between.
            if (walkDirForward < -1)
            {
                walkDirForward = -1;
            }
            if (walkDirForward > 1)
            {
                walkDirForward = 1;
            }
            if (walkDirRight < -1)
            {
                walkDirRight = -1;
            }
            if (walkDirRight > 1)
            {
                walkDirRight = 1;
            }

            // This hack stops the player movement immediately when we stop walking
            //TODO: do some actual friction simulation instead
            if (walkDirRight == 0 && walkDirRight == 0 && TouchesGround && e.PressType == InputPressArgs.KeyPressType.Up)
            {
                Body.SetVelocity(vec3.Zero);
            }
        }
Beispiel #3
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);
                            }
                        }
                    });
                }
            }
        }