예제 #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);
        }
예제 #2
0
 /// <summary>
 /// Alternate constructor used for creating
 /// a copy of a gunfire object for hit testing.
 /// </summary>
 /// <param name="gunfire">
 /// The gunfire object to copy.
 /// </param>
 public Gunfire(Gunfire gunfire) : base(gunfire.Position, gunfire.Color, gunfire.Player)
 {
     // copy all properties to the new gunfire object
     Rotation     = gunfire.Rotation;
     RotIncrement = gunfire.RotIncrement;
     XSpeed       = gunfire.XSpeed;
     YSpeed       = gunfire.YSpeed;
     _model       = gunfire._model;
 }
예제 #3
0
        /// <summary>
        /// Checks for fire input, and fires the player's gun
        /// </summary>
        /// <param name="input">Player input.</param>
        /// <param name="thisPlayer">Player data.</param>
        /// <param name="tank">Player tank.</param>
        /// <param name="movingShapes">Collection of moving shapes.</param>
        private void SetFireWeapon(AbstractInput input, PlayerData thisPlayer,
                                   Tank tank, List <DynamicShape> movingShapes)
        {
            if (input.Fire)
            {
                // check if not reloading
                if (thisPlayer.CheckReload())
                {
                    // get gunshot color (white by default)
                    Color shotColor = Color.White;
                    switch (thisPlayer.Player)
                    {
                    case PlayerNumber.One:
                        switch (thisPlayer.CurrentWeapon)
                        {
                        case GunType.MachineGun:
                            shotColor = _cPlayer1MGColor;
                            break;

                        case GunType.Rocket:
                            shotColor = _cPlayer1RocketColor;
                            break;
                        }
                        break;

                    case PlayerNumber.Two:
                        switch (thisPlayer.CurrentWeapon)
                        {
                        case GunType.MachineGun:
                            shotColor = _cPlayer2MGColor;
                            break;

                        case GunType.Rocket:
                            shotColor = _cPlayer2RocketColor;
                            break;
                        }
                        break;
                    }

                    // get a new gunshot
                    Gunfire newShot = new Gunfire(tank.Position, tank.Rotation,
                                                  thisPlayer.CurrentWeapon, shotColor, thisPlayer.Player);

                    // start reloading timer
                    thisPlayer.StartReloading();

                    //add new gunshot it to the list of moving shapes
                    movingShapes.Add(newShot);
                }
            }
        }
예제 #4
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);
        }