Example #1
0
        /// <summary>
        /// Randomize spawn location for ship
        /// </summary>
        /// <param name="ship"></param>
        public static void SpawnShip(Ship ship)
        {
            Random   rand = new Random();
            int      LocX;
            int      LocY;
            bool     safeSpawn = false;
            Vector2D position  = new Vector2D(0.0, 0.0);

            //loop through potential random spawn locations to find location that is
            //not near a star or a ship.
            while (!safeSpawn)
            {
                int worldSize = (int)gameSettings["UniverseSize"] - 1;
                // finding possible random variables for the spawn location
                LocX     = rand.Next(-(worldSize / 2), worldSize / 2);
                LocY     = rand.Next(-(worldSize / 2), worldSize / 2);
                position = new Vector2D(LocX, LocY);

                // flags to determine if ship should spawn or not.
                bool starSpawn = true; // set for spawning on top of stars
                bool shipSpawn = true; // set for spawning on top of other ships

                //checks to see if potential spawn location is too close to a star
                foreach (Star star in TheWorld.GetStars())
                {
                    if ((star.loc - position).Length() <= 50)
                    {
                        starSpawn = false;
                    }
                }

                //checks to see if potential spawn location is too close to a ship
                foreach (Ship shipLoop in TheWorld.GetShipsAll())
                {
                    if ((shipLoop.loc - position).Length() <= 50)
                    {
                        shipSpawn = false;
                    }
                }

                //If neither star or ship is hindering spawn, break out of loop
                if (starSpawn == true && shipSpawn == true)
                {
                    safeSpawn = true;
                }
            }

            ship.SetLoc(position);
            ship.SetHp((int)gameSettings["StartingHP"]);
            ship.SetThrust(false);
            Vector2D vel = new Vector2D(0, 0);

            ship.SetVelocity(vel);
            ship.SetDeathCounter(0);
            Vector2D Dir = new Vector2D(0, 1);

            ship.SetDir(Dir);
            ship.SetFireRateCounter((int)(gameSettings["FramesPerShot"]));
        }