diff --git a/lib/animation.dart b/lib/animation.dart index e6a53c362..ade4306ab 100644 --- a/lib/animation.dart +++ b/lib/animation.dart @@ -13,18 +13,23 @@ class Animation { Animation.spriteList(this.sprites, {this.stepTime, this.lifeTime}); - Animation.sequenced(String imagePath, int amount, - {double textureX = 0.0, - double textureY = 0.0, - double textureWidth = null, - double textureHeight = null}) { - sprites = new List(amount); + Animation.sequenced( + String imagePath, + int amount, { + double textureX = 0.0, + double textureY = 0.0, + double textureWidth = null, + double textureHeight = null, + }) { + this.sprites = new List(amount); for (var i = 0; i < amount; i++) { - sprites[i] = new Sprite(imagePath, - x: textureX + i * textureWidth, - y: textureY, - width: textureWidth, - height: textureHeight); + this.sprites[i] = new Sprite( + imagePath, + x: textureX + i * textureWidth, + y: textureY, + width: textureWidth, + height: textureHeight, + ); } } diff --git a/lib/audio.dart b/lib/audio.dart index c8af128d5..4f121b5dc 100644 --- a/lib/audio.dart +++ b/lib/audio.dart @@ -8,7 +8,7 @@ import 'package:audioplayers/audioplayer.dart'; import 'package:path_provider/path_provider.dart'; class Audio { - Map loadedFiles = new Map(); + Map loadedFiles = {}; void clear(String fileName) { loadedFiles.remove(fileName); diff --git a/lib/box2d/box2d_component.dart b/lib/box2d/box2d_component.dart index fe5a4e449..95e4bf45b 100644 --- a/lib/box2d/box2d_component.dart +++ b/lib/box2d/box2d_component.dart @@ -17,20 +17,20 @@ abstract class Box2DComponent extends Component { int positionIterations; World world; - List components = new List(); - + List components = []; 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}) { - this.world = new World.withPool(new Vector2(0.0, gravity), - new DefaultWorldPool(worldPoolSize, worldPoolContainerSize)); + 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, + }) { + final pool = new DefaultWorldPool(worldPoolSize, worldPoolContainerSize); + this.world = new World.withPool(new Vector2(0.0, gravity), pool); this.viewport = new Viewport(dimensions, scale); } @@ -66,10 +66,16 @@ abstract class Box2DComponent extends Component { void initializeWorld(); - void cameraFollow(BodyComponent component, - {double horizontal, double vertical}) { - viewport.cameraFollow(component, - horizontal: horizontal, vertical: vertical); + void cameraFollow( + BodyComponent component, { + double horizontal, + double vertical, + }) { + viewport.cameraFollow( + component, + horizontal: horizontal, + vertical: vertical, + ); } } @@ -80,7 +86,7 @@ abstract class BodyComponent extends Component { Body body; - BodyComponent(this.box) {} + BodyComponent(this.box); World get world => box.world; @@ -127,7 +133,7 @@ abstract class BodyComponent extends Component { void renderCircle(Canvas canvas, Offset center, double radius) { final Paint paint = new Paint() - ..color = new Color.fromARGB(255, 255, 255, 255); + ..color = const Color.fromARGB(255, 255, 255, 255); canvas.drawCircle(center, radius, paint); } @@ -152,7 +158,7 @@ abstract class BodyComponent extends Component { void renderPolygon(Canvas canvas, List points) { final path = new Path()..addPolygon(points, true); final Paint paint = new Paint() - ..color = new Color.fromARGB(255, 255, 255, 255); + ..color = const Color.fromARGB(255, 255, 255, 255); canvas.drawPath(path, paint); } } diff --git a/lib/box2d/viewport.dart b/lib/box2d/viewport.dart index 72da9a51d..b42f9c5d0 100644 --- a/lib/box2d/viewport.dart +++ b/lib/box2d/viewport.dart @@ -14,9 +14,7 @@ class Viewport extends ViewportTransform { double worldAlignBottom(double height) => -(size.height / 2 / scale) + height; - /** - * Resizes the current view port. - */ + /// Resizes the current view port. void resize(Size size) { this.size = size; this.extents = @@ -25,24 +23,17 @@ class Viewport extends ViewportTransform { new Vector2.copy(new Vector2(size.width / 2, size.height / 2)); } - /** - * Computes the number of horizontal world meters of this viewport considering a - * percentage of its width. - * - * @param percent percetage of the width in [0, 1] range - */ + /// Computes the number of horizontal world meters of this viewport considering a percentage of its width. + /// + /// @param percent percetage of the width in [0, 1] range. double worldWidth(double percent) { return percent * (size.width / scale); } - /** - * Computes the scroll percentage of total screen width of the current viwerport - * center position. - * - * @param screens multiplies the visible screen with to create a bigger virtual - * screen. - * @return the percentage in the range of [0, 1] - */ + /// Computes the scroll percentage of total screen width of the current viwerport center position. + /// + /// @param screens multiplies the visible screen with to create a bigger virtual screen. + /// @return the percentage in the range of [0, 1] double getCenterHorizontalScreenPercentage({double screens: 1.0}) { var width = size.width * screens; var x = center.x + ((screens - 1) * size.width / 2); @@ -51,14 +42,11 @@ class Viewport extends ViewportTransform { return x > 0 ? scroll : 1 - scroll; } - /** - * Follows the spececified body component using a sliding focus window - * defined as a percentage of the total viewport. - * - * @param component to follow. - * @param horizontal percentage of the horizontal viewport. Null means no horizontal following. - * @param vertical percentage of the vertical viewport. Null means no vertical following. - */ + /// Follows the spececified body component using a sliding focus window defined as a percentage of the total viewport. + /// + /// @param component to follow. + /// @param horizontal percentage of the horizontal viewport. Null means no horizontal following. + /// @param vertical percentage of the vertical viewport. Null means no vertical following. void cameraFollow(BodyComponent component, {double horizontal, double vertical}) { Vector2 position = component.center; diff --git a/lib/components/animation_component.dart b/lib/components/animation_component.dart index 4b7568634..4b0327b79 100644 --- a/lib/components/animation_component.dart +++ b/lib/components/animation_component.dart @@ -11,18 +11,26 @@ 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 @@ -32,7 +40,7 @@ class AnimationComponent extends PositionComponent { @override void render(Canvas canvas) { - if (loaded() && x != null && y != null) { + if (loaded()) { prepareCanvas(canvas); animation.getSprite().render(canvas, width, height); } diff --git a/lib/components/component.dart b/lib/components/component.dart index 174b9219f..74d2718d2 100644 --- a/lib/components/component.dart +++ b/lib/components/component.dart @@ -5,26 +5,48 @@ import '../sprite.dart'; import '../position.dart'; import 'package:flutter/painting.dart'; +/// This represents a Component for your game. +/// +/// Components can be bullets flying on the screen, a spaship or your player's fighter. +/// Anything that either renders or updates can be added to the list on [BaseGame]. It will deal with calling those methods for you. +/// Components also have other methods that can help you out if you want to overwrite them. abstract class Component { + /// This method is called periodically by the game engine to request that your component updates itself. + /// + /// The time [t] in seconds (with microseconds precision provided by Flutter) since the last update cycle. + /// This time can vary according to hardware capacity, so make sure to update your state considering this. + /// All components on [BaseGame] are always updated by the same amount. The time each one takes to update adds up to the next update cycle. void update(double t); + /// Renders this component on the provided Canvas [c]. void render(Canvas c); + /// This is a hook called by [BaseGame] to let this component know that the screen (or flame draw area) has been update. + /// + /// It receives the new size. + /// You can use the [Resizable] mixin if you want an implementation of this hook that keeps track of the current size. void resize(Size size) {} - bool loaded() { - return true; - } + /// Wether this component has been loaded yet. If not loaded, [BaseGame] will not try to render it. + /// + /// Sprite based components can use this to let [BaseGame] know not to try to render when the [Sprite] has not been loaded yet. + /// Note that for a more consistent experience, you can pre-load all your assets beforehand with [Flame.images.loadAll]. + bool loaded() => true; - bool destroy() { - return false; - } + /// Wether this should be destroyed or not. + /// + /// It will be called once per component per loop, and if it returns true, [BaseGame] will mark your component for deletion and remove it before the next loop. + bool destroy() => false; - bool isHud() { - return false; - } + /// Wether this component is HUD object or not. + /// + /// HUD objects ignore the [BaseGame.camera] when rendered (so their position coordinates are considered relative to the device screen). + bool isHud() => false; } +/// A [Component] implementation that represents a component that has a specific, possibly mutatable position on the screen. +/// +/// It represents a rectangle of dimension ([width], [height]), on the position ([x], [y]), rotate around its center with angle [angle]. abstract class PositionComponent extends Component { double x = 0.0, y = 0.0, angle = 0.0; double width = 0.0, height = 0.0; diff --git a/lib/components/parallax_component.dart b/lib/components/parallax_component.dart index a2f0d3867..f68e2a280 100644 --- a/lib/components/parallax_component.dart +++ b/lib/components/parallax_component.dart @@ -8,7 +8,7 @@ import 'component.dart'; class ParallaxRenderer { String filename; - Future future; + Future future; Image image; double scroll = 0.0; @@ -17,7 +17,7 @@ class ParallaxRenderer { this.future = _load(); } - Future _load() { + Future _load() { return Flame.images.load(filename).then((image) { this.image = image; }); @@ -39,10 +39,11 @@ class ParallaxRenderer { -scroll * imageWidth, rect.top, (count + 1) * imageWidth, rect.height); paintImage( - canvas: canvas, - image: image, - rect: fullRect, - repeat: ImageRepeat.repeatX); + canvas: canvas, + image: image, + rect: fullRect, + repeat: ImageRepeat.repeatX, + ); } } @@ -59,23 +60,18 @@ abstract class ParallaxComponent extends PositionComponent { this._size = size; } - /** - * Loads the images defined by this list of filenames. All images - * are positioned at its scroll center. - * - * @param filenames Image filenames - */ + /// Loads the images defined by this list of filenames. All images are positioned at its scroll center. + /// + /// @param filenames Image filenames void load(List filenames) { - var futures = - filenames.fold(new List(), (List result, filename) { - var layer = new ParallaxRenderer(filename); + final futures = filenames.fold(>[], + (List> result, String filename) { + final layer = new ParallaxRenderer(filename); _layers.add(layer); result.add(layer.future); return result; }); - Future.wait(futures).then((r) { - _loaded = true; - }); + Future.wait(futures).then((r) => _loaded = true); } void updateScroll(int layerIndex, scroll) { @@ -101,10 +97,8 @@ abstract class ParallaxComponent extends PositionComponent { void _drawLayers(Canvas canvas) { Rect rect = new Rect.fromPoints( - new Offset(0.0, 0.0), new Offset(_size.width, _size.height)); - _layers.forEach((layer) { - layer.render(canvas, rect); - }); + const Offset(0.0, 0.0), new Offset(_size.width, _size.height)); + _layers.forEach((layer) => layer.render(canvas, rect)); } @override @@ -112,8 +106,8 @@ abstract class ParallaxComponent extends PositionComponent { if (!this.loaded()) { return; } - for (var i = 0; i < _layers.length; i++) { - var scroll = _layers[i].scroll; + for (int i = 0; i < _layers.length; i++) { + double scroll = _layers[i].scroll; scroll += (BASE_SPEED + i * LAYER_DELTA) * delta / _size.width; if (scroll > 1) { scroll = scroll % 1; diff --git a/lib/game.dart b/lib/game.dart index e2e935ef5..64364acfe 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -234,6 +234,7 @@ abstract class BaseGame extends Game { /// Returns `false` by default. Override to use the debug mode. /// In debug mode, the [_recordDt] method actually records every `dt` for statistics. /// Then, you can use the [fps] method to check the game FPS. + /// You can also use this value to enable other debug behaviors for your game. bool debugMode() => false; /// This is a hook that comes from the RenderBox to allow recording of render times and statistics. diff --git a/lib/images.dart b/lib/images.dart index b0aff0802..0a671870f 100644 --- a/lib/images.dart +++ b/lib/images.dart @@ -4,7 +4,7 @@ import 'dart:ui'; import 'dart:async'; class Images { - Map loadedFiles = new Map(); + Map loadedFiles = {}; void clear(String fileName) { loadedFiles.remove(fileName); @@ -29,9 +29,7 @@ class Images { ByteData data = await rootBundle.load('assets/images/' + name); Uint8List bytes = new Uint8List.view(data.buffer); Completer completer = new Completer(); - decodeImageFromList(bytes, (image) { - completer.complete(image); - }); + decodeImageFromList(bytes, (image) => completer.complete(image)); return completer.future; } } diff --git a/lib/sprite.dart b/lib/sprite.dart index db4eb1057..a8f6a60a4 100644 --- a/lib/sprite.dart +++ b/lib/sprite.dart @@ -11,11 +11,13 @@ 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(); @@ -28,11 +30,13 @@ class Sprite { }); } - Sprite.fromImage(this.image, - {double x = 0.0, - double y = 0.0, - double width = null, - double height = null}) { + 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(); } @@ -42,14 +46,21 @@ class Sprite { 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 8f72e7197..ac319488e 100644 --- a/lib/util.dart +++ b/lib/util.dart @@ -17,7 +17,7 @@ class Util { // "In release mode we start off at 0x0 but we don't in debug mode" return await new Future(() { if (window.physicalSize.isEmpty) { - var completer = new Completer(); + final completer = new Completer(); window.onMetricsChanged = () { if (!window.physicalSize.isEmpty) { completer.complete(window.physicalSize / window.devicePixelRatio); @@ -29,17 +29,28 @@ 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.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); + 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); + text: span, + textAlign: textAlign, + textDirection: textDirection, + ); tp.layout(); return tp; } diff --git a/test/box2d/viewport_test.dart b/test/box2d/viewport_test.dart index 7819ce114..bdef463a6 100644 --- a/test/box2d/viewport_test.dart +++ b/test/box2d/viewport_test.dart @@ -4,7 +4,7 @@ import 'package:flame/box2d/viewport.dart'; import 'package:test/test.dart'; void main() { - var viewport = new Viewport(new Size(100.0, 100.0), 1.0); + final viewport = new Viewport(new Size(100.0, 100.0), 1.0); group("getCenterHorizontalScreenPercentage", () { test("center starts in the middle", () {