예제 #1
0
        /// <summary>
        /// This function will receive a Universe and a StarDefaultSettings object and create Stars from the data
        /// provided
        /// </summary>
        /// <param name="universe"></param>
        /// <param name="starDefaultSettings"></param>
        /// <param name="starData"></param>
        /// <param name="nameGeneration"></param>
        /// <returns>
        /// A newly modified Universe
        /// </returns>
        public Universe AddStars(Universe universe, StarDefaultSettings starDefaultSettings, StarData starData,
                                 NameGeneration nameGeneration)
        {
            // Check if the Stars have been initialized prior
            universe.Stars ??= new List <Star>();

            // Set the number of stars to create. The default is 1d10+20
            var starLen   = starData.Stars.Count;
            var starCount = starDefaultSettings.StarCount < 0
                ? Rand.Next(0, 10) + 20
                : starDefaultSettings.StarCount;

            var sCount = 0;

            while (sCount < starCount)
            {
                var star = new Star();

                // Generate the unique ID for the Star
                IdGen.GenerateId(star);

                // Set Grid Location of the Star
                var zone = universe.Zones[Rand.Next(0, universe.Zones.Count)];
                if (zone.StarId == null)
                {
                    zone.StarId = star.Id;
                }
                else
                {
                    continue;
                }

                // If that ID exists roll a new one
                if (universe.Stars.Exists(a => a.Id == star.Id))
                {
                    continue;
                }

                // Pick a random Name for the Star
                star.Name = Rand.Next(0, 4) == 2
                    ? nameGeneration.GenerateName()
                    : starData.Stars[Rand.Next(0, starLen - 1)];

                // If that Name exists roll a new one
                if (universe.Stars.Exists(a => a.Name == star.Name))
                {
                    continue;
                }

                // Set the type of Star
                star.StarType = starData.StarTypes[Rand.Next(0, 8) + Rand.Next(0, 8) + Rand.Next(0, 8)];

                // Add the Star to the Universe
                universe.Stars.Add(star);

                sCount++;
            }

            return(universe);
        }
예제 #2
0
        /// <summary>
        /// This method should receive the Universe to add Stars to and a set of StarDefaultSettings
        ///
        /// Default values are handled in StarCreation.AddStars
        /// </summary>
        /// <param name="universe"></param>
        /// <param name="starDefaultSettings"></param>
        /// <returns>
        /// Return the newly edited Universe
        /// </returns>
        /// <exception cref="FileNotFoundException"></exception>
        public Universe CreateStars(Universe universe, StarDefaultSettings starDefaultSettings)
        {
            // If there is no Grid for the Stars to be placed in then throw an exception
            if (universe.Grid == null)
            {
                throw new FileNotFoundException("No grid has been set for the universe");
            }

            // Set the Universe to the Universe returned from StarCreation.AddStars and serialize/return it
            universe = new StarCreation().AddStars(universe, starDefaultSettings, _starData, StarNameGeneration);
            SerializeData(universe);
            return(universe);
        }