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 {
|
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 {
|
||||||
@ -42,16 +43,18 @@ class Audio {
|
|||||||
return loadedFiles[fileName];
|
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);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,13 +21,14 @@ abstract class Box2DComponent extends Component {
|
|||||||
|
|
||||||
Viewport viewport;
|
Viewport viewport;
|
||||||
|
|
||||||
Box2DComponent({this.dimensions: const Size(0.0, 0.0),
|
Box2DComponent(
|
||||||
int worldPoolSize: DEFAULT_WORLD_POOL_SIZE,
|
{this.dimensions: const Size(0.0, 0.0),
|
||||||
int worldPoolContainerSize: DEFAULT_WORLD_POOL_CONTAINER_SIZE,
|
int worldPoolSize: DEFAULT_WORLD_POOL_SIZE,
|
||||||
double gravity: DEFAULT_GRAVITY,
|
int worldPoolContainerSize: DEFAULT_WORLD_POOL_CONTAINER_SIZE,
|
||||||
this.velocityIterations: DEFAULT_VELOCITY_ITERATIONS,
|
double gravity: DEFAULT_GRAVITY,
|
||||||
this.positionIterations: DEFAULT_POSITION_ITERATIONS,
|
this.velocityIterations: DEFAULT_VELOCITY_ITERATIONS,
|
||||||
double scale: DEFAULT_SCALE}) {
|
this.positionIterations: DEFAULT_POSITION_ITERATIONS,
|
||||||
|
double scale: DEFAULT_SCALE}) {
|
||||||
this.world = new World.withPool(new Vector2(0.0, gravity),
|
this.world = new World.withPool(new Vector2(0.0, gravity),
|
||||||
new DefaultWorldPool(worldPoolSize, worldPoolContainerSize));
|
new DefaultWorldPool(worldPoolSize, worldPoolContainerSize));
|
||||||
this.viewport = new Viewport(dimensions, scale);
|
this.viewport = new Viewport(dimensions, scale);
|
||||||
@ -94,8 +95,8 @@ abstract class BodyComponent extends Component {
|
|||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
body.getFixtureList();
|
body.getFixtureList();
|
||||||
for (Fixture fixture = body.getFixtureList();
|
for (Fixture fixture = body.getFixtureList();
|
||||||
fixture != null;
|
fixture != null;
|
||||||
fixture = fixture.getNext()) {
|
fixture = fixture.getNext()) {
|
||||||
switch (fixture.getType()) {
|
switch (fixture.getType()) {
|
||||||
case ShapeType.CHAIN:
|
case ShapeType.CHAIN:
|
||||||
throw new Exception("not implemented");
|
throw new Exception("not implemented");
|
||||||
@ -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);
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -61,9 +61,9 @@ abstract class PositionComponent extends Component {
|
|||||||
canvas.translate(x, y);
|
canvas.translate(x, y);
|
||||||
|
|
||||||
// rotate around center
|
// rotate around center
|
||||||
canvas.translate(width/2, height/2);
|
canvas.translate(width / 2, height / 2);
|
||||||
canvas.rotate(angle);
|
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.
|
/// 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 = [];
|
||||||
|
|
||||||
@ -200,12 +199,12 @@ abstract class BaseGame extends Game {
|
|||||||
/// It translates the camera unless hud, call the render method and restore the canvas.
|
/// 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.
|
/// This makes sure the canvas is not messed up by one component and all components render independently.
|
||||||
void renderComponent(Canvas canvas, Component c) {
|
void renderComponent(Canvas canvas, Component c) {
|
||||||
if (!c.isHud()) {
|
if (!c.isHud()) {
|
||||||
canvas.translate(-camera.x, -camera.y);
|
canvas.translate(-camera.x, -camera.y);
|
||||||
}
|
}
|
||||||
c.render(canvas);
|
c.render(canvas);
|
||||||
canvas.restore();
|
canvas.restore();
|
||||||
canvas.save();
|
canvas.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This implementation of update updates every component in the list.
|
/// 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.
|
/// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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) {
|
||||||
|
|||||||
@ -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
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;
|
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,19 +28,28 @@ class Sprite {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Sprite.fromImage(this.image, {double x = 0.0, double y = 0.0, double width = null, double height = null}) {
|
Sprite.fromImage(this.image,
|
||||||
if (width == null) {
|
{double x = 0.0,
|
||||||
width = image.width.toDouble();
|
double y = 0.0,
|
||||||
}
|
double width = null,
|
||||||
if (height == null) {
|
double height = null}) {
|
||||||
height = image.height.toDouble();
|
if (width == null) {
|
||||||
}
|
width = image.width.toDouble();
|
||||||
this.src = new Rect.fromLTWH(x, y, width, height);
|
}
|
||||||
|
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);
|
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() {
|
||||||
|
|||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user