private void Awake() { if (Instance != null && Instance != this) { Destroy(Instance.gameObject); } Instance = this; }
/// <summary> /// Loads building entries from provided file into provided dictionary /// </summary> /// <param id="dict">Dictionary the entries will be loaded into</param> /// <param id="xmlFile">File the entries are loaded from</param> public static void LoadBuildings(SortedDictionary<string, BuildingDb> dict, XDocument xmlFile) { var bldgEntries = xmlFile.Descendants("Building"); foreach (var bldgXml in bldgEntries) { BuildingDb bldgEntry = new BuildingDb(); List<CostEntry> costEntries; foreach (XNode en in bldgXml.Nodes()) { XElement e = en as XElement; if (e == null) continue; switch (e.Name.LocalName.ToLower()) { case "id": bldgEntry.Id = e.Value; break; case "name": bldgEntry.Name = e.Value; break; case "tier": bldgEntry.Tier = int.Parse(e.Value); break; case "health": bldgEntry.Health = int.Parse(e.Value); break; case "constructiontime": bldgEntry.ConstructionAmount = int.Parse(e.Value); break; case "texture": if (!UI.Instance.HaveTexture(e.Value)) { throw new Exception("Entry: " + bldgEntry.Id + " - texture " + e.Value + " not found"); } bldgEntry.Texture = UI.Instance.GetTexture(e.Value); break; case "textureconstruction": if (!UI.Instance.HaveTexture(e.Value)) { throw new Exception("Entry: " + bldgEntry.Id + " - texture " + e.Value + " not found"); } bldgEntry.TextureConstruction = UI.Instance.GetTexture(e.Value); break; case "resourcecenter": bldgEntry.ResourceCenter = bool.Parse(e.Value); break; case "onlyoneallowed": bldgEntry.OnlyOneAllowed = bool.Parse(e.Value); break; case "builtonstart": bldgEntry.BuiltOnStart = bool.Parse(e.Value); break; case "unlockedby": bldgEntry.UnlockedByUR = e.Value; break; case "costs": #region costEntries = new List<CostEntry>(); foreach (XNode eCostsn in e.Nodes()) { XElement eCosts = eCostsn as XElement; if (eCosts == null) continue; CostEntry costEntry = new CostEntry(); foreach (XNode een in eCosts.Nodes()) { XElement ee = een as XElement; if (ee == null) continue; switch (ee.Name.LocalName.ToLower()) { case "resource": costEntry.EntryUR = ee.Value; break; case "amount": costEntry.Amount = int.Parse(ee.Value); break; } } costEntries.Add(costEntry); } bldgEntry.Cost = costEntries.ToArray(); #endregion break; case "trains": #region var trainEntries = new List<TrainEntry>(); foreach (XNode eTrainn in e.Nodes()) { XElement eTrain = eTrainn as XElement; if (eTrain == null) continue; TrainEntry trainEntry = new TrainEntry(); foreach (XNode een in eTrain.Nodes()) { XElement ee = een as XElement; if (ee == null) continue; switch (ee.Name.LocalName.ToLower()) { case "from": trainEntry.TrainFromUR = ee.Value; break; case "to": trainEntry.TrainToUR = ee.Value; break; case "speed": trainEntry.Speed = int.Parse(ee.Value); break; case "costs": costEntries = new List<CostEntry>(); foreach (XNode eCostsn in ee.Nodes()) { XElement eCosts = eCostsn as XElement; if (eCosts == null) continue; CostEntry costEntry = new CostEntry(); foreach (XNode eeen in eCosts.Nodes()) { XElement eee = eeen as XElement; if (eee == null) continue; switch (eee.Name.LocalName.ToLower()) { case "resource": costEntry.EntryUR = eee.Value; break; case "amount": costEntry.Amount = int.Parse(eee.Value); break; } } costEntries.Add(costEntry); } trainEntry.Cost = costEntries.ToArray(); break; } } trainEntries.Add(trainEntry); } bldgEntry.Trains = trainEntries.ToArray(); #endregion break; case "spawns": #region List<BuildingDb.SpawnEntry> spawnEntries = new List<BuildingDb.SpawnEntry>(); foreach (XNode eSpawnsn in e.Nodes()) { XElement eSpawns = eSpawnsn as XElement; if (eSpawns == null) continue; BuildingDb.SpawnEntry spawnEntry = new BuildingDb.SpawnEntry(); foreach (XNode een in eSpawns.Nodes()) { XElement ee = een as XElement; if (ee == null) continue; switch (ee.Name.LocalName.ToLower()) { case "entry": spawnEntry.EntryUR = ee.Value; break; case "speed": spawnEntry.Speed = int.Parse(ee.Value); break; case "capacity": spawnEntry.Capacity = int.Parse(ee.Value); break; } } spawnEntries.Add(spawnEntry); } bldgEntry.Spawns = spawnEntries.ToArray(); #endregion break; } } dict.Add(bldgEntry.Id, bldgEntry); } }