/// <summary> /// load sheet data /// </summary> /// <param name="contentManager">the game contentmanager</param> /// <param name="xmlFile">the data file</param> /// <returns>error code, 0 if errorless</returns> public int Load(ContentManager contentManager, string xmlFile) { animationList = new Dictionary<string, Animation>(); XElement document = XElement.Load(Path.Combine(contentManager.RootDirectory, xmlFile) + ".xml"); int end = xmlFile.LastIndexOf('\\') + 1; string directory = xmlFile.Remove(end, xmlFile.Length - end); //this.texture = contentManager.Load<Texture2D>(directory + document.Name.LocalName); //textures foreach (XElement texture in document.Element("Textures").Elements()) { if (texture.Name.LocalName == "Texture") { Texture2D newTexture = contentManager.Load<Texture2D>(directory + texture.Attribute("name").Value); this.textures.Add(texture.Attribute("name").Value, newTexture); } } int x = 0, y = 0; double scale = 0.0, rate = 0.0; //mesh if (document.Element("Mesh") != null) { if (int.TryParse(document.Element("Mesh").Attribute("x").Value, out x) && int.TryParse(document.Element("Mesh").Attribute("y").Value, out y)) mesh = new Rectangle(x, y, textures.First().Value.Width / x, textures.First().Value.Height / y); } //framerate if (double.TryParse(document.Element("Framerate").Attribute("default").Value, out rate)) defaultFrameRate = (float)rate; //default scale if (double.TryParse(document.Element("Scale").Attribute("default").Value, out scale)) defaultScale = (float)scale; //size //if (int.TryParse(document.Element("Size").Attribute("x").Value, out x) && // int.TryParse(document.Element("Size").Attribute("y").Value, out y)) // defaultSpriteSize = new Vector2((float)x, (float)y); //animations foreach (XElement animation in document.Element("Animations").Elements()) { Animation newAnimation = new Animation(); newAnimation.frameList = new List<Frame>(); //frames foreach (XElement frame in animation.Elements("Frame")) { Frame newFrame = new Frame(); if (mesh != null) { //not null, we create as it were a solid mesh newFrame.coords.Width = mesh.Value.Width; newFrame.coords.Height = mesh.Value.Height; //coords if (int.TryParse(frame.Attribute("x").Value, out x)) newFrame.coords.X = x * newFrame.coords.Width; if (int.TryParse(frame.Attribute("y").Value, out y)) newFrame.coords.Y = y * newFrame.coords.Height; } else { //null, so we grab the manually defined values if (int.TryParse(animation.Attribute("width").Value, out x)) newFrame.coords.Width = x; if (int.TryParse(animation.Attribute("height").Value, out y)) newFrame.coords.Height = y; //coords, per-pixel if (int.TryParse(frame.Attribute("x").Value, out x)) newFrame.coords.X = x; if (int.TryParse(frame.Attribute("y").Value, out y)) newFrame.coords.Y = y; } //check if flip if(frame.Attribute("flip") != null) switch (frame.Attribute("flip").Value) { case "H": newFrame.flip = SpriteEffects.FlipHorizontally; break; case "V": newFrame.flip = SpriteEffects.FlipVertically; break; default: break; } newAnimation.frameList.Add(newFrame); } newAnimation.returnAnimation = animation.Element("End").Attribute("return").Value; newAnimation.name = animation.Name.LocalName; //add to list animationList.Add(animation.Name.LocalName, newAnimation); } //done return 0; }
/// <summary> /// update /// </summary> /// <param name="gametime"></param> public void Update(GameTime gametime) { //if empty, then no frame if (currentAnimation != null) { //time currentAnimatonTime += (float)gametime.ElapsedGameTime.TotalSeconds * frameRate; //if gone past animation life, switch to return if (currentAnimatonTime > currentAnimation.Value.frameList.Count) { //set to return SetAnimation(currentAnimation.Value.returnAnimation); if (currentAnimation == null) { //Call Animation End event if (OnAnimationEnd != null) OnAnimationEnd(); } } else { //get render frame from list currentFrame = currentAnimation.Value.frameList.ElementAt((int)currentAnimatonTime); } } }