fix some apis

This commit is contained in:
Luan Nico
2018-04-15 10:00:18 -03:00
parent 6871b2993a
commit 879b358ad3
7 changed files with 42 additions and 30 deletions

View File

@ -29,20 +29,20 @@ abstract class PositionComponent extends Component {
double x = 0.0, y = 0.0, angle = 0.0; double x = 0.0, y = 0.0, angle = 0.0;
double width = 0.0, height = 0.0; double width = 0.0, height = 0.0;
Position get position => new Position(x, y); Position toPosition() => new Position(x, y);
void set position(Position position) { void setByPosition(Position position) {
this.x = position.x; this.x = position.x;
this.y = position.y; this.y = position.y;
} }
Position get size => new Position(width, height); Position toSize() => new Position(width, height);
void set size(Position size) { void setBySize(Position size) {
this.width = size.x; this.width = size.x;
this.height = size.y; this.height = size.y;
} }
Rect get rect => new Rect.fromLTWH(x, y, width, height); Rect toRect() => new Rect.fromLTWH(x, y, width, height);
void set rect(Rect rect) { void setByRect(Rect rect) {
this.x = rect.left; this.x = rect.left;
this.y = rect.top; this.y = rect.top;
this.width = rect.width; this.width = rect.width;
@ -50,7 +50,7 @@ abstract class PositionComponent extends Component {
} }
double angleBetween(PositionComponent c) { double angleBetween(PositionComponent c) {
return (atan2(c.x - this.x, this.y - c.y) - PI / 2) % (2 * PI); return (atan2(c.x - this.x, this.y - c.y) - pi / 2) % (2 * pi);
} }
double distance(PositionComponent c) { double distance(PositionComponent c) {

View File

@ -45,7 +45,7 @@ class GameRenderBox extends RenderBox {
int _frameCallbackId; int _frameCallbackId;
Duration previous = Duration.ZERO; Duration previous = Duration.zero;
GameRenderBox(this.context, this.game); GameRenderBox(this.context, this.game);
@ -91,11 +91,11 @@ class GameRenderBox extends RenderBox {
double _computeDeltaT(Duration now) { double _computeDeltaT(Duration now) {
Duration delta = now - previous; Duration delta = now - previous;
if (previous == Duration.ZERO) { if (previous == Duration.zero) {
delta = Duration.ZERO; delta = Duration.zero;
} }
previous = now; previous = now;
return delta.inMicroseconds / Duration.MICROSECONDS_PER_SECOND; return delta.inMicroseconds / Duration.microsecondsPerSecond;
} }
@override @override

View File

@ -82,7 +82,7 @@ class Position {
@override @override
String toString() { String toString() {
return "($x, $y)"; return '($x, $y)';
} }
static ui.Rect rectFrom(Position topLeft, Position size) { static ui.Rect rectFrom(Position topLeft, Position size) {

View File

@ -1,4 +1,5 @@
import 'dart:ui'; import 'dart:ui';
import 'dart:async';
import 'package:flame/position.dart'; import 'package:flame/position.dart';
import 'package:flame/flame.dart'; import 'package:flame/flame.dart';
@ -10,10 +11,6 @@ class Sprite {
static final Paint paint = new Paint()..color = Colors.white; static final Paint paint = new Paint()..color = Colors.white;
Sprite.fromImage(this.image) {
this.src = new Rect.fromLTWH(0.0, 0.0, image.width.toDouble(), image.height.toDouble());
}
Sprite(String fileName, {double x = 0.0, double y = 0.0, double width = -1.0, double height = -1.0}) { Sprite(String fileName, {double x = 0.0, double y = 0.0, double width = -1.0, double height = -1.0}) {
Flame.images.load(fileName).then((img) { Flame.images.load(fileName).then((img) {
if (width == -1.0) { if (width == -1.0) {
@ -27,6 +24,21 @@ class Sprite {
}); });
} }
Sprite.fromImage(this.image, {double x = 0.0, double y = 0.0, double width = -1.0, double height = -1.0}) {
if (width == -1.0) {
width = image.width.toDouble();
}
if (height == -1.0) {
height = image.height.toDouble();
}
this.src = new Rect.fromLTWH(x, y, width, height);
}
static Future<Sprite> loadSprite(String fileName, {double x = 0.0, double y = 0.0, double width = -1.0, double height = -1.0}) async {
Image image = await Flame.images.load(fileName);
return new Sprite.fromImage(image, x: x, y: y, width: width, height: height);
}
bool loaded() { bool loaded() {
return image != null && src != null; return image != null && src != null;
} }

View File

@ -11,11 +11,11 @@ dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
audioplayers: ^0.4.1 audioplayers: ^0.4.1
path_provider: ^0.2.1+1 path_provider: ^0.4.0
box2d: ^0.4.0 box2d: ^0.4.0
dev_dependencies: dev_dependencies:
test: 0.12.24+8 test: ^0.12.34
environment: environment:
sdk: ">=1.13.0 <2.0.0" sdk: ">=1.13.0 <2.0.0"

View File

@ -10,10 +10,10 @@ void main() {
PositionComponent c = new SpriteComponent(); PositionComponent c = new SpriteComponent();
c.x = 2.2; c.x = 2.2;
c.y = 3.4; c.y = 3.4;
expect(c.position.x, 2.2); expect(c.toPosition().x, 2.2);
expect(c.position.y, 3.4); expect(c.toPosition().y, 3.4);
c.position = new Position(1.0, 0.0); c.setByPosition(new Position(1.0, 0.0));
expect(c.x, 1.0); expect(c.x, 1.0);
expect(c.y, 0.0); expect(c.y, 0.0);
}); });
@ -22,10 +22,10 @@ void main() {
PositionComponent c = new SpriteComponent(); PositionComponent c = new SpriteComponent();
c.width = 2.2; c.width = 2.2;
c.height = 3.4; c.height = 3.4;
expect(c.size.x, 2.2); expect(c.toSize().x, 2.2);
expect(c.size.y, 3.4); expect(c.toSize().y, 3.4);
c.size = new Position(1.0, 0.0); c.setBySize(new Position(1.0, 0.0));
expect(c.width, 1.0); expect(c.width, 1.0);
expect(c.height, 0.0); expect(c.height, 0.0);
}); });
@ -36,12 +36,12 @@ void main() {
c.y = 1.0; c.y = 1.0;
c.width = 2.0; c.width = 2.0;
c.height = 2.0; c.height = 2.0;
expect(c.rect.left, 0.0); expect(c.toRect().left, 0.0);
expect(c.rect.top, 1.0); expect(c.toRect().top, 1.0);
expect(c.rect.width, 2.0); expect(c.toRect().width, 2.0);
expect(c.rect.height, 2.0); expect(c.toRect().height, 2.0);
c.rect = new Rect.fromLTWH(10.0, 10.0, 1.0, 1.0); c.setByRect(new Rect.fromLTWH(10.0, 10.0, 1.0, 1.0));
expect(c.x, 10.0); expect(c.x, 10.0);
expect(c.y, 10.0); expect(c.y, 10.0);
expect(c.width, 1.0); expect(c.width, 1.0);

View File

@ -27,7 +27,7 @@ void main() {
}); });
test('test rotate', () { test('test rotate', () {
Position p = new Position(1.0, 0.0).rotate(math.PI / 2); Position p = new Position(1.0, 0.0).rotate(math.pi / 2);
expectDouble(p.x, 0.0); expectDouble(p.x, 0.0);
expectDouble(p.y, 1.0); expectDouble(p.y, 1.0);
}); });