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