formatting

This commit is contained in:
Luan Nico
2018-05-21 01:59:07 -03:00
parent 011aff0fff
commit 6be3d28493
10 changed files with 107 additions and 57 deletions

View File

@ -28,7 +28,8 @@ class Audio {
Future<File> _fetchToMemory(String fileName) async { Future<File> _fetchToMemory(String fileName) async {
final file = new File('${(await getTemporaryDirectory()).path}/$fileName'); final file = new File('${(await getTemporaryDirectory()).path}/$fileName');
return await file.writeAsBytes((await _fetchAsset(fileName)).buffer.asUint8List()); return await file
.writeAsBytes((await _fetchAsset(fileName)).buffer.asUint8List());
} }
Future<List<File>> loadAll(List<String> fileNames) async { Future<List<File>> loadAll(List<String> fileNames) async {
@ -44,14 +45,16 @@ class Audio {
Future<AudioPlayer> play(String fileName, {volume: 1.0}) async { Future<AudioPlayer> play(String fileName, {volume: 1.0}) async {
File file = await load(fileName); File file = await load(fileName);
return await new AudioPlayer()..play(file.path, isLocal: true, volume: volume); return await new AudioPlayer()
..play(file.path, isLocal: true, volume: volume);
} }
Future<AudioPlayer> loop(String fileName, {volume: 1.0}) async { Future<AudioPlayer> loop(String fileName, {volume: 1.0}) async {
File file = await load(fileName); File file = await load(fileName);
AudioPlayer player = new AudioPlayer(); AudioPlayer player = new AudioPlayer();
player.setCompletionHandler(() => player.play(file.path, isLocal: true, volume: volume)); player.setCompletionHandler(
return await player..play(file.path, isLocal: true); () => player.play(file.path, isLocal: true, volume: volume));
return await player
..play(file.path, isLocal: true);
} }
} }

View File

@ -21,7 +21,8 @@ abstract class Box2DComponent extends Component {
Viewport viewport; Viewport viewport;
Box2DComponent({this.dimensions: const Size(0.0, 0.0), Box2DComponent(
{this.dimensions: const Size(0.0, 0.0),
int worldPoolSize: DEFAULT_WORLD_POOL_SIZE, int worldPoolSize: DEFAULT_WORLD_POOL_SIZE,
int worldPoolContainerSize: DEFAULT_WORLD_POOL_CONTAINER_SIZE, int worldPoolContainerSize: DEFAULT_WORLD_POOL_CONTAINER_SIZE,
double gravity: DEFAULT_GRAVITY, double gravity: DEFAULT_GRAVITY,
@ -149,8 +150,7 @@ abstract class BodyComponent extends Component {
} }
void renderPolygon(Canvas canvas, List<Offset> points) { void renderPolygon(Canvas canvas, List<Offset> points) {
final path = new Path() final path = new Path()..addPolygon(points, true);
..addPolygon(points, true);
final Paint paint = new Paint() final Paint paint = new Paint()
..color = new Color.fromARGB(255, 255, 255, 255); ..color = new Color.fromARGB(255, 255, 255, 255);
canvas.drawPath(path, paint); canvas.drawPath(path, paint);

View File

@ -4,7 +4,6 @@ import 'component.dart';
import 'package:flame/animation.dart'; import 'package:flame/animation.dart';
class AnimationComponent extends PositionComponent { class AnimationComponent extends PositionComponent {
Animation animation; Animation animation;
AnimationComponent(double width, double height, this.animation) { AnimationComponent(double width, double height, this.animation) {
@ -12,10 +11,18 @@ class AnimationComponent extends PositionComponent {
this.height = height; this.height = height;
} }
AnimationComponent.sequenced(width, height, String imagePath, int amount, { double textureX = 0.0, double textureY = 0.0, double textureWidth = null, double textureHeight = null}) { AnimationComponent.sequenced(width, height, String imagePath, int amount,
{double textureX = 0.0,
double textureY = 0.0,
double textureWidth = null,
double textureHeight = null}) {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.animation = new Animation.sequenced(imagePath, amount, textureX: textureX, textureY: textureY, textureWidth: textureWidth, textureHeight: textureHeight); this.animation = new Animation.sequenced(imagePath, amount,
textureX: textureX,
textureY: textureY,
textureWidth: textureWidth,
textureHeight: textureHeight);
} }
@override @override

View File

@ -14,7 +14,6 @@ import 'position.dart';
/// 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 { abstract class Game {
/// 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.
/// ///
/// Keep the updates as short as possible. [t] is in seconds, with microsseconds precision. /// Keep the updates as short as possible. [t] is in seconds, with microsseconds precision.
@ -58,7 +57,8 @@ class _GameRenderObjectWidget extends SingleChildRenderObjectWidget {
_GameRenderObjectWidget(this.game); _GameRenderObjectWidget(this.game);
@override @override
RenderObject createRenderObject(BuildContext context) => new _GameRenderBox(context, this.game); RenderObject createRenderObject(BuildContext context) =>
new _GameRenderBox(context, this.game);
} }
class _GameRenderBox extends RenderBox with WidgetsBindingObserver { class _GameRenderBox extends RenderBox with WidgetsBindingObserver {
@ -150,7 +150,6 @@ class _GameRenderBox extends RenderBox with WidgetsBindingObserver {
/// This is the recommended strucutre to use for most games. /// This is the recommended strucutre to use for most games.
/// It is based on the Component system. /// It is based on the Component system.
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.
List<Component> components = []; List<Component> components = [];
@ -265,6 +264,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() / Duration.microsecondsPerSecond; return new DateTime.now().microsecondsSinceEpoch.toDouble() /
Duration.microsecondsPerSecond;
} }
} }

View File

@ -4,7 +4,6 @@ import 'dart:ui';
import 'dart:async'; import 'dart:async';
class Images { class Images {
Map<String, Image> loadedFiles = new Map(); Map<String, Image> loadedFiles = new Map();
void clear(String fileName) { void clear(String fileName) {

View File

@ -8,7 +8,6 @@ import 'package:box2d/box2d.dart' as b2d;
/// Also, it offers helpful converters and a some useful methods for manipulation. /// Also, it offers helpful converters and a some useful methods for manipulation.
/// It always uses double values to store the coordinates. /// It always uses double values to store the coordinates.
class Position { class Position {
/// Coordinates /// Coordinates
double x, y; double x, y;
@ -110,6 +109,7 @@ 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(new ui.Offset(minx, miny), new ui.Offset(maxx, maxy)); return new ui.Rect.fromPoints(
new ui.Offset(minx, miny), new ui.Offset(maxx, maxy));
} }
} }

27
lib/profiler.dart Normal file
View File

@ -0,0 +1,27 @@
class Records {
static void save(Profiler p) {
print('${p.name} : ${p.dts.last - p.dts.first} ms');
}
}
class Profiler {
final String name;
List<double> dts = [];
Profiler(this.name) {
this.tick();
}
void tick() {
this.dts.add(currentTime());
}
void end() {
this.tick();
Records.save(this);
}
static double currentTime() =>
new DateTime.now().microsecondsSinceEpoch.toDouble() /
Duration.microsecondsPerMillisecond;
}

View File

@ -11,7 +11,11 @@ class Sprite {
static final Paint paint = new Paint()..color = Colors.white; static final Paint paint = new Paint()..color = Colors.white;
Sprite(String fileName, {double x = 0.0, double y = 0.0, double width = null, double height = null}) { Sprite(String fileName,
{double x = 0.0,
double y = 0.0,
double width = null,
double height = null}) {
Flame.images.load(fileName).then((img) { Flame.images.load(fileName).then((img) {
if (width == null) { if (width == null) {
width = img.width.toDouble(); width = img.width.toDouble();
@ -24,7 +28,11 @@ class Sprite {
}); });
} }
Sprite.fromImage(this.image, {double x = 0.0, double y = 0.0, double width = null, double height = null}) { Sprite.fromImage(this.image,
{double x = 0.0,
double y = 0.0,
double width = null,
double height = null}) {
if (width == null) { if (width == null) {
width = image.width.toDouble(); width = image.width.toDouble();
} }
@ -34,9 +42,14 @@ class Sprite {
this.src = new Rect.fromLTWH(x, y, width, height); this.src = new Rect.fromLTWH(x, y, width, height);
} }
static Future<Sprite> loadSprite(String fileName, {double x = 0.0, double y = 0.0, double width = null, double height = null}) async { static Future<Sprite> loadSprite(String fileName,
{double x = 0.0,
double y = 0.0,
double width = null,
double height = null}) async {
Image image = await Flame.images.load(fileName); Image image = await Flame.images.load(fileName);
return new Sprite.fromImage(image, x: x, y: y, width: width, height: height); return new Sprite.fromImage(image,
x: x, y: y, width: width, height: height);
} }
bool loaded() { bool loaded() {

View File

@ -29,16 +29,17 @@ class Util {
}); });
} }
material.TextPainter text(String text, { material.TextPainter text(String text,
double fontSize: 24.0, {double fontSize: 24.0,
Color color: material.Colors.black, Color color: material.Colors.black,
String fontFamily: 'Arial', String fontFamily: 'Arial',
TextAlign textAlign: TextAlign.left, TextAlign textAlign: TextAlign.left,
TextDirection textDirection: TextDirection.ltr TextDirection textDirection: TextDirection.ltr}) {
}) { material.TextStyle style = new material.TextStyle(
material.TextStyle style = new material.TextStyle(color: color, fontSize: fontSize, fontFamily: fontFamily); color: color, fontSize: fontSize, fontFamily: fontFamily);
material.TextSpan span = new material.TextSpan(style: style, text: text); material.TextSpan span = new material.TextSpan(style: style, text: text);
material.TextPainter tp = new material.TextPainter(text: span, textAlign: textAlign, textDirection: textDirection); material.TextPainter tp = new material.TextPainter(
text: span, textAlign: textAlign, textDirection: textDirection);
tp.layout(); tp.layout();
return tp; return tp;
} }