protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); animation = Animation.LoadAnimation(GraphicsDevice, animationFile); transforms = new BoneTransformation[animation.Keyframes[0].DrawBones.Count]; }
public static Animation LoadAnimation(GraphicsDevice graphicsDevice, string animationFile) { Animation animation = new Animation(); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(animationFile); int loopFrame = Int32.Parse(xmlDocument.SelectSingleNode("/Animation/LoopFrame").InnerText); float frameRate = 1.0f / Int32.Parse(xmlDocument.SelectSingleNode("/Animation/FrameRate").InnerText); animation.Loop = loopFrame != -1; if (animation.Loop) animation.LoopTime = frameRate * loopFrame; XmlNodeList nodeList = xmlDocument.SelectNodes("/Animation/Texture"); foreach (XmlNode node in nodeList) { animation.Textures.Add(LoadTexture(graphicsDevice, node.InnerText, Path.GetFullPath(animationFile))); } nodeList = xmlDocument.SelectNodes("/Animation/Keyframe"); foreach (XmlNode node in nodeList) { Keyframe keyframe = new Keyframe(); keyframe.FrameTime = frameRate * Int32.Parse(node.Attributes["frame"].Value); if (node.Attributes["vflip"] != null) keyframe.FlipVertically = bool.Parse(node.Attributes["vflip"].Value); else keyframe.FlipVertically = false; if (node.Attributes["hflip"] != null) keyframe.FlipHorizontally = bool.Parse(node.Attributes["hflip"].Value); else keyframe.FlipHorizontally = false; XmlNodeList boneList = node.SelectNodes("Bone"); foreach (XmlNode boneNode in boneList) { Bone bone = new Bone(); bone.Name = boneNode.Attributes["name"].Value; bone.TextureIndex = Int32.Parse(boneNode.SelectSingleNode("TextureIndex").InnerText); bone.ParentIndex = Int32.Parse(boneNode.SelectSingleNode("ParentIndex").InnerText); bone.Hidden = bool.Parse(boneNode.SelectSingleNode("Hidden").InnerText); bone.Position = new Vector2(float.Parse(boneNode.SelectSingleNode("Position/X").InnerText, CultureInfo.InvariantCulture), float.Parse(boneNode.SelectSingleNode("Position/Y").InnerText, CultureInfo.InvariantCulture)); bone.Rotation = float.Parse(boneNode.SelectSingleNode("Rotation").InnerText, CultureInfo.InvariantCulture); bone.Scale = new Vector2(float.Parse(boneNode.SelectSingleNode("Scale/X").InnerText, CultureInfo.InvariantCulture), float.Parse(boneNode.SelectSingleNode("Scale/Y").InnerText, CultureInfo.InvariantCulture)); bone.SelfIndex = keyframe.DrawBones.Count; bone.FlipHorizontally = bool.Parse(boneNode.SelectSingleNode("TextureFlipHorizontal").InnerText); bone.FlipVertically = bool.Parse(boneNode.SelectSingleNode("TextureFlipVertical").InnerText); keyframe.DrawBones.Add(bone); } // keyframe.UpdateBones foreach (Bone bone in keyframe.DrawBones) { BoneSortAdd(bone, keyframe.UpdateBones, keyframe.DrawBones); } animation.Keyframes.Add(keyframe); } return animation; }