//Assigns a template to the dictionary based upon a string. public static void SetTemplate(string templateName, ShipTemplate template) { //First check if the dictionary is null. if (shipTemplateDict != null) { //Then check if the dictionary already contains the given string. if (shipTemplateDict.ContainsKey(templateName)) { //Replacing the old ShipTemplate with the new one. shipTemplateDict[templateName] = template; } else { //Else, add the template to the dictionary. shipTemplateDict.Add(templateName, template); } } else { //If the dictionary is null, something went wrong. Debug.Log("Attempting to set material in uninitialized ShipTemplateDict."); } }
//Builds the ShipTemplate dictionary from the XML database. public static void BuildShipTemplateDict() { //Check that the dictionary hasn't already been built. if (!shipTemplateDictBuilt) { //First try to read ShipDefs.xml try { reader = new XmlTextReader("Assets/Resources/Defs/ShipDefs.xml"); } catch { //Catch the file not being found. Debug.Log("Could not find ShipDefs.xml"); return; } //Read until the end of the file. while (reader.Read()) { //Check if the element is a start element. if (reader.IsStartElement()) { //If it is, determine what variable it's referring to, and assign it. switch (reader.Name.ToString()) { //Begin a new ShipTemplate. case "ShipTemplate": workShipTemplate = new ShipTemplate(); break; //Assign the graphicName to the template. case "graphicName": workShipTemplate.SetGraphicName(reader.ReadString()); break; //Build the material for the template and then add it to the material dictionary. case "graphicPath": workMat = new Material(Shader.Find("Sprites/Default")); workMat.mainTexture = Resources.Load(reader.ReadString()) as Texture2D; workMat.mainTexture.filterMode = FilterMode.Bilinear; MatDict.SetMaterial(workShipTemplate.graphicName, workMat); break; //Assign the movementSpeed to the template. case "movementSpeed": workShipTemplate.SetMovementSpeed(int.Parse(reader.ReadString())); break; } } else { //Check if the node type is an end element. if (reader.NodeType == XmlNodeType.EndElement) { //If it is, is this the end of a template? if (reader.Name.Equals("ShipTemplate")) { //Finalize the template and add it to the ShipTemplate dictionary. workShipTemplate.Finalized(); ShipTemplateDict.SetTemplate(workShipTemplate.graphicName, workShipTemplate); } } } } //Close the file after it's been read and mark the dictionary built. reader.Close(); shipTemplateDictBuilt = true; } else { //Otherwise, the dictionary has already been built and something went wrong. Debug.Log("Attempting to build already built ShipTemplateDict."); } }