Example #1
0
        public override void Act(KeyboardState ks, MouseState ms)
        {
            if (!CurrentWindow.Focused)
            {
                return;
            }

            // Basic controls:
            if (CurrentWorld.IsFirstPersonMode && CurrentWorld.GetFirstPersonObject().Equals(this))
            {
                float forward = 0;
                float strafe  = 0;
                if (ks[Key.A])
                {
                    strafe -= 1;
                }
                if (ks[Key.D])
                {
                    strafe += 1;
                }
                if (ks[Key.W])
                {
                    forward += 1;
                }
                if (ks[Key.S])
                {
                    forward -= 1;
                }

                MoveAndStrafeFirstPerson(forward, strafe, _movementSpeed * KWEngine.DeltaTimeFactor);
            }

            if (_state == PlayerState.OnFloor && ms.RightButton == ButtonState.Pressed && !_jumpButtonPressed)
            {
                _state             = PlayerState.Jump;
                _momentum          = 0.15f;
                _jumpButtonPressed = true;
            }
            if (ms.RightButton == ButtonState.Released)
            {
                _jumpButtonPressed = false;
            }

            DoStates();
            DoCollisionDetection();

            if (Position.Y < -5)
            {
                SetPosition(0, 0.9f, 0);
                _momentum = 0;
            }

            MoveFPSCamera(ms);
        }
Example #2
0
        public override void Act(KeyboardState ks, MouseState ms)
        {
            // Basic controls:
            if (CurrentWorld.IsFirstPersonMode && CurrentWorld.GetFirstPersonObject().Equals(this))
            {
                MoveFPSCamera(ms);

                float forward = 0;
                float strafe  = 0;
                if (ks[Key.A])
                {
                    strafe -= 1;
                }
                if (ks[Key.D])
                {
                    strafe += 1;
                }
                if (ks[Key.W])
                {
                    forward += 1;
                }
                if (ks[Key.S])
                {
                    forward -= 1;
                }

                if (ks[Key.Q])
                {
                    MoveOffset(0, -_speed * KWEngine.DeltaTimeFactor, 0);
                }
                if (ks[Key.E])
                {
                    MoveOffset(0, _speed * KWEngine.DeltaTimeFactor, 0);
                }

                MoveAndStrafeFirstPersonXYZ(forward, strafe, _speed * KWEngine.DeltaTimeFactor);


                //Console.WriteLine(Position);
            }
        }
Example #3
0
        public override void Act(KeyboardState ks, MouseState ms, float deltaTimeFactor)
        {
            bool runs = false;

            if (CurrentWorld.IsFirstPersonMode && CurrentWorld.GetFirstPersonObject().Equals(this))
            {
                float forward = 0;
                float strafe  = 0;
                if (ks[Key.A])
                {
                    strafe -= 1;
                    runs    = true;
                }
                if (ks[Key.D])
                {
                    strafe += 1;
                    runs    = true;
                }
                if (ks[Key.W])
                {
                    forward += 1;
                    runs     = true;
                }
                if (ks[Key.S])
                {
                    forward -= 1;
                    runs     = true;
                }
                MoveFPSCamera(ms);
                MoveAndStrafeFirstPerson(forward, strafe, 0.1f * deltaTimeFactor);
                FPSEyeOffset = 5;
                if (ks[Key.Q])
                {
                    MoveOffset(0, -0.2f, 0);
                }
                if (ks[Key.E])
                {
                    MoveOffset(0, +0.2f, 0);
                }
            }
            else
            {
                TurnTowardsXZ(GetMouseIntersectionPoint(ms, Plane.Y));
                Vector3 cameraLookAt = GetCameraLookAtVector();
                cameraLookAt.Y = 0;
                cameraLookAt.NormalizeFast();

                Vector3 strafe = HelperRotation.RotateVector(cameraLookAt, 90, Plane.Y);

                if (ks[Key.A])
                {
                    MoveAlongVector(strafe, 0.1f * deltaTimeFactor);
                    runs = true;
                }
                if (ks[Key.D])
                {
                    MoveAlongVector(strafe, -0.1f * deltaTimeFactor);
                    runs = true;
                }
                if (ks[Key.W])
                {
                    MoveAlongVector(cameraLookAt, 0.1f * deltaTimeFactor);
                    runs = true;
                }
                if (ks[Key.S])
                {
                    MoveAlongVector(cameraLookAt, -0.1f * deltaTimeFactor);
                    runs = true;
                }

                if (ks[Key.T])
                {
                    MoveOffset(0, 0.2f * deltaTimeFactor, 0);
                }

                if (ks[Key.Q])
                {
                    _height += 0.5f;
                }
                if (ks[Key.E])
                {
                    _height -= 0.5f;
                }
            }

            if (IsMouseCursorInsideMyHitbox(ms))
            {
                SetColorOutline(0, 1, 0, 0.2f);
            }
            else
            {
                SetColorOutline(0, 1, 0, 0);
            }

            /*
             * if (ms.LeftButton == ButtonState.Pressed)
             * {
             *  GameObject o = PickGameObject(ms);
             *  Console.WriteLine(o);
             * }
             */

            MoveOffset(0, -0.1f * deltaTimeFactor, 0);
            List <Intersection> intersections = GetIntersections();

            foreach (Intersection i in intersections)
            {
                if (i.IsTerrain)
                {
                    SetPosition(Position.X, i.HeightOnTerrainSuggested, Position.Z);
                }
                else
                {
                    Position += i.MTV;
                }
            }

            AdjustFlashlight();
            AdjustAnimation(runs, deltaTimeFactor);

            Vector3 camPos = this.Position + new Vector3(50, _height, 50);

            camPos.Y = _height;
            CurrentWorld.SetCameraPosition(camPos);
            CurrentWorld.SetCameraTarget(Position.X, 0, Position.Z);
        }
Example #4
0
        internal static void DrawCoordinateSystem(ref Matrix4 viewProjection)
        {
            if (CurrentWorld == null)
            {
                return;
            }

            CSScale = CurrentWorld.IsFirstPersonMode ? Vector3.Distance(CurrentWorld.GetFirstPersonObject().GetCenterPointForAllHitboxes(), CoordinateSystemCenter) : Vector3.Distance(CurrentWorld.GetCameraPosition(), CoordinateSystemCenter) / 6f;

            GL.UseProgram(RendererSimple.GetProgramId());
            Matrix4 _modelViewProjection;

            // Simple lines:
            CoordinateSystemMatrix = Matrix4.CreateScale(CSScale);
            Matrix4.Mult(ref CoordinateSystemMatrix, ref viewProjection, out _modelViewProjection);
            GL.UniformMatrix4(RendererSimple.GetUniformHandleMVP(), false, ref _modelViewProjection);
            foreach (string meshName in CoordinateSystem.Meshes.Keys)
            {
                GeoMesh mesh = CoordinateSystem.Meshes[meshName];

                GL.Uniform3(RendererSimple.GetUniformBaseColor(), mesh.Material.ColorDiffuse.X, mesh.Material.ColorDiffuse.Y, mesh.Material.ColorDiffuse.Z);

                GL.BindVertexArray(mesh.VAO);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, mesh.VBOIndex);
                GL.DrawElements(mesh.Primitive, mesh.IndexCount, DrawElementsType.UnsignedInt, 0);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

                GL.BindVertexArray(0);
            }

            // X
            CoordinateSystemMatrixX     = Matrix4.CreateScale(CSScale * 3f) * Matrix4.CreateFromQuaternion(HelperRotation.GetRotationForPoint(CoordinateSystemXOffset * CSScale, CurrentWorld.IsFirstPersonMode ? CurrentWorld.GetFirstPersonObject().GetCenterPointForAllHitboxes() : CurrentWorld.GetCameraPosition()));
            CoordinateSystemMatrixX.M41 = CoordinateSystemXOffset.X * CSScale * 1.05f;
            CoordinateSystemMatrixX.M42 = CoordinateSystemXOffset.Y;
            CoordinateSystemMatrixX.M43 = CoordinateSystemXOffset.Z;
            Matrix4.Mult(ref CoordinateSystemMatrixX, ref viewProjection, out _modelViewProjection);
            GL.UniformMatrix4(RendererSimple.GetUniformHandleMVP(), false, ref _modelViewProjection);
            foreach (string meshName in CoordinateSystemX.Meshes.Keys)
            {
                GeoMesh mesh = CoordinateSystemX.Meshes[meshName];

                GL.Uniform3(RendererSimple.GetUniformBaseColor(), mesh.Material.ColorDiffuse.X, mesh.Material.ColorDiffuse.Y, mesh.Material.ColorDiffuse.Z);

                GL.BindVertexArray(mesh.VAO);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, mesh.VBOIndex);
                GL.DrawElements(mesh.Primitive, mesh.IndexCount, DrawElementsType.UnsignedInt, 0);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

                GL.BindVertexArray(0);
            }

            // Y
            CoordinateSystemMatrixY     = Matrix4.CreateScale(CSScale * 3f) * Matrix4.CreateFromQuaternion(HelperRotation.GetRotationForPoint(CoordinateSystemYOffset * CSScale, CurrentWorld.IsFirstPersonMode ? CurrentWorld.GetFirstPersonObject().GetCenterPointForAllHitboxes() : CurrentWorld.GetCameraPosition()));
            CoordinateSystemMatrixY.M41 = CoordinateSystemYOffset.X;
            CoordinateSystemMatrixY.M42 = CoordinateSystemYOffset.Y * CSScale * 1.05f;
            CoordinateSystemMatrixY.M43 = CoordinateSystemYOffset.Z;
            Matrix4.Mult(ref CoordinateSystemMatrixY, ref viewProjection, out _modelViewProjection);
            GL.UniformMatrix4(RendererSimple.GetUniformHandleMVP(), false, ref _modelViewProjection);
            foreach (string meshName in CoordinateSystemY.Meshes.Keys)
            {
                GeoMesh mesh = CoordinateSystemY.Meshes[meshName];

                GL.Uniform3(RendererSimple.GetUniformBaseColor(), mesh.Material.ColorDiffuse.X, mesh.Material.ColorDiffuse.Y, mesh.Material.ColorDiffuse.Z);

                GL.BindVertexArray(mesh.VAO);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, mesh.VBOIndex);
                GL.DrawElements(mesh.Primitive, mesh.IndexCount, DrawElementsType.UnsignedInt, 0);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

                GL.BindVertexArray(0);
            }

            // Z
            CoordinateSystemMatrixZ     = Matrix4.CreateScale(CSScale * 3f) * Matrix4.CreateFromQuaternion(HelperRotation.GetRotationForPoint(CoordinateSystemZOffset * CSScale, CurrentWorld.IsFirstPersonMode ? CurrentWorld.GetFirstPersonObject().GetCenterPointForAllHitboxes() : CurrentWorld.GetCameraPosition()));
            CoordinateSystemMatrixZ.M41 = CoordinateSystemZOffset.X;
            CoordinateSystemMatrixZ.M42 = CoordinateSystemZOffset.Y;
            CoordinateSystemMatrixZ.M43 = CoordinateSystemZOffset.Z * CSScale * 1.05f;
            Matrix4.Mult(ref CoordinateSystemMatrixZ, ref viewProjection, out _modelViewProjection);
            GL.UniformMatrix4(RendererSimple.GetUniformHandleMVP(), false, ref _modelViewProjection);
            foreach (string meshName in CoordinateSystemZ.Meshes.Keys)
            {
                GeoMesh mesh = CoordinateSystemZ.Meshes[meshName];

                GL.Uniform3(RendererSimple.GetUniformBaseColor(), mesh.Material.ColorDiffuse.X, mesh.Material.ColorDiffuse.Y, mesh.Material.ColorDiffuse.Z);

                GL.BindVertexArray(mesh.VAO);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, mesh.VBOIndex);
                GL.DrawElements(mesh.Primitive, mesh.IndexCount, DrawElementsType.UnsignedInt, 0);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

                GL.BindVertexArray(0);
            }

            GL.UseProgram(0);
        }
Example #5
0
        public override void Act(KeyboardState ks, MouseState ms, float deltaTimeFactor)
        {
            if (!CurrentWindow.Focused)
            {
                return;
            }

            // Debugging purposes:
            if (ks[Key.R])
            {
                SetPosition(0, 13, 0);
                _phase             = Phase.Fall;
                _airTime           = 0;
                _heightAtJumpStart = 13;
            }


            // Basic controls:
            if (CurrentWorld.IsFirstPersonMode && CurrentWorld.GetFirstPersonObject().Equals(this))
            {
                float forward = 0;
                float strafe  = 0;
                if (ks[Key.A])
                {
                    strafe -= 1;
                }
                if (ks[Key.D])
                {
                    strafe += 1;
                }
                if (ks[Key.W])
                {
                    forward += 1;
                }
                if (ks[Key.S])
                {
                    forward -= 1;
                }

                MoveAndStrafeFirstPerson(forward, strafe, _movementSpeed * deltaTimeFactor);
            }

            // Jump controls:
            if (ms.RightButton == ButtonState.Pressed && _phase == Phase.Stand && _jumpButtonPressed == false)
            {
                _jumpButtonPressed = true;
                _phase             = Phase.Jump;
                _airTime           = 0;
                _lastGain          = 0;
                _heightAtJumpStart = Position.Y;
            }

            if (ms.RightButton == ButtonState.Released)
            {
                _jumpButtonPressed = false;
            }


            // Jump behaviour:
            if (_phase == Phase.Jump)
            {
                _airTime += (deltaTimeFactor * 16.666667f) / 1000f;
                float gain = -14f * (float)Math.Pow(_airTime - 0.4f, 2) + 2.25f;
                SetPositionY(_heightAtJumpStart + gain);
                if (gain <= _lastGain)
                {
                    _phase             = Phase.Fall;
                    _lastGain          = 0;
                    _airTime           = 0;
                    _heightAtJumpStart = Position.Y;
                }
                else
                {
                    _lastGain = gain;
                }
            }
            else if (_phase == Phase.Fall)
            {
                _airTime += (deltaTimeFactor * 16.666667f) / 1000f;
                float gain = 14f * (float)Math.Pow(_airTime, 2);
                SetPositionY(_heightAtJumpStart - gain);
            }
            else if (_phase == Phase.Stand)
            {
                MoveOffset(0, -_movementSpeed * 0.1f * deltaTimeFactor, 0);
            }


            // Collision detection:
            List <Intersection> intersections = GetIntersections();
            bool upCorrection = false;

            foreach (Intersection i in intersections)
            {
                Vector3 mtv = i.MTV;
                if (i.Object is Immovable && (i.Object as Immovable).IsStair && i.MTVUp.Y > 0)
                {
                    mtv = i.MTVUp;
                }
                MoveOffset(mtv);

                if (mtv.Y > 0.00001f)
                {
                    if (_phase == Phase.Fall)
                    {
                        _phase       = Phase.Stand;
                        _airTime     = 0;
                        upCorrection = true;
                    }
                }
                if (mtv.Y < 0 &&
                    _phase == Phase.Jump &&
                    Math.Abs(mtv.Y) > Math.Abs(mtv.X) && Math.Abs(mtv.Y) > Math.Abs(mtv.Z)
                    )
                {
                    _phase             = Phase.Fall;
                    _airTime           = 0;
                    _heightAtJumpStart = Position.Y;
                }
            }
            if (!upCorrection && _phase == Phase.Stand)
            {
                _phase             = Phase.Fall;
                _airTime           = 0;
                _heightAtJumpStart = Position.Y;
            }

            // Has to happen last!
            // (otherwise the camera would be set before
            //  the collision correction causing the cam
            //  to bob up and down rapidly)
            MoveFPSCamera(ms);
        }