Beyond_Beyaan.Galaxy.Load C# (CSharp) Метод

Load() публичный Метод

public Load ( System.Xml.Linq.XElement root, GameMain gameMain ) : bool
root System.Xml.Linq.XElement
gameMain GameMain
Результат bool
        public bool Load(XElement root, GameMain gameMain)
        {
            var galaxyElement = root.Element("Galaxy");
            if (galaxyElement == null)
            {
                return false;
            }
            GalaxySize = int.Parse(galaxyElement.Attribute("Width").Value);
            starSystems = new List<StarSystem>();
            _quickLookupSystem = new Dictionary<int, StarSystem>();
            var starsElement = galaxyElement.Element("Stars");
            foreach (var star in starsElement.Elements())
            {
                string name = star.Attribute("Name").Value;
                string description = star.Attribute("Description").Value;
                int id = int.Parse(star.Attribute("ID").Value);
                int xPos = int.Parse(star.Attribute("XPos").Value);
                int yPos = int.Parse(star.Attribute("YPos").Value);
                string[] color = star.Attribute("Color").Value.Split(new[] {','});
                float[] starColor = new float[4];
                for (int i = 0; i < 4; i++)
                {
                    starColor[i] = float.Parse(color[i]);
                }
                StarSystem newStar = new StarSystem(name, id, xPos, yPos, Color.FromArgb((int)(255 * starColor[3]), (int)(255 * starColor[0]), (int)(255 * starColor[1]), (int)(255 * starColor[2])), description, gameMain.Random);
                newStar.ExploredByIDs = star.Attribute("ExploredBy").Value;
                foreach (var planetElement in star.Elements())
                {
                    string planetName = planetElement.Attribute("Name").Value;
                    string type = planetElement.Attribute("Type").Value;
                    int owner = -1;
                    if (!string.IsNullOrEmpty(planetElement.Attribute("Owner").Value))
                    {
                        owner = int.Parse(planetElement.Attribute("Owner").Value);
                    }
                    int populationMax = int.Parse(planetElement.Attribute("MaxPopulation").Value);
                    var newPlanet = new Planet(planetName, type, populationMax, null, newStar, gameMain.Random);
                    newPlanet.OwnerID = owner;
                    newPlanet.Factories = float.Parse(planetElement.Attribute("Buildings").Value);
                    newPlanet.EnvironmentAmount = int.Parse(planetElement.Attribute("EnvironmentPercentage").Value);
                    newPlanet.InfrastructureAmount = int.Parse(planetElement.Attribute("InfrastructurePercentage").Value);
                    newPlanet.DefenseAmount = int.Parse(planetElement.Attribute("DefensePercentage").Value);
                    newPlanet.ConstructionAmount = int.Parse(planetElement.Attribute("ConstructionPercentage").Value);
                    newPlanet.ResearchAmount = int.Parse(planetElement.Attribute("ResearchPercentage").Value);
                    newPlanet.EnvironmentLocked = bool.Parse(planetElement.Attribute("EnvironmentLocked").Value);
                    newPlanet.InfrastructureLocked = bool.Parse(planetElement.Attribute("InfrastructureLocked").Value);
                    newPlanet.DefenseLocked = bool.Parse(planetElement.Attribute("DefenseLocked").Value);
                    newPlanet.ConstructionLocked = bool.Parse(planetElement.Attribute("ConstructionLocked").Value);
                    newPlanet.ResearchLocked = bool.Parse(planetElement.Attribute("ResearchLocked").Value);
                    newPlanet.ShipBeingBuiltID = int.Parse(planetElement.Attribute("ShipBuilding").Value);
                    newPlanet.ShipConstructionAmount = float.Parse(planetElement.Attribute("AmountBuilt").Value);
                    newPlanet.RelocateToSystemID = int.Parse(planetElement.Attribute("RelocatingTo").Value);
                    var transferTo = planetElement.Element("TransferTo");
                    if (transferTo != null)
                    {
                        newPlanet.TransferSystemID = new KeyValuePair<int, int>(int.Parse(transferTo.Attribute("StarSystem").Value), int.Parse(transferTo.Attribute("Amount").Value));
                    }
                    var races = planetElement.Element("Races");
                    foreach (var race in races.Elements())
                    {
                        newPlanet.AddRacePopulation(gameMain.RaceManager.GetRace(race.Attribute("RaceName").Value), float.Parse(race.Attribute("Amount").Value));
                    }
                    newStar.Planets.Add(newPlanet);
                }
                starSystems.Add(newStar);
                _quickLookupSystem.Add(newStar.ID, newStar);
            }
            //Now match up IDs to actual classes
            foreach (var starSystem in starSystems)
            {
                foreach (var planet in starSystem.Planets)
                {
                    if (planet.RelocateToSystemID != -1)
                    {
                        planet.RelocateToSystem = new TravelNode {StarSystem = _quickLookupSystem[planet.RelocateToSystemID]};
                        planet.RelocateToSystemID = -1;
                    }
                    if (!planet.TransferSystemID.Equals(new KeyValuePair<int, int>()))
                    {
                        planet.TransferSystem = new KeyValuePair<TravelNode, int>(new TravelNode { StarSystem = _quickLookupSystem[planet.TransferSystemID.Key] }, planet.TransferSystemID.Value);
                    }
                }
            }
            return true;
        }

Usage Example

Пример #1
0
        public bool LoadGame(string filename)
        {
            string path = Path.Combine(GameDataSet.FullName, "Saves");

            if (!Directory.Exists(path))
            {
                //No folder exists, impossible to load anything
                return(false);
            }
            try
            {
                XDocument doc  = XDocument.Load(Path.Combine(path, filename));
                XElement  root = doc.Root;
                if (!Galaxy.Load(root, this))
                {
                    return(false);
                }
                if (!EmpireManager.Load(root))
                {
                    return(false);
                }
                Galaxy.Reconcilate(EmpireManager);
                if (_galaxyScreen != null)
                {
                    _galaxyScreen.ResetCamera();
                }
                if (_processingTurnScreen != null)
                {
                    _processingTurnScreen.ResetCamera();
                }
                ChangeToScreen(Screen.Galaxy);
                return(true);
            }
            catch
            {
                return(false);
            }
        }