I've been reading about Entity Component design and thought it's pretty neat. I've been trying to write a quick 2D engine in XNA.

I think I've laid the proper groundwork for registering and updating entities, since I've created some "PositionComponents" that seem to work with my Camera Entity (an Entity that simply owns ZoomableComponent, PositionComponent, MouseInputComponent).

All the tutorials seem to however seem to end at this stage of design. I'm pretty lose now on how I would like to design my animation/sprite components.

  • I know I want some sort of 'RenderableComponent' which you assign it a Texture
  • I need some sort of FSM (Finite State Machine) to hold Entity States
  • I need some sort of Animation Manager.

Does anyone have any tips?


3 Answers 3


Ok, this is just a quick End of Week brain dump

// IRenderable is the interface for your renderable components
public class CmpAnimation : IRenderable
  // Updates the animation, Inherited from IRenderable
  public bool Update(float ElapsedTime);
  // Dumps your data into a render stream or whatever you do to render, Inherited from IRenderable
  public bool Render();

  // Called from Update when our current frames is 
  public bool OnAnimationComplete();> our End Frame value

  // Some way to load the animation data, either the sprite sheet or a keyframe data file
  public bool LoadAnimation(string AnimationName);

  // The animation to go to once we are done with this one
  public CmpAnimation NextLinkedAnimation { get; set; }

  // Loaded values that you may want to change or access at run time
  // The rate at which the animation plays
  public float Rate { get; set; }
  // The frame the animation starts on
  public int BeginFrame { get; set; }
  // The fame the animation ends on
  public int EndFrame { get; set; }
  // Does this animation loop until stopped by some external thing?
  public bool CanLoop { get; set; }

  // Informational values for internal or external use
  public bool IsAtEnd { get; }
  public bool IsAtStart { get; }
  public bool IsPlaying { get; }

Basically Update() updates its internal state so advance the animation to where it should be for its time stamp, if it reaches the end it should then go to the next animation (mark it as active, deactivate this one.. however you want to do that part based on your component systems flow).

This lets you put your FSM in to data, the animations know where they exist within the data they have loaded (Good for having a single 'Jump' animation but where you want the jump to have 'Jump', 'Jumping', 'Falling' and 'Landing' at different key frame indexes within that data).

Again, end of the week brain dump to help get you going in the direction you want to go in :)

I hope this helps.

  • \$\begingroup\$ Interesting...didn't think of making it a linked list. \$\endgroup\$
    – Setheron
    Aug 22, 2011 at 13:34
  • \$\begingroup\$ My question is more on the dependancy of animations with character state. It's seems that there will be a tight coupling between a character state component and the animation component in order to trigger animation changes? \$\endgroup\$
    – Setheron
    Aug 22, 2011 at 13:35
  • \$\begingroup\$ @Setheron Its not a linked list per say, its the FSM you were mentioning in that each state knows which state to go to next. And yes, you would need some sort of way to kick off an animation sequence based, presumably, by the character state, this can either be done directly or though a messaging system, depending on what you have designed. \$\endgroup\$
    – James
    Aug 22, 2011 at 16:46

Let me stop you there before going any further with your implementation and suggest you look at the awesome Artemis library. I recently discovered this and I think I have found the light! :P

There is a port for C# which I use for my XNA game, and there are examples there to clear many things.

Definitely have a look at it!

Basically, in Artemis, Components are plain data and you use Systems to manipulate the data (which make it also easy to have your game data-driven).

So, for example, a TextureComponent would have an array of Texture2Ds, and then you would have a TextureSystem that will render them to screen.

  • 1
    \$\begingroup\$ +1 for the link to Artemis (the article they link to rocks). I've poked around with my own entity component system in XNA and used the awkward dependency resolution method. Never thought of treating the components as just data buckets. \$\endgroup\$ Aug 22, 2011 at 4:46

Artemis uses Components to store data and EntitySystems to manipulate them but C# already comes interfaces so like James said use a IRenderable interface for entities that you want to have rendered, IPositionable interface for entities that need to be moved, IColorable interface for coloring entities, etc etc. How about a IConfigurable interface that lets you set a Dictionary of properties/variables for a entity?

The only downside to this method, that I found, is that if you wanted to extend functionality on to an Entity you would have to create a new class inheriting IRenderable or IPositionable. Hopefully in C# 5 they will make available anonymous classes. Wouldn't recommend using NET 4.0 Dynamic because it is costly.

The upside is that you could store Entity references by their interface like list of IRenderables and then write a system that processes all IRenderables in your EntityManager. In a MovementSystem process all IPositionable based on their distance from the player. I think this method would outperform Artemis.

It is also a lot easier to write a de/serializer using this method then it is for Artemis.

  • 1
    \$\begingroup\$ Late answers are perfectly acceptable. There are even some badges to encourage late answers. \$\endgroup\$ Apr 30, 2012 at 22:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .