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: