Ejemplo n.º 1
0
        /// <summary>
        /// Makes a copy of the invoking DynamicShape's region,
        /// and checks if it is intersecting with the given Shape's region,
        /// with respect to the given Graphics object.
        /// </summary>
        /// <param name="shape">
        /// Shape being compared against.
        /// </param>
        /// <param name="gr">
        /// Screen graphics.
        /// </param>
        /// <returns>
        /// True for a confirmed intersection,
        /// false for an empty intersection.
        /// </returns>
        public bool IsIntersecting(Shape shape, Graphics gr)
        {
            // create a copy of the invoker based on its derived type
            DynamicShape thisCopy = null;

            if (this is Tank)
            {
                thisCopy = new Tank(this as Tank);
            }
            else if (this is Gunfire)
            {
                thisCopy = new Gunfire(this as Gunfire);
            }

            // tick the copy if not null
            thisCopy?.Tick();

            // get the copy's region
            Region rCopy = new Region(thisCopy.GetPath());

            // intersect the copy with the provided shape
            rCopy.Intersect(shape.GetPath());

            // if it's empty, not colliding
            if (rCopy.IsEmpty(gr))
            {
                return(false);
            }

            // not empty, a collision has occured
            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks if the dynamic shape is out of bounds.
        /// </summary>
        /// <param name="level">The level to check bounds.</param>
        /// <param name="gr">Screen graphics object.</param>
        /// <returns>
        /// True if the shape is out of bounds, and
        /// false if the shape is within bounds.
        /// </returns>
        public bool IsOutOfBounds(Level level, Graphics gr)
        {
            // make a copy of the invoker, based on derived type
            DynamicShape thisCopy = null;

            if (this is Tank)
            {
                thisCopy = new Tank(this as Tank);
            }
            else if (this is Gunfire)
            {
                thisCopy = new Gunfire(this as Gunfire);
            }

            // tick the copy if not null
            thisCopy?.Tick();

            // get the level bounds
            RectangleF bounds = level.GetPath().GetBounds();

            // check if the copy is within 1 tilesize from the bounds
            if (thisCopy.Position.X - Tilesize >= bounds.Left &&
                thisCopy.Position.X + Tilesize < bounds.Right &&
                thisCopy.Position.Y - Tilesize >= bounds.Top &&
                thisCopy.Position.Y + Tilesize < bounds.Bottom)
            {
                return(false);
            }

            // copy is within a tilesize from the edge -- compare regions

            // get the copy's region
            Region rCopy = new Region(thisCopy.GetPath());

            // exclude the copy from the level's graphics path outline
            rCopy.Exclude(level.GetPath());

            // if it's empty, not out of bounds
            if (rCopy.IsEmpty(gr))
            {
                return(false);
            }

            // not empty, some of the copy is out of bounds
            return(true);
        }