Adding SpriteSheet class

This commit is contained in:
erickzanardo
2019-07-05 14:56:26 -03:00
committed by Erick
parent e6bf525753
commit c929c35e2c

48
lib/spritesheet.dart Normal file
View File

@ -0,0 +1,48 @@
import 'sprite.dart';
import 'animation.dart';
class SpriteSheet {
String imageName;
int textureWidth;
int textureHeight;
int columns;
int rows;
List<List<Sprite>> _sprites;
SpriteSheet({ this.imageName, this.textureWidth, this.textureHeight, this.columns, this.rows }) {
_sprites = List.generate(rows, (y) => List.generate(columns, (x) =>
Sprite(
imageName,
x: (x * textureWidth).toDouble(),
y: (y * textureHeight).toDouble(),
width: textureWidth.toDouble(),
height: textureHeight.toDouble()
))
);
}
Sprite getSprite(int row, int column) => _sprites[row][column];
/// Creates an animation from this SpriteSheet
///
/// An [from] and a [to] parameter can be specified to create an animation from a subset of the columns on the row
Animation createAnimation(int row, { double stepTime, loop = true, int from = 0, int to }) {
List<Sprite> spriteRow = _sprites[row];
if (spriteRow != null) {
to ??= spriteRow.length;
final spriteList = spriteRow.sublist(from, to);
return Animation.spriteList(
spriteList,
stepTime: stepTime,
loop: loop,
);
}
return null;
}
}