Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
Interesting...didn't think of making it a linked list. –  Setheron Aug 22 '11 at 13:34
    
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? –  Setheron Aug 22 '11 at 13:35
    
@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. –  James Aug 22 '11 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.

share|improve this answer
1  
+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. –  michael.bartnett Aug 22 '11 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.

share|improve this answer
1  
Late answers are perfectly acceptable. There are even some badges to encourage late answers. –  doppelgreener Apr 30 '12 at 22:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.