public void AddAnimation(string animationKey, string textureName, CelRange celRange, int celWidth, int celHeight, int numberOfCels, int framesPerSecond) { CelAnimation ca = new CelAnimation(textureName, celRange, framesPerSecond); if (!textures.ContainsKey(textureName)) { textures.Add(textureName, content.Load <Texture2D>(contentPath + textureName)); } ca.CelWidth = celWidth; ca.CelHeight = celHeight; ca.NumberOfCels = numberOfCels; ca.CelsPerRow = textures[textureName].Width / celWidth; if (animations.ContainsKey(animationKey)) { animations[animationKey] = ca; } else { animations.Add(animationKey, ca); } }
public void DrawBottomCenter(float elapsedTime, string animationKey, SpriteBatch batch, int frame, Vector2 position, Color color) { if (!animations.ContainsKey(animationKey)) { return; } CelAnimation ca = animations[animationKey]; //first get our x increase amount (add our offset-1 to our current frame) int xincrease = (ca.Frame + ca.CelRange.FirstCelX - 1); //now we need to wrap the value so it will loop to the next row int xwrapped = xincrease % ca.CelsPerRow; //finally we need to take the product of our wrapped value and a cel's width int x = xwrapped * ca.CelWidth; //to determine how much we should increase y, we need to look at how much we //increased x and do an integer divide int yincrease = xincrease / ca.CelsPerRow; //now we can take this increase and add it to our Y offset-1 and multiply the sum by //our cel height int y = (yincrease + ca.CelRange.FirstCelY - 1) * ca.CelHeight; Rectangle cel = new Rectangle(x, y, ca.CelWidth, ca.CelHeight); Vector2 orgin = new Vector2((ca.CelWidth / 2), (ca.CelHeight)); batch.Draw(textures[ca.TextureName], position, cel, color, 0.0f, orgin, 1.0f, SpriteEffects.None, 0f); //batch.Draw(textures[ca.TextureName], position, cel, color); }
public Rectangle GetCurrentDrawRect(float elapsedTime, string animationKey, float scale) { if (!animations.ContainsKey(animationKey)) { throw new Exception("animationKey not found"); } CelAnimation ca = animations[animationKey]; //first get our x increase amount (add our offset-1 to our current frame) int xincrease = (ca.Frame + ca.CelRange.FirstCelX - 1); //now we need to wrap the value so it will loop to the next row int xwrapped = xincrease % ca.CelsPerRow; //finally we need to take the product of our wrapped value and a cel's width int x = xwrapped * ca.CelWidth; //to determine how much we should increase y, we need to look at how much we //increased x and do an integer divide int yincrease = xincrease / ca.CelsPerRow; //now we can take this increase and add it to our Y offset-1 and multiply the sum by //our cel height int y = (yincrease + ca.CelRange.FirstCelY - 1) * ca.CelHeight; Rectangle cel = new Rectangle(x, y, (int)(ca.CelWidth), (int)(ca.CelHeight)); return(cel); }
public override void Update(GameTime gameTime) { foreach (KeyValuePair <string, CelAnimation> animation in animations) { CelAnimation ca = animation.Value; if (ca.Paused) { continue; //no need to update this animation, check next one } ca.TotalElapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds; if (ca.TotalElapsedTime > ca.TimePerFrame) { ca.Frame++; //min: 0, max: total cels if (ca.Frame >= ca.NumberOfCels) { ca.LoopCount++; } ca.Frame = ca.Frame % (ca.NumberOfCels); //reset our timer ca.TotalElapsedTime -= ca.TimePerFrame; } } base.Update(gameTime); }
public void Draw(float elapsedTime, string animationKey, SpriteBatch batch, int frame, Vector2 position, Color color) { if (!animations.ContainsKey(animationKey)) { return; } CelAnimation ca = animations[animationKey]; Rectangle cel = this.GetCurrentDrawRect(elapsedTime, animationKey); //Vector2 orgin = new Vector2((ca.CelWidth / 2), (ca.CelHeight / 2)); //batch.Draw(textures[ca.TextureName], position, cel, color, 0.0f, orgin, 1.0f, SpriteEffects.None, 0f); batch.Draw(textures[ca.TextureName], position, cel, color); }
/// <summary> /// Get the current loop count for and aniamtion /// </summary> /// <param name="animationKey">Name of animation</param> /// <returns></returns> public int GetLoopCount(string animationKey) { CelAnimation ca = animations[animationKey]; return(ca.LoopCount); }