From e42b495805efd2e969cfe412b069ffcc6e828ad6 Mon Sep 17 00:00:00 2001 From: oloshe <40467840+oloshe@users.noreply.github.com> Date: Tue, 26 Apr 2022 06:04:13 +0800 Subject: [PATCH] feat: Add range constructor on SpriteAnimationData (#1572) --- packages/flame/lib/src/sprite_animation.dart | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/flame/lib/src/sprite_animation.dart b/packages/flame/lib/src/sprite_animation.dart index fd3c0a8259f..f4ed3cac47f 100644 --- a/packages/flame/lib/src/sprite_animation.dart +++ b/packages/flame/lib/src/sprite_animation.dart @@ -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 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.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({