Пример #1
0
        /// <summary>
        /// Starts the server, including a client connection loop and the game loop.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            settings = SpaceWarsSettings.ReadXML("settings.xml");
            clients  = new LinkedList <SocketState>();
            watch    = new Stopwatch();
            gameData = new StringBuilder();

            theWorld = new World(settings.GetSize(), settings.GetStars(), settings.GetShotDelay(), settings.GetRespawnDelay(), settings.TeamModeEnabled());

            Networking.ServerAwaitingClientLoop(HandleNewClient);

            watch.Start();

            gameLoop = new Thread(() =>
            {
                while (true)
                {
                    Update();
                }
            });

            gameLoop.Start();
        }
Пример #2
0
        /// <summary>
        /// Returns a set of settings from an XML file. If categories in the XML aren't present, default values will be used in their place.
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static SpaceWarsSettings ReadXML(string filePath)
        {
            SpaceWarsSettings settings = new SpaceWarsSettings();

            try
            {
                using (XmlReader reader = XmlReader.Create(filePath))
                {
                    while (reader.Read())
                    {
                        if (reader.IsStartElement())
                        {
                            switch (reader.Name)
                            {
                            case "UniverseSize":
                                settings.size = reader.ReadElementContentAsInt();
                                break;

                            case "MSPerFrame":
                                settings.msPerFrame = (uint)reader.ReadElementContentAsInt();
                                break;

                            case "FramesPerShot":
                                settings.shotDelay = (uint)reader.ReadElementContentAsInt();
                                break;

                            case "RespawnRate":
                                settings.respawnDelay = (uint)reader.ReadElementContentAsInt();
                                break;

                            case "StartingHP":
                                settings.startingHP = reader.ReadElementContentAsInt();
                                break;

                            case "Team":
                                settings.teamMode = reader.ReadElementContentAsBoolean();
                                break;

                            case "ShotSpeed":
                                settings.shotSpeed = (uint)reader.ReadElementContentAsInt();
                                break;

                            case "ThrustStrength":
                                settings.thrustStrength = reader.ReadElementContentAsDouble();
                                break;

                            case "TurningRate":
                                settings.turningRate = reader.ReadElementContentAsDouble();
                                break;

                            case "ShipHitBoxSize":
                                settings.shipHitBoxSize = (uint)reader.ReadElementContentAsInt();
                                break;

                            case "StarHitBoxSize":
                                settings.starHitBoxSize = (uint)reader.ReadElementContentAsInt();
                                break;

                            case "Star":
                                Star star = ReadStar(reader, settings.starHitBoxSize);
                                if (star != null)
                                {
                                    settings.stars.Add(star);
                                }
                                break;
                            }
                        }
                    }
                }

                return(settings);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error reading settings. Using stored defaults.");
                SpaceWarsSettings defaults = new SpaceWarsSettings();
                defaults.stars.Add(new Star(new Vector2D(0, 0), 0.01, defaults.starHitBoxSize));
                return(defaults);
            }
        }