/// <summary>
        /// Returns an interactable in the current world's scene that p_interactable collides with if its position were changed.
        /// if p_interactable is colliding with p_exclude, that collision doesn't count
        /// (i.e. it doesn't make sense to check whether something is colliding with itself)
        /// </summary>
        /// <param name="p_interactable">Interactable to check</param>
        /// <param name="p_offset">The proposed movement of the Interactable</param>
        /// <param name="p_exclude">An interactable to exclude from collision checking</param>
        /// <returns>An interactable that p_interactable collides with; null if no collisions are detected</returns>
        public static IInteractable collisionWithInteractableAtRelative(IInteractable p_interactable, Point p_offset, IInteractable p_exclude)
        {
            IInteractable l_collidedObject = null;
            foreach (IInteractable i in WorldManager.m_currentRoom.Interactables)
            {
                if (i == p_exclude || m_excluded.Contains(i))
                {
                    continue;
                }
                Rectangle l_copy = p_interactable.getBoundingRect();    //preserve this just in case
                Rectangle l_spriteBounds;
                l_spriteBounds.X = l_copy.X + p_offset.X;
                l_spriteBounds.Y = l_copy.Y + p_offset.Y;
                l_spriteBounds.Width = l_copy.Width;
                l_spriteBounds.Height = l_copy.Height;

                Rectangle l_charBounds = i.getBoundingRect();
                if (l_spriteBounds.Intersects(l_charBounds))
                {
                    l_collidedObject = i;
                    break;
                }
            }
            return l_collidedObject;
        }
        // returns an object in the specified Tiled map object layer that the given sprite collides with
        // throws exception if the specified layer does not exist!
        public static MapObject collisionWithObjectAtRelative(IInteractable p_interactable, Point p_offset, String p_layer)
        {
            MapObject l_collidedObject = null;

            foreach (MapObject m in ((MapObjectLayer) WorldManager.m_currentRoom.background.GetLayer(p_layer)).Objects) {
                Rectangle l_copy = p_interactable.getBoundingRect();    //preserve this just in case
                Rectangle l_spriteBounds;
                l_spriteBounds.X = l_copy.X + p_offset.X;
                l_spriteBounds.Y = l_copy.Y + p_offset.Y;
                l_spriteBounds.Width = l_copy.Width;
                l_spriteBounds.Height = l_copy.Height;

                if (l_spriteBounds.Intersects(m.Bounds)) {
                    l_collidedObject = m;
                    break;
                }
            }

            return l_collidedObject;
        }
Beispiel #3
0
 /// <summary>
 /// Checks in this Character is within a given range with another Interactable to perform any action.
 /// The range is another rectangle a given number of pixels thicker than the sprite's drawing rectangle.
 /// E.g. talk to another character, pickup an item, collision detection
 /// </summary>
 /// <param name="other">The other Interactable to range check against</param>
 /// <param name="offset">The pixels offset for the range check</param>
 /// <returns>True if the Character is in range, false if not</returns>
 public bool inRange(IInteractable other, int offset)
 {
     Rectangle copy = other.getBoundingRect();   // not sure if it returns the reference
     Rectangle inflatedRect;
     inflatedRect.X = copy.X - offset;
     inflatedRect.Y = copy.Y - offset;
     inflatedRect.Width = copy.Width + offset * 2;
     inflatedRect.Height = copy.Height + offset * 2;
     return this.getBoundingRect().Intersects(inflatedRect);
 }
Beispiel #4
0
 /// <summary>
 /// Checks if this Character is facing another Interactable (e.g. other Character); used as a check for talking action
 /// </summary>
 /// <param name="other">The other Interactable</param>
 /// <returns>True if this character is facing the other, false if not</returns>
 public bool facing(IInteractable other)
 {
     Rectangle rect = other.getBoundingRect();
     return ((this.getDirection().Equals(Direction.North) && this.getY() > rect.Y) ||
            (this.getDirection().Equals(Direction.South) && this.getY() < rect.Y) ||
            (this.getDirection().Equals(Direction.East) && this.getX() < rect.X) ||
            (this.getDirection().Equals(Direction.West) && this.getX() > rect.X));
 }