/// <summary> /// Load sprite data from an XML file /// </summary> /// <param name="XMLFile">Path and name of the XML file to load</param> /// <param name="RootPath">Path within the XML scheme to look for the sprite tags</param> public void LoadSpriteXML(string XMLFile, string RootPath) { XmlDocument SpriteXML; XmlNodeList SpriteList, AnimList, FrameList; SpriteInfo NewSprite; SpriteAnimation NewAnim; Point Pos = new Point(); cSpriteList.Clear(); try { SpriteXML = new XmlDocument(); SpriteXML.Load(XMLFile); } catch (Exception ExErr) { throw new Exception(String.Format("Failed to load XML File {1}{0}Exception {2}{0}Message {3}", Environment.NewLine, XMLFile, ExErr.GetType().ToString(), ExErr.Message)); } SpriteList = SpriteXML.DocumentElement.SelectNodes(RootPath + "/sprite"); foreach (XmlNode SpriteNode in SpriteList) { //Load all details regarding this scene if (SpriteNode.Attributes["name"] != null) { NewSprite = new SpriteInfo(SpriteNode.Attributes["name"].InnerText); } else { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a sprite tag with no name attribute.", Environment.NewLine, XMLFile)); } if (SpriteNode.Attributes["tileset"] != null) { NewSprite.TileSet = SpriteNode.Attributes["tileset"].InnerText; } else { throw new Exception(String.Format("Failed to load XML File {1}{0}In sprite named {2} Encountered a sprite tag with no tileset attribute.", Environment.NewLine, XMLFile, NewSprite.Name)); } if (SpriteNode.Attributes["height"] != null) { if (Int32.TryParse(SpriteNode.Attributes["height"].InnerText, out NewSprite.ScreenRect.Height) == false) { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a sprite tag named {2} with an invalid height attribute.", Environment.NewLine, XMLFile, NewSprite.Name)); } } else { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a sprite tag named {2} with no height attribute.", Environment.NewLine, XMLFile, NewSprite.Name)); } if (SpriteNode.Attributes["width"] != null) { if (Int32.TryParse(SpriteNode.Attributes["width"].InnerText, out NewSprite.ScreenRect.Width) == false) { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a sprite tag named {2} with an invalid width attribute.", Environment.NewLine, XMLFile, NewSprite.Name)); } } else { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a sprite tag named {2} with no width attribute.", Environment.NewLine, XMLFile, NewSprite.Name)); } FrameList = SpriteNode.SelectNodes("default"); if (FrameList.Count != 1) { throw new Exception(String.Format("Failed to load XML File {1}{0}In sprite named {2} Encountered a sprite tag with no incorrect number of default tags.", Environment.NewLine, XMLFile, NewSprite.Name)); } if (FrameList[0].Attributes["down"] != null) { if (RegEx.QuickTest(FrameList[0].Attributes["down"].InnerText, "^([0-9+]),([0-9])+$") == true) { Pos.X = Int32.Parse(RegEx.GetRegExGroup(FrameList[0].Attributes["down"].InnerText, "^([0-9+]),([0-9])+$", 1)); Pos.Y = Int32.Parse(RegEx.GetRegExGroup(FrameList[0].Attributes["down"].InnerText, "^([0-9+]),([0-9])+$", 2)); NewSprite.DefaultTilePos[SpriteFacing.Down] = Pos; } else { throw new Exception(String.Format("Failed to load XML File {1}{0}In sprite named {2} Encountered a default tag with invalid down attribute.", Environment.NewLine, XMLFile, NewSprite.Name)); } } if (FrameList[0].Attributes["up"] != null) { if (RegEx.QuickTest(FrameList[0].Attributes["up"].InnerText, "^([0-9+]),([0-9])+$") == true) { Pos.X = Int32.Parse(RegEx.GetRegExGroup(FrameList[0].Attributes["up"].InnerText, "^([0-9+]),([0-9])+$", 1)); Pos.Y = Int32.Parse(RegEx.GetRegExGroup(FrameList[0].Attributes["up"].InnerText, "^([0-9+]),([0-9])+$", 2)); NewSprite.DefaultTilePos[SpriteFacing.Up] = Pos; } else { throw new Exception(String.Format("Failed to load XML File {1}{0}In sprite named {2} Encountered a default tag with invalid up attribute.", Environment.NewLine, XMLFile, NewSprite.Name)); } } if (FrameList[0].Attributes["left"] != null) { if (RegEx.QuickTest(FrameList[0].Attributes["left"].InnerText, "^([0-9+]),([0-9])+$") == true) { Pos.X = Int32.Parse(RegEx.GetRegExGroup(FrameList[0].Attributes["left"].InnerText, "^([0-9+]),([0-9])+$", 1)); Pos.Y = Int32.Parse(RegEx.GetRegExGroup(FrameList[0].Attributes["left"].InnerText, "^([0-9+]),([0-9])+$", 2)); NewSprite.DefaultTilePos[SpriteFacing.Left] = Pos; } else { throw new Exception(String.Format("Failed to load XML File {1}{0}In sprite named {2} Encountered a default tag with invalid left attribute.", Environment.NewLine, XMLFile, NewSprite.Name)); } } if (FrameList[0].Attributes["right"] != null) { if (RegEx.QuickTest(FrameList[0].Attributes["right"].InnerText, "^([0-9+]),([0-9])+$") == true) { Pos.X = Int32.Parse(RegEx.GetRegExGroup(FrameList[0].Attributes["right"].InnerText, "^([0-9+]),([0-9])+$", 1)); Pos.Y = Int32.Parse(RegEx.GetRegExGroup(FrameList[0].Attributes["right"].InnerText, "^([0-9+]),([0-9])+$", 2)); NewSprite.DefaultTilePos[SpriteFacing.Right] = Pos; } else { throw new Exception(String.Format("Failed to load XML File {1}{0}In sprite named {2} Encountered a default tag with invalid right attribute.", Environment.NewLine, XMLFile, NewSprite.Name)); } } AnimList = SpriteNode.SelectNodes("animation"); foreach (XmlNode AnimNode in AnimList) { if (AnimNode.Attributes["name"] != null) { NewAnim = new SpriteAnimation(AnimNode.Attributes["name"].InnerText); } else { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered an animation tag with no name attribute.", Environment.NewLine, XMLFile)); } if (AnimNode.Attributes["duration"] != null) { if (Int32.TryParse(AnimNode.Attributes["duration"].InnerText, out NewAnim.TimeMS) == false) { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a animation tag named {2} with an invalid duration attribute.", Environment.NewLine, XMLFile, NewAnim.Name)); } } else { throw new Exception(String.Format("Failed to load XML File {1}{0}Encountered a animation tag named {2} with no duration attribute.", Environment.NewLine, XMLFile, NewAnim.Name)); } FrameList = AnimNode.SelectNodes("frame"); foreach (XmlNode FrameNode in FrameList) { if (FrameNode.Attributes["down"] != null) { if (RegEx.QuickTest(FrameNode.Attributes["down"].InnerText, "^([0-9+]),([0-9])+$") == true) { Pos.X = Int32.Parse(RegEx.GetRegExGroup(FrameNode.Attributes["down"].InnerText, "^([0-9+]),([0-9])+$", 1)); Pos.Y = Int32.Parse(RegEx.GetRegExGroup(FrameNode.Attributes["down"].InnerText, "^([0-9+]),([0-9])+$", 2)); NewAnim.TilePos[SpriteFacing.Down].Add(Pos); } else { throw new Exception(String.Format("Failed to load XML File {1}{0}In sprite named {2} Encountered a default tag with invalid down attribute.", Environment.NewLine, XMLFile, NewAnim.Name)); } } if (FrameNode.Attributes["up"] != null) { if (RegEx.QuickTest(FrameNode.Attributes["up"].InnerText, "^([0-9+]),([0-9])+$") == true) { Pos.X = Int32.Parse(RegEx.GetRegExGroup(FrameNode.Attributes["up"].InnerText, "^([0-9+]),([0-9])+$", 1)); Pos.Y = Int32.Parse(RegEx.GetRegExGroup(FrameNode.Attributes["up"].InnerText, "^([0-9+]),([0-9])+$", 2)); NewAnim.TilePos[SpriteFacing.Up].Add(Pos); } else { throw new Exception(String.Format("Failed to load XML File {1}{0}In sprite named {2} Encountered a default tag with invalid up attribute.", Environment.NewLine, XMLFile, NewAnim.Name)); } } if (FrameNode.Attributes["left"] != null) { if (RegEx.QuickTest(FrameNode.Attributes["left"].InnerText, "^([0-9+]),([0-9])+$") == true) { Pos.X = Int32.Parse(RegEx.GetRegExGroup(FrameNode.Attributes["left"].InnerText, "^([0-9+]),([0-9])+$", 1)); Pos.Y = Int32.Parse(RegEx.GetRegExGroup(FrameNode.Attributes["left"].InnerText, "^([0-9+]),([0-9])+$", 2)); NewAnim.TilePos[SpriteFacing.Left].Add(Pos); } else { throw new Exception(String.Format("Failed to load XML File {1}{0}In sprite named {2} Encountered a default tag with invalid left attribute.", Environment.NewLine, XMLFile, NewAnim.Name)); } } if (FrameNode.Attributes["right"] != null) { if (RegEx.QuickTest(FrameNode.Attributes["right"].InnerText, "^([0-9+]),([0-9])+$") == true) { Pos.X = Int32.Parse(RegEx.GetRegExGroup(FrameNode.Attributes["right"].InnerText, "^([0-9+]),([0-9])+$", 1)); Pos.Y = Int32.Parse(RegEx.GetRegExGroup(FrameNode.Attributes["right"].InnerText, "^([0-9+]),([0-9])+$", 2)); NewAnim.TilePos[SpriteFacing.Right].Add(Pos); } else { throw new Exception(String.Format("Failed to load XML File {1}{0}In sprite named {2} Encountered a default tag with invalid right attribute.", Environment.NewLine, XMLFile, NewAnim.Name)); } } } NewSprite.AnimationList.Add(NewAnim.Name, NewAnim); } cSpriteList.Add(NewSprite.Name, NewSprite); } }