From 9c69937b5b2375c3a069322c60f05d49c04ab5eb Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Sun, 10 Mar 2019 15:17:53 -0300 Subject: [PATCH] Fixes build (tests), adding ditance method to position, fix linting (formating, quotes), removed uneeded new keywords everywhere --- analysis_options.yaml | 7 ---- doc/examples/animation_widget/lib/main.dart | 6 ++-- doc/examples/aseprite/lib/main.dart | 7 ++-- doc/examples/text/lib/main.dart | 6 ++-- doc/examples/tiled/lib/main.dart | 6 ++-- example/lib/main.dart | 4 +-- lib/anchor.dart | 4 +-- lib/animation.dart | 37 +++++++++++---------- lib/assets_cache.dart | 2 +- lib/audio_pool.dart | 6 ++-- lib/box2d/box2d_component.dart | 28 ++++++++-------- lib/box2d/viewport.dart | 14 ++++---- lib/components/animation_component.dart | 2 +- lib/components/component.dart | 8 ++--- lib/components/composed_component.dart | 2 +- lib/components/debug_component.dart | 4 +-- lib/components/parallax_component.dart | 10 +++--- lib/components/text_box_component.dart | 10 +++--- lib/components/tiled_component.dart | 8 ++--- lib/flame.dart | 10 +++--- lib/game.dart | 24 ++++++------- lib/images.dart | 4 +-- lib/palette.dart | 2 +- lib/position.dart | 19 ++++++----- lib/profiler.dart | 2 +- lib/sprite.dart | 14 ++++---- lib/text_config.dart | 6 ++-- lib/util.dart | 8 ++--- scripts/build.sh | 2 -- scripts/gen-doc.sh | 3 ++ test/components/box2d/viewport_test.dart | 16 ++++----- test/components/component_test.dart | 12 +++---- test/components/tiled_test.dart | 8 ++--- test/position_test.dart | 17 +++++++--- test/resizable_test.dart | 12 +++---- 35 files changed, 165 insertions(+), 165 deletions(-) create mode 100755 scripts/gen-doc.sh diff --git a/analysis_options.yaml b/analysis_options.yaml index 4f4747215..357014f28 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,10 +1,3 @@ -analyzer: - language: - enablePreviewDart2: true - enableStrictCallChecks: true - enableSuperMixins: true - strong-mode: true - # Source of linter options: # http://dart-lang.github.io/linter/lints/options/options.html diff --git a/doc/examples/animation_widget/lib/main.dart b/doc/examples/animation_widget/lib/main.dart index 7ed529191..c9c49fb0a 100644 --- a/doc/examples/animation_widget/lib/main.dart +++ b/doc/examples/animation_widget/lib/main.dart @@ -23,14 +23,14 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { void _clickFab(GlobalKey key) { - key.currentState.showSnackBar(new SnackBar( - content: new Text('You clicked the FAB!'), + key.currentState.showSnackBar(SnackBar( + content: Text('You clicked the FAB!'), )); } @override Widget build(BuildContext context) { - final key = new GlobalKey(); + final key = GlobalKey(); return Scaffold( key: key, appBar: AppBar( diff --git a/doc/examples/aseprite/lib/main.dart b/doc/examples/aseprite/lib/main.dart index 0641d3e6f..afe5396fb 100644 --- a/doc/examples/aseprite/lib/main.dart +++ b/doc/examples/aseprite/lib/main.dart @@ -4,7 +4,7 @@ import 'package:flame/animation.dart' as FlameAnimation; import 'package:flame/components/animation_component.dart'; import 'package:flutter/material.dart'; -void main() => runApp(new MyGame().widget); +void main() => runApp(MyGame()); class MyGame extends BaseGame { MyGame() { @@ -15,9 +15,7 @@ class MyGame extends BaseGame { Size size = await Flame.util.initialDimensions(); final animation = await FlameAnimation.Animation.fromAsepriteData( - "chopper.png", - "chopper.json" - ); + 'chopper.png', 'chopper.json'); final animationComponent = AnimationComponent(200, 200, animation); animationComponent.x = (size.width / 2) - 100; @@ -26,4 +24,3 @@ class MyGame extends BaseGame { add(animationComponent); } } - diff --git a/doc/examples/text/lib/main.dart b/doc/examples/text/lib/main.dart index 9393d3f40..b9d62f44f 100644 --- a/doc/examples/text/lib/main.dart +++ b/doc/examples/text/lib/main.dart @@ -9,7 +9,7 @@ import 'package:flame/palette.dart'; import 'package:flame/text_config.dart'; import 'package:flutter/material.dart'; -void main() => runApp(new MyGame().widget); +void main() => runApp(MyGame()); TextConfig regular = TextConfig(color: BasicPalette.white.color); TextConfig tiny = regular.withFontSize(12.0); @@ -21,10 +21,10 @@ class MyTextBox extends TextBoxComponent { @override void drawBackground(Canvas c) { Rect rect = Rect.fromLTWH(0, 0, width, height); - c.drawRect(rect, new Paint()..color = Color(0xFFFF00FF)); + c.drawRect(rect, Paint()..color = Color(0xFFFF00FF)); c.drawRect( rect.deflate(boxConfig.margin), - new Paint() + Paint() ..color = BasicPalette.black.color ..style = PaintingStyle.stroke); } diff --git a/doc/examples/tiled/lib/main.dart b/doc/examples/tiled/lib/main.dart index 92c9efadb..d7ed4e7b7 100644 --- a/doc/examples/tiled/lib/main.dart +++ b/doc/examples/tiled/lib/main.dart @@ -3,12 +3,12 @@ import 'package:flame/game.dart'; import 'package:flutter/widgets.dart'; void main() { - var game = new TiledGame(); - runApp(game.widget); + TiledGame game = TiledGame(); + runApp(game); } class TiledGame extends BaseGame { TiledGame() { - add(new TiledComponent('map.tmx')); + add(TiledComponent('map.tmx')); } } diff --git a/example/lib/main.dart b/example/lib/main.dart index 3066d277d..d0b6160a0 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -7,7 +7,7 @@ import 'package:flame/game.dart'; import 'package:flame/palette.dart'; import 'package:flutter/material.dart'; -void main() => runApp(new MyGame().widget); +void main() => runApp(MyGame()); class Palette { static const PaletteEntry white = BasicPalette.white; @@ -47,6 +47,6 @@ class Square extends PositionComponent { class MyGame extends BaseGame { MyGame() { - add(new Square(64.0)); + add(Square(64.0)); } } diff --git a/lib/anchor.dart b/lib/anchor.dart index 81c2b2f36..8ced13831 100644 --- a/lib/anchor.dart +++ b/lib/anchor.dart @@ -18,7 +18,7 @@ class Anchor { const Anchor(this.relativePosition); Position translate(Position p, Position size) { - return p.clone().minus(new Position( - size.x * relativePosition.dx, size.y * relativePosition.dy)); + return p.clone().minus( + Position(size.x * relativePosition.dx, size.y * relativePosition.dy)); } } diff --git a/lib/animation.dart b/lib/animation.dart index 551c9987b..a105a50bb 100644 --- a/lib/animation.dart +++ b/lib/animation.dart @@ -41,7 +41,7 @@ class Animation { /// /// All frames have the same [stepTime]. Animation.spriteList(List sprites, {double stepTime, this.loop}) { - this.frames = sprites.map((s) => new Frame(s, stepTime)).toList(); + this.frames = sprites.map((s) => Frame(s, stepTime)).toList(); } /// Creates an animation given a list of frames. @@ -57,7 +57,7 @@ class Animation { /// [textureHeight]: height of each frame (defaults to null, that is, full height of the sprite sheet) /// /// For example, if you have a spritesheet where each row is an animation, and each frame is 32x32 - /// new Animation.sequenced('sheet.png', 8, textureY: 32.0 * i, textureWidth: 32.0, textureHeight: 32.0); + /// Animation.sequenced('sheet.png', 8, textureY: 32.0 * i, textureWidth: 32.0, textureHeight: 32.0); /// This will create the i-th animation on the 'sheet.png', given it has 8 frames. Animation.sequenced( String imagePath, @@ -68,16 +68,16 @@ class Animation { double textureHeight, double stepTime = 0.1, }) { - this.frames = new List(amount); + this.frames = List(amount); for (var i = 0; i < amount; i++) { - Sprite sprite = new Sprite( + Sprite sprite = Sprite( imagePath, x: textureX + i * textureWidth, y: textureY, width: textureWidth, height: textureHeight, ); - this.frames[i] = new Frame(sprite, stepTime); + this.frames[i] = Frame(sprite, stepTime); } } @@ -91,16 +91,16 @@ class Animation { double textureWidth, double textureHeight, }) { - this.frames = new List(amount); + this.frames = List(amount); for (var i = 0; i < amount; i++) { - Sprite sprite = new Sprite( + Sprite sprite = Sprite( imagePath, x: textureX + i * textureWidth, y: textureY, width: textureWidth, height: textureHeight, ); - this.frames[i] = new Frame(sprite, stepTimes[i]); + this.frames[i] = Frame(sprite, stepTimes[i]); } } @@ -109,22 +109,23 @@ class Animation { /// /// [imagePath]: Source of the spritesheet animation /// [dataPath]: Animation's exported data in json format - static Future fromAsepriteData(String imagePath, String dataPath) async { + static Future fromAsepriteData( + String imagePath, String dataPath) async { String content = await Flame.assets.readFile(dataPath); Map json = jsonDecode(content); - Map jsonFrames = json["frames"]; + Map jsonFrames = json['frames']; var frames = jsonFrames.values.map((value) { - final frameData = value["frame"]; - final int x = frameData["x"]; - final int y = frameData["y"]; - final int width = frameData["w"]; - final int height = frameData["h"]; + final frameData = value['frame']; + final int x = frameData['x']; + final int y = frameData['y']; + final int width = frameData['w']; + final int height = frameData['h']; - final stepTime = value["duration"] / 1000; + final stepTime = value['duration'] / 1000; - Sprite sprite = new Sprite( + Sprite sprite = Sprite( imagePath, x: x.toDouble(), y: y.toDouble(), @@ -132,7 +133,7 @@ class Animation { height: height.toDouble(), ); - return new Frame(sprite, stepTime); + return Frame(sprite, stepTime); }); return Animation(frames.toList(), loop: true); diff --git a/lib/assets_cache.dart b/lib/assets_cache.dart index 71b405c72..393320ff0 100644 --- a/lib/assets_cache.dart +++ b/lib/assets_cache.dart @@ -26,6 +26,6 @@ class AssetsCache { } Future _readFile(String fileName) async { - return await rootBundle.loadString("assets/" + fileName); + return await rootBundle.loadString('assets/$fileName'); } } diff --git a/lib/audio_pool.dart b/lib/audio_pool.dart index b184eabf5..82e79dd38 100644 --- a/lib/audio_pool.dart +++ b/lib/audio_pool.dart @@ -19,14 +19,14 @@ class AudioPool { bool repeating; int minPlayers, maxPlayers; - Lock _lock = new Lock(); + Lock _lock = Lock(); AudioPool(this.sound, {this.repeating = false, this.maxPlayers = 1, this.minPlayers = 1, String prefix = 'audio/sfx/'}) { - cache = new AudioCache(prefix: prefix); + cache = AudioCache(prefix: prefix); } void init() async { @@ -66,7 +66,7 @@ class AudioPool { } Future _createNewAudioPlayer() async { - AudioPlayer player = new AudioPlayer(); + AudioPlayer player = AudioPlayer(); String url = (await cache.load(sound)).path; await player.setUrl(url); await player.setReleaseMode(ReleaseMode.STOP); diff --git a/lib/box2d/box2d_component.dart b/lib/box2d/box2d_component.dart index af455c57f..fa857f79c 100644 --- a/lib/box2d/box2d_component.dart +++ b/lib/box2d/box2d_component.dart @@ -32,9 +32,9 @@ abstract class Box2DComponent extends Component { if (this.dimensions == null) { this.dimensions = window.physicalSize; } - final pool = new DefaultWorldPool(worldPoolSize, worldPoolContainerSize); - this.world = new World.withPool(new Vector2(0.0, gravity), pool); - this.viewport = new Viewport(dimensions, scale); + final pool = DefaultWorldPool(worldPoolSize, worldPoolContainerSize); + this.world = World.withPool(Vector2(0.0, gravity), pool); + this.viewport = Viewport(dimensions, scale); } @override @@ -47,7 +47,7 @@ abstract class Box2DComponent extends Component { @override void render(canvas) { - if (viewport.size == new Size(0.0, 0.0)) { + if (viewport.size == Size(0.0, 0.0)) { return; } components.forEach((c) { @@ -119,13 +119,13 @@ abstract class BodyComponent extends Component { fixture = fixture.getNext()) { switch (fixture.getType()) { case ShapeType.CHAIN: - throw new Exception("not implemented"); + throw Exception('not implemented'); break; case ShapeType.CIRCLE: _renderCircle(canvas, fixture); break; case ShapeType.EDGE: - throw new Exception("not implemented"); + throw Exception('not implemented'); break; case ShapeType.POLYGON: _renderPolygon(canvas, fixture); @@ -137,16 +137,16 @@ abstract class BodyComponent extends Component { Vector2 get center => this.body.worldCenter; void _renderCircle(Canvas canvas, Fixture fixture) { - Vector2 center = new Vector2.zero(); + Vector2 center = Vector2.zero(); CircleShape circle = fixture.getShape(); body.getWorldPointToOut(circle.p, center); viewport.getWorldToScreen(center, center); renderCircle( - canvas, new Offset(center.x, center.y), circle.radius * viewport.scale); + canvas, Offset(center.x, center.y), circle.radius * viewport.scale); } void renderCircle(Canvas canvas, Offset center, double radius) { - final Paint paint = new Paint() + final Paint paint = Paint() ..color = const Color.fromARGB(255, 255, 255, 255); canvas.drawCircle(center, radius, paint); } @@ -154,24 +154,24 @@ abstract class BodyComponent extends Component { void _renderPolygon(Canvas canvas, Fixture fixture) { PolygonShape polygon = fixture.getShape(); assert(polygon.count <= MAX_POLYGON_VERTICES); - List vertices = new Vec2Array().get(polygon.count); + List vertices = Vec2Array().get(polygon.count); for (int i = 0; i < polygon.count; ++i) { body.getWorldPointToOut(polygon.vertices[i], vertices[i]); viewport.getWorldToScreen(vertices[i], vertices[i]); } - List points = new List(); + List points = []; for (int i = 0; i < polygon.count; i++) { - points.add(new Offset(vertices[i].x, vertices[i].y)); + points.add(Offset(vertices[i].x, vertices[i].y)); } renderPolygon(canvas, points); } void renderPolygon(Canvas canvas, List points) { - final path = new Path()..addPolygon(points, true); - final Paint paint = new Paint() + final path = Path()..addPolygon(points, true); + final Paint paint = Paint() ..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 bedf49c4b..f1248447e 100644 --- a/lib/box2d/viewport.dart +++ b/lib/box2d/viewport.dart @@ -9,18 +9,16 @@ class Viewport extends ViewportTransform { double scale; Viewport(this.size, this.scale) - : super(new Vector2(size.width / 2, size.height / 2), - new Vector2(size.width / 2, size.height / 2), scale); + : super(Vector2(size.width / 2, size.height / 2), + Vector2(size.width / 2, size.height / 2), scale); double worldAlignBottom(double height) => -(size.height / 2 / scale) + height; /// Resizes the current view port. void resize(Size size) { this.size = size; - this.extents = - new Vector2.copy(new Vector2(size.width / 2, size.height / 2)); - this.center = - new Vector2.copy(new Vector2(size.width / 2, size.height / 2)); + this.extents = Vector2.copy(Vector2(size.width / 2, size.height / 2)); + this.center = Vector2.copy(Vector2(size.width / 2, size.height / 2)); } /// Computes the number of horizontal world meters of this viewport considering a percentage of its width. @@ -59,7 +57,7 @@ class Viewport extends ViewportTransform { double y = center.y; if (horizontal != null) { - Vector2 temp = new Vector2.zero(); + Vector2 temp = Vector2.zero(); getWorldToScreen(position, temp); var margin = horizontal / 2 * size.width / 2; @@ -73,7 +71,7 @@ class Viewport extends ViewportTransform { } if (vertical != null) { - Vector2 temp = new Vector2.zero(); + Vector2 temp = Vector2.zero(); getWorldToScreen(position, temp); var margin = vertical / 2 * size.height / 2; diff --git a/lib/components/animation_component.dart b/lib/components/animation_component.dart index 45d4e4bfc..aa5d9f9db 100644 --- a/lib/components/animation_component.dart +++ b/lib/components/animation_component.dart @@ -25,7 +25,7 @@ class AnimationComponent extends PositionComponent { }) { this.width = width; this.height = height; - this.animation = new Animation.sequenced( + this.animation = Animation.sequenced( imagePath, amount, textureX: textureX, diff --git a/lib/components/component.dart b/lib/components/component.dart index ff0d14a79..9901ea574 100644 --- a/lib/components/component.dart +++ b/lib/components/component.dart @@ -63,19 +63,19 @@ abstract class PositionComponent extends Component { double width = 0.0, height = 0.0; Anchor anchor = Anchor.topLeft; - Position toPosition() => new Position(x, y); + Position toPosition() => Position(x, y); void setByPosition(Position position) { this.x = position.x; this.y = position.y; } - Position toSize() => new Position(width, height); + Position toSize() => Position(width, height); void setBySize(Position size) { this.width = size.x; this.height = size.y; } - Rect toRect() => new Rect.fromLTWH(x, y, width, height); + Rect toRect() => Rect.fromLTWH(x, y, width, height); void setByRect(Rect rect) { this.x = rect.left; this.y = rect.top; @@ -113,7 +113,7 @@ class SpriteComponent extends PositionComponent { : this.rectangle(size, size, imagePath); SpriteComponent.rectangle(double width, double height, String imagePath) - : this.fromSprite(width, height, new Sprite(imagePath)); + : this.fromSprite(width, height, Sprite(imagePath)); SpriteComponent.fromSprite(double width, double height, this.sprite) { this.width = width; diff --git a/lib/components/composed_component.dart b/lib/components/composed_component.dart index 5631c3e15..263ebf761 100644 --- a/lib/components/composed_component.dart +++ b/lib/components/composed_component.dart @@ -10,7 +10,7 @@ import 'package:ordered_set/ordered_set.dart'; /// It resembles [BaseGame]. It has an [components] property and an [add] method mixin ComposedComponent on Component { OrderedSet components = - new OrderedSet(Comparing.on((c) => c.priority())); + OrderedSet(Comparing.on((c) => c.priority())); @override render(Canvas canvas) { diff --git a/lib/components/debug_component.dart b/lib/components/debug_component.dart index e1a33d06e..38b0b91dd 100644 --- a/lib/components/debug_component.dart +++ b/lib/components/debug_component.dart @@ -12,7 +12,7 @@ class DebugComponent extends PositionComponent { Color color = const Color(0xFFFF00FF); /// The actual paint used; by default it paints with stroke only and [color]. - Paint get paint => new Paint() + Paint get paint => Paint() ..color = color ..style = PaintingStyle.stroke; @@ -22,7 +22,7 @@ class DebugComponent extends PositionComponent { /// Renders the rectangle void render(Canvas c) { prepareCanvas(c); - c.drawRect(new Rect.fromLTWH(0.0, 0.0, width, height), paint); + c.drawRect(Rect.fromLTWH(0.0, 0.0, width, height), paint); } /// Don't do anything (change as desired) diff --git a/lib/components/parallax_component.dart b/lib/components/parallax_component.dart index f68e2a280..2004d93c6 100644 --- a/lib/components/parallax_component.dart +++ b/lib/components/parallax_component.dart @@ -35,7 +35,7 @@ class ParallaxRenderer { (rect.height / imageHeight) * (image.width / window.devicePixelRatio); var count = rect.width / imageWidth; - Rect fullRect = new Rect.fromLTWH( + Rect fullRect = Rect.fromLTWH( -scroll * imageWidth, rect.top, (count + 1) * imageWidth, rect.height); paintImage( @@ -51,7 +51,7 @@ abstract class ParallaxComponent extends PositionComponent { final BASE_SPEED = 30; final LAYER_DELTA = 40; - List _layers = new List(); + List _layers = []; Size _size; bool _loaded = false; @@ -66,7 +66,7 @@ abstract class ParallaxComponent extends PositionComponent { void load(List filenames) { final futures = filenames.fold(>[], (List> result, String filename) { - final layer = new ParallaxRenderer(filename); + final layer = ParallaxRenderer(filename); _layers.add(layer); result.add(layer.future); return result; @@ -96,8 +96,8 @@ abstract class ParallaxComponent extends PositionComponent { } void _drawLayers(Canvas canvas) { - Rect rect = new Rect.fromPoints( - const Offset(0.0, 0.0), new Offset(_size.width, _size.height)); + Rect rect = Rect.fromPoints( + const Offset(0.0, 0.0), Offset(_size.width, _size.height)); _layers.forEach((layer) => layer.render(canvas, rect)); } diff --git a/lib/components/text_box_component.dart b/lib/components/text_box_component.dart index 33aeae5eb..3e5c15ded 100644 --- a/lib/components/text_box_component.dart +++ b/lib/components/text_box_component.dart @@ -137,9 +137,9 @@ class TextBoxComponent extends PositionComponent with Resizable { } Future _redrawCache() { - PictureRecorder recorder = new PictureRecorder(); - Canvas c = new Canvas(recorder, - new Rect.fromLTWH(0.0, 0.0, width.toDouble(), height.toDouble())); + PictureRecorder recorder = PictureRecorder(); + Canvas c = Canvas( + recorder, Rect.fromLTWH(0.0, 0.0, width.toDouble(), height.toDouble())); _fullRender(c); return recorder.endRecording().toImage(width.toInt(), height.toInt()); } @@ -156,13 +156,13 @@ class TextBoxComponent extends PositionComponent with Resizable { charCount += _lines[line].length; _config .toTextPainter(_lines[line]) - .paint(c, new Offset(_boxConfig.margin, dy)); + .paint(c, Offset(_boxConfig.margin, dy)); dy += _lineHeight; } int max = math.min(currentChar - charCount, _lines[_currentLine].length); _config .toTextPainter(_lines[_currentLine].substring(0, max)) - .paint(c, new Offset(_boxConfig.margin, dy)); + .paint(c, Offset(_boxConfig.margin, dy)); } void redrawLater() async { diff --git a/lib/components/tiled_component.dart b/lib/components/tiled_component.dart index ff4e0ca51..522b4581d 100644 --- a/lib/components/tiled_component.dart +++ b/lib/components/tiled_component.dart @@ -10,11 +10,11 @@ class TiledComponent extends Component { String filename; TileMap map; Image image; - Map images = new Map(); + Map images = Map(); Future future; bool _loaded = false; - static Paint paint = new Paint()..color = Colors.white; + static Paint paint = Paint()..color = Colors.white; TiledComponent(this.filename) { this.future = _load(); @@ -29,13 +29,13 @@ class TiledComponent extends Component { Future _loadMap() { return Flame.bundle.loadString('assets/tiles/' + filename).then((contents) { - var parser = new TileMapParser(); + var parser = TileMapParser(); return parser.parse(contents); }); } Future> _loadImages(TileMap map) async { - var result = new Map(); + Map result = {}; await Future.forEach(map.tilesets, (tileset) async { await Future.forEach(tileset.images, (tmxImage) async { result[tmxImage.source] = await Flame.images.load(tmxImage.source); diff --git a/lib/flame.dart b/lib/flame.dart index 20f72a06f..15b1eaec7 100644 --- a/lib/flame.dart +++ b/lib/flame.dart @@ -20,16 +20,16 @@ class Flame { static AssetBundle get bundle => _bundle == null ? rootBundle : _bundle; /// Access a shared instance of the [AudioCache] class. - static AudioCache audio = new AudioCache(prefix: 'audio/'); + static AudioCache audio = AudioCache(prefix: 'audio/'); /// Access a shared instance of the [Images] class. - static Images images = new Images(); + static Images images = Images(); /// Access a shared instance of the [Util] class. - static Util util = new Util(); + static Util util = Util(); /// Access a shard instance of [AssetsCache] class. - static AssetsCache assets = new AssetsCache(); + static AssetsCache assets = AssetsCache(); static Future init( {AssetBundle bundle, @@ -62,7 +62,7 @@ class FlameBiding extends BindingBase with GestureBinding, ServicesBinding { static FlameBiding instance; static FlameBiding ensureInitialized() { - if (FlameBiding.instance == null) new FlameBiding(); + if (FlameBiding.instance == null) FlameBiding(); return FlameBiding.instance; } } diff --git a/lib/game.dart b/lib/game.dart index 8f3f91d5f..229ce3445 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -14,10 +14,10 @@ import 'position.dart'; /// Represents a generic game. /// /// Subclass this to implement the [update] and [render] methods. -/// Flame will deal with calling these methods properly when the game's [widget] is rendered. +/// Flame will deal with calling these methods properly when the game's widget is rendered. abstract class Game extends StatelessWidget { // Widget Builder for this Game - final builder = new WidgetBuilder(); + final builder = WidgetBuilder(); /// Implement this method to update the game state, given that a time [t] has passed. /// @@ -52,10 +52,10 @@ abstract class Game extends StatelessWidget { class WidgetBuilder { Offset offset = Offset.zero; - Widget build(Game game) => new Center( - child: new Directionality( + Widget build(Game game) => Center( + child: Directionality( textDirection: TextDirection.ltr, - child: new _GameRenderObjectWidget(game))); + child: _GameRenderObjectWidget(game))); } class _GameRenderObjectWidget extends SingleChildRenderObjectWidget { @@ -65,7 +65,7 @@ class _GameRenderObjectWidget extends SingleChildRenderObjectWidget { @override RenderObject createRenderObject(BuildContext context) => - new _GameRenderBox(context, this.game); + _GameRenderBox(context, this.game); @override void updateRenderObject(BuildContext context, _GameRenderBox _gameRenderBox) { @@ -167,7 +167,7 @@ class _GameRenderBox extends RenderBox with WidgetsBindingObserver { abstract class BaseGame extends Game { /// The list of components to be updated and rendered by the base game. OrderedSet components = - new OrderedSet(Comparing.on((c) => c.priority())); + OrderedSet(Comparing.on((c) => c.priority())); /// Components added by the [addLater] method List _addLater = []; @@ -176,7 +176,7 @@ abstract class BaseGame extends Game { Size size; /// Camera position; every non-HUD component is translated so that the camera is drawn in the center of the screen - Position camera = new Position.empty(); + Position camera = Position.empty(); /// List of deltas used in debug mode to calculate FPS List _dts = []; @@ -297,7 +297,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() / + return DateTime.now().microsecondsSinceEpoch.toDouble() / Duration.microsecondsPerSecond; } } @@ -325,7 +325,7 @@ class EmbeddedGameWidget extends StatefulWidget { @override State createState() { - return new _EmbeddedGameWidgetState(game, size: size); + return _EmbeddedGameWidgetState(game, size: size); } } @@ -355,10 +355,10 @@ class _EmbeddedGameWidgetState extends State { @override Widget build(BuildContext context) { if (size == null) { - return game.widget; + return game; } return Container( - child: game.widget, + child: game, constraints: BoxConstraints( minWidth: size.x, maxWidth: size.x, diff --git a/lib/images.dart b/lib/images.dart index b13271c90..17e5d26cd 100644 --- a/lib/images.dart +++ b/lib/images.dart @@ -28,8 +28,8 @@ class Images { Future _fetchToMemory(String name) async { ByteData data = await Flame.bundle.load('assets/images/' + name); - Uint8List bytes = new Uint8List.view(data.buffer); - Completer completer = new Completer(); + Uint8List bytes = Uint8List.view(data.buffer); + Completer completer = Completer(); decodeImageFromList(bytes, (image) => completer.complete(image)); return completer.future; } diff --git a/lib/palette.dart b/lib/palette.dart index b88b46126..c48547407 100644 --- a/lib/palette.dart +++ b/lib/palette.dart @@ -3,7 +3,7 @@ import 'dart:ui'; class PaletteEntry { final Color color; - Paint get paint => new Paint()..color = color; + Paint get paint => Paint()..color = color; const PaletteEntry(this.color); diff --git a/lib/position.dart b/lib/position.dart index 8604da0b4..98f8e2d3f 100644 --- a/lib/position.dart +++ b/lib/position.dart @@ -75,24 +75,28 @@ class Position { return this; } + double distance(Position other) { + return this.minus(other).length(); + } + ui.Offset toOffset() { - return new ui.Offset(x, y); + return ui.Offset(x, y); } ui.Size toSize() { - return new ui.Size(x, y); + return ui.Size(x, y); } math.Point toPoint() { - return new math.Point(x, y); + return math.Point(x, y); } b2d.Vector2 toVector() { - return new b2d.Vector2(x, y); + return b2d.Vector2(x, y); } Position clone() { - return new Position.fromPosition(this); + return Position.fromPosition(this); } @override @@ -101,7 +105,7 @@ class Position { } static ui.Rect rectFrom(Position topLeft, Position size) { - return new ui.Rect.fromLTWH(topLeft.x, topLeft.y, size.x, size.y); + return ui.Rect.fromLTWH(topLeft.x, topLeft.y, size.x, size.y); } static ui.Rect bounds(List pts) { @@ -109,7 +113,6 @@ 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 ui.Rect.fromPoints(ui.Offset(minx, miny), ui.Offset(maxx, maxy)); } } diff --git a/lib/profiler.dart b/lib/profiler.dart index a8b0b74b9..8df91bb89 100644 --- a/lib/profiler.dart +++ b/lib/profiler.dart @@ -22,6 +22,6 @@ class Profiler { } static double currentTime() => - new DateTime.now().microsecondsSinceEpoch.toDouble() / + DateTime.now().microsecondsSinceEpoch.toDouble() / Duration.microsecondsPerMillisecond; } diff --git a/lib/sprite.dart b/lib/sprite.dart index fba12d69b..fd78fb41a 100644 --- a/lib/sprite.dart +++ b/lib/sprite.dart @@ -25,7 +25,7 @@ class Sprite { height = img.height.toDouble(); } this.image = img; - this.src = new Rect.fromLTWH(x, y, width, height); + this.src = Rect.fromLTWH(x, y, width, height); }); } @@ -42,7 +42,7 @@ class Sprite { if (height == null) { height = image.height.toDouble(); } - this.src = new Rect.fromLTWH(x, y, width, height); + this.src = Rect.fromLTWH(x, y, width, height); } static Future loadSprite( @@ -53,7 +53,7 @@ class Sprite { double height = null, }) async { Image image = await Flame.images.load(fileName); - return new Sprite.fromImage( + return Sprite.fromImage( image, x: x, y: y, @@ -74,11 +74,11 @@ class Sprite { if (!loaded()) { return null; } - return new Position(_imageWidth, _imageHeight); + return Position(_imageWidth, _imageHeight); } Position get size { - return new Position(src.width, src.height); + return Position(src.width, src.height); } /// Renders this Sprite on the position [p], scaled by the [scale] factor provided. @@ -107,7 +107,7 @@ class Sprite { } width ??= this.size.x; height ??= this.size.y; - renderRect(canvas, new Rect.fromLTWH(0.0, 0.0, width, height)); + renderRect(canvas, Rect.fromLTWH(0.0, 0.0, width, height)); } void renderRect(Canvas canvas, Rect dst) { @@ -127,6 +127,6 @@ class Sprite { } size ??= this.size; renderRect(canvas, - new Rect.fromLTWH(p.x - size.x / 2, p.y - size.y / 2, size.x, size.y)); + Rect.fromLTWH(p.x - size.x / 2, p.y - size.y / 2, size.x, size.y)); } } diff --git a/lib/text_config.dart b/lib/text_config.dart index a3e1e2ed0..d892eaa9c 100644 --- a/lib/text_config.dart +++ b/lib/text_config.dart @@ -90,16 +90,16 @@ class TextConfig { /// However, you probably want to use the [render] method witch already renders for you considering the anchor. /// That way, you don't need to perform the math for yourself. material.TextPainter toTextPainter(String text) { - material.TextStyle style = new material.TextStyle( + material.TextStyle style = material.TextStyle( color: color, fontSize: fontSize, fontFamily: fontFamily, ); - material.TextSpan span = new material.TextSpan( + material.TextSpan span = material.TextSpan( style: style, text: text, ); - material.TextPainter tp = new material.TextPainter( + material.TextPainter tp = material.TextPainter( text: span, textAlign: textAlign, textDirection: textDirection, diff --git a/lib/util.dart b/lib/util.dart index 0770363e7..9cb3c3575 100644 --- a/lib/util.dart +++ b/lib/util.dart @@ -38,9 +38,9 @@ class Util { Future initialDimensions() async { // https://github.com/flutter/flutter/issues/5259 // "In release mode we start off at 0x0 but we don't in debug mode" - return await new Future(() { + return await Future(() { if (window.physicalSize.isEmpty) { - final completer = new Completer(); + final completer = Completer(); window.onMetricsChanged = () { if (!window.physicalSize.isEmpty) { completer.complete(window.physicalSize / window.devicePixelRatio); @@ -57,8 +57,8 @@ class Util { /// Use this in order to get it to work in case your app also contains other widgets. void addGestureRecognizer(GestureRecognizer recognizer) { if (GestureBinding.instance == null) { - throw new Exception( - "GestureBinding is not initialized yet, this probably happened because addGestureRecognizer was called before the runApp method"); + throw Exception( + 'GestureBinding is not initialized yet, this probably happened because addGestureRecognizer was called before the runApp method'); } GestureBinding.instance.pointerRouter.addGlobalRoute((PointerEvent e) { diff --git a/scripts/build.sh b/scripts/build.sh index 83e093617..b57deb00e 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -3,5 +3,3 @@ dartanalyzer . flutter format . flutter test - -dartdoc diff --git a/scripts/gen-doc.sh b/scripts/gen-doc.sh new file mode 100755 index 000000000..ce4c9bb5b --- /dev/null +++ b/scripts/gen-doc.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +dartdoc diff --git a/test/components/box2d/viewport_test.dart b/test/components/box2d/viewport_test.dart index bdef463a6..6cee719ff 100644 --- a/test/components/box2d/viewport_test.dart +++ b/test/components/box2d/viewport_test.dart @@ -4,25 +4,25 @@ import 'package:flame/box2d/viewport.dart'; import 'package:test/test.dart'; void main() { - final viewport = new Viewport(new Size(100.0, 100.0), 1.0); + final viewport = Viewport(Size(100.0, 100.0), 1.0); - group("getCenterHorizontalScreenPercentage", () { - test("center starts in the middle", () { + group('getCenterHorizontalScreenPercentage', () { + test('center starts in the middle', () { viewport.setCamera(50.0, viewport.center.y, 1.0); expect(viewport.getCenterHorizontalScreenPercentage(), equals(0.5)); }); - test("it increases when it move to right", () { + test('it increases when it move to right', () { viewport.setCamera(75.0, viewport.center.y, 1.0); expect(viewport.getCenterHorizontalScreenPercentage(), equals(0.75)); }); - test("it decreases when it moves to left", () { + test('it decreases when it moves to left', () { viewport.setCamera(25.0, viewport.center.y, 1.0); expect(viewport.getCenterHorizontalScreenPercentage(), equals(0.25)); }); - test("it flips on edges", () { + test('it flips on edges', () { viewport.setCamera(110.0, viewport.center.y, 1.0); expect(viewport.getCenterHorizontalScreenPercentage(), equals(0.10)); @@ -30,7 +30,7 @@ void main() { expect(viewport.getCenterHorizontalScreenPercentage(), equals(0.90)); }); - test("it increases slower with more screens", () { + test('it increases slower with more screens', () { viewport.setCamera(50.0, viewport.center.y, 1.0); expect(viewport.getCenterHorizontalScreenPercentage(screens: 2.0), equals(0.5)); @@ -44,7 +44,7 @@ void main() { equals(0.55)); }); - test("it flips on edges also with more screens", () { + test('it flips on edges also with more screens', () { viewport.setCamera(170.0, viewport.center.y, 1.0); expect(viewport.getCenterHorizontalScreenPercentage(screens: 2.0), equals(0.10)); diff --git a/test/components/component_test.dart b/test/components/component_test.dart index ff08b120c..d768c304d 100644 --- a/test/components/component_test.dart +++ b/test/components/component_test.dart @@ -7,31 +7,31 @@ import 'package:flame/position.dart'; void main() { group('component test', () { test('test get/set x/y or position', () { - PositionComponent c = new SpriteComponent(); + PositionComponent c = SpriteComponent(); c.x = 2.2; c.y = 3.4; expect(c.toPosition().x, 2.2); expect(c.toPosition().y, 3.4); - c.setByPosition(new Position(1.0, 0.0)); + c.setByPosition(Position(1.0, 0.0)); expect(c.x, 1.0); expect(c.y, 0.0); }); test('test get/set widt/height or size', () { - PositionComponent c = new SpriteComponent(); + PositionComponent c = SpriteComponent(); c.width = 2.2; c.height = 3.4; expect(c.toSize().x, 2.2); expect(c.toSize().y, 3.4); - c.setBySize(new Position(1.0, 0.0)); + c.setBySize(Position(1.0, 0.0)); expect(c.width, 1.0); expect(c.height, 0.0); }); test('test get/set rect', () { - PositionComponent c = new SpriteComponent(); + PositionComponent c = SpriteComponent(); c.x = 0.0; c.y = 1.0; c.width = 2.0; @@ -41,7 +41,7 @@ void main() { expect(c.toRect().width, 2.0); expect(c.toRect().height, 2.0); - c.setByRect(new Rect.fromLTWH(10.0, 10.0, 1.0, 1.0)); + c.setByRect(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/components/tiled_test.dart b/test/components/tiled_test.dart index 7ef1be64c..34be9edcc 100644 --- a/test/components/tiled_test.dart +++ b/test/components/tiled_test.dart @@ -9,8 +9,8 @@ import 'package:test/test.dart'; void main() { test('my first widget test', () async { - Flame.initialize(new TestAssetBundle()); - var tiled = new TiledComponent('x'); + await Flame.init(bundle: TestAssetBundle()); + var tiled = TiledComponent('x'); await tiled.future; expect(1, equals(1)); }); @@ -18,11 +18,11 @@ void main() { class TestAssetBundle extends CachingAssetBundle { @override - Future load(String key) async => new File('assets/map-level1.png') + Future load(String key) async => File('assets/map-level1.png') .readAsBytes() .then((bytes) => ByteData.view(Uint8List.fromList(bytes).buffer)); @override Future loadString(String key, {bool cache = true}) => - new File('assets/map.tmx').readAsString(); + File('assets/map.tmx').readAsString(); } diff --git a/test/position_test.dart b/test/position_test.dart index 16d894183..6f1a2811d 100644 --- a/test/position_test.dart +++ b/test/position_test.dart @@ -10,15 +10,15 @@ void expectDouble(double d1, double d2) { void main() { group('position test', () { test('test add', () { - Position p = new Position(0.0, 5.0); - Position p2 = p.add(new Position(5.0, 5.0)); + Position p = Position(0.0, 5.0); + Position p2 = p.add(Position(5.0, 5.0)); expect(p, p2); expectDouble(p.x, 5.0); expectDouble(p.y, 10.0); }); test('test clone', () { - Position p = new Position(1.0, 0.0); + Position p = Position(1.0, 0.0); Position clone = p.clone(); clone.times(2.0); @@ -27,14 +27,21 @@ void main() { }); test('test rotate', () { - Position p = new Position(1.0, 0.0).rotate(math.pi / 2); + Position p = Position(1.0, 0.0).rotate(math.pi / 2); expectDouble(p.x, 0.0); expectDouble(p.y, 1.0); }); test('test length', () { - Position p = new Position(3.0, 4.0); + Position p = Position(3.0, 4.0); expectDouble(p.length(), 5.0); }); + + test('test distance', () { + Position p1 = Position(10.0, 20.0); + Position p2 = Position(13.0, 24.0); + double result = p1.distance(p2); + expectDouble(result, 5.0); + }); }); } diff --git a/test/resizable_test.dart b/test/resizable_test.dart index f39063f33..0ca7a56de 100644 --- a/test/resizable_test.dart +++ b/test/resizable_test.dart @@ -23,23 +23,23 @@ Size size = const Size(1.0, 1.0); void main() { group('resizable test', () { test('propagate resize to children', () { - MyComponent a = new MyComponent('a'); - MyComponent b = new MyComponent('b', myChildren: [a]); + MyComponent a = MyComponent('a'); + MyComponent b = MyComponent('b', myChildren: [a]); b.resize(size); expect(a.size, size); }); test('game calls resize on add', () { - MyComponent a = new MyComponent('a'); - MyGame game = new MyGame(); + MyComponent a = MyComponent('a'); + MyGame game = MyGame(); game.resize(size); game.add(a); expect(a.size, size); }); test('game calls resize after added', () { - MyComponent a = new MyComponent('a'); - MyGame game = new MyGame(); + MyComponent a = MyComponent('a'); + MyGame game = MyGame(); game.add(a); game.resize(size); expect(a.size, size);