Ejemplo n.º 1
0
        /// <summary>
        /// Ticks player movement.
        /// </summary>
        /// <param name="delta">The tick delta</param>
        /// <param name="custom">Whether tihis is a custom tick call (IE, anything outside the normal tick)</param>
        public void TickMovement(double delta, bool custom = false)
        {
            if (delta == 0)
            {
                return;
            }
            while (Direction.X < 0)
            {
                Direction.X += 360;
            }
            while (Direction.X > 360)
            {
                Direction.X -= 360;
            }
            if (Direction.Y > 89.9f)
            {
                Direction.Y = 89.9f;
            }
            if (Direction.Y < -89.9f)
            {
                Direction.Y = -89.9f;
            }
            Location movement = Location.Zero;

            if (Leftward)
            {
                movement.Y = -1;
            }
            if (Rightward)
            {
                movement.Y = 1;
            }
            if (Backward)
            {
                movement.X = 1;
            }
            if (Forward)
            {
                movement.X = -1;
            }
            if (Downward)
            {
                Maxes.Z = 0.1;
            }
            else
            {
                Maxes.Z = 1.5;
            }
            bool on_ground = Collision.Box(InWorld, Position - (DefaultHalfSize + new Location(0, 0, 0.1)), Position + DefaultHalfSize) && Velocity.Z < 0.01;

            if (Upward)
            {
                if (on_ground && !Jumped)
                {
                    Velocity.Z += 10;
                    Jumped      = true;
                }
            }
            else
            {
                Jumped = false;
            }
            if (movement.LengthSquared() > 0)
            {
                movement = Utilities.RotateVector(movement, Direction.X * Utilities.PI180);
            }
            float MoveSpeed = 15;

            Velocity.X += ((movement.X * MoveSpeed * (Slow || Downward ? 0.5 : 1)) - Velocity.X) * delta * 8;
            Velocity.Y += ((movement.Y * MoveSpeed * (Slow || Downward ? 0.5 : 1)) - Velocity.Y) * delta * 8;
            Velocity.Z += delta * -9.8 / 0.5666; // 1 unit = 0.5666 meters
            Location ppos   = Position;
            Location target = Position + Velocity * delta;
            Location pos    = Position;

            if (target != pos)
            {
                // TODO: Better handling (Based on impact normal)
                pos = Collision.BoxRayTrace(InWorld, -DefaultHalfSize, DefaultHalfSize, pos, new Location(target.X, target.Y, pos.Z), 1);
                pos = Collision.BoxRayTrace(InWorld, -DefaultHalfSize, DefaultHalfSize, pos, new Location(target.X, pos.Y, pos.Z), 1);
                pos = Collision.BoxRayTrace(InWorld, -DefaultHalfSize, DefaultHalfSize, pos, new Location(pos.X, target.Y, pos.Z), 1);
                pos = Collision.BoxRayTrace(InWorld, -DefaultHalfSize, DefaultHalfSize, pos, new Location(pos.X, pos.Y, target.Z), 1);
                Reposition(pos);
                Velocity = (pos - ppos) / delta;
            }
        }