diff --git a/lib/components/animation.dart b/lib/components/animation.dart new file mode 100644 index 000000000..7ff4e78c6 --- /dev/null +++ b/lib/components/animation.dart @@ -0,0 +1,37 @@ +import '../sprite.dart'; + +class Animation { + List sprites; + double stepTime = 0.1; + double lifeTime = 0.0; + + Animation() { + this.sprites = []; + } + + Animation.spriteList(this.sprites, {this.stepTime, this.lifeTime}); + + Animation.sequenced(String imagePath, int amount, + {double textureX = 0.0, + double textureY = 0.0, + double textureWidth = -1.0, + double textureHeight = -1.0}) { + sprites = new List(amount); + for (var i = 0; i < amount; i++) { + sprites[i] = new Sprite(imagePath, + x: textureX + i * textureWidth, + y: textureY, + width: textureWidth, + height: textureHeight); + } + } + + Sprite getSprite() { + int i = (lifeTime / stepTime).round(); + return sprites[i % sprites.length]; + } + + void update(double t) { + this.lifeTime += t; + } +} diff --git a/lib/components/animation_component.dart b/lib/components/animation_component.dart index 569eb1f91..5685f007c 100644 --- a/lib/components/animation_component.dart +++ b/lib/components/animation_component.dart @@ -1,41 +1,44 @@ import 'dart:ui'; import 'component.dart'; +import 'animation.dart'; import '../sprite.dart'; class AnimationComponent extends PositionComponent { - double width, height; + Animation animation; - List sprites; - double stepTime = 0.1; - double lifeTime = 0.0; + AnimationComponent(double width, double height, this.animation) { + this.width = width; + this.height = height; + } - AnimationComponent.spriteList(this.width, this.height, this.sprites, { this.stepTime, this.lifeTime }); + AnimationComponent.sequenced(width, height, String imagePath, int amount, { double textureX = 0.0, double textureY = 0.0, double textureWidth = -1.0, double textureHeight = -1.0}) { + this.width = width; + this.height = height; - AnimationComponent.sequenced(this.width, this.height, String imagePath, int amount, { double textureX = 0.0, double textureY = 0.0, double textureWidth = -1.0, double textureHeight = -1.0}) { if (textureWidth == -1) { textureWidth = this.width; } if (textureHeight == -1) { textureHeight = this.height; } - sprites = new List(amount); + + animation = new Animation(); + animation.sprites = new List(amount); for (var i = 0; i < amount; i++) { - sprites[i] = new Sprite(imagePath, x: textureX + i*textureWidth, y: textureY, width: textureWidth, height: textureHeight); + animation.sprites[i] = new Sprite(imagePath, x: textureX + i*textureWidth, y: textureY, width: textureWidth, height: textureHeight); } } @override void render(Canvas canvas) { prepareCanvas(canvas); - - int i = (lifeTime / stepTime).round(); - sprites[i % sprites.length].render(canvas, width, height); + animation.getSprite().render(canvas, width, height); } @override void update(double t) { - this.lifeTime += t; + animation.update(t); } } diff --git a/lib/components/component.dart b/lib/components/component.dart index 873b17650..f51db7e2e 100644 --- a/lib/components/component.dart +++ b/lib/components/component.dart @@ -22,6 +22,7 @@ abstract class Component { abstract class PositionComponent extends Component { double x = 0.0, y = 0.0, angle = 0.0; + double width = 0.0, height = 0.0; double angleBetween(PositionComponent c) { return (atan2(c.x - this.x, this.y - c.y) - PI / 2) % (2 * PI); @@ -33,12 +34,19 @@ abstract class PositionComponent extends Component { void prepareCanvas(Canvas canvas) { canvas.translate(x, y); - canvas.rotate(angle); // TODO: rotate around center + + // rotate around center + canvas.translate(width/2, height/2); + canvas.rotate(angle); + canvas.translate(-width/2, -height/2); + } + + Rect toRect() { + return new Rect.fromLTWH(x, y, width, height); } } class SpriteComponent extends PositionComponent { - double width, height; Sprite sprite; final Paint paint = new Paint()..color = new Color(0xffffffff); @@ -46,11 +54,13 @@ class SpriteComponent extends PositionComponent { SpriteComponent.square(double size, String imagePath) : this.rectangle(size, size, imagePath); - SpriteComponent.rectangle(this.width, this.height, String imagePath) { - this.sprite = new Sprite(imagePath); - } + SpriteComponent.rectangle(double width, double height, String imagePath) + : this.fromSprite(width, height, new Sprite(imagePath)); - SpriteComponent.fromSprite(this.width, this.height, this.sprite); + SpriteComponent.fromSprite(double width, double height, this.sprite) { + this.width = width; + this.height = height; + } @override render(Canvas canvas) {