public override void CreateFramesFromXML(string dir) { base.CreateFramesFromXML(dir); dir = "Content/" + dir + ".xml"; m_Animations = new Dictionary<string, AnimaionSet>(); //m_Animations = LoadAnim(dir); Dictionary<string, AnimaionSet> dic = LoadAnim(dir); foreach (KeyValuePair<string, AnimaionSet> set in dic) { AnimaionSet newset = new AnimaionSet(); AnimaionSet curset = set.Value; for (int i = 0; i < curset.animFrames.Count; i++) { newset.animFrames.Add(curset.animFrames[i]); } newset.curFrame = curset.curFrame; newset.Speed = curset.Speed; m_Animations.Add(set.Key, newset); } return; XDocument doc = XDocument.Load(dir); foreach (XElement animPart in doc.Descendants()) { if (animPart.Name == "Animations") { foreach (XElement animSet in animPart.Descendants()) { if (animSet.Name == "AnimSet") { AnimaionSet set = new AnimaionSet(); string name = ""; foreach (XElement animBit in animSet.Descendants()) { if (animBit.Name == "Name") { name = animBit.Value; } if (animBit.Name == "Speed") { set.Speed = float.Parse(animBit.Value); } if (animBit.Name == "AnimFrames") { string first = ""; string last = ""; bool regularFrame = false; bool frameLoop = false; foreach (XElement frame in animBit.Descendants()) { if (frame.Name == "Frame") { foreach (XElement frameElem in frame.Descendants()) { if (frameElem.Name == "Key") { regularFrame = true; set.animFrames.Add(frameElem.Value); } } } else if (frame.Name == "FrameStart") { first = frame.Value; } else if (frame.Name == "FrameEnd") { last = frame.Value; } else if (frame.Name == "Loop") { frameLoop = true; } } if (first != "" && last == "" || first == "" && last != "" || ((first != "" || last != "") && regularFrame)) { throw new InvalidOperationException("error: start frame defined and last frame not, or a key frame was deifned along side a start and end frame, for shame!"); } if (first != "" && last != "") { string start = first; string end = last; // match constant part and ending digits var matchstart = Regex.Match(start, @"^(.*?)(\d+)$"); int numberstart = int.Parse(matchstart.Groups[2].Value); var matchend = Regex.Match(end, @"^(.*?)(\d+)$"); int numberend = int.Parse(matchend.Groups[2].Value); // constant parts must be the same if (matchstart.Groups[1].Value != matchend.Groups[1].Value) throw new ArgumentException(""); // create a format string with same number of digits as original string format = new string('0', matchstart.Groups[2].Length); for (int ii = numberstart; ii <= numberend; ++ii) set.animFrames.Add(matchstart.Groups[1].Value + ii.ToString(format));// Console.WriteLine(matchstart.Groups[1].Value + ii.ToString(format)); } if (frameLoop) { for (int i = set.animFrames.Count - 2; i > 0; i--) { set.animFrames.Add(set.animFrames[i]); } } } } m_Animations.Add(name, set); } } } } }
public override void CreateFramesFromXML(string dir) { base.CreateFramesFromXML(dir); dir = "Content/" + dir + ".xml"; XDocument doc = XDocument.Load(dir); foreach (XElement animPart in doc.Descendants()) { if (animPart.Name == "Animations") { foreach (XElement animSet in animPart.Descendants()) { if (animSet.Name == "AnimSet") { AnimaionSet set = new AnimaionSet(); string name = ""; foreach (XElement animBit in animSet.Descendants()) { if (animBit.Name == "Name") { name = animBit.Value; } if (animBit.Name == "Speed") { set.Speed = float.Parse(animBit.Value); } if (animBit.Name == "AnimFrames") { string first = ""; string last = ""; bool regularFrame = false; bool frameLoop = false; foreach (XElement frame in animBit.Descendants()) { if (frame.Name == "Frame") { foreach (XElement frameElem in frame.Descendants()) { if (frameElem.Name == "Key") { regularFrame = true; set.animFrames.Add(frameElem.Value); } } } else if (frame.Name == "FrameStart") { first = frame.Value; } else if (frame.Name == "FrameEnd") { last = frame.Value; } else if (frame.Name == "Loop") { frameLoop = true; } } if (first != "" && last == "" || first == "" && last != "" || ((first != "" || last != "") && regularFrame)) { throw new InvalidOperationException("error: start frame defined and last frame not, or a key frame was deifned along side a start and end frame, for shame!"); } if (first != "" && last != "") { string start = first; string end = last; // match constant part and ending digits var matchstart = Regex.Match(start, @"^(.*?)(\d+)$"); int numberstart = int.Parse(matchstart.Groups[2].Value); var matchend = Regex.Match(end, @"^(.*?)(\d+)$"); int numberend = int.Parse(matchend.Groups[2].Value); // constant parts must be the same if (matchstart.Groups[1].Value != matchend.Groups[1].Value) { throw new ArgumentException(""); } // create a format string with same number of digits as original string format = new string('0', matchstart.Groups[2].Length); for (int ii = numberstart; ii <= numberend; ++ii) { set.animFrames.Add(matchstart.Groups[1].Value + ii.ToString(format));// Console.WriteLine(matchstart.Groups[1].Value + ii.ToString(format)); } } if (frameLoop) { for (int i = set.animFrames.Count - 2; i > 0; i--) { set.animFrames.Add(set.animFrames[i]); } } } } m_Animations.Add(name, set); } } } } }
static Dictionary<string, AnimaionSet> LoadAnim(string dir) { if (loadedAnims.ContainsKey(dir)) { return loadedAnims[dir]; } XDocument doc = DocumentLoad(dir); Dictionary<string, AnimaionSet> thisset = new Dictionary<string, AnimaionSet>(); foreach (XElement animPart in doc.Descendants()) { if (animPart.Name == "Animations") { foreach (XElement animSet in animPart.Descendants()) { if (animSet.Name == "AnimSet") { AnimaionSet set = new AnimaionSet(); string name = ""; foreach (XElement animBit in animSet.Descendants()) { if (animBit.Name == "Name") { name = animBit.Value; } if (animBit.Name == "Speed") { set.Speed = float.Parse(animBit.Value); } if (animBit.Name == "AnimFrames") { string first = ""; string last = ""; bool regularFrame = false; bool frameLoop = false; foreach (XElement frame in animBit.Descendants()) { if (frame.Name == "Frame") { foreach (XElement frameElem in frame.Descendants()) { if (frameElem.Name == "Key") { regularFrame = true; set.animFrames.Add(frameElem.Value); } } } else if (frame.Name == "FrameStart") { first = frame.Value; } else if (frame.Name == "FrameEnd") { last = frame.Value; } else if (frame.Name == "Loop") { frameLoop = true; } } if (first != "" && last == "" || first == "" && last != "" || ((first != "" || last != "") && regularFrame)) { throw new InvalidOperationException("error: start frame defined and last frame not, or a key frame was deifned along side a start and end frame, for shame!"); } if (first != "" && last != "") { string start = first; string end = last; // match constant part and ending digits var matchstart = Regex.Match(start, @"^(.*?)(\d+)$"); int numberstart = int.Parse(matchstart.Groups[2].Value); var matchend = Regex.Match(end, @"^(.*?)(\d+)$"); int numberend = int.Parse(matchend.Groups[2].Value); // constant parts must be the same if (matchstart.Groups[1].Value != matchend.Groups[1].Value) throw new ArgumentException(""); // create a format string with same number of digits as original string format = new string('0', matchstart.Groups[2].Length); for (int ii = numberstart; ii <= numberend; ++ii) set.animFrames.Add(matchstart.Groups[1].Value + ii.ToString(format));// Console.WriteLine(matchstart.Groups[1].Value + ii.ToString(format)); } if (frameLoop) { for (int i = set.animFrames.Count - 2; i > 0; i--) { set.animFrames.Add(set.animFrames[i]); } } } } thisset.Add(name, set); } } } } loadedAnims.Add(dir, thisset); return thisset; }