コード例 #1
0
        /// <summary>
        /// Checks if there was a collision between a laser and an alien by iterating through the AlienSquad
        /// array. Returns True if a collision was detected, False if there was no collision. Also checks for
        /// a collision with the mothership.
        /// </summary>
        /// <param name="laser">Laser that was launched by a player to be checked for collisions.</param>
        /// <returns>A boolean representing if a collision was detected</returns>
        protected override bool checkCollision(ProjectileSprite laser)
        {
            bool collision = false;
            int  pts       = 0;

            for (int row = 0; row < alienSquad.getAlienRowCount(); row++)
            {
                for (int col = 0; col < alienSquad.getAlienColumnCount(); col++)
                {
                    if (laser.GetBoundary().Intersects(alienSquad[row, col].GetBoundary()))
                    {
                        if (alienSquad[row, col].GetAlienState() == AlienState.ACTIVE)
                        {
                            // Only gives points if Alien dies, on higher difficulties multiple hits will not award points
                            if (alienSquad[row, col].GetHitPoints() == 1)
                            {
                                pts = 10 + (alienSquad.getAlienRowCount() - 1 - row) * 10;
                            }

                            onAlienCollision(alienSquad[row, col], pts);
                            collision = true;

                            //remove laser after it hits alien
                            laser.Dispose();
                            bullets.Remove(laser);

                            return(collision);
                        }
                    }
                }
            }
            if (laser.GetBoundary().Intersects(mothership.GetBoundary()))
            {
                if (mothership.GetAlienState() == AlienState.ACTIVE)
                {
                    onMothershipCollision(mothership, 100);
                    collision = true;

                    // remove laser after it hits alien
                    laser.Dispose();
                    bullets.Remove(laser);

                    return(collision);
                }
            }

            return(collision);
        }