private void loadFromTileSet() { foreach (TmxTileset tileset in tilesets) { SpriteSheetAnimations spriteSheetAnimation = new SpriteSheetAnimations(); spriteSheetAnimation.Width = tileset.TileWidth; spriteSheetAnimation.Height = tileset.TileHeight; spriteSheetAnimation.Margin = tileset.Margin; spriteSheetAnimation.Spacing = tileset.Spacing; foreach (KeyValuePair <int, TmxTilesetTile> tile in tileset.Tiles) { if (tile.Value.AnimationFrames.Count > 0) { AnimationTracker animationTracker = new AnimationTracker(spriteSheetAnimation); string name = "Unamed " + tile.Key; if (tile.Value.Properties.ContainsKey("Animation")) { name = tile.Value.Properties["Animation"]; } foreach (TmxAnimationFrame animationFrame in tile.Value.AnimationFrames) { int x = (int)(animationFrame.Id % tileset.Columns); int y = (int)Math.Floor(animationFrame.Id / (float)tileset.Columns); animationTracker.AddFrame(animationFrame.Duration / (float)1000, x, y); } animationData[name] = animationTracker; } } } }
private AnimationTracker getAnimation(Entity entity) { Animation animation = entity.GetComponent <Animation>(); if (animation.AnimationTracker.ContainsKey(animation.CurrentAnimation)) { return(animation.AnimationTracker[animation.CurrentAnimation]); } if (animationData.ContainsKey(animation.CurrentAnimation)) { AnimationTracker animationTracker = new AnimationTracker(animationData[animation.CurrentAnimation].Parent); animationTracker.Frames = animationData[animation.CurrentAnimation].Frames; animation.AnimationTracker[animation.CurrentAnimation] = animationTracker; return(animationTracker); } Debug.WriteLine($"Animation '{animation.CurrentAnimation}' was not found"); return(null); }
public void UpdateAnimations(GameTime gameTime) { foreach (Entity entity in Entities) { AnimationTracker current = getAnimation(entity); if (current != null) { Sprite sprite = entity.GetComponent <Sprite>(); current.TimeIntoAnimation += gameTime.ElapsedGameTime.TotalSeconds; if (current.TimeIntoAnimation > current.TotalDuration) { current.TimeIntoAnimation = 0; } current.FrameNumber = (int)Math.Floor(current.TimeIntoAnimation / current.FrameDuration); sprite.SpriteLocation = current.CurrentFrame; } } }