Entity animation in MVC style program

Hi guys,

I’ve been working hard on splitting my game up into a more manageable format and MVC seemed the best fit for my needs (so far at least!).

I’m having a hard time getting my head around drawing entities to the screen and then animating their actions whilst at the same time keeping them separate from the Model and Controller.

I should note that this is a java 2d game using drawImage(…) with BufferedImage.

At the moment my code is split like so:
Engine - holds the game loop and calls Controller.update() followed by Renderer.render().
Controller - stores all the entity objects, loops over them during update() and calls their own update() methods.
Renderer - draws the game to the screen.

Renderer at the moment uses an ImageManager class which it passes an entity reference to get an appropriate sprite to draw to the screen:

public static BufferedImage getSpriteForEntity(DrawingEntity entity, ImageManager im) {

if (entity instanceof Player) {

  return im.getSprite(ImageManager.PLAYER_SPRITES, ((Player)entity).getFacingDirection(), 0);

}

if (entity instanceof TrainingDummy) {

  return im.getSprite(ImageManager.TRAINING_DUMMY_SPRITES, 0, 0);

}

// ... etc

I got some working animation code by stuffing some extra variables into the individual Model code but that’s now how it’s supposed to be done, the entity shouldn’t have any idea that it may be animated somewhere:


public class Player implements Entity, ControllableEntity, DrawingEntity {

  private double animationTimer, animationLength = 800, animationFrameCount = 8;

  public updateAnimationState(long delta) {

    animationTimer += (delta/1000);
    if (animationTimer >= animationLength) {
      animationTimer = 0;
    }

  }

  public byte getAnimationState() {

    //  returns a whole number between 1 and animationFrameCount
    return (byte)Math.floor(animationTimer / (animationLength / animationFrameCount))+1;

  }

}

I’m kind of at a loss for ideas on how to keep the animation code separate from the Model code whilst at the same time not duplicating work by maintaining a separate list of entities with individual animation states and then needing to keep those lists synchronised somehow so the View doesn’t try and show an entity that doesn’t exist or (much worse) doesn’t show an entity that exists.

Any ideas much appreciated on this one guys and thanks for all the help you’ve given me so far since I’ve been here :slight_smile:

See Super Spineboy:

Character (the model) has state and stateTime:


The state determines the animation. The stateTime is how long the character has been in that state. This is all you need for the view to be able to animate.

That should be enough to get you going, but if you want to see how Super Spineboy does it, every frame the view calls CharacterView#setAnimation:


If the animation has changed, Character#setAnimation changes the animation in AnimationState.

AnimationState is part of Spine and keeps track of animations, their time, cross fading when animations change, etc. You may not need this, in a simpler system your renderer would just draw the animation for the current state, using stateTime to determine the animation frame.

A couple more notes on how Super Spineboy uses MVC. It is done loosely, you should feel free to break the rules where it makes things easier. The controller is here:


It does very little.

The model objects have a field for the view but it is never used in the model, it is just for convenience to avoid a model -> view object mapping. There are a few events that the model tells the controller about via a method. I figured proper events would be overkill. Reaction to the events could have gone in the view, but it was easier doing it there.

Awesome, looking over your code there it looks like we’re on the same page (except your version is far more advanced than mine!).

[quote]“break the rules where it makes things easier”
[/quote]
isn’t this how we get into big problems? :wink:

Just make sure you break the rules only at the exact right places. :slight_smile:

Someone playing your game doesn’t care about MVC, all they care about is that it works. MVC is for you. If it is making things difficult, cut corners so it doesn’t. Eg, put a view object in the model so you don’t have to map model to view objects, since you are unlikely to have multiple views.

A-ha! That’s exactly what I am likely to have!

I’m starting out with Java2d, but I’m more than expecting to move up to LibGDX or Slick to gain knowledge of those and I would even like to try out creating a 3d view as well at some point.

But these things are a long way off so the code will probably all be completely different by then :slight_smile:

Ooh, a thought just struck me. I could make an Animatable interface and include a reference in a model to that then the views can map their own objects that implement that to each model?

Awesome, I created an Animation class which sets up animations to play then I attach the animations to the Entity and “play” them from the View. The problems I have now are how to attach the animations from the View each frame but I’m sure I’ll crack it soon enough.

I made a little screen capture of the animations at work!

The frame rate is a bit low, but I’m working on that lol