Start v1.0 apis (sprite, animation, box2d, etc)

This commit is contained in:
Luan Nico
2017-12-02 11:17:35 -02:00
committed by feroult
parent f5442c3e4a
commit 2fa22f2f7c
7 changed files with 187 additions and 72 deletions

View File

@ -0,0 +1,69 @@
import 'dart:math';
import 'dart:ui';
import 'package:flame/sprite.dart';
import 'package:flutter/painting.dart';
abstract class Component {
void update(double t);
void render(Canvas c);
bool loaded() {
return true;
}
bool destroy() {
return false;
}
}
abstract class PositionComponent extends Component {
double x = 0.0, y = 0.0, angle = 0.0;
double angleBetween(PositionComponent c) {
return (atan2(c.x - this.x, this.y - c.y) - PI / 2) % (2 * PI);
}
double distance(PositionComponent c) {
return sqrt(pow(this.y - c.y, 2) + pow(this.x - c.x, 2));
}
void prepareCanvas(Canvas canvas) {
canvas.translate(x, y);
canvas.rotate(angle); // TODO: rotate around center
}
}
class SpriteComponent extends PositionComponent {
double width, height;
Sprite sprite;
final Paint paint = new Paint()..color = new Color(0xffffffff);
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.fromSprite(this.width, this.height, this.sprite);
@override
render(Canvas canvas) {
if (sprite.loaded()) {
prepareCanvas(canvas);
sprite.render(canvas, width, height);
}
}
@override
bool loaded() {
return this.sprite.loaded();
}
@override
void update(double t) {
}
}