示例#1
0
        // update the status of all player input devices on all appropriate entities
        private void updateControls()
        {
            InputDevice inputDevice = InputDevice.WSAD;

            switch (m_controlsItems[m_controlsIndex].index)
            {
            case 0:
                inputDevice = InputDevice.WSAD;
                break;

            case 1:
                inputDevice = InputDevice.Arrows;
                break;

            case 2:
                inputDevice = InputDevice.Controller1;
                break;

            case 3:
                inputDevice = InputDevice.Controller2;
                break;

            case 4:
                inputDevice = InputDevice.Controller3;
                break;

            case 5:
                inputDevice = InputDevice.Controller4;
                break;
            }
            m_players.getSpaceShip(m_controlsIndex).inputDevice = inputDevice;
            m_settings.setInputDevice(m_controlsIndex, inputDevice);
        }
示例#2
0
        // spawn a random asteroid at a random location with random attributes
        public void spawnAsteroid()
        {
            int    asteroidIndex;
            bool   bigAsteroid;
            Random rand = new Random();

            if (rand.Next(0, 99) < 30)
            {
                // spawn small asteroid (30% chance)
                asteroidIndex = rand.Next(0, 4) + 2;
                bigAsteroid   = false;
            }
            else
            {
                // spawn big asteroid (70% chance)
                asteroidIndex = rand.Next(0, 2) % 2;
                bigAsteroid   = true;
            }

            // if there are too many asteroids of this type, do not spawn it
            if (bigAsteroid && m_numberOfBigAsteroids >= m_maxBigAsteroids)
            {
                return;
            }
            if (!bigAsteroid && m_numberOfSmallAsteroids >= m_maxSmallAsteroids)
            {
                return;
            }

            bool     isInsideOfAsteroid;
            bool     isTooCloseToShip;
            Asteroid newAsteroid = new Asteroid(m_asteroidSprites.getSprite(asteroidIndex), bigAsteroid, m_settings);

            do
            {
                // make sure that the new asteroid is not too close to a space ship
                isTooCloseToShip = false;
                for (int i = 0; i < m_spaceShipSystem.size(); i++)
                {
                    if (m_spaceShipSystem.getSpaceShip(i).checkExtendedCollision(newAsteroid))
                    {
                        isTooCloseToShip = true;
                    }
                }

                // also make sure that the asteroid is not stuck inside of another asteroid
                isInsideOfAsteroid = false;
                if (!isTooCloseToShip)
                {
                    for (int i = 0; i < m_asteroids.Count(); i++)
                    {
                        if (m_asteroids[i].checkCollision(newAsteroid))
                        {
                            isInsideOfAsteroid = true;
                        }
                    }
                }

                // if it is too close to a ship or inside of another asteroid, then re-randomize its position until it isn't
                if (isTooCloseToShip || isInsideOfAsteroid)
                {
                    newAsteroid.randomizePosition();
                }
            } while(isTooCloseToShip || isInsideOfAsteroid);

            // store the asteroid
            if (newAsteroid.bigAsteroid)
            {
                m_numberOfBigAsteroids++;
            }
            else
            {
                m_numberOfSmallAsteroids++;
            }
            m_asteroids.Add(newAsteroid);
        }
示例#3
0
        public void handleCollisions()
        {
            if (m_projectileSystem == null ||
                m_spaceShipSystem == null ||
                m_asteroidSystem == null ||
                m_explosionSystem == null ||
                m_scoreSystem == null)
            {
                return;
            }

            // remove any collisions that have been handled and are no longer in collision
            for (int i = 0; i < m_asteroidSystem.size(); i++)
            {
                Asteroid a1 = m_asteroidSystem.getAsteroid(i);
                for (int j = 0; j < m_asteroidSystem.size(); j++)
                {
                    Asteroid a2 = m_asteroidSystem.getAsteroid(j);
                    if (i != j &&
                        (a1.isCollidingWith(a2) && !a1.checkCollision(a2)) ||
                        (a2.isCollidingWith(a1) && !a2.checkCollision(a1)))
                    {
                        a1.removeCollision(a2);
                        a2.removeCollision(a1);
                    }
                }
            }

            // check asteroid > asteroid collision
            for (int i = 0; i < m_asteroidSystem.size(); i++)
            {
                for (int j = i + 1; j < m_asteroidSystem.size(); j++)
                {
                    if (i != j && m_asteroidSystem.getAsteroid(i).checkCollision(m_asteroidSystem.getAsteroid(j)))
                    {
                        handleCollision(m_asteroidSystem.getAsteroid(i), m_asteroidSystem.getAsteroid(j));
                    }
                }

                // check projectile > asteroid collision
                for (int j = 0; j < m_projectileSystem.size(); j++)
                {
                    if (m_projectileSystem.getProjectile(j).checkCollision(m_asteroidSystem.getAsteroid(i)))
                    {
                        // [potentially] create a cluster of smaller asteroids if this was a big asteroid
                        m_scoreSystem.addPoints(m_projectileSystem.getProjectile(j).getProjectileSource(), (m_asteroidSystem.getAsteroid(i).bigAsteroid) ? ScoreType.BigAsteroid : ScoreType.SmallAsteroid);

                        if (m_asteroidSystem.getAsteroid(i).bigAsteroid)
                        {
                            m_asteroidSystem.spawnAsteroidCluster(m_asteroidSystem.getAsteroid(i));
                        }

                        m_explosionSystem.spawnExplosion(m_asteroidSystem.getAsteroid(i));

                        clearCollisions(m_asteroidSystem.getAsteroid(i));

                        m_asteroidSystem.removeAsteroid(i);
                        m_projectileSystem.removeProjectile(j);

                        i--;
                        j--;

                        break;
                    }
                }
            }

            // check ship > ship collision
            for (int i = 0; i < m_spaceShipSystem.size(); i++)
            {
                for (int j = i + 1; j < m_spaceShipSystem.size(); j++)
                {
                    if (i != j && m_spaceShipSystem.getSpaceShip(i).checkCollision(m_spaceShipSystem.getSpaceShip(j)))
                    {
                        m_explosionSystem.spawnExplosion(m_spaceShipSystem.getSpaceShip(i));
                        m_explosionSystem.spawnExplosion(m_spaceShipSystem.getSpaceShip(j));

                        m_spaceShipSystem.getSpaceShip(i).reset();
                        m_spaceShipSystem.getSpaceShip(j).reset();
                    }
                }

                // check ship > asteroid collision
                for (int j = 0; j < m_asteroidSystem.size(); j++)
                {
                    if (m_spaceShipSystem.getSpaceShip(i).checkCollision(m_asteroidSystem.getAsteroid(j)))
                    {
                        m_explosionSystem.spawnExplosion(m_spaceShipSystem.getSpaceShip(i));

                        m_spaceShipSystem.getSpaceShip(i).reset();

                        clearCollisions(m_asteroidSystem.getAsteroid(j));

                        m_asteroidSystem.removeAsteroid(j);
                        j--;
                    }
                }

                // check projectile > ship collision
                for (int j = 0; j < m_projectileSystem.size(); j++)
                {
                    if (!m_projectileSystem.getProjectile(j).getProjectileSource().Equals(m_spaceShipSystem.getSpaceShip(i)) &&
                        m_projectileSystem.getProjectile(j).checkCollision(m_spaceShipSystem.getSpaceShip(i)))
                    {
                        m_scoreSystem.addPoints(m_projectileSystem.getProjectile(j).getProjectileSource(), ScoreType.SpaceShip);

                        m_explosionSystem.spawnExplosion(m_spaceShipSystem.getSpaceShip(i));
                        m_spaceShipSystem.getSpaceShip(i).reset();

                        m_projectileSystem.removeProjectile(j);
                        j--;
                    }
                }
            }
        }
示例#4
0
        // draw the scores to the top of the screen
        public void draw(SpriteBatch spriteBatch)
        {
            if (m_players == null || m_settings == null || spriteBatch == null)
            {
                return;
            }

            else if (m_players.numberOfSpaceShips == 0)
            {
                return;
            }
            else if (m_players.numberOfSpaceShips == 1)
            {
                spriteBatch.DrawString(m_scoreFont, m_score[0].ToString(), new Vector2(m_settings.screenWidth / 2, 0), getColour(m_players.getSpaceShip(0)));
            }
            else if (m_players.numberOfSpaceShips == 2)
            {
                spriteBatch.DrawString(m_scoreFont, m_score[0].ToString(), new Vector2(m_settings.screenWidth / 3, 0), getColour(m_players.getSpaceShip(0)));
                spriteBatch.DrawString(m_scoreFont, m_score[1].ToString(), new Vector2(2 * (m_settings.screenWidth / 3), 0), getColour(m_players.getSpaceShip(1)));
            }
            else if (m_players.numberOfSpaceShips == 3)
            {
                spriteBatch.DrawString(m_scoreFont, m_score[0].ToString(), new Vector2(m_settings.screenWidth / 4, 0), getColour(m_players.getSpaceShip(0)));
                spriteBatch.DrawString(m_scoreFont, m_score[1].ToString(), new Vector2(2 * (m_settings.screenWidth / 4), 0), getColour(m_players.getSpaceShip(1)));
                spriteBatch.DrawString(m_scoreFont, m_score[2].ToString(), new Vector2(3 * (m_settings.screenWidth / 4), 0), getColour(m_players.getSpaceShip(2)));
            }
            else if (m_players.numberOfSpaceShips == 4)
            {
                spriteBatch.DrawString(m_scoreFont, m_score[0].ToString(), new Vector2(m_settings.screenWidth / 5, 0), getColour(m_players.getSpaceShip(0)));
                spriteBatch.DrawString(m_scoreFont, m_score[1].ToString(), new Vector2(2 * (m_settings.screenWidth / 5), 0), getColour(m_players.getSpaceShip(1)));
                spriteBatch.DrawString(m_scoreFont, m_score[2].ToString(), new Vector2(3 * (m_settings.screenWidth / 5), 0), getColour(m_players.getSpaceShip(2)));
                spriteBatch.DrawString(m_scoreFont, m_score[3].ToString(), new Vector2(4 * (m_settings.screenWidth / 5), 0), getColour(m_players.getSpaceShip(3)));
            }
        }