SpriteSheetAnimator is a shared content object
The current design of SpriteSheetAnimator is causing the animator to be shared across any instances accessed from the content pipeline. This prevents different objects trying to use the same SpriteSheetAnimator content but with different animations/states/etc.
Should refactor so that you retrieve a content based object from the content pipeline, which you can then instantiate an SpriteSheetAnimator instance so that state can be isolated to different instances.
This is how I'm working around the problem right now:
public static SpriteSheetAnimator Clone(this SpriteSheetAnimator obj)
{
var regions = obj.Frames;
var sprite = new Sprite(regions.First());
var newAnimator = new SpriteSheetAnimator(sprite, regions);
var anType = typeof(SpriteSheetAnimator);
var animationField = anType.GetField("_animations", BindingFlags.Instance | BindingFlags.NonPublic);
if (animationField != null)
{
var animations = animationField.GetValue(obj) as Dictionary<string, SpriteSheetAnimation>;
foreach(var a in animations.Values)
{
newAnimator.AddAnimation(a.Name, a.FramesPerSecond, a.FrameIndicies);
}
}
return newAnimator;
}
then to create an animator for each object:
var masterCopy = contentManager.Load<SpriteSheetAnimator>(AvatarTexture);
_sprite = masterCopy.Clone();
Yes, I agree this is a bug. I actually ran across it myself a couple of weeks ago and started working on it but forgot to raise an issue and completely forgot about it. My bad. Thanks for raising the issue.
I guess it should work in much the same way you'd load a texture and render multiple sprites with it.
var texture = Content.Load<Texture2D>("texture");
var sprite1 = new Sprite(texture);
var sprite2 = new Sprite(texture);
So what's the equivalent for sprite sheet animations? So far I've got something like this:
_explosionAnimations = Content.Load<SpriteSheetAnimationGroup>("explosion-animations");
_animator1 = new SpriteSheetAnimator(_explosionAnimations);
_animator1 = new SpriteSheetAnimator(_explosionAnimations);
Something still doesn't feel right about this. Any thoughts?
I think a good name for the content itself would be SpriteSheetAnimation or SpriteAnimation. Other than that, I like it.
The way it currently works is that there's multiple animations per "content" file. For example:
{
"TextureAtlas": "zombie-atlas.json",
"Animations": [
{
"Name": "appear",
"FramesPerSecond": 16,
"Frames": [ "appear_01", "appear_02", "appear_03", "appear_04", "appear_05", "appear_06", "appear_07", "appear_08", "appear_09", "appear_10", "appear_11" ]
},
{
"Name": "die",
"FramesPerSecond": 12,
"Frames": [ "die_01", "die_02", "die_03", "die_04", "die_05", "die_06", "die_07", "die_08" ]
},
{
"Name": "walk",
"FramesPerSecond": 12,
"Frames": [ "go_01", "go_02", "go_03", "go_04", "go_05", "go_06", "go_07", "go_08", "go_09", "go_10" ]
}
]
}
The sprites are packed into a texture atlas and the animations are lists of "frames" within that sprite sheet.
I agree that the word Group feels a little wrong but calling the content SpriteSheetAnimation is misleading. It's not just one animation.
Separate it out? Decouple the Animation from the SpriteSheet? The SpriteSheet could just hold the frames used per texture and the Animation would copy that information when it needs it?
Separate it out? Decouple the Animation from the 'SpriteSheet`?
How do you propose we do that?
Hmm. I'm going to think about this from the bottom up. The Sprite needs a Texture2D and a source Rectangle to render with a SpriteBatch. It looks like TextureAtlas is just a map of source Rectangles by index and String names. So maybe copy the required source Rectangles from the TextureAtlas to the SpriteAnimation when it is loaded. The SpriteAnimator could then copy the source Rectangle of the current frame of the SpriteAnimation when it plays?
EDIT: I just noticed that the name of the classes I mentioned are not quite doing what I thought. I thought a SpriteAnimator would take a Sprite and an SpriteAnimation and do it's thing, but I see that's not quite exactly what's going on.
EDIT2: I guess what I'm trying to say is I thought SpriteAnimation to be a shared resource which you could just use Content.Load<SpriteAnimation>("myAnimationName"); to get it from the content cache and SpriteAnimator to be created as a new instance for each Sprite.
I like where you're going with this but I think we still need to support storing multiple animations in single content file. Maybe something more like this:
var animationCollection = Content.Load<SpriteSheetAnimationCollection>("zomie-animations");
var walkAnimation = animationCollection.Get("walk");
var animator = new SpriteAnimator(walkAnimation);
If we did it the other way, you'd end up with lots of files in your content but for performance reasons you'd still want them all pointing at the same texture atlas.
- zombie-texture-atlas.png
- zombie-texture-atlas-data.json
- zombie-walk.animation
- zombie-die.animation
- zombie-appear.animation
I guess this could work. It may even have some advantages but it seems like it could make updating and managing them more difficult.
An annoying side note: you can't have two content files with the same name and different extensions in XNA / MonoGame. This is why I've named the 2 texture atlas files differently.
Please don't limit your SpriteAnimator to a single animation. Think about how this is going to be used in development.
You're going to attach an animation controller to your render object. You want that animation controller to play different animations (walk/run/jump/etc). The AnimationCollection has that right now, and making the SpriteAniamtor the controller into different animations is the logical step.
Also, please add stop/pause/restart interfaces. The only way I can stop an animation right now is to stop calling update (storing my own flag).
One more bug: You are duplicating your frames within the Animator right now. It's initialized with the frames of the atlas, and then you loop through and add each frame again. Small bug.
I guess this could work. It may even have some advantages but it seems like it could make updating and managing them more difficult.
@craftworkgames Okay yes I see. It's definitely better to have one file where some or all the sprite animations reside so they can be logically grouped. Maybe have a SpriteAnimation and a SpriteAnimationCollection for that purpose? I don't think the SpriteAnimator should know whether some SpriteAnimations are grouped into a SpriteAnimationCollection. I think the SpriteAnimator should be happy to play, pause, stop, change, etc any SpriteAnimation at any time.
Please don't limit your SpriteAnimator to a single animation. Think about how this is going to be used in development.
@whilke The SpriteAnimator should of course be able to change the SpriteAnimation being played at any time.
Think about how this is going to be used in development.
Yes, I think that's the key point here. We need to design it so it fits the purpose. I've already seen examples of where it's used with multiple animations so it's certain that we need to support that.
@whilke I won't get to this for a while, if you want to take a look at it I can create a branch and you can have a go at fixing it up the way you think it should be?
The branch is called sprite-sheet-animations. If you don't get to it I'll add it to my to-do list.
Okay so I'm finally around to refactoring this. I'm giving the whole thing a bit of an overhaul in the develop branch right now.
The old code was quite horrible. I don't know what I was thinking at the time. I'm going to open it up a lot more so animations can be created in code as well as being loaded from a file.
Any other thoughts on this before I finish it?