Exemple #1
0
        public void Update(double delta)
        {
            var mouseMovement = MouseHelper.GetMouseMovement();

            TargetRotations += new Vector3(mouseMovement.Y * 0.001f, mouseMovement.X * 0.001f, 0);
            Rotations        = Vector3.Lerp(Rotations, TargetRotations, 0.25f);

            EyeForward = ((Matrix4.CreateRotationY(Rotations.Y) *
                           Matrix4.CreateRotationX(Rotations.X)) * new Vector4(0, 0, -1, 0)).Xyz;
            Forward = new Vector3((float)Math.Sin(Rotations.Y), 0, -(float)Math.Cos(Rotations.Y));
            Right   = new Vector3((float)Math.Sin(Rotations.Y + PION2), 0, -(float)Math.Cos(Rotations.Y + PION2));

            var isShiftPressed = KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.ShiftLeft);

            if (KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.W) ||
                KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.S) ||
                KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.A) ||
                KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.D) ||
                KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.Space) ||
                KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.ControlLeft))
            {
                var targetValocity = new Vector3(0);
                if (KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.W))
                {
                    targetValocity += Forward * 5 * (isShiftPressed ? 20 : 1);
                }
                if (KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.S))
                {
                    targetValocity += -Forward * 5 * (isShiftPressed ? 20 : 1);
                }
                if (KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.A))
                {
                    targetValocity += -Right * 5 * (isShiftPressed ? 20 : 1);
                }
                if (KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.D))
                {
                    targetValocity += Right * 5 * (isShiftPressed ? 20 : 1);
                }
                if (KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.Space))
                {
                    targetValocity += UP * 7 * (isShiftPressed ? 20 : 1);
                }
                if (KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.ControlLeft))
                {
                    targetValocity += -UP * 7 * (isShiftPressed ? 20 : 1);
                }
                Velocity = Vector3.Lerp(Velocity, targetValocity, 0.2f);
            }
            else
            {
                Velocity *= 0.8f;
            }
            Position += Velocity * (float)delta;
        }
Exemple #2
0
        public void Update(double time)
        {
            if (Dead)
            {
                return;
            }
            Score += _scoreIncrementation * (float)time;
            Speed += (float)time * _speedIncrease;
            if (Speed > _maxSpeed)
            {
                Speed = _maxSpeed;
            }
            _animTime += ((float)time * Speed) / 12.5f;
            if (KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.Right))
            {
                _sidewayMove -= _sidewaySpeed;
                if (_sidewayMove < -_maxSidewayDistance)
                {
                    _sidewayMove = -_maxSidewayDistance;
                }
            }
            else if (KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.Left))
            {
                _sidewayMove += _sidewaySpeed;
                if (_sidewayMove > _maxSidewayDistance)
                {
                    _sidewayMove = _maxSidewayDistance;
                }
            }
            else
            {
                if (_sidewayMove > 0.01)
                {
                    _sidewayMove -= _sidewaySpeed;
                }
                else if (_sidewayMove < -0.01)
                {
                    _sidewayMove += _sidewaySpeed;
                }
                else
                {
                    _sidewayMove = 0;
                }
            }

            if (KeyboardHelper.IsKeyPressed(OpenTK.Input.Key.Up) && _onGround)
            {
                _onGround  = false;
                _yVelocity = _jumpPower;
            }

            var intersection = GetIntersection();

            if (intersection != null)
            {
                if (KeyboardHelper.IsKeyPressed(OpenTK.Input.Key.Left))
                {
                    Position = new Vector3(intersection.Position.X, Position.Y, intersection.Position.Z) + DirectionHelper.GetVectorFromDirection(intersection.Direction) * 3f;
                    var dir = (int)CurrentDirection - 1;
                    if (dir < 0)
                    {
                        dir = (int)Direction.WEST;
                    }
                    CurrentDirection  = (Direction)dir;
                    intersection.Used = true;
                }
                else if (KeyboardHelper.IsKeyPressed(OpenTK.Input.Key.Right))
                {
                    Position = new Vector3(intersection.Position.X, Position.Y, intersection.Position.Z) + DirectionHelper.GetVectorFromDirection(intersection.Direction) * 3f;
                    var dir = (int)CurrentDirection + 1;
                    if (dir > 3)
                    {
                        dir = (int)Direction.NORTH;
                    }
                    CurrentDirection  = (Direction)dir;
                    intersection.Used = true;
                }
            }

            _yVelocity -= _gravity;

            Position += DirectionHelper.GetVectorFromDirection(CurrentDirection) * Speed * (float)time + (Vector3.UnitY * _yVelocity);

            var pos = GetPosition();
            var intersectionWithGrounds = World.Grounds.Where(g => g.BoundingBox.IntersectWith(g.Position, BoundingBox, pos)).ToList();

            if (intersectionWithGrounds.Count != 0)
            {
                _yVelocity = 0;
            }
            float?maxY = null;

            for (int i = 0; i < intersectionWithGrounds.Count; i++)
            {
                var intersectionY = intersectionWithGrounds[i].BoundingBox.Max.Y + intersectionWithGrounds[i].Position.Y;
                if (maxY == null || maxY < intersectionY)
                {
                    maxY = intersectionY;
                }
            }

            if (intersectionWithGrounds.Count == 0)
            {
                _onGround = false;
            }

            if (maxY != null)
            {
                if (maxY > pos.Y + _maxStepSize)
                {
                    Dead = true;
                }
                else if (maxY > pos.Y)
                {
                    Position = new Vector3(Position.X, (float)maxY, Position.Z);
                }
                _onGround = true;
            }

            if (_onGround)
            {
                _lastY = Position.Y;
            }
            else if (Position.Y < _lastY - 5f)
            {
                Dead = true;
            }

            if (CollideWithObstacle())
            {
                Dead = true;
            }

            var coin = GetCoin();

            if (coin != null)
            {
                World.Coins.Remove(coin);
                Score += 20;
            }
        }