Esempio n. 1
0
 public void Move(IPortalable moveable)
 {
     Transform2 v = moveable.GetVelocity();
     Transform2 t = moveable.GetTransform();
     if (Input.KeyDown(Key.Left))
     {
         v.Position += t.GetRight() * -Acceleration * Math.Abs(Transform2.GetSize(moveable));
     }
     if (Input.KeyDown(Key.Right))
     {
         v.Position += t.GetRight() * Acceleration * Math.Abs(Transform2.GetSize(moveable));
     }
     if (Input.KeyDown(Key.Up))
     {
         v.Position += t.GetUp() * Acceleration * Math.Abs(Transform2.GetSize(moveable));
     }
     if (Input.KeyDown(Key.Down))
     {
         v.Position += t.GetUp() * -Acceleration * Math.Abs(Transform2.GetSize(moveable));
     }
     v.Position *= 1 - Friction;
     moveable.SetVelocity(v);
 }
Esempio n. 2
0
        private static void _rayCast(IPortalable placeable, IEnumerable<IPortal> portals, double movementLeft, IPortal portalPrevious, Action<EnterCallbackData, double> portalEnter, Settings settings, int count)
        {
            Transform2 begin = placeable.GetTransform();
            Transform2 velocity = placeable.GetVelocity().Multiply(settings.TimeScale);
            if (settings.MaxIterations <= count)
            {
                //If we run out of iterations before running out of movement, call _rayEnd with 0 movementLeft just to make sure the AdjustEnpoint setting is handled.
                _rayEnd(placeable, portals, 0, portalPrevious, settings, begin, velocity);
                return;
            }
            if (!placeable.IsPortalable)
            {
                _rayEnd(placeable, portals, movementLeft, portalPrevious, settings, begin, velocity);
                return;
            }
            double distanceMin = movementLeft;
            IPortal portalNearest = null;
            IntersectCoord intersectNearest = new IntersectCoord();
            LineF ray = new LineF(begin.Position, begin.Position + velocity.Position);
            foreach (IPortal p in portals)
            {
                if (!Portal.IsValid(p) || portalPrevious == p)
                {
                    continue;
                }

                LineF portalLine = new LineF(Portal.GetWorldVerts(p));
                IntersectCoord intersect = MathExt.LineLineIntersect(portalLine, ray, true);
                double distance = ((Vector2d)begin.Position - intersect.Position).Length;
                if (intersect.Exists && distance < distanceMin)
                {
                    distanceMin = distance;
                    portalNearest = p;
                    intersectNearest = intersect;
                }
            }
            if (portalNearest != null)
            {
                movementLeft -= distanceMin;

                double t = (velocity.Position.Length - movementLeft) / velocity.Position.Length;

                begin.Position = (Vector2)intersectNearest.Position;
                placeable.SetTransform(begin);
                Portal.Enter(portalNearest, placeable, (float)intersectNearest.TFirst, true);

                portalEnter?.Invoke(new EnterCallbackData(portalNearest, placeable, intersectNearest.TFirst), t);

                movementLeft *= Math.Abs(placeable.GetTransform().Size / begin.Size);
                _rayCast(placeable, portals, movementLeft, portalNearest.Linked, portalEnter, settings, count + 1);
            }
            else
            {
                _rayEnd(placeable, portals, movementLeft, portalPrevious, settings, begin, velocity);
            }
        }