Skip to content

Commit

Permalink
feat: Add range constructor on SpriteAnimationData (#1572)
Browse files Browse the repository at this point in the history
  • Loading branch information
oloshe authored Apr 25, 2022
1 parent b99e351 commit e42b495
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/flame/lib/src/sprite_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,37 @@ class SpriteAnimationData {
});
}

/// Specifies the range of the sprite grid.
///
/// Make sure your sprites are placed left-to-right and top-to-bottom
SpriteAnimationData.range({
required int start,
required int end,
required int amount,
required List<double> stepTimes,
required Vector2 textureSize,
int? amountPerRow,
Vector2? texturePosition,
this.loop = true,
}) : assert(amountPerRow == null || amount >= amountPerRow),
assert(start <= end && start >= 0 && end <= amount),
assert(stepTimes.length == end - start) {
amountPerRow ??= amount;
texturePosition ??= Vector2.zero();
frames = List<SpriteAnimationFrameData>.generate(end - start, (index) {
final i = index + start;
final position = Vector2(
texturePosition!.x + (i % amountPerRow!) * textureSize.x,
texturePosition.y + (i ~/ amountPerRow) * textureSize.y,
);
return SpriteAnimationFrameData(
stepTime: stepTimes[i],
srcPosition: position,
srcSize: textureSize,
);
});
}

/// Works just like [SpriteAnimationData.variable] but uses the same
/// [stepTime] for all frames.
factory SpriteAnimationData.sequenced({
Expand Down

0 comments on commit e42b495

Please sign in to comment.