diff --git a/lib/audio.dart b/lib/audio.dart index d52a3fbbe..c8af128d5 100644 --- a/lib/audio.dart +++ b/lib/audio.dart @@ -28,7 +28,8 @@ class Audio { Future _fetchToMemory(String fileName) async { final file = new File('${(await getTemporaryDirectory()).path}/$fileName'); - return await file.writeAsBytes((await _fetchAsset(fileName)).buffer.asUint8List()); + return await file + .writeAsBytes((await _fetchAsset(fileName)).buffer.asUint8List()); } Future> loadAll(List fileNames) async { @@ -42,16 +43,18 @@ class Audio { return loadedFiles[fileName]; } - Future play(String fileName, { volume: 1.0 }) async { + Future play(String fileName, {volume: 1.0}) async { File file = await load(fileName); - return await new AudioPlayer()..play(file.path, isLocal: true, volume: volume); + return await new AudioPlayer() + ..play(file.path, isLocal: true, volume: volume); } - Future loop(String fileName, { volume: 1.0 }) async { + Future loop(String fileName, {volume: 1.0}) async { File file = await load(fileName); AudioPlayer player = new AudioPlayer(); - player.setCompletionHandler(() => player.play(file.path, isLocal: true, volume: volume)); - return await player..play(file.path, isLocal: true); + player.setCompletionHandler( + () => player.play(file.path, isLocal: true, volume: volume)); + return await player + ..play(file.path, isLocal: true); } - } diff --git a/lib/box2d/box2d_component.dart b/lib/box2d/box2d_component.dart index 081539da6..fe5a4e449 100644 --- a/lib/box2d/box2d_component.dart +++ b/lib/box2d/box2d_component.dart @@ -21,13 +21,14 @@ abstract class Box2DComponent extends Component { Viewport viewport; - Box2DComponent({this.dimensions: const Size(0.0, 0.0), - int worldPoolSize: DEFAULT_WORLD_POOL_SIZE, - int worldPoolContainerSize: DEFAULT_WORLD_POOL_CONTAINER_SIZE, - double gravity: DEFAULT_GRAVITY, - this.velocityIterations: DEFAULT_VELOCITY_ITERATIONS, - this.positionIterations: DEFAULT_POSITION_ITERATIONS, - double scale: DEFAULT_SCALE}) { + Box2DComponent( + {this.dimensions: const Size(0.0, 0.0), + int worldPoolSize: DEFAULT_WORLD_POOL_SIZE, + int worldPoolContainerSize: DEFAULT_WORLD_POOL_CONTAINER_SIZE, + double gravity: DEFAULT_GRAVITY, + this.velocityIterations: DEFAULT_VELOCITY_ITERATIONS, + this.positionIterations: DEFAULT_POSITION_ITERATIONS, + double scale: DEFAULT_SCALE}) { this.world = new World.withPool(new Vector2(0.0, gravity), new DefaultWorldPool(worldPoolSize, worldPoolContainerSize)); this.viewport = new Viewport(dimensions, scale); @@ -94,8 +95,8 @@ abstract class BodyComponent extends Component { void render(Canvas canvas) { body.getFixtureList(); for (Fixture fixture = body.getFixtureList(); - fixture != null; - fixture = fixture.getNext()) { + fixture != null; + fixture = fixture.getNext()) { switch (fixture.getType()) { case ShapeType.CHAIN: throw new Exception("not implemented"); @@ -149,8 +150,7 @@ abstract class BodyComponent extends Component { } void renderPolygon(Canvas canvas, List points) { - final path = new Path() - ..addPolygon(points, true); + final path = new Path()..addPolygon(points, true); final Paint paint = new Paint() ..color = new Color.fromARGB(255, 255, 255, 255); canvas.drawPath(path, paint); diff --git a/lib/components/animation_component.dart b/lib/components/animation_component.dart index 83b3566dc..4b7568634 100644 --- a/lib/components/animation_component.dart +++ b/lib/components/animation_component.dart @@ -4,7 +4,6 @@ import 'component.dart'; import 'package:flame/animation.dart'; class AnimationComponent extends PositionComponent { - Animation animation; AnimationComponent(double width, double height, this.animation) { @@ -12,10 +11,18 @@ class AnimationComponent extends PositionComponent { this.height = height; } - AnimationComponent.sequenced(width, height, String imagePath, int amount, { double textureX = 0.0, double textureY = 0.0, double textureWidth = null, double textureHeight = null}) { + AnimationComponent.sequenced(width, height, String imagePath, int amount, + {double textureX = 0.0, + double textureY = 0.0, + double textureWidth = null, + double textureHeight = null}) { this.width = width; this.height = height; - this.animation = new Animation.sequenced(imagePath, amount, textureX: textureX, textureY: textureY, textureWidth: textureWidth, textureHeight: textureHeight); + this.animation = new Animation.sequenced(imagePath, amount, + textureX: textureX, + textureY: textureY, + textureWidth: textureWidth, + textureHeight: textureHeight); } @override diff --git a/lib/components/component.dart b/lib/components/component.dart index f93c68f5c..174b9219f 100644 --- a/lib/components/component.dart +++ b/lib/components/component.dart @@ -61,9 +61,9 @@ abstract class PositionComponent extends Component { canvas.translate(x, y); // rotate around center - canvas.translate(width/2, height/2); + canvas.translate(width / 2, height / 2); canvas.rotate(angle); - canvas.translate(-width/2, -height/2); + canvas.translate(-width / 2, -height / 2); } } diff --git a/lib/game.dart b/lib/game.dart index 6a76ba7b3..e2e935ef5 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -14,7 +14,6 @@ import 'position.dart'; /// Subclass this to implement the [update] and [render] methods. /// Flame will deal with calling these methods properly when the game's [widget] is rendered. abstract class Game { - /// Implement this method to update the game state, given that a time [t] has passed. /// /// Keep the updates as short as possible. [t] is in seconds, with microsseconds precision. @@ -58,7 +57,8 @@ class _GameRenderObjectWidget extends SingleChildRenderObjectWidget { _GameRenderObjectWidget(this.game); @override - RenderObject createRenderObject(BuildContext context) => new _GameRenderBox(context, this.game); + RenderObject createRenderObject(BuildContext context) => + new _GameRenderBox(context, this.game); } class _GameRenderBox extends RenderBox with WidgetsBindingObserver { @@ -150,7 +150,6 @@ class _GameRenderBox extends RenderBox with WidgetsBindingObserver { /// This is the recommended strucutre to use for most games. /// It is based on the Component system. abstract class BaseGame extends Game { - /// The list of components to be updated and rendered by the base game. List components = []; @@ -200,12 +199,12 @@ abstract class BaseGame extends Game { /// It translates the camera unless hud, call the render method and restore the canvas. /// This makes sure the canvas is not messed up by one component and all components render independently. void renderComponent(Canvas canvas, Component c) { - if (!c.isHud()) { - canvas.translate(-camera.x, -camera.y); - } - c.render(canvas); - canvas.restore(); - canvas.save(); + if (!c.isHud()) { + canvas.translate(-camera.x, -camera.y); + } + c.render(canvas); + canvas.restore(); + canvas.save(); } /// This implementation of update updates every component in the list. @@ -265,6 +264,7 @@ abstract class BaseGame extends Game { /// /// This is compatible with the `dt` value used in the [update] method. double currentTime() { - return new DateTime.now().microsecondsSinceEpoch.toDouble() / Duration.microsecondsPerSecond; + return new DateTime.now().microsecondsSinceEpoch.toDouble() / + Duration.microsecondsPerSecond; } } diff --git a/lib/images.dart b/lib/images.dart index 73552d877..b0aff0802 100644 --- a/lib/images.dart +++ b/lib/images.dart @@ -4,7 +4,6 @@ import 'dart:ui'; import 'dart:async'; class Images { - Map loadedFiles = new Map(); void clear(String fileName) { @@ -35,4 +34,4 @@ class Images { }); return completer.future; } -} \ No newline at end of file +} diff --git a/lib/position.dart b/lib/position.dart index c8aa0931e..4973e61bf 100644 --- a/lib/position.dart +++ b/lib/position.dart @@ -8,7 +8,6 @@ import 'package:box2d/box2d.dart' as b2d; /// Also, it offers helpful converters and a some useful methods for manipulation. /// It always uses double values to store the coordinates. class Position { - /// Coordinates double x, y; @@ -110,6 +109,7 @@ class Position { double maxx = pts.map((e) => e.x).reduce(math.max); double miny = pts.map((e) => e.y).reduce(math.min); double maxy = pts.map((e) => e.y).reduce(math.max); - return new ui.Rect.fromPoints(new ui.Offset(minx, miny), new ui.Offset(maxx, maxy)); + return new ui.Rect.fromPoints( + new ui.Offset(minx, miny), new ui.Offset(maxx, maxy)); } } diff --git a/lib/profiler.dart b/lib/profiler.dart new file mode 100644 index 000000000..a8b0b74b9 --- /dev/null +++ b/lib/profiler.dart @@ -0,0 +1,27 @@ +class Records { + static void save(Profiler p) { + print('${p.name} : ${p.dts.last - p.dts.first} ms'); + } +} + +class Profiler { + final String name; + List dts = []; + + Profiler(this.name) { + this.tick(); + } + + void tick() { + this.dts.add(currentTime()); + } + + void end() { + this.tick(); + Records.save(this); + } + + static double currentTime() => + new DateTime.now().microsecondsSinceEpoch.toDouble() / + Duration.microsecondsPerMillisecond; +} diff --git a/lib/sprite.dart b/lib/sprite.dart index 22d7e3c4b..db4eb1057 100644 --- a/lib/sprite.dart +++ b/lib/sprite.dart @@ -11,7 +11,11 @@ class Sprite { static final Paint paint = new Paint()..color = Colors.white; - Sprite(String fileName, {double x = 0.0, double y = 0.0, double width = null, double height = null}) { + Sprite(String fileName, + {double x = 0.0, + double y = 0.0, + double width = null, + double height = null}) { Flame.images.load(fileName).then((img) { if (width == null) { width = img.width.toDouble(); @@ -24,19 +28,28 @@ class Sprite { }); } - Sprite.fromImage(this.image, {double x = 0.0, double y = 0.0, double width = null, double height = null}) { - if (width == null) { - width = image.width.toDouble(); - } - if (height == null) { - height = image.height.toDouble(); - } - this.src = new Rect.fromLTWH(x, y, width, height); + Sprite.fromImage(this.image, + {double x = 0.0, + double y = 0.0, + double width = null, + double height = null}) { + if (width == null) { + width = image.width.toDouble(); + } + if (height == null) { + 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 = null, double height = null}) async { + static Future loadSprite(String fileName, + {double x = 0.0, + double y = 0.0, + double width = null, + double height = null}) async { Image image = await Flame.images.load(fileName); - return new Sprite.fromImage(image, x: x, y: y, width: width, height: height); + return new Sprite.fromImage(image, + x: x, y: y, width: width, height: height); } bool loaded() { diff --git a/lib/util.dart b/lib/util.dart index 77483f5b2..8f72e7197 100644 --- a/lib/util.dart +++ b/lib/util.dart @@ -29,16 +29,17 @@ class Util { }); } - material.TextPainter text(String text, { - double fontSize: 24.0, - Color color: material.Colors.black, - String fontFamily: 'Arial', - TextAlign textAlign: TextAlign.left, - TextDirection textDirection: TextDirection.ltr - }) { - material.TextStyle style = new material.TextStyle(color: color, fontSize: fontSize, fontFamily: fontFamily); + material.TextPainter text(String text, + {double fontSize: 24.0, + Color color: material.Colors.black, + String fontFamily: 'Arial', + TextAlign textAlign: TextAlign.left, + TextDirection textDirection: TextDirection.ltr}) { + material.TextStyle style = new material.TextStyle( + color: color, fontSize: fontSize, fontFamily: fontFamily); material.TextSpan span = new material.TextSpan(style: style, text: text); - material.TextPainter tp = new material.TextPainter(text: span, textAlign: textAlign, textDirection: textDirection); + material.TextPainter tp = new material.TextPainter( + text: span, textAlign: textAlign, textDirection: textDirection); tp.layout(); return tp; }