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 {
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 {
@ -42,16 +43,18 @@ class Audio {
return loadedFiles[fileName];
}
Future<AudioPlayer> play(String fileName, { volume: 1.0 }) async {
Future<AudioPlayer> play(String fileName, {volume: 1.0}) async {
File file = await load(fileName);
return await new AudioPlayer()..play(file.path, isLocal: true, volume: volume);
return await new AudioPlayer()
..play(file.path, isLocal: true, volume: volume);
}
Future<AudioPlayer> loop(String fileName, { volume: 1.0 }) async {
Future<AudioPlayer> loop(String fileName, {volume: 1.0}) async {
File file = await load(fileName);
AudioPlayer player = new AudioPlayer();
player.setCompletionHandler(() => player.play(file.path, isLocal: true, volume: volume));
return await player..play(file.path, isLocal: true);
player.setCompletionHandler(
() => player.play(file.path, isLocal: true, volume: volume));
return await player
..play(file.path, isLocal: true);
}
}

View File

@ -21,13 +21,14 @@ abstract class Box2DComponent extends Component {
Viewport viewport;
Box2DComponent({this.dimensions: const Size(0.0, 0.0),
int worldPoolSize: DEFAULT_WORLD_POOL_SIZE,
int worldPoolContainerSize: DEFAULT_WORLD_POOL_CONTAINER_SIZE,
double gravity: DEFAULT_GRAVITY,
this.velocityIterations: DEFAULT_VELOCITY_ITERATIONS,
this.positionIterations: DEFAULT_POSITION_ITERATIONS,
double scale: DEFAULT_SCALE}) {
Box2DComponent(
{this.dimensions: const Size(0.0, 0.0),
int worldPoolSize: DEFAULT_WORLD_POOL_SIZE,
int worldPoolContainerSize: DEFAULT_WORLD_POOL_CONTAINER_SIZE,
double gravity: DEFAULT_GRAVITY,
this.velocityIterations: DEFAULT_VELOCITY_ITERATIONS,
this.positionIterations: DEFAULT_POSITION_ITERATIONS,
double scale: DEFAULT_SCALE}) {
this.world = new World.withPool(new Vector2(0.0, gravity),
new DefaultWorldPool(worldPoolSize, worldPoolContainerSize));
this.viewport = new Viewport(dimensions, scale);
@ -94,8 +95,8 @@ abstract class BodyComponent extends Component {
void render(Canvas canvas) {
body.getFixtureList();
for (Fixture fixture = body.getFixtureList();
fixture != null;
fixture = fixture.getNext()) {
fixture != null;
fixture = fixture.getNext()) {
switch (fixture.getType()) {
case ShapeType.CHAIN:
throw new Exception("not implemented");
@ -149,8 +150,7 @@ abstract class BodyComponent extends Component {
}
void renderPolygon(Canvas canvas, List<Offset> points) {
final path = new Path()
..addPolygon(points, true);
final path = new Path()..addPolygon(points, true);
final Paint paint = new Paint()
..color = new Color.fromARGB(255, 255, 255, 255);
canvas.drawPath(path, paint);

View File

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

View File

@ -61,9 +61,9 @@ abstract class PositionComponent extends Component {
canvas.translate(x, y);
// rotate around center
canvas.translate(width/2, height/2);
canvas.translate(width / 2, height / 2);
canvas.rotate(angle);
canvas.translate(-width/2, -height/2);
canvas.translate(-width / 2, -height / 2);
}
}

View File

@ -14,7 +14,6 @@ import 'position.dart';
/// Subclass this to implement the [update] and [render] methods.
/// Flame will deal with calling these methods properly when the game's [widget] is rendered.
abstract class Game {
/// Implement this method to update the game state, given that a time [t] has passed.
///
/// Keep the updates as short as possible. [t] is in seconds, with microsseconds precision.
@ -58,7 +57,8 @@ class _GameRenderObjectWidget extends SingleChildRenderObjectWidget {
_GameRenderObjectWidget(this.game);
@override
RenderObject createRenderObject(BuildContext context) => new _GameRenderBox(context, this.game);
RenderObject createRenderObject(BuildContext context) =>
new _GameRenderBox(context, this.game);
}
class _GameRenderBox extends RenderBox with WidgetsBindingObserver {
@ -150,7 +150,6 @@ class _GameRenderBox extends RenderBox with WidgetsBindingObserver {
/// This is the recommended strucutre to use for most games.
/// It is based on the Component system.
abstract class BaseGame extends Game {
/// The list of components to be updated and rendered by the base game.
List<Component> components = [];
@ -200,12 +199,12 @@ abstract class BaseGame extends Game {
/// It translates the camera unless hud, call the render method and restore the canvas.
/// This makes sure the canvas is not messed up by one component and all components render independently.
void renderComponent(Canvas canvas, Component c) {
if (!c.isHud()) {
canvas.translate(-camera.x, -camera.y);
}
c.render(canvas);
canvas.restore();
canvas.save();
if (!c.isHud()) {
canvas.translate(-camera.x, -camera.y);
}
c.render(canvas);
canvas.restore();
canvas.save();
}
/// This implementation of update updates every component in the list.
@ -265,6 +264,7 @@ abstract class BaseGame extends Game {
///
/// This is compatible with the `dt` value used in the [update] method.
double currentTime() {
return new DateTime.now().microsecondsSinceEpoch.toDouble() / Duration.microsecondsPerSecond;
return new DateTime.now().microsecondsSinceEpoch.toDouble() /
Duration.microsecondsPerSecond;
}
}

View File

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

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.
/// It always uses double values to store the coordinates.
class Position {
/// Coordinates
double x, y;
@ -110,6 +109,7 @@ class Position {
double maxx = pts.map((e) => e.x).reduce(math.max);
double miny = pts.map((e) => e.y).reduce(math.min);
double maxy = pts.map((e) => e.y).reduce(math.max);
return new ui.Rect.fromPoints(new ui.Offset(minx, miny), new ui.Offset(maxx, maxy));
return new ui.Rect.fromPoints(
new ui.Offset(minx, miny), new ui.Offset(maxx, maxy));
}
}

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;
Sprite(String fileName, {double x = 0.0, double y = 0.0, double width = null, double height = null}) {
Sprite(String fileName,
{double x = 0.0,
double y = 0.0,
double width = null,
double height = null}) {
Flame.images.load(fileName).then((img) {
if (width == null) {
width = img.width.toDouble();
@ -24,19 +28,28 @@ class Sprite {
});
}
Sprite.fromImage(this.image, {double x = 0.0, double y = 0.0, double width = null, double height = null}) {
if (width == null) {
width = image.width.toDouble();
}
if (height == null) {
height = image.height.toDouble();
}
this.src = new Rect.fromLTWH(x, y, width, height);
Sprite.fromImage(this.image,
{double x = 0.0,
double y = 0.0,
double width = null,
double height = null}) {
if (width == null) {
width = image.width.toDouble();
}
if (height == null) {
height = image.height.toDouble();
}
this.src = new Rect.fromLTWH(x, y, width, height);
}
static Future<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);
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() {

View File

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