mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-11-01 01:18:38 +08:00 
			
		
		
		
	Fixes build (tests), adding ditance method to position, fix linting (formating, quotes), removed uneeded new keywords everywhere
This commit is contained in:
		| @ -1,10 +1,3 @@ | |||||||
| analyzer: |  | ||||||
|   language: |  | ||||||
|     enablePreviewDart2: true |  | ||||||
|     enableStrictCallChecks: true |  | ||||||
|     enableSuperMixins: true |  | ||||||
|   strong-mode: true |  | ||||||
|  |  | ||||||
| # Source of linter options: | # Source of linter options: | ||||||
| # http://dart-lang.github.io/linter/lints/options/options.html | # http://dart-lang.github.io/linter/lints/options/options.html | ||||||
|  |  | ||||||
|  | |||||||
| @ -23,14 +23,14 @@ class MyHomePage extends StatefulWidget { | |||||||
|  |  | ||||||
| class _MyHomePageState extends State<MyHomePage> { | class _MyHomePageState extends State<MyHomePage> { | ||||||
|   void _clickFab(GlobalKey<ScaffoldState> key) { |   void _clickFab(GlobalKey<ScaffoldState> key) { | ||||||
|     key.currentState.showSnackBar(new SnackBar( |     key.currentState.showSnackBar(SnackBar( | ||||||
|       content: new Text('You clicked the FAB!'), |       content: Text('You clicked the FAB!'), | ||||||
|     )); |     )); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   Widget build(BuildContext context) { |   Widget build(BuildContext context) { | ||||||
|     final key = new GlobalKey<ScaffoldState>(); |     final key = GlobalKey<ScaffoldState>(); | ||||||
|     return Scaffold( |     return Scaffold( | ||||||
|       key: key, |       key: key, | ||||||
|       appBar: AppBar( |       appBar: AppBar( | ||||||
|  | |||||||
| @ -4,7 +4,7 @@ import 'package:flame/animation.dart' as FlameAnimation; | |||||||
| import 'package:flame/components/animation_component.dart'; | import 'package:flame/components/animation_component.dart'; | ||||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||||
|  |  | ||||||
| void main() => runApp(new MyGame().widget); | void main() => runApp(MyGame()); | ||||||
|  |  | ||||||
| class MyGame extends BaseGame { | class MyGame extends BaseGame { | ||||||
|   MyGame() { |   MyGame() { | ||||||
| @ -15,9 +15,7 @@ class MyGame extends BaseGame { | |||||||
|     Size size = await Flame.util.initialDimensions(); |     Size size = await Flame.util.initialDimensions(); | ||||||
|  |  | ||||||
|     final animation = await FlameAnimation.Animation.fromAsepriteData( |     final animation = await FlameAnimation.Animation.fromAsepriteData( | ||||||
|       "chopper.png", |         'chopper.png', 'chopper.json'); | ||||||
|       "chopper.json" |  | ||||||
|     ); |  | ||||||
|     final animationComponent = AnimationComponent(200, 200, animation); |     final animationComponent = AnimationComponent(200, 200, animation); | ||||||
|  |  | ||||||
|     animationComponent.x = (size.width / 2) - 100; |     animationComponent.x = (size.width / 2) - 100; | ||||||
| @ -26,4 +24,3 @@ class MyGame extends BaseGame { | |||||||
|     add(animationComponent); |     add(animationComponent); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | |||||||
| @ -9,7 +9,7 @@ import 'package:flame/palette.dart'; | |||||||
| import 'package:flame/text_config.dart'; | import 'package:flame/text_config.dart'; | ||||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||||
|  |  | ||||||
| void main() => runApp(new MyGame().widget); | void main() => runApp(MyGame()); | ||||||
|  |  | ||||||
| TextConfig regular = TextConfig(color: BasicPalette.white.color); | TextConfig regular = TextConfig(color: BasicPalette.white.color); | ||||||
| TextConfig tiny = regular.withFontSize(12.0); | TextConfig tiny = regular.withFontSize(12.0); | ||||||
| @ -21,10 +21,10 @@ class MyTextBox extends TextBoxComponent { | |||||||
|   @override |   @override | ||||||
|   void drawBackground(Canvas c) { |   void drawBackground(Canvas c) { | ||||||
|     Rect rect = Rect.fromLTWH(0, 0, width, height); |     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( |     c.drawRect( | ||||||
|         rect.deflate(boxConfig.margin), |         rect.deflate(boxConfig.margin), | ||||||
|         new Paint() |         Paint() | ||||||
|           ..color = BasicPalette.black.color |           ..color = BasicPalette.black.color | ||||||
|           ..style = PaintingStyle.stroke); |           ..style = PaintingStyle.stroke); | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -3,12 +3,12 @@ import 'package:flame/game.dart'; | |||||||
| import 'package:flutter/widgets.dart'; | import 'package:flutter/widgets.dart'; | ||||||
|  |  | ||||||
| void main() { | void main() { | ||||||
|   var game = new TiledGame(); |   TiledGame game = TiledGame(); | ||||||
|   runApp(game.widget); |   runApp(game); | ||||||
| } | } | ||||||
|  |  | ||||||
| class TiledGame extends BaseGame { | class TiledGame extends BaseGame { | ||||||
|   TiledGame() { |   TiledGame() { | ||||||
|     add(new TiledComponent('map.tmx')); |     add(TiledComponent('map.tmx')); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -7,7 +7,7 @@ import 'package:flame/game.dart'; | |||||||
| import 'package:flame/palette.dart'; | import 'package:flame/palette.dart'; | ||||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||||
|  |  | ||||||
| void main() => runApp(new MyGame().widget); | void main() => runApp(MyGame()); | ||||||
|  |  | ||||||
| class Palette { | class Palette { | ||||||
|   static const PaletteEntry white = BasicPalette.white; |   static const PaletteEntry white = BasicPalette.white; | ||||||
| @ -47,6 +47,6 @@ class Square extends PositionComponent { | |||||||
|  |  | ||||||
| class MyGame extends BaseGame { | class MyGame extends BaseGame { | ||||||
|   MyGame() { |   MyGame() { | ||||||
|     add(new Square(64.0)); |     add(Square(64.0)); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -18,7 +18,7 @@ class Anchor { | |||||||
|   const Anchor(this.relativePosition); |   const Anchor(this.relativePosition); | ||||||
|  |  | ||||||
|   Position translate(Position p, Position size) { |   Position translate(Position p, Position size) { | ||||||
|     return p.clone().minus(new Position( |     return p.clone().minus( | ||||||
|         size.x * relativePosition.dx, size.y * relativePosition.dy)); |         Position(size.x * relativePosition.dx, size.y * relativePosition.dy)); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -41,7 +41,7 @@ class Animation { | |||||||
|   /// |   /// | ||||||
|   /// All frames have the same [stepTime]. |   /// All frames have the same [stepTime]. | ||||||
|   Animation.spriteList(List<Sprite> sprites, {double stepTime, this.loop}) { |   Animation.spriteList(List<Sprite> 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. |   /// 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) |   /// [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 |   /// 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. |   /// This will create the i-th animation on the 'sheet.png', given it has 8 frames. | ||||||
|   Animation.sequenced( |   Animation.sequenced( | ||||||
|     String imagePath, |     String imagePath, | ||||||
| @ -68,16 +68,16 @@ class Animation { | |||||||
|     double textureHeight, |     double textureHeight, | ||||||
|     double stepTime = 0.1, |     double stepTime = 0.1, | ||||||
|   }) { |   }) { | ||||||
|     this.frames = new List<Frame>(amount); |     this.frames = List<Frame>(amount); | ||||||
|     for (var i = 0; i < amount; i++) { |     for (var i = 0; i < amount; i++) { | ||||||
|       Sprite sprite = new Sprite( |       Sprite sprite = Sprite( | ||||||
|         imagePath, |         imagePath, | ||||||
|         x: textureX + i * textureWidth, |         x: textureX + i * textureWidth, | ||||||
|         y: textureY, |         y: textureY, | ||||||
|         width: textureWidth, |         width: textureWidth, | ||||||
|         height: textureHeight, |         height: textureHeight, | ||||||
|       ); |       ); | ||||||
|       this.frames[i] = new Frame(sprite, stepTime); |       this.frames[i] = Frame(sprite, stepTime); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @ -91,16 +91,16 @@ class Animation { | |||||||
|     double textureWidth, |     double textureWidth, | ||||||
|     double textureHeight, |     double textureHeight, | ||||||
|   }) { |   }) { | ||||||
|     this.frames = new List<Frame>(amount); |     this.frames = List<Frame>(amount); | ||||||
|     for (var i = 0; i < amount; i++) { |     for (var i = 0; i < amount; i++) { | ||||||
|       Sprite sprite = new Sprite( |       Sprite sprite = Sprite( | ||||||
|         imagePath, |         imagePath, | ||||||
|         x: textureX + i * textureWidth, |         x: textureX + i * textureWidth, | ||||||
|         y: textureY, |         y: textureY, | ||||||
|         width: textureWidth, |         width: textureWidth, | ||||||
|         height: textureHeight, |         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 |   /// [imagePath]: Source of the spritesheet animation | ||||||
|   /// [dataPath]: Animation's exported data in json format |   /// [dataPath]: Animation's exported data in json format | ||||||
|   static Future<Animation> fromAsepriteData(String imagePath, String dataPath) async { |   static Future<Animation> fromAsepriteData( | ||||||
|  |       String imagePath, String dataPath) async { | ||||||
|     String content = await Flame.assets.readFile(dataPath); |     String content = await Flame.assets.readFile(dataPath); | ||||||
|     Map<String, dynamic> json = jsonDecode(content); |     Map<String, dynamic> json = jsonDecode(content); | ||||||
|  |  | ||||||
|     Map<String, dynamic> jsonFrames = json["frames"]; |     Map<String, dynamic> jsonFrames = json['frames']; | ||||||
|  |  | ||||||
|     var frames = jsonFrames.values.map((value) { |     var frames = jsonFrames.values.map((value) { | ||||||
|       final frameData = value["frame"]; |       final frameData = value['frame']; | ||||||
|       final int x = frameData["x"]; |       final int x = frameData['x']; | ||||||
|       final int y = frameData["y"]; |       final int y = frameData['y']; | ||||||
|       final int width = frameData["w"]; |       final int width = frameData['w']; | ||||||
|       final int height = frameData["h"]; |       final int height = frameData['h']; | ||||||
|  |  | ||||||
|       final stepTime = value["duration"] / 1000; |       final stepTime = value['duration'] / 1000; | ||||||
|  |  | ||||||
|       Sprite sprite = new Sprite( |       Sprite sprite = Sprite( | ||||||
|         imagePath, |         imagePath, | ||||||
|         x: x.toDouble(), |         x: x.toDouble(), | ||||||
|         y: y.toDouble(), |         y: y.toDouble(), | ||||||
| @ -132,7 +133,7 @@ class Animation { | |||||||
|         height: height.toDouble(), |         height: height.toDouble(), | ||||||
|       ); |       ); | ||||||
|  |  | ||||||
|       return new Frame(sprite, stepTime); |       return Frame(sprite, stepTime); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     return Animation(frames.toList(), loop: true); |     return Animation(frames.toList(), loop: true); | ||||||
|  | |||||||
| @ -26,6 +26,6 @@ class AssetsCache { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   Future<String> _readFile(String fileName) async { |   Future<String> _readFile(String fileName) async { | ||||||
|     return await rootBundle.loadString("assets/" + fileName); |     return await rootBundle.loadString('assets/$fileName'); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -19,14 +19,14 @@ class AudioPool { | |||||||
|   bool repeating; |   bool repeating; | ||||||
|   int minPlayers, maxPlayers; |   int minPlayers, maxPlayers; | ||||||
|  |  | ||||||
|   Lock _lock = new Lock(); |   Lock _lock = Lock(); | ||||||
|  |  | ||||||
|   AudioPool(this.sound, |   AudioPool(this.sound, | ||||||
|       {this.repeating = false, |       {this.repeating = false, | ||||||
|       this.maxPlayers = 1, |       this.maxPlayers = 1, | ||||||
|       this.minPlayers = 1, |       this.minPlayers = 1, | ||||||
|       String prefix = 'audio/sfx/'}) { |       String prefix = 'audio/sfx/'}) { | ||||||
|     cache = new AudioCache(prefix: prefix); |     cache = AudioCache(prefix: prefix); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void init() async { |   void init() async { | ||||||
| @ -66,7 +66,7 @@ class AudioPool { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   Future<AudioPlayer> _createNewAudioPlayer() async { |   Future<AudioPlayer> _createNewAudioPlayer() async { | ||||||
|     AudioPlayer player = new AudioPlayer(); |     AudioPlayer player = AudioPlayer(); | ||||||
|     String url = (await cache.load(sound)).path; |     String url = (await cache.load(sound)).path; | ||||||
|     await player.setUrl(url); |     await player.setUrl(url); | ||||||
|     await player.setReleaseMode(ReleaseMode.STOP); |     await player.setReleaseMode(ReleaseMode.STOP); | ||||||
|  | |||||||
| @ -32,9 +32,9 @@ abstract class Box2DComponent extends Component { | |||||||
|     if (this.dimensions == null) { |     if (this.dimensions == null) { | ||||||
|       this.dimensions = window.physicalSize; |       this.dimensions = window.physicalSize; | ||||||
|     } |     } | ||||||
|     final pool = new DefaultWorldPool(worldPoolSize, worldPoolContainerSize); |     final pool = DefaultWorldPool(worldPoolSize, worldPoolContainerSize); | ||||||
|     this.world = new World.withPool(new Vector2(0.0, gravity), pool); |     this.world = World.withPool(Vector2(0.0, gravity), pool); | ||||||
|     this.viewport = new Viewport(dimensions, scale); |     this.viewport = Viewport(dimensions, scale); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
| @ -47,7 +47,7 @@ abstract class Box2DComponent extends Component { | |||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   void render(canvas) { |   void render(canvas) { | ||||||
|     if (viewport.size == new Size(0.0, 0.0)) { |     if (viewport.size == Size(0.0, 0.0)) { | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     components.forEach((c) { |     components.forEach((c) { | ||||||
| @ -119,13 +119,13 @@ abstract class BodyComponent extends Component { | |||||||
|         fixture = fixture.getNext()) { |         fixture = fixture.getNext()) { | ||||||
|       switch (fixture.getType()) { |       switch (fixture.getType()) { | ||||||
|         case ShapeType.CHAIN: |         case ShapeType.CHAIN: | ||||||
|           throw new Exception("not implemented"); |           throw Exception('not implemented'); | ||||||
|           break; |           break; | ||||||
|         case ShapeType.CIRCLE: |         case ShapeType.CIRCLE: | ||||||
|           _renderCircle(canvas, fixture); |           _renderCircle(canvas, fixture); | ||||||
|           break; |           break; | ||||||
|         case ShapeType.EDGE: |         case ShapeType.EDGE: | ||||||
|           throw new Exception("not implemented"); |           throw Exception('not implemented'); | ||||||
|           break; |           break; | ||||||
|         case ShapeType.POLYGON: |         case ShapeType.POLYGON: | ||||||
|           _renderPolygon(canvas, fixture); |           _renderPolygon(canvas, fixture); | ||||||
| @ -137,16 +137,16 @@ abstract class BodyComponent extends Component { | |||||||
|   Vector2 get center => this.body.worldCenter; |   Vector2 get center => this.body.worldCenter; | ||||||
|  |  | ||||||
|   void _renderCircle(Canvas canvas, Fixture fixture) { |   void _renderCircle(Canvas canvas, Fixture fixture) { | ||||||
|     Vector2 center = new Vector2.zero(); |     Vector2 center = Vector2.zero(); | ||||||
|     CircleShape circle = fixture.getShape(); |     CircleShape circle = fixture.getShape(); | ||||||
|     body.getWorldPointToOut(circle.p, center); |     body.getWorldPointToOut(circle.p, center); | ||||||
|     viewport.getWorldToScreen(center, center); |     viewport.getWorldToScreen(center, center); | ||||||
|     renderCircle( |     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) { |   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); |       ..color = const Color.fromARGB(255, 255, 255, 255); | ||||||
|     canvas.drawCircle(center, radius, paint); |     canvas.drawCircle(center, radius, paint); | ||||||
|   } |   } | ||||||
| @ -154,24 +154,24 @@ abstract class BodyComponent extends Component { | |||||||
|   void _renderPolygon(Canvas canvas, Fixture fixture) { |   void _renderPolygon(Canvas canvas, Fixture fixture) { | ||||||
|     PolygonShape polygon = fixture.getShape(); |     PolygonShape polygon = fixture.getShape(); | ||||||
|     assert(polygon.count <= MAX_POLYGON_VERTICES); |     assert(polygon.count <= MAX_POLYGON_VERTICES); | ||||||
|     List<Vector2> vertices = new Vec2Array().get(polygon.count); |     List<Vector2> vertices = Vec2Array().get(polygon.count); | ||||||
|  |  | ||||||
|     for (int i = 0; i < polygon.count; ++i) { |     for (int i = 0; i < polygon.count; ++i) { | ||||||
|       body.getWorldPointToOut(polygon.vertices[i], vertices[i]); |       body.getWorldPointToOut(polygon.vertices[i], vertices[i]); | ||||||
|       viewport.getWorldToScreen(vertices[i], vertices[i]); |       viewport.getWorldToScreen(vertices[i], vertices[i]); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     List<Offset> points = new List(); |     List<Offset> points = []; | ||||||
|     for (int i = 0; i < polygon.count; i++) { |     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); |     renderPolygon(canvas, points); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void renderPolygon(Canvas canvas, List<Offset> points) { |   void renderPolygon(Canvas canvas, List<Offset> points) { | ||||||
|     final path = new Path()..addPolygon(points, true); |     final path = Path()..addPolygon(points, true); | ||||||
|     final Paint paint = new Paint() |     final Paint paint = Paint() | ||||||
|       ..color = const Color.fromARGB(255, 255, 255, 255); |       ..color = const Color.fromARGB(255, 255, 255, 255); | ||||||
|     canvas.drawPath(path, paint); |     canvas.drawPath(path, paint); | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -9,18 +9,16 @@ class Viewport extends ViewportTransform { | |||||||
|   double scale; |   double scale; | ||||||
|  |  | ||||||
|   Viewport(this.size, this.scale) |   Viewport(this.size, this.scale) | ||||||
|       : super(new Vector2(size.width / 2, size.height / 2), |       : super(Vector2(size.width / 2, size.height / 2), | ||||||
|             new Vector2(size.width / 2, size.height / 2), scale); |             Vector2(size.width / 2, size.height / 2), scale); | ||||||
|  |  | ||||||
|   double worldAlignBottom(double height) => -(size.height / 2 / scale) + height; |   double worldAlignBottom(double height) => -(size.height / 2 / scale) + height; | ||||||
|  |  | ||||||
|   /// Resizes the current view port. |   /// Resizes the current view port. | ||||||
|   void resize(Size size) { |   void resize(Size size) { | ||||||
|     this.size = size; |     this.size = size; | ||||||
|     this.extents = |     this.extents = Vector2.copy(Vector2(size.width / 2, size.height / 2)); | ||||||
|         new Vector2.copy(new Vector2(size.width / 2, size.height / 2)); |     this.center = Vector2.copy(Vector2(size.width / 2, size.height / 2)); | ||||||
|     this.center = |  | ||||||
|         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. |   /// 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; |     double y = center.y; | ||||||
|  |  | ||||||
|     if (horizontal != null) { |     if (horizontal != null) { | ||||||
|       Vector2 temp = new Vector2.zero(); |       Vector2 temp = Vector2.zero(); | ||||||
|       getWorldToScreen(position, temp); |       getWorldToScreen(position, temp); | ||||||
|  |  | ||||||
|       var margin = horizontal / 2 * size.width / 2; |       var margin = horizontal / 2 * size.width / 2; | ||||||
| @ -73,7 +71,7 @@ class Viewport extends ViewportTransform { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (vertical != null) { |     if (vertical != null) { | ||||||
|       Vector2 temp = new Vector2.zero(); |       Vector2 temp = Vector2.zero(); | ||||||
|       getWorldToScreen(position, temp); |       getWorldToScreen(position, temp); | ||||||
|  |  | ||||||
|       var margin = vertical / 2 * size.height / 2; |       var margin = vertical / 2 * size.height / 2; | ||||||
|  | |||||||
| @ -25,7 +25,7 @@ class AnimationComponent extends PositionComponent { | |||||||
|   }) { |   }) { | ||||||
|     this.width = width; |     this.width = width; | ||||||
|     this.height = height; |     this.height = height; | ||||||
|     this.animation = new Animation.sequenced( |     this.animation = Animation.sequenced( | ||||||
|       imagePath, |       imagePath, | ||||||
|       amount, |       amount, | ||||||
|       textureX: textureX, |       textureX: textureX, | ||||||
|  | |||||||
| @ -63,19 +63,19 @@ abstract class PositionComponent extends Component { | |||||||
|   double width = 0.0, height = 0.0; |   double width = 0.0, height = 0.0; | ||||||
|   Anchor anchor = Anchor.topLeft; |   Anchor anchor = Anchor.topLeft; | ||||||
|  |  | ||||||
|   Position toPosition() => new Position(x, y); |   Position toPosition() => Position(x, y); | ||||||
|   void setByPosition(Position position) { |   void setByPosition(Position position) { | ||||||
|     this.x = position.x; |     this.x = position.x; | ||||||
|     this.y = position.y; |     this.y = position.y; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   Position toSize() => new Position(width, height); |   Position toSize() => Position(width, height); | ||||||
|   void setBySize(Position size) { |   void setBySize(Position size) { | ||||||
|     this.width = size.x; |     this.width = size.x; | ||||||
|     this.height = size.y; |     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) { |   void setByRect(Rect rect) { | ||||||
|     this.x = rect.left; |     this.x = rect.left; | ||||||
|     this.y = rect.top; |     this.y = rect.top; | ||||||
| @ -113,7 +113,7 @@ class SpriteComponent extends PositionComponent { | |||||||
|       : this.rectangle(size, size, imagePath); |       : this.rectangle(size, size, imagePath); | ||||||
|  |  | ||||||
|   SpriteComponent.rectangle(double width, double height, String 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) { |   SpriteComponent.fromSprite(double width, double height, this.sprite) { | ||||||
|     this.width = width; |     this.width = width; | ||||||
|  | |||||||
| @ -10,7 +10,7 @@ import 'package:ordered_set/ordered_set.dart'; | |||||||
| /// It resembles [BaseGame]. It has an [components] property and an [add] method | /// It resembles [BaseGame]. It has an [components] property and an [add] method | ||||||
| mixin ComposedComponent on Component { | mixin ComposedComponent on Component { | ||||||
|   OrderedSet<Component> components = |   OrderedSet<Component> components = | ||||||
|       new OrderedSet(Comparing.on((c) => c.priority())); |       OrderedSet(Comparing.on((c) => c.priority())); | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   render(Canvas canvas) { |   render(Canvas canvas) { | ||||||
|  | |||||||
| @ -12,7 +12,7 @@ class DebugComponent extends PositionComponent { | |||||||
|   Color color = const Color(0xFFFF00FF); |   Color color = const Color(0xFFFF00FF); | ||||||
|  |  | ||||||
|   /// The actual paint used; by default it paints with stroke only and [color]. |   /// The actual paint used; by default it paints with stroke only and [color]. | ||||||
|   Paint get paint => new Paint() |   Paint get paint => Paint() | ||||||
|     ..color = color |     ..color = color | ||||||
|     ..style = PaintingStyle.stroke; |     ..style = PaintingStyle.stroke; | ||||||
|  |  | ||||||
| @ -22,7 +22,7 @@ class DebugComponent extends PositionComponent { | |||||||
|   /// Renders the rectangle |   /// Renders the rectangle | ||||||
|   void render(Canvas c) { |   void render(Canvas c) { | ||||||
|     prepareCanvas(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) |   /// Don't do anything (change as desired) | ||||||
|  | |||||||
| @ -35,7 +35,7 @@ class ParallaxRenderer { | |||||||
|         (rect.height / imageHeight) * (image.width / window.devicePixelRatio); |         (rect.height / imageHeight) * (image.width / window.devicePixelRatio); | ||||||
|     var count = rect.width / imageWidth; |     var count = rect.width / imageWidth; | ||||||
|  |  | ||||||
|     Rect fullRect = new Rect.fromLTWH( |     Rect fullRect = Rect.fromLTWH( | ||||||
|         -scroll * imageWidth, rect.top, (count + 1) * imageWidth, rect.height); |         -scroll * imageWidth, rect.top, (count + 1) * imageWidth, rect.height); | ||||||
|  |  | ||||||
|     paintImage( |     paintImage( | ||||||
| @ -51,7 +51,7 @@ abstract class ParallaxComponent extends PositionComponent { | |||||||
|   final BASE_SPEED = 30; |   final BASE_SPEED = 30; | ||||||
|   final LAYER_DELTA = 40; |   final LAYER_DELTA = 40; | ||||||
|  |  | ||||||
|   List<ParallaxRenderer> _layers = new List(); |   List<ParallaxRenderer> _layers = []; | ||||||
|   Size _size; |   Size _size; | ||||||
|   bool _loaded = false; |   bool _loaded = false; | ||||||
|  |  | ||||||
| @ -66,7 +66,7 @@ abstract class ParallaxComponent extends PositionComponent { | |||||||
|   void load(List<String> filenames) { |   void load(List<String> filenames) { | ||||||
|     final futures = filenames.fold(<Future<Image>>[], |     final futures = filenames.fold(<Future<Image>>[], | ||||||
|         (List<Future<Image>> result, String filename) { |         (List<Future<Image>> result, String filename) { | ||||||
|       final layer = new ParallaxRenderer(filename); |       final layer = ParallaxRenderer(filename); | ||||||
|       _layers.add(layer); |       _layers.add(layer); | ||||||
|       result.add(layer.future); |       result.add(layer.future); | ||||||
|       return result; |       return result; | ||||||
| @ -96,8 +96,8 @@ abstract class ParallaxComponent extends PositionComponent { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   void _drawLayers(Canvas canvas) { |   void _drawLayers(Canvas canvas) { | ||||||
|     Rect rect = new Rect.fromPoints( |     Rect rect = Rect.fromPoints( | ||||||
|         const Offset(0.0, 0.0), new Offset(_size.width, _size.height)); |         const Offset(0.0, 0.0), Offset(_size.width, _size.height)); | ||||||
|     _layers.forEach((layer) => layer.render(canvas, rect)); |     _layers.forEach((layer) => layer.render(canvas, rect)); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  | |||||||
| @ -137,9 +137,9 @@ class TextBoxComponent extends PositionComponent with Resizable { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   Future<Image> _redrawCache() { |   Future<Image> _redrawCache() { | ||||||
|     PictureRecorder recorder = new PictureRecorder(); |     PictureRecorder recorder = PictureRecorder(); | ||||||
|     Canvas c = new Canvas(recorder, |     Canvas c = Canvas( | ||||||
|         new Rect.fromLTWH(0.0, 0.0, width.toDouble(), height.toDouble())); |         recorder, Rect.fromLTWH(0.0, 0.0, width.toDouble(), height.toDouble())); | ||||||
|     _fullRender(c); |     _fullRender(c); | ||||||
|     return recorder.endRecording().toImage(width.toInt(), height.toInt()); |     return recorder.endRecording().toImage(width.toInt(), height.toInt()); | ||||||
|   } |   } | ||||||
| @ -156,13 +156,13 @@ class TextBoxComponent extends PositionComponent with Resizable { | |||||||
|       charCount += _lines[line].length; |       charCount += _lines[line].length; | ||||||
|       _config |       _config | ||||||
|           .toTextPainter(_lines[line]) |           .toTextPainter(_lines[line]) | ||||||
|           .paint(c, new Offset(_boxConfig.margin, dy)); |           .paint(c, Offset(_boxConfig.margin, dy)); | ||||||
|       dy += _lineHeight; |       dy += _lineHeight; | ||||||
|     } |     } | ||||||
|     int max = math.min(currentChar - charCount, _lines[_currentLine].length); |     int max = math.min(currentChar - charCount, _lines[_currentLine].length); | ||||||
|     _config |     _config | ||||||
|         .toTextPainter(_lines[_currentLine].substring(0, max)) |         .toTextPainter(_lines[_currentLine].substring(0, max)) | ||||||
|         .paint(c, new Offset(_boxConfig.margin, dy)); |         .paint(c, Offset(_boxConfig.margin, dy)); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void redrawLater() async { |   void redrawLater() async { | ||||||
|  | |||||||
| @ -10,11 +10,11 @@ class TiledComponent extends Component { | |||||||
|   String filename; |   String filename; | ||||||
|   TileMap map; |   TileMap map; | ||||||
|   Image image; |   Image image; | ||||||
|   Map<String, Image> images = new Map<String, Image>(); |   Map<String, Image> images = Map<String, Image>(); | ||||||
|   Future future; |   Future future; | ||||||
|   bool _loaded = false; |   bool _loaded = false; | ||||||
|  |  | ||||||
|   static Paint paint = new Paint()..color = Colors.white; |   static Paint paint = Paint()..color = Colors.white; | ||||||
|  |  | ||||||
|   TiledComponent(this.filename) { |   TiledComponent(this.filename) { | ||||||
|     this.future = _load(); |     this.future = _load(); | ||||||
| @ -29,13 +29,13 @@ class TiledComponent extends Component { | |||||||
|  |  | ||||||
|   Future<TileMap> _loadMap() { |   Future<TileMap> _loadMap() { | ||||||
|     return Flame.bundle.loadString('assets/tiles/' + filename).then((contents) { |     return Flame.bundle.loadString('assets/tiles/' + filename).then((contents) { | ||||||
|       var parser = new TileMapParser(); |       var parser = TileMapParser(); | ||||||
|       return parser.parse(contents); |       return parser.parse(contents); | ||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   Future<Map<String, Image>> _loadImages(TileMap map) async { |   Future<Map<String, Image>> _loadImages(TileMap map) async { | ||||||
|     var result = new Map<String, Image>(); |     Map<String, Image> result = {}; | ||||||
|     await Future.forEach(map.tilesets, (tileset) async { |     await Future.forEach(map.tilesets, (tileset) async { | ||||||
|       await Future.forEach(tileset.images, (tmxImage) async { |       await Future.forEach(tileset.images, (tmxImage) async { | ||||||
|         result[tmxImage.source] = await Flame.images.load(tmxImage.source); |         result[tmxImage.source] = await Flame.images.load(tmxImage.source); | ||||||
|  | |||||||
| @ -20,16 +20,16 @@ class Flame { | |||||||
|   static AssetBundle get bundle => _bundle == null ? rootBundle : _bundle; |   static AssetBundle get bundle => _bundle == null ? rootBundle : _bundle; | ||||||
|  |  | ||||||
|   /// Access a shared instance of the [AudioCache] class. |   /// 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. |   /// 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. |   /// Access a shared instance of the [Util] class. | ||||||
|   static Util util = new Util(); |   static Util util = Util(); | ||||||
|  |  | ||||||
|   /// Access a shard instance of [AssetsCache] class. |   /// Access a shard instance of [AssetsCache] class. | ||||||
|   static AssetsCache assets = new AssetsCache(); |   static AssetsCache assets = AssetsCache(); | ||||||
|  |  | ||||||
|   static Future<void> init( |   static Future<void> init( | ||||||
|       {AssetBundle bundle, |       {AssetBundle bundle, | ||||||
| @ -62,7 +62,7 @@ class FlameBiding extends BindingBase with GestureBinding, ServicesBinding { | |||||||
|   static FlameBiding instance; |   static FlameBiding instance; | ||||||
|  |  | ||||||
|   static FlameBiding ensureInitialized() { |   static FlameBiding ensureInitialized() { | ||||||
|     if (FlameBiding.instance == null) new FlameBiding(); |     if (FlameBiding.instance == null) FlameBiding(); | ||||||
|     return FlameBiding.instance; |     return FlameBiding.instance; | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -14,10 +14,10 @@ import 'position.dart'; | |||||||
| /// Represents a generic game. | /// Represents a generic game. | ||||||
| /// | /// | ||||||
| /// Subclass this to implement the [update] and [render] methods. | /// 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 { | abstract class Game extends StatelessWidget { | ||||||
|   // Widget Builder for this Game |   // 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. |   /// 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 { | class WidgetBuilder { | ||||||
|   Offset offset = Offset.zero; |   Offset offset = Offset.zero; | ||||||
|   Widget build(Game game) => new Center( |   Widget build(Game game) => Center( | ||||||
|       child: new Directionality( |       child: Directionality( | ||||||
|           textDirection: TextDirection.ltr, |           textDirection: TextDirection.ltr, | ||||||
|           child: new _GameRenderObjectWidget(game))); |           child: _GameRenderObjectWidget(game))); | ||||||
| } | } | ||||||
|  |  | ||||||
| class _GameRenderObjectWidget extends SingleChildRenderObjectWidget { | class _GameRenderObjectWidget extends SingleChildRenderObjectWidget { | ||||||
| @ -65,7 +65,7 @@ class _GameRenderObjectWidget extends SingleChildRenderObjectWidget { | |||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   RenderObject createRenderObject(BuildContext context) => |   RenderObject createRenderObject(BuildContext context) => | ||||||
|       new _GameRenderBox(context, this.game); |       _GameRenderBox(context, this.game); | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   void updateRenderObject(BuildContext context, _GameRenderBox _gameRenderBox) { |   void updateRenderObject(BuildContext context, _GameRenderBox _gameRenderBox) { | ||||||
| @ -167,7 +167,7 @@ class _GameRenderBox extends RenderBox with WidgetsBindingObserver { | |||||||
| abstract class BaseGame extends Game { | abstract class BaseGame extends Game { | ||||||
|   /// The list of components to be updated and rendered by the base game. |   /// The list of components to be updated and rendered by the base game. | ||||||
|   OrderedSet<Component> components = |   OrderedSet<Component> components = | ||||||
|       new OrderedSet(Comparing.on((c) => c.priority())); |       OrderedSet(Comparing.on((c) => c.priority())); | ||||||
|  |  | ||||||
|   /// Components added by the [addLater] method |   /// Components added by the [addLater] method | ||||||
|   List<Component> _addLater = []; |   List<Component> _addLater = []; | ||||||
| @ -176,7 +176,7 @@ abstract class BaseGame extends Game { | |||||||
|   Size size; |   Size size; | ||||||
|  |  | ||||||
|   /// Camera position; every non-HUD component is translated so that the camera is drawn in the center of the screen |   /// 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 of deltas used in debug mode to calculate FPS | ||||||
|   List<double> _dts = []; |   List<double> _dts = []; | ||||||
| @ -297,7 +297,7 @@ abstract class BaseGame extends Game { | |||||||
|   /// |   /// | ||||||
|   /// This is compatible with the `dt` value used in the [update] method. |   /// This is compatible with the `dt` value used in the [update] method. | ||||||
|   double currentTime() { |   double currentTime() { | ||||||
|     return new DateTime.now().microsecondsSinceEpoch.toDouble() / |     return DateTime.now().microsecondsSinceEpoch.toDouble() / | ||||||
|         Duration.microsecondsPerSecond; |         Duration.microsecondsPerSecond; | ||||||
|   } |   } | ||||||
| } | } | ||||||
| @ -325,7 +325,7 @@ class EmbeddedGameWidget extends StatefulWidget { | |||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   State<StatefulWidget> createState() { |   State<StatefulWidget> createState() { | ||||||
|     return new _EmbeddedGameWidgetState(game, size: size); |     return _EmbeddedGameWidgetState(game, size: size); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| @ -355,10 +355,10 @@ class _EmbeddedGameWidgetState extends State<EmbeddedGameWidget> { | |||||||
|   @override |   @override | ||||||
|   Widget build(BuildContext context) { |   Widget build(BuildContext context) { | ||||||
|     if (size == null) { |     if (size == null) { | ||||||
|       return game.widget; |       return game; | ||||||
|     } |     } | ||||||
|     return Container( |     return Container( | ||||||
|       child: game.widget, |       child: game, | ||||||
|       constraints: BoxConstraints( |       constraints: BoxConstraints( | ||||||
|           minWidth: size.x, |           minWidth: size.x, | ||||||
|           maxWidth: size.x, |           maxWidth: size.x, | ||||||
|  | |||||||
| @ -28,8 +28,8 @@ class Images { | |||||||
|  |  | ||||||
|   Future<Image> _fetchToMemory(String name) async { |   Future<Image> _fetchToMemory(String name) async { | ||||||
|     ByteData data = await Flame.bundle.load('assets/images/' + name); |     ByteData data = await Flame.bundle.load('assets/images/' + name); | ||||||
|     Uint8List bytes = new Uint8List.view(data.buffer); |     Uint8List bytes = Uint8List.view(data.buffer); | ||||||
|     Completer<Image> completer = new Completer(); |     Completer<Image> completer = Completer(); | ||||||
|     decodeImageFromList(bytes, (image) => completer.complete(image)); |     decodeImageFromList(bytes, (image) => completer.complete(image)); | ||||||
|     return completer.future; |     return completer.future; | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -3,7 +3,7 @@ import 'dart:ui'; | |||||||
| class PaletteEntry { | class PaletteEntry { | ||||||
|   final Color color; |   final Color color; | ||||||
|  |  | ||||||
|   Paint get paint => new Paint()..color = color; |   Paint get paint => Paint()..color = color; | ||||||
|  |  | ||||||
|   const PaletteEntry(this.color); |   const PaletteEntry(this.color); | ||||||
|  |  | ||||||
|  | |||||||
| @ -75,24 +75,28 @@ class Position { | |||||||
|     return this; |     return this; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   double distance(Position other) { | ||||||
|  |     return this.minus(other).length(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   ui.Offset toOffset() { |   ui.Offset toOffset() { | ||||||
|     return new ui.Offset(x, y); |     return ui.Offset(x, y); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   ui.Size toSize() { |   ui.Size toSize() { | ||||||
|     return new ui.Size(x, y); |     return ui.Size(x, y); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   math.Point toPoint() { |   math.Point toPoint() { | ||||||
|     return new math.Point(x, y); |     return math.Point(x, y); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   b2d.Vector2 toVector() { |   b2d.Vector2 toVector() { | ||||||
|     return new b2d.Vector2(x, y); |     return b2d.Vector2(x, y); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   Position clone() { |   Position clone() { | ||||||
|     return new Position.fromPosition(this); |     return Position.fromPosition(this); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
| @ -101,7 +105,7 @@ class Position { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   static ui.Rect rectFrom(Position topLeft, Position size) { |   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<Position> pts) { |   static ui.Rect bounds(List<Position> pts) { | ||||||
| @ -109,7 +113,6 @@ class Position { | |||||||
|     double maxx = pts.map((e) => e.x).reduce(math.max); |     double maxx = pts.map((e) => e.x).reduce(math.max); | ||||||
|     double miny = pts.map((e) => e.y).reduce(math.min); |     double miny = pts.map((e) => e.y).reduce(math.min); | ||||||
|     double maxy = pts.map((e) => e.y).reduce(math.max); |     double maxy = pts.map((e) => e.y).reduce(math.max); | ||||||
|     return new ui.Rect.fromPoints( |     return ui.Rect.fromPoints(ui.Offset(minx, miny), ui.Offset(maxx, maxy)); | ||||||
|         new ui.Offset(minx, miny), new ui.Offset(maxx, maxy)); |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -22,6 +22,6 @@ class Profiler { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   static double currentTime() => |   static double currentTime() => | ||||||
|       new DateTime.now().microsecondsSinceEpoch.toDouble() / |       DateTime.now().microsecondsSinceEpoch.toDouble() / | ||||||
|       Duration.microsecondsPerMillisecond; |       Duration.microsecondsPerMillisecond; | ||||||
| } | } | ||||||
|  | |||||||
| @ -25,7 +25,7 @@ class Sprite { | |||||||
|         height = img.height.toDouble(); |         height = img.height.toDouble(); | ||||||
|       } |       } | ||||||
|       this.image = img; |       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) { |     if (height == null) { | ||||||
|       height = image.height.toDouble(); |       height = image.height.toDouble(); | ||||||
|     } |     } | ||||||
|     this.src = new Rect.fromLTWH(x, y, width, height); |     this.src = Rect.fromLTWH(x, y, width, height); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   static Future<Sprite> loadSprite( |   static Future<Sprite> loadSprite( | ||||||
| @ -53,7 +53,7 @@ class Sprite { | |||||||
|     double height = null, |     double height = null, | ||||||
|   }) async { |   }) async { | ||||||
|     Image image = await Flame.images.load(fileName); |     Image image = await Flame.images.load(fileName); | ||||||
|     return new Sprite.fromImage( |     return Sprite.fromImage( | ||||||
|       image, |       image, | ||||||
|       x: x, |       x: x, | ||||||
|       y: y, |       y: y, | ||||||
| @ -74,11 +74,11 @@ class Sprite { | |||||||
|     if (!loaded()) { |     if (!loaded()) { | ||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
|     return new Position(_imageWidth, _imageHeight); |     return Position(_imageWidth, _imageHeight); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   Position get size { |   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. |   /// Renders this Sprite on the position [p], scaled by the [scale] factor provided. | ||||||
| @ -107,7 +107,7 @@ class Sprite { | |||||||
|     } |     } | ||||||
|     width ??= this.size.x; |     width ??= this.size.x; | ||||||
|     height ??= this.size.y; |     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) { |   void renderRect(Canvas canvas, Rect dst) { | ||||||
| @ -127,6 +127,6 @@ class Sprite { | |||||||
|     } |     } | ||||||
|     size ??= this.size; |     size ??= this.size; | ||||||
|     renderRect(canvas, |     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)); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -90,16 +90,16 @@ class TextConfig { | |||||||
|   /// However, you probably want to use the [render] method witch already renders for you considering the anchor. |   /// 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. |   /// That way, you don't need to perform the math for yourself. | ||||||
|   material.TextPainter toTextPainter(String text) { |   material.TextPainter toTextPainter(String text) { | ||||||
|     material.TextStyle style = new material.TextStyle( |     material.TextStyle style = material.TextStyle( | ||||||
|       color: color, |       color: color, | ||||||
|       fontSize: fontSize, |       fontSize: fontSize, | ||||||
|       fontFamily: fontFamily, |       fontFamily: fontFamily, | ||||||
|     ); |     ); | ||||||
|     material.TextSpan span = new material.TextSpan( |     material.TextSpan span = material.TextSpan( | ||||||
|       style: style, |       style: style, | ||||||
|       text: text, |       text: text, | ||||||
|     ); |     ); | ||||||
|     material.TextPainter tp = new material.TextPainter( |     material.TextPainter tp = material.TextPainter( | ||||||
|       text: span, |       text: span, | ||||||
|       textAlign: textAlign, |       textAlign: textAlign, | ||||||
|       textDirection: textDirection, |       textDirection: textDirection, | ||||||
|  | |||||||
| @ -38,9 +38,9 @@ class Util { | |||||||
|   Future<Size> initialDimensions() async { |   Future<Size> initialDimensions() async { | ||||||
|     // https://github.com/flutter/flutter/issues/5259 |     // https://github.com/flutter/flutter/issues/5259 | ||||||
|     // "In release mode we start off at 0x0 but we don't in debug mode" |     // "In release mode we start off at 0x0 but we don't in debug mode" | ||||||
|     return await new Future<Size>(() { |     return await Future<Size>(() { | ||||||
|       if (window.physicalSize.isEmpty) { |       if (window.physicalSize.isEmpty) { | ||||||
|         final completer = new Completer<Size>(); |         final completer = Completer<Size>(); | ||||||
|         window.onMetricsChanged = () { |         window.onMetricsChanged = () { | ||||||
|           if (!window.physicalSize.isEmpty) { |           if (!window.physicalSize.isEmpty) { | ||||||
|             completer.complete(window.physicalSize / window.devicePixelRatio); |             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. |   /// Use this in order to get it to work in case your app also contains other widgets. | ||||||
|   void addGestureRecognizer(GestureRecognizer recognizer) { |   void addGestureRecognizer(GestureRecognizer recognizer) { | ||||||
|     if (GestureBinding.instance == null) { |     if (GestureBinding.instance == null) { | ||||||
|       throw new Exception( |       throw Exception( | ||||||
|           "GestureBinding is not initialized yet, this probably happened because addGestureRecognizer was called before the runApp method"); |           'GestureBinding is not initialized yet, this probably happened because addGestureRecognizer was called before the runApp method'); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     GestureBinding.instance.pointerRouter.addGlobalRoute((PointerEvent e) { |     GestureBinding.instance.pointerRouter.addGlobalRoute((PointerEvent e) { | ||||||
|  | |||||||
| @ -3,5 +3,3 @@ | |||||||
| dartanalyzer . | dartanalyzer . | ||||||
| flutter format . | flutter format . | ||||||
| flutter test | flutter test | ||||||
|  |  | ||||||
| dartdoc |  | ||||||
|  | |||||||
							
								
								
									
										3
									
								
								scripts/gen-doc.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										3
									
								
								scripts/gen-doc.sh
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,3 @@ | |||||||
|  | #!/bin/bash | ||||||
|  |  | ||||||
|  | dartdoc | ||||||
| @ -4,25 +4,25 @@ import 'package:flame/box2d/viewport.dart'; | |||||||
| import 'package:test/test.dart'; | import 'package:test/test.dart'; | ||||||
|  |  | ||||||
| void main() { | 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", () { |   group('getCenterHorizontalScreenPercentage', () { | ||||||
|     test("center starts in the middle", () { |     test('center starts in the middle', () { | ||||||
|       viewport.setCamera(50.0, viewport.center.y, 1.0); |       viewport.setCamera(50.0, viewport.center.y, 1.0); | ||||||
|       expect(viewport.getCenterHorizontalScreenPercentage(), equals(0.5)); |       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); |       viewport.setCamera(75.0, viewport.center.y, 1.0); | ||||||
|       expect(viewport.getCenterHorizontalScreenPercentage(), equals(0.75)); |       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); |       viewport.setCamera(25.0, viewport.center.y, 1.0); | ||||||
|       expect(viewport.getCenterHorizontalScreenPercentage(), equals(0.25)); |       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); |       viewport.setCamera(110.0, viewport.center.y, 1.0); | ||||||
|       expect(viewport.getCenterHorizontalScreenPercentage(), equals(0.10)); |       expect(viewport.getCenterHorizontalScreenPercentage(), equals(0.10)); | ||||||
|  |  | ||||||
| @ -30,7 +30,7 @@ void main() { | |||||||
|       expect(viewport.getCenterHorizontalScreenPercentage(), equals(0.90)); |       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); |       viewport.setCamera(50.0, viewport.center.y, 1.0); | ||||||
|       expect(viewport.getCenterHorizontalScreenPercentage(screens: 2.0), |       expect(viewport.getCenterHorizontalScreenPercentage(screens: 2.0), | ||||||
|           equals(0.5)); |           equals(0.5)); | ||||||
| @ -44,7 +44,7 @@ void main() { | |||||||
|           equals(0.55)); |           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); |       viewport.setCamera(170.0, viewport.center.y, 1.0); | ||||||
|       expect(viewport.getCenterHorizontalScreenPercentage(screens: 2.0), |       expect(viewport.getCenterHorizontalScreenPercentage(screens: 2.0), | ||||||
|           equals(0.10)); |           equals(0.10)); | ||||||
|  | |||||||
| @ -7,31 +7,31 @@ import 'package:flame/position.dart'; | |||||||
| void main() { | void main() { | ||||||
|   group('component test', () { |   group('component test', () { | ||||||
|     test('test get/set x/y or position', () { |     test('test get/set x/y or position', () { | ||||||
|       PositionComponent c = new SpriteComponent(); |       PositionComponent c = SpriteComponent(); | ||||||
|       c.x = 2.2; |       c.x = 2.2; | ||||||
|       c.y = 3.4; |       c.y = 3.4; | ||||||
|       expect(c.toPosition().x, 2.2); |       expect(c.toPosition().x, 2.2); | ||||||
|       expect(c.toPosition().y, 3.4); |       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.x, 1.0); | ||||||
|       expect(c.y, 0.0); |       expect(c.y, 0.0); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     test('test get/set widt/height or size', () { |     test('test get/set widt/height or size', () { | ||||||
|       PositionComponent c = new SpriteComponent(); |       PositionComponent c = SpriteComponent(); | ||||||
|       c.width = 2.2; |       c.width = 2.2; | ||||||
|       c.height = 3.4; |       c.height = 3.4; | ||||||
|       expect(c.toSize().x, 2.2); |       expect(c.toSize().x, 2.2); | ||||||
|       expect(c.toSize().y, 3.4); |       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.width, 1.0); | ||||||
|       expect(c.height, 0.0); |       expect(c.height, 0.0); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     test('test get/set rect', () { |     test('test get/set rect', () { | ||||||
|       PositionComponent c = new SpriteComponent(); |       PositionComponent c = SpriteComponent(); | ||||||
|       c.x = 0.0; |       c.x = 0.0; | ||||||
|       c.y = 1.0; |       c.y = 1.0; | ||||||
|       c.width = 2.0; |       c.width = 2.0; | ||||||
| @ -41,7 +41,7 @@ void main() { | |||||||
|       expect(c.toRect().width, 2.0); |       expect(c.toRect().width, 2.0); | ||||||
|       expect(c.toRect().height, 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.x, 10.0); | ||||||
|       expect(c.y, 10.0); |       expect(c.y, 10.0); | ||||||
|       expect(c.width, 1.0); |       expect(c.width, 1.0); | ||||||
|  | |||||||
| @ -9,8 +9,8 @@ import 'package:test/test.dart'; | |||||||
|  |  | ||||||
| void main() { | void main() { | ||||||
|   test('my first widget test', () async { |   test('my first widget test', () async { | ||||||
|     Flame.initialize(new TestAssetBundle()); |     await Flame.init(bundle: TestAssetBundle()); | ||||||
|     var tiled = new TiledComponent('x'); |     var tiled = TiledComponent('x'); | ||||||
|     await tiled.future; |     await tiled.future; | ||||||
|     expect(1, equals(1)); |     expect(1, equals(1)); | ||||||
|   }); |   }); | ||||||
| @ -18,11 +18,11 @@ void main() { | |||||||
|  |  | ||||||
| class TestAssetBundle extends CachingAssetBundle { | class TestAssetBundle extends CachingAssetBundle { | ||||||
|   @override |   @override | ||||||
|   Future<ByteData> load(String key) async => new File('assets/map-level1.png') |   Future<ByteData> load(String key) async => File('assets/map-level1.png') | ||||||
|       .readAsBytes() |       .readAsBytes() | ||||||
|       .then((bytes) => ByteData.view(Uint8List.fromList(bytes).buffer)); |       .then((bytes) => ByteData.view(Uint8List.fromList(bytes).buffer)); | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   Future<String> loadString(String key, {bool cache = true}) => |   Future<String> loadString(String key, {bool cache = true}) => | ||||||
|       new File('assets/map.tmx').readAsString(); |       File('assets/map.tmx').readAsString(); | ||||||
| } | } | ||||||
|  | |||||||
| @ -10,15 +10,15 @@ void expectDouble(double d1, double d2) { | |||||||
| void main() { | void main() { | ||||||
|   group('position test', () { |   group('position test', () { | ||||||
|     test('test add', () { |     test('test add', () { | ||||||
|       Position p = new Position(0.0, 5.0); |       Position p = Position(0.0, 5.0); | ||||||
|       Position p2 = p.add(new Position(5.0, 5.0)); |       Position p2 = p.add(Position(5.0, 5.0)); | ||||||
|       expect(p, p2); |       expect(p, p2); | ||||||
|       expectDouble(p.x, 5.0); |       expectDouble(p.x, 5.0); | ||||||
|       expectDouble(p.y, 10.0); |       expectDouble(p.y, 10.0); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     test('test clone', () { |     test('test clone', () { | ||||||
|       Position p = new Position(1.0, 0.0); |       Position p = Position(1.0, 0.0); | ||||||
|       Position clone = p.clone(); |       Position clone = p.clone(); | ||||||
|  |  | ||||||
|       clone.times(2.0); |       clone.times(2.0); | ||||||
| @ -27,14 +27,21 @@ void main() { | |||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     test('test rotate', () { |     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.x, 0.0); | ||||||
|       expectDouble(p.y, 1.0); |       expectDouble(p.y, 1.0); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     test('test length', () { |     test('test length', () { | ||||||
|       Position p = new Position(3.0, 4.0); |       Position p = Position(3.0, 4.0); | ||||||
|       expectDouble(p.length(), 5.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); | ||||||
|  |     }); | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
|  | |||||||
| @ -23,23 +23,23 @@ Size size = const Size(1.0, 1.0); | |||||||
| void main() { | void main() { | ||||||
|   group('resizable test', () { |   group('resizable test', () { | ||||||
|     test('propagate resize to children', () { |     test('propagate resize to children', () { | ||||||
|       MyComponent a = new MyComponent('a'); |       MyComponent a = MyComponent('a'); | ||||||
|       MyComponent b = new MyComponent('b', myChildren: [a]); |       MyComponent b = MyComponent('b', myChildren: [a]); | ||||||
|       b.resize(size); |       b.resize(size); | ||||||
|       expect(a.size, size); |       expect(a.size, size); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     test('game calls resize on add', () { |     test('game calls resize on add', () { | ||||||
|       MyComponent a = new MyComponent('a'); |       MyComponent a = MyComponent('a'); | ||||||
|       MyGame game = new MyGame(); |       MyGame game = MyGame(); | ||||||
|       game.resize(size); |       game.resize(size); | ||||||
|       game.add(a); |       game.add(a); | ||||||
|       expect(a.size, size); |       expect(a.size, size); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     test('game calls resize after added', () { |     test('game calls resize after added', () { | ||||||
|       MyComponent a = new MyComponent('a'); |       MyComponent a = MyComponent('a'); | ||||||
|       MyGame game = new MyGame(); |       MyGame game = MyGame(); | ||||||
|       game.add(a); |       game.add(a); | ||||||
|       game.resize(size); |       game.resize(size); | ||||||
|       expect(a.size, size); |       expect(a.size, size); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Luan Nico
					Luan Nico