update method

dynamic update(
  1. dynamic delta
)

Implementation

update(delta) {
  var currentColumn, currentRow, complete = [], x, animation;

  for (x = 0; animation; 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 =
          Math.floor(animation.currentTile / animation.tilesHorizontal);

      // 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 (x = 0; animation; animation = complete[x++]) {
      if (animation.looped >= animation.repeat) {
        free(animation);
      }
    }
  }
}