mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 10:38:17 +08:00
formatting
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -21,7 +21,8 @@ abstract class Box2DComponent extends Component {
|
||||
|
||||
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 worldPoolContainerSize: DEFAULT_WORLD_POOL_CONTAINER_SIZE,
|
||||
double gravity: DEFAULT_GRAVITY,
|
||||
@ -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);
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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 = [];
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,6 @@ import 'dart:ui';
|
||||
import 'dart:async';
|
||||
|
||||
class Images {
|
||||
|
||||
Map<String, Image> loadedFiles = new Map();
|
||||
|
||||
void clear(String fileName) {
|
||||
|
||||
@ -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
27
lib/profiler.dart
Normal 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;
|
||||
}
|
||||
@ -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,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) {
|
||||
width = image.width.toDouble();
|
||||
}
|
||||
@ -34,9 +42,14 @@ class Sprite {
|
||||
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() {
|
||||
|
||||
@ -29,16 +29,17 @@ class Util {
|
||||
});
|
||||
}
|
||||
|
||||
material.TextPainter text(String text, {
|
||||
double fontSize: 24.0,
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user