public void UpdateAnimations(GameTime gameTime) { foreach (Entity entity in Entities) { Animation animation = entity.GetComponent <Animation>(); if (animation.Enabled) { if (animation.Playing) { animation.CurrentAnimation = animation.PlayeringAnimation; } AnimationTracker current = getAnimation(entity); if (current != null) { Sprite sprite = entity.GetComponent <Sprite>(); current.TimeIntoAnimation += gameTime.ElapsedGameTime.TotalSeconds; if (current.TimeIntoAnimation >= current.FrameDuration) { current.FrameNumber++; current.TimeIntoAnimation = 0; if (current.FrameNumber == current.Frames.Count) { current.FrameNumber = 0; } setCurrentCollisions(animation, entity, current); } sprite.SpriteLocation = current.CurrentFrame; if (current.FrameNumber == current.Frames.Count - 1 && animation.Playing) { animation.Playing = false; current.FrameNumber = 0; } } } } }
private void loadFromTileSet(Tileset tileset) { for (int i = 0; i < tileset.Count; i++) { SpriteSheetAnimations spriteSheetAnimation = new SpriteSheetAnimations(); spriteSheetAnimation.Width = tileset[i].Width; spriteSheetAnimation.Height = tileset[i].Height; spriteSheetAnimation.Margin = tileset[i].Margin; spriteSheetAnimation.Spacing = tileset[i].Spacing; AnimationTracker animationTracker = new AnimationTracker(spriteSheetAnimation); string name = tileset[i].Animation.Name; foreach (TilesetFrame frame in tileset[i].Animation.Frames) { Dictionary <string, BoxCollision> colliders = new Dictionary <string, BoxCollision>(); foreach (KeyValuePair <string, List <BoxCol> > collider in tileset[frame.Id].BoxColliders) { BoxCollision collision = new BoxCollision(); collision.World = SceneManager.GetCurrentScene().World; for (int j = 0; j < collider.Value.Count; j++) { BoxData box = new BoxData(); box.Position = new Vector(collider.Value[j].Rectangle.X / (float)tileset[frame.Id].Width, collider.Value[j].Rectangle.Y / (float)tileset[frame.Id].Height); box.Scale = new Vector(collider.Value[j].Rectangle.Width / (float)tileset[frame.Id].Width, collider.Value[j].Rectangle.Height / (float)tileset[frame.Id].Height); box.TriggerOnly = collider.Value[j].TriggerOnly; IBox ibox = collision.World.Create(0, 0, box.Scale.X, box.Scale.Y).AddTags(box.Layer); ibox.Data = box; collision.World.Remove(ibox); collision.Boxes.Add(ibox); } colliders.Add(collider.Key, collision); } animationTracker.AddFrame(frame.Duration, frame.Source.X, frame.Source.Y, colliders); } animationData[name] = animationTracker; } }
private void setCurrentCollisions(Animation animation, Entity entity, AnimationTracker current) { animation.CurrentCollisions = new Dictionary <string, BoxCollision>(); foreach (KeyValuePair <string, BoxCollision> pair in current.Frames[current.FrameNumber].Colliders) { if (pair.Key == "Collision") { BoxCollision boxCollision = entity.GetComponent <BoxCollision>(); Componets.Transform transform = entity.GetComponent <Componets.Transform>(); List <IBox> newBoxes = new List <IBox>(); for (int i = 0; i < pair.Value.Boxes.Count; i++) { BoxData boxData = (BoxData)pair.Value.Boxes[i].Data; IBox newIbox = boxCollision.World.Create(transform.Position.X + transform.Scale.X * boxData.Position.X, transform.Position.Y + transform.Scale.Y * boxData.Position.Y, transform.Scale.X * boxData.Scale.X, transform.Scale.Y * boxData.Scale.Y).AddTags(boxData.Layer); newIbox.Data = boxData; if (boxCollision.Boxes.Count > i) { boxCollision.World.Remove(boxCollision.Boxes[i]); boxCollision.Boxes.Remove(boxCollision.Boxes[i]); } newBoxes.Add(newIbox); } boxCollision.Boxes = newBoxes; } else { animation.CurrentCollisions.Add(pair.Key, pair.Value); } } }
private AnimationTracker getAnimation(Entity entity) { Animation animation = entity.GetComponent <Animation>(); if (animation.AnimationTracker.ContainsKey(animation.CurrentAnimation)) { animation.AnimationScale = new Vector(animation.AnimationTracker[animation.CurrentAnimation].Parent.Width, animation.AnimationTracker[animation.CurrentAnimation].Parent.Height); 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; animation.AnimationScale = new Vector(animationTracker.Parent.Width, animationTracker.Parent.Height); return(animationTracker); } //Debug.WriteLine($"Animation '{animation.CurrentAnimation}' was not found for entity '{entity.Name}'"); return(null); }