From 879b358ad30d7ec6749f596eb5de01dd2bc2542e Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Sun, 15 Apr 2018 10:00:18 -0300 Subject: [PATCH] fix some apis --- lib/components/component.dart | 14 +++++++------- lib/game.dart | 8 ++++---- lib/position.dart | 2 +- lib/sprite.dart | 20 ++++++++++++++++---- pubspec.yaml | 4 ++-- test/component_test.dart | 22 +++++++++++----------- test/position_test.dart | 2 +- 7 files changed, 42 insertions(+), 30 deletions(-) diff --git a/lib/components/component.dart b/lib/components/component.dart index 825a61b50..f93c68f5c 100644 --- a/lib/components/component.dart +++ b/lib/components/component.dart @@ -29,20 +29,20 @@ abstract class PositionComponent extends Component { double x = 0.0, y = 0.0, angle = 0.0; double width = 0.0, height = 0.0; - Position get position => new Position(x, y); - void set position(Position position) { + Position toPosition() => new Position(x, y); + void setByPosition(Position position) { this.x = position.x; this.y = position.y; } - Position get size => new Position(width, height); - void set size(Position size) { + Position toSize() => new Position(width, height); + void setBySize(Position size) { this.width = size.x; this.height = size.y; } - Rect get rect => new Rect.fromLTWH(x, y, width, height); - void set rect(Rect rect) { + Rect toRect() => new Rect.fromLTWH(x, y, width, height); + void setByRect(Rect rect) { this.x = rect.left; this.y = rect.top; this.width = rect.width; @@ -50,7 +50,7 @@ abstract class PositionComponent extends Component { } 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) { diff --git a/lib/game.dart b/lib/game.dart index cb403ebb5..c906c3ce7 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -45,7 +45,7 @@ class GameRenderBox extends RenderBox { int _frameCallbackId; - Duration previous = Duration.ZERO; + Duration previous = Duration.zero; GameRenderBox(this.context, this.game); @@ -91,11 +91,11 @@ class GameRenderBox extends RenderBox { double _computeDeltaT(Duration now) { Duration delta = now - previous; - if (previous == Duration.ZERO) { - delta = Duration.ZERO; + if (previous == Duration.zero) { + delta = Duration.zero; } previous = now; - return delta.inMicroseconds / Duration.MICROSECONDS_PER_SECOND; + return delta.inMicroseconds / Duration.microsecondsPerSecond; } @override diff --git a/lib/position.dart b/lib/position.dart index b016479a6..cd9deb963 100644 --- a/lib/position.dart +++ b/lib/position.dart @@ -82,7 +82,7 @@ class Position { @override String toString() { - return "($x, $y)"; + return '($x, $y)'; } static ui.Rect rectFrom(Position topLeft, Position size) { diff --git a/lib/sprite.dart b/lib/sprite.dart index 383bbd615..ef0898857 100644 --- a/lib/sprite.dart +++ b/lib/sprite.dart @@ -1,4 +1,5 @@ import 'dart:ui'; +import 'dart:async'; import 'package:flame/position.dart'; import 'package:flame/flame.dart'; @@ -10,10 +11,6 @@ class Sprite { 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}) { Flame.images.load(fileName).then((img) { 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 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() { return image != null && src != null; } diff --git a/pubspec.yaml b/pubspec.yaml index cd87d2876..929b55367 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,11 +11,11 @@ dependencies: flutter: sdk: flutter audioplayers: ^0.4.1 - path_provider: ^0.2.1+1 + path_provider: ^0.4.0 box2d: ^0.4.0 dev_dependencies: - test: 0.12.24+8 + test: ^0.12.34 environment: sdk: ">=1.13.0 <2.0.0" diff --git a/test/component_test.dart b/test/component_test.dart index b6ab864f9..ff08b120c 100644 --- a/test/component_test.dart +++ b/test/component_test.dart @@ -10,10 +10,10 @@ void main() { PositionComponent c = new SpriteComponent(); c.x = 2.2; c.y = 3.4; - expect(c.position.x, 2.2); - expect(c.position.y, 3.4); + expect(c.toPosition().x, 2.2); + 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.y, 0.0); }); @@ -22,10 +22,10 @@ void main() { PositionComponent c = new SpriteComponent(); c.width = 2.2; c.height = 3.4; - expect(c.size.x, 2.2); - expect(c.size.y, 3.4); + expect(c.toSize().x, 2.2); + 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.height, 0.0); }); @@ -36,12 +36,12 @@ void main() { c.y = 1.0; c.width = 2.0; c.height = 2.0; - expect(c.rect.left, 0.0); - expect(c.rect.top, 1.0); - expect(c.rect.width, 2.0); - expect(c.rect.height, 2.0); + expect(c.toRect().left, 0.0); + expect(c.toRect().top, 1.0); + expect(c.toRect().width, 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.y, 10.0); expect(c.width, 1.0); diff --git a/test/position_test.dart b/test/position_test.dart index 07277a79c..16d894183 100644 --- a/test/position_test.dart +++ b/test/position_test.dart @@ -27,7 +27,7 @@ void main() { }); 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.y, 1.0); });