Ejemplo n.º 1
0
        public static float NumberOfPixelsThatOverlap(this IRectangular rectangle, IRectangular other)
        {
            var ret =
                Math.Max(0, Math.Min(rectangle.Right(), other.Right()) - Math.Max(rectangle.Left, other.Left)) *
                Math.Max(0, Math.Min(rectangle.Bottom(), other.Bottom()) - Math.Max(rectangle.Top, other.Top));

            return(ret);
        }
 public RectangularHelper(IRectangular rect)
 {
     this.rect = rect;
     x         = rect.Location.X;
     y         = rect.Location.Y;
     w         = rect.Size.Width;
     h         = rect.Size.Height;
     hoek      = rect.Angle;
 }
Ejemplo n.º 3
0
        public static void MoveTowards(Character character, IRectangular destination, float increment)
        {
            if (destination.Width == 0 || destination.Height == 0)
            {
                return;
            }
            if (mostRecentlyVisited.ContainsKey(character) == false)
            {
                mostRecentlyVisited.Add(character, null);
                character.Lifetime.OnDisposed(() => { mostRecentlyVisited.Remove(character); });
            }

            if (character.CalculateLineOfSight(destination, increment).Obstacles.Where(o => o is Waypoint == false).Count() == 0)
            {
                var newTopLeft = character.TopLeft().MoveTowards(destination.Center(), increment);
                character.MoveTo(newTopLeft.Left, newTopLeft.Top);
                return;
            }

            var bestWaypoint = SpaceTime.CurrentSpaceTime.Elements
                               .Where(e => e is Waypoint)
                               .Select(e => e as Waypoint)
                               .Where(w => w != mostRecentlyVisited[character])
                               .Where(w => character.CalculateLineOfSight(w, increment).Obstacles.Where(o => o is Waypoint == false).Count() == 0)
                               .OrderBy(w => w.CalculateLineOfSight(destination, increment).Obstacles.Where(o => o is Waypoint == false).Count() == 0 ? 0 : 1)
                               .ThenBy(w => w.CalculateDistanceTo(destination))
                               .ThenBy(w => character.CalculateDistanceTo(w))
                               .FirstOrDefault();

            if (bestWaypoint != null)
            {
                var newTopLeft = character.TopLeft().MoveTowards(bestWaypoint.Center(), increment);
                character.MoveTo(newTopLeft.Left, newTopLeft.Top);

                if (character.CalculateDistanceTo(bestWaypoint) <= 1.5)
                {
                    mostRecentlyVisited[character] = bestWaypoint;
                }
            }
        }
Ejemplo n.º 4
0
        public static Direction GetHitDirection(this IRectangular rectangle, IRectangular other)
        {
            float rightProximity  = Math.Abs(other.Left - (rectangle.Left + rectangle.Width));
            float leftProximity   = Math.Abs(other.Left + other.Width - rectangle.Left);
            float topProximity    = Math.Abs(other.Top + other.Height - rectangle.Top);
            float bottomProximity = Math.Abs(other.Top - (rectangle.Top + rectangle.Height));

            rightProximity  = ((int)((rightProximity * 10) + .5f)) / 10f;
            leftProximity   = ((int)((leftProximity * 10) + .5f)) / 10f;
            topProximity    = ((int)((topProximity * 10) + .5f)) / 10f;
            bottomProximity = ((int)((bottomProximity * 10) + .5f)) / 10f;

            if (leftProximity == topProximity)
            {
                return(Direction.UpLeft);
            }
            if (rightProximity == topProximity)
            {
                return(Direction.UpRight);
            }
            if (leftProximity == bottomProximity)
            {
                return(Direction.DownLeft);
            }
            if (rightProximity == bottomProximity)
            {
                return(Direction.DownRight);
            }

            List <KeyValuePair <Direction, float> > items = new List <KeyValuePair <Direction, float> >();

            items.Add(new KeyValuePair <Direction, float>(Direction.Right, rightProximity));
            items.Add(new KeyValuePair <Direction, float>(Direction.Left, leftProximity));
            items.Add(new KeyValuePair <Direction, float>(Direction.Up, topProximity));
            items.Add(new KeyValuePair <Direction, float>(Direction.Down, bottomProximity));
            return(items.OrderBy(item => item.Value).First().Key);
        }
Ejemplo n.º 5
0
        public static Route CalculateLineOfSight(IRectangular from, ILocation to, float increment)
        {
            Route        ret     = new Route();
            IRectangular current = from;

            while (from.Center().CalculateDistanceTo(to) > increment)
            {
                current = Rectangular.Create(MoveTowards(current.Center(), to, increment), from);
                ret.Steps.Add(current.Center());

                var obstacles = SpaceTime.CurrentSpaceTime.Elements
                                .Where(el => el.NumberOfPixelsThatOverlap(current) > 0);

                foreach (var obstacle in obstacles)
                {
                    if (ret.Obstacles.Contains(obstacle) == false)
                    {
                        ret.Obstacles.Add(obstacle);
                    }
                }
            }

            return(ret);
        }
Ejemplo n.º 6
0
        public static Route CalculateLineOfSight(this IRectangular from, IRectangular to, float increment)
        {
            Route        ret             = new Route();
            IRectangular current         = from;
            var          currentDistance = current.CalculateDistanceTo(to);
            var          firstDistance   = currentDistance;

            while (currentDistance > increment)
            {
#if DEBUG
                if (currentDistance > firstDistance)
                {
                    throw new Exception("Bug, we got farther away");
                }
#endif

                current = Rectangular.Create(MoveTowards(current.Center(), to.Center(), increment), from);
                current = Rectangular.Create(current.Left - from.Width / 2, current.Top - from.Height / 2, from.Width, from.Height);
                ret.Steps.Add(current);

                var obstacles = SpaceTime.CurrentSpaceTime.Elements
                                .Where(el => el != from && el != to && el.NumberOfPixelsThatOverlap(current) > 0);

                foreach (var obstacle in obstacles)
                {
                    if (ret.Obstacles.Contains(obstacle) == false)
                    {
                        ret.Obstacles.Add(obstacle);
                    }
                }

                currentDistance = current.CalculateDistanceTo(to);
            }

            return(ret);
        }
 public static Int32 Area(IRectangular rect)
 {
     return(rect.Width() * rect.Height());
 }
Ejemplo n.º 8
0
 public override void Initialize()
 {
     startLocation = this.Bounds;
     // todo - replace with bullet speed from config
     force = new Force(Speed, SpaceExtensions.NormalizeQuantity(Accelleration, angle), angle);
 }
Ejemplo n.º 9
0
 public static float CalculateAngleTo(this IRectangular from, IRectangular to) => CalculateAngleTo(from.Center(), to.Center());
Ejemplo n.º 10
0
 public static float CalculateDistanceTo(this IRectangular rectangular, IRectangular other) => rectangular.Center().CalculateDistanceTo(other.Center());
Ejemplo n.º 11
0
 public static float Bottom(this IRectangular rectangle) => rectangle.Top + rectangle.Height;
Ejemplo n.º 12
0
 public static bool Touches(this IRectangular rectangle, IRectangular other) => OverlapPercentage(rectangle, other) > 0;
Ejemplo n.º 13
0
 public override void Initialize()
 {
     this.initialBonds = this.CopyBounds();
     this.initialTime  = Time.CurrentTime.Now;
 }
Ejemplo n.º 14
0
 public static float OverlapPercentage(this IRectangular rectangle, IRectangular other) => NumberOfPixelsThatOverlap(rectangle, other) / (other.Width * other.Height);
Ejemplo n.º 15
0
 public static ILocation TopLeft(this IRectangular rectangular) => Location.Create(rectangular.Left, rectangular.Top);
Ejemplo n.º 16
0
 public Projectile(float x, float y, IRectangular target) : this()
 {
     this.MoveTo(x, y);
     this.angle = this.CalculateAngleTo(target);
 }
Ejemplo n.º 17
0
 public static ILocation Center(this IRectangular rectangular) => Location.Create(rectangular.CenterX(), rectangular.CenterY());
Ejemplo n.º 18
0
 public static float CenterY(this IRectangular rectangular) => rectangular.Top + (rectangular.Height / 2);
Ejemplo n.º 19
0
 public static float CenterX(this IRectangular rectangular) => rectangular.Left + (rectangular.Width / 2);
 public RectangularGlyph(IRectangular vorm, AnchorStyles plaatsing)
 {
     this.vorm      = vorm;
     this.plaatsing = plaatsing;
 }
Ejemplo n.º 21
0
 public static bool Contains(this IRectangular rectangle, IRectangular other) => OverlapPercentage(rectangle, other) == 1;
Ejemplo n.º 22
0
 public static IRectangular CopyBounds(this IRectangular rectangular) => Rectangular.Create(rectangular.Left, rectangular.Top, rectangular.Width, rectangular.Height);
Ejemplo n.º 23
0
 public static float Right(this IRectangular rectangle) => rectangle.Left + rectangle.Width;