update method

void update(
  1. int delta
)

Implementation

void update(int delta) {
  int currentColumn, currentRow;
  List<SpriteAnimation> complete = [];

  for (int x = 0; x < animations.length;x++) {
    final animation = animations[x];
    animation.duration += delta;

    // Have we gone longer than the duration of this tile? Show the
    // next one
    if (animation.duration > 1 / animation.fps) {
      // Advance this animation to the next tile
      animation.currentTile =
          (animation.currentTile + 1) % animation.numberOfTiles;

      // Calcualte the new column and row
      currentColumn = animation.currentTile % animation.tilesHorizontal;
      currentRow = (animation.currentTile / animation.tilesHorizontal).floor();

      // Calculate the texture offset. The y was found through trial
      // and error and I have no idea why it works
      animation.texture.offset.x = currentColumn / animation.tilesHorizontal;
      animation.texture.offset.y = 1 -
          (1 / animation.tilesHorizontal) -
          (currentRow / animation.tilesVertical);

      animation.duration = 0;

      // If we're on the last frame (currentTile is 0 indexed), keep
      // track of this one for later
      if (animation.currentTile == animation.numberOfTiles - 1) {
        animation.looped++;
        complete.add(animation);
      }
    }
  }

  // Go over all completed animations. If we exceed our looping quota,
  // free it
  if (complete.isNotEmpty) {
    for (int x = 0; x < complete.length;x++) {
      final animation = complete[x];
      if (animation.looped >= animation.repeat) {
        free(animation);
      }
    }
  }
}