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

@ -0,0 +1,37 @@
import '../sprite.dart';
class Animation {
List<Sprite> 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<Sprite>(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;
}
}

View File

@ -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<Sprite> 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<Sprite>(amount);
animation = new Animation();
animation.sprites = new List<Sprite>(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);
}
}

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