extracting animation from component, fix rotate around center bug, add width and height to PositionComponent

This commit is contained in:
Luan Nico
2017-12-24 16:16:29 -02:00
parent 050c2658ba
commit b7e127ac11
3 changed files with 68 additions and 18 deletions

View File

@ -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) {