Beyond_Beyaan.Data_Modules.Race.Initialize C# (CSharp) Method

Initialize() public method

public Initialize ( FileInfo file, Random r, string &reason ) : bool
file System.IO.FileInfo
r System.Random
reason string
return bool
        public bool Initialize(FileInfo file, Random r, out string reason)
        {
            XDocument doc = XDocument.Load(file.FullName);
            XElement root = doc.Element("Race");

            SetBaseDefaults();

            RaceName = root.Attribute("name").Value;
            SingularRaceName = root.Attribute("singularName").Value;
            RaceDescription = root.Attribute("raceDescription").Value;
            NeutralAvatar = SpriteManager.GetSprite(root.Attribute("neutralPortrait").Value, r);
            HappyAvatar = SpriteManager.GetSprite(root.Attribute("happyPortrait").Value, r);
            AngryAvatar = SpriteManager.GetSprite(root.Attribute("angryPortrait").Value, r);
            MiniAvatar = SpriteManager.GetSprite(root.Attribute("thumbnail").Value, r);
            GroundTroop = SpriteManager.GetSprite(root.Attribute("groundTroop").Value, r);
            DyingTroop = SpriteManager.GetSprite(root.Attribute("dyingTroop").Value, r);
            FleetIcon = SpriteManager.GetSprite(root.Attribute("fleetIcon").Value, r);
            TransportIcon = SpriteManager.GetSprite(root.Attribute("transportIcon").Value, r);
            City = SpriteManager.GetSprite(root.Attribute("city").Value, r);
            LandingShip = SpriteManager.GetSprite(root.Attribute("landingShip").Value, r);

            XElement shipTypes = root.Element("ShipTypes");
            if (shipTypes == null)
            {
                reason = "ShipTypes not found in " + RaceName + " race";
                return false;
            }

            ShipTypes = new List<RaceShipType>();
            foreach (XElement shipType in shipTypes.Elements())
            {
                RaceShipType newType = new RaceShipType();
                newType.TypeName = shipType.Attribute("name").Value;
                newType.Space = int.Parse(shipType.Attribute("space").Value);
                newType.Width = int.Parse(shipType.Attribute("width").Value);
                newType.Height = int.Parse(shipType.Attribute("height").Value);
                newType.Bodies = new List<BBSprite>();
                foreach (XElement body in shipType.Elements())
                {
                    newType.Bodies.Add(SpriteManager.GetSprite(body.Attribute("sprite").Value, r));
                }
                ShipTypes.Add(newType);
            }
            //Order ships based on space
            ShipTypes.Sort((a, b) => { return a.Space.CompareTo(b.Space); });

            reason = null;
            return true;
        }

Usage Example

Beispiel #1
0
 public bool Initialize(DirectoryInfo directory, Random r, out string reason)
 {
     try
     {
         string path = Path.Combine(directory.FullName, "races");
         DirectoryInfo di = new DirectoryInfo(path);
         if (!di.Exists)
         {
             //If it don't exist, create one so users can add races
             di.Create();
         }
         foreach (FileInfo fi in di.GetFiles("*.xml"))
         {
             Race race = new Race();
             if (!race.Initialize(fi, r, out reason))
             {
                 return false;
             }
             Races.Add(race);
         }
         reason = null;
         return true;
     }
     catch (Exception e)
     {
         reason = e.Message;
         return false;
     }
 }