Exemplo n.º 1
0
        /// <summary>
        /// Moves the world object and stops when it hits a solid.
        /// Returns true if there was a collision and outputs the overlap.
        /// </summary>
        /// <param name="worldObject"></param>
        /// <param name="direction"></param>
        /// <param name="overlap"></param>
        /// <returns></returns>
        public static bool MoveAndCollide(this WorldObject worldObject, Vector2 direction, out Overlap overlap)
        {
            overlap = new Overlap();
            if (direction == Vector2.Zero)
            {
                return(false);
            }

            bool result = false;

            if (worldObject.Layout.Grid.IsCollidingAtOffset(worldObject, direction.X, 0f, out var overlapX))
            {
                direction.X += overlapX.Depth.X;
                overlap      = overlapX;
                result       = true;
            }

            if (worldObject.Layout.Grid.IsCollidingAtOffset(worldObject, 0f, direction.Y, out var overlapY))
            {
                direction.Y += overlapY.Depth.Y;

                if (!result)
                {
                    overlap = overlapY;
                }

                result = true;
            }

            worldObject.Move(direction.X, direction.Y);
            return(result);
        }
Exemplo n.º 2
0
        public bool IsOverlapping(WorldObject worldObject, string[] ignoreTags, out Overlap overlap)
        {
            overlap = new Overlap();
            var overlappedObject = QueryBounds(worldObject.Bounds).FirstOrDefault(other => other != worldObject && !ignoreTags.Any(tag => other.Tags.Contains(tag)));

            if (overlappedObject != null)
            {
                overlap.Depth = worldObject.Bounds.GetIntersectionDepth(overlappedObject.Bounds);
                overlap.Other = overlappedObject;
                return(true);
            }

            return(false);
        }
Exemplo n.º 3
0
        public bool IsOverlapping(WorldObject worldObject, out Overlap overlap)
        {
            overlap = new Overlap();
            var overlappedObject = QueryBounds(worldObject.Bounds).FirstOrDefault(other => other != worldObject);

            if (overlappedObject != null)
            {
                overlap.Depth = worldObject.Bounds.GetIntersectionDepth(overlappedObject.Bounds);
                overlap.Other = overlappedObject;
                return(true);
            }

            return(false);
        }
Exemplo n.º 4
0
        public bool IsCollidingAtOffset(WorldObject worldObject, float xOffset, float yOffset, out Overlap overlap)
        {
            overlap = new Overlap();
            var offsetBounds = worldObject.Bounds;

            offsetBounds.Offset(xOffset, yOffset);

            var overlappedObject = QueryBounds(offsetBounds).FirstOrDefault(other => other != worldObject && other.IsSolid);

            if (overlappedObject != null)
            {
                overlap.Depth = offsetBounds.GetIntersectionDepth(overlappedObject.Bounds);
                overlap.Other = overlappedObject;
                return(true);
            }

            return(false);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Moves the world object while looking for overlaps.
 /// Returns true if there is an overlap and outputs the overlap.
 /// </summary>
 /// <param name="worldObject"></param>
 /// <param name="direction"></param>
 /// <param name="overlap"></param>
 /// <returns></returns>
 public static bool MoveAndOverlap(this WorldObject worldObject, Vector2 direction, out Overlap overlap)
 {
     worldObject.Move(direction);
     return(worldObject.Layout.Grid.IsOverlapping(worldObject, out overlap));
 }