diff --git a/doc/examples/aseprite/lib/main.dart b/doc/examples/aseprite/lib/main.dart
index 2bf9f2417..c24a9e85e 100644
--- a/doc/examples/aseprite/lib/main.dart
+++ b/doc/examples/aseprite/lib/main.dart
@@ -12,7 +12,7 @@ class MyGame extends BaseGame {
}
_start() async {
- Size size = await Flame.util.initialDimensions();
+ final Size size = await Flame.util.initialDimensions();
final animation = await FlameAnimation.Animation.fromAsepriteData(
'chopper.png', 'chopper.json');
diff --git a/doc/examples/text/lib/main.dart b/doc/examples/text/lib/main.dart
index 6be32872d..d3e534313 100644
--- a/doc/examples/text/lib/main.dart
+++ b/doc/examples/text/lib/main.dart
@@ -20,8 +20,8 @@ class MyTextBox extends TextBoxComponent {
@override
void drawBackground(Canvas c) {
- Rect rect = Rect.fromLTWH(0, 0, width, height);
- c.drawRect(rect, Paint()..color = Color(0xFFFF00FF));
+ final Rect rect = Rect.fromLTWH(0, 0, width, height);
+ c.drawRect(rect, Paint()..color = const Color(0xFFFF00FF));
c.drawRect(
rect.deflate(boxConfig.margin),
Paint()
@@ -36,7 +36,7 @@ class MyGame extends BaseGame {
}
_start() async {
- Size size = await Flame.util.initialDimensions();
+ final Size size = await Flame.util.initialDimensions();
add(TextComponent('Hello, Flame', config: regular)
..anchor = Anchor.topCenter
diff --git a/doc/examples/tiled/lib/main.dart b/doc/examples/tiled/lib/main.dart
index dacf75756..1aabc4cbd 100644
--- a/doc/examples/tiled/lib/main.dart
+++ b/doc/examples/tiled/lib/main.dart
@@ -3,7 +3,7 @@ import 'package:flame/game.dart';
import 'package:flutter/widgets.dart';
void main() {
- TiledGame game = TiledGame();
+ final TiledGame game = TiledGame();
runApp(game.widget);
}
diff --git a/example/lib/main.dart b/example/lib/main.dart
index 7494e59d8..f17967d81 100644
--- a/example/lib/main.dart
+++ b/example/lib/main.dart
@@ -41,7 +41,7 @@ class Square extends PositionComponent {
@override
void update(double t) {
angle += SPEED * t;
- angle %= (2 * math.pi);
+ angle %= 2 * math.pi;
}
}
diff --git a/lib/animation.dart b/lib/animation.dart
index 0a9e6dcbe..788a7f102 100644
--- a/lib/animation.dart
+++ b/lib/animation.dart
@@ -71,7 +71,7 @@ class Animation {
}) {
frames = List(amount);
for (var i = 0; i < amount; i++) {
- Sprite sprite = Sprite(
+ final Sprite sprite = Sprite(
imagePath,
x: textureX + i * textureWidth,
y: textureY,
@@ -94,7 +94,7 @@ class Animation {
}) {
frames = List(amount);
for (var i = 0; i < amount; i++) {
- Sprite sprite = Sprite(
+ final Sprite sprite = Sprite(
imagePath,
x: textureX + i * textureWidth,
y: textureY,
@@ -112,12 +112,12 @@ class Animation {
/// [dataPath]: Animation's exported data in json format
static Future fromAsepriteData(
String imagePath, String dataPath) async {
- String content = await Flame.assets.readFile(dataPath);
- Map json = jsonDecode(content);
+ final String content = await Flame.assets.readFile(dataPath);
+ final Map json = jsonDecode(content);
- Map jsonFrames = json['frames'];
+ final Map jsonFrames = json['frames'];
- var frames = jsonFrames.values.map((value) {
+ final frames = jsonFrames.values.map((value) {
final frameData = value['frame'];
final int x = frameData['x'];
final int y = frameData['y'];
@@ -126,7 +126,7 @@ class Animation {
final stepTime = value['duration'] / 1000;
- Sprite sprite = Sprite(
+ final Sprite sprite = Sprite(
imagePath,
x: x.toDouble(),
y: y.toDouble(),
@@ -203,7 +203,7 @@ class Animation {
/// Returns a new Animation based on this animation, but with its frames in reversed order
Animation reversed() {
- return Animation(this.frames.reversed.toList(), loop: this.loop);
+ return Animation(frames.reversed.toList(), loop: loop);
}
/// Wether all sprites composing this animation are loaded.
diff --git a/lib/audio_pool.dart b/lib/audio_pool.dart
index 82e79dd38..97f8e5e76 100644
--- a/lib/audio_pool.dart
+++ b/lib/audio_pool.dart
@@ -19,7 +19,7 @@ class AudioPool {
bool repeating;
int minPlayers, maxPlayers;
- Lock _lock = Lock();
+ final Lock _lock = Lock();
AudioPool(this.sound,
{this.repeating = false,
@@ -40,13 +40,13 @@ class AudioPool {
if (availablePlayers.isEmpty) {
availablePlayers.add(await _createNewAudioPlayer());
}
- AudioPlayer player = availablePlayers.removeAt(0);
+ final AudioPlayer player = availablePlayers.removeAt(0);
currentPlayers[player.playerId] = player;
await player.setVolume(volume);
await player.resume();
- Stoppable stop = () {
+ final Stoppable stop = () {
_lock.synchronized(() async {
- AudioPlayer p = currentPlayers.remove(player.playerId);
+ final AudioPlayer p = currentPlayers.remove(player.playerId);
p.completionHandler = null;
await p.stop();
if (availablePlayers.length >= maxPlayers) {
@@ -66,8 +66,8 @@ class AudioPool {
}
Future _createNewAudioPlayer() async {
- AudioPlayer player = AudioPlayer();
- String url = (await cache.load(sound)).path;
+ final AudioPlayer player = AudioPlayer();
+ final String url = (await cache.load(sound)).path;
await player.setUrl(url);
await player.setReleaseMode(ReleaseMode.STOP);
return player;
diff --git a/lib/box2d/box2d_component.dart b/lib/box2d/box2d_component.dart
index 8dd0b4e89..f8b2b7dc4 100644
--- a/lib/box2d/box2d_component.dart
+++ b/lib/box2d/box2d_component.dart
@@ -21,17 +21,15 @@ abstract class Box2DComponent extends Component {
Viewport viewport;
Box2DComponent({
- this.dimensions: null,
- 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.dimensions,
+ 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,
}) {
- if (dimensions == null) {
- dimensions = window.physicalSize;
- }
+ dimensions ??= window.physicalSize;
final pool = DefaultWorldPool(worldPoolSize, worldPoolContainerSize);
world = World.withPool(Vector2(0.0, gravity), pool);
viewport = Viewport(dimensions, scale);
@@ -137,8 +135,8 @@ abstract class BodyComponent extends Component {
Vector2 get center => body.worldCenter;
void _renderCircle(Canvas canvas, Fixture fixture) {
- Vector2 center = Vector2.zero();
- CircleShape circle = fixture.getShape();
+ final Vector2 center = Vector2.zero();
+ final CircleShape circle = fixture.getShape();
body.getWorldPointToOut(circle.p, center);
viewport.getWorldToScreen(center, center);
renderCircle(
@@ -152,16 +150,16 @@ abstract class BodyComponent extends Component {
}
void _renderPolygon(Canvas canvas, Fixture fixture) {
- PolygonShape polygon = fixture.getShape();
+ final PolygonShape polygon = fixture.getShape();
assert(polygon.count <= MAX_POLYGON_VERTICES);
- List vertices = Vec2Array().get(polygon.count);
+ final List 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 points = [];
+ final List points = [];
for (int i = 0; i < polygon.count; i++) {
points.add(Offset(vertices[i].x, vertices[i].y));
}
diff --git a/lib/box2d/viewport.dart b/lib/box2d/viewport.dart
index 7fff22d83..03716fc00 100644
--- a/lib/box2d/viewport.dart
+++ b/lib/box2d/viewport.dart
@@ -37,10 +37,10 @@ class Viewport extends ViewportTransform {
/// @param screens multiplies the visible screen with to create a bigger virtual screen.
/// @return the percentage in the range of [0, 1]
double getCenterHorizontalScreenPercentage({double screens: 1.0}) {
- var width = size.width * screens;
- var x = center.x + ((screens - 1) * size.width / 2);
- double rest = x.abs() % width;
- double scroll = rest / width;
+ final width = size.width * screens;
+ final x = center.x + ((screens - 1) * size.width / 2);
+ final double rest = x.abs() % width;
+ final double scroll = rest / width;
return x > 0 ? scroll : 1 - scroll;
}
@@ -51,17 +51,17 @@ class Viewport extends ViewportTransform {
/// @param vertical percentage of the vertical viewport. Null means no vertical following.
void cameraFollow(BodyComponent component,
{double horizontal, double vertical}) {
- Vector2 position = component.center;
+ final Vector2 position = component.center;
double x = center.x;
double y = center.y;
if (horizontal != null) {
- Vector2 temp = Vector2.zero();
+ final Vector2 temp = Vector2.zero();
getWorldToScreen(position, temp);
- var margin = horizontal / 2 * size.width / 2;
- var focus = size.width / 2 - temp.x;
+ final margin = horizontal / 2 * size.width / 2;
+ final focus = size.width / 2 - temp.x;
if (focus.abs() > margin) {
x = size.width / 2 +
@@ -71,11 +71,11 @@ class Viewport extends ViewportTransform {
}
if (vertical != null) {
- Vector2 temp = Vector2.zero();
+ final Vector2 temp = Vector2.zero();
getWorldToScreen(position, temp);
- var margin = vertical / 2 * size.height / 2;
- var focus = size.height / 2 - temp.y;
+ final margin = vertical / 2 * size.height / 2;
+ final focus = size.height / 2 - temp.y;
if (focus.abs() > margin) {
y = size.height / 2 +
diff --git a/lib/components/component.dart b/lib/components/component.dart
index fa92e97dd..412e0c1d4 100644
--- a/lib/components/component.dart
+++ b/lib/components/component.dart
@@ -96,8 +96,8 @@ abstract class PositionComponent extends Component {
canvas.translate(x, y);
canvas.rotate(angle);
- double dx = -anchor.relativePosition.dx * width;
- double dy = -anchor.relativePosition.dy * height;
+ final double dx = -anchor.relativePosition.dx * width;
+ final double dy = -anchor.relativePosition.dy * height;
canvas.translate(dx, dy);
}
}
@@ -145,7 +145,7 @@ class SvgComponent extends PositionComponent {
}
@override
- render(Canvas canvas) {
+ void render(Canvas canvas) {
prepareCanvas(canvas);
svg.render(canvas, width, height);
}
diff --git a/lib/components/composed_component.dart b/lib/components/composed_component.dart
index 53fc417a0..0e0f94427 100644
--- a/lib/components/composed_component.dart
+++ b/lib/components/composed_component.dart
@@ -60,7 +60,7 @@ mixin ComposedComponent on Component {
if (this is Resizable) {
// first time resize
- Resizable thisResizable = this as Resizable;
+ final Resizable thisResizable = this as Resizable;
if (thisResizable.size != null) {
c.resize(thisResizable.size);
}
diff --git a/lib/components/debug_component.dart b/lib/components/debug_component.dart
index 38b0b91dd..e7792d69c 100644
--- a/lib/components/debug_component.dart
+++ b/lib/components/debug_component.dart
@@ -17,18 +17,24 @@ class DebugComponent extends PositionComponent {
..style = PaintingStyle.stroke;
/// Don't do anything (change as desired)
+ @override
void update(double t) {}
/// Renders the rectangle
+ @override
void render(Canvas c) {
prepareCanvas(c);
c.drawRect(Rect.fromLTWH(0.0, 0.0, width, height), paint);
}
/// Don't do anything (change as desired)
+ @override
void resize(Size size) {}
+ @override
bool loaded() => true;
+ @override
bool destroy() => false;
+ @override
bool isHud() => false;
}
diff --git a/lib/components/parallax_component.dart b/lib/components/parallax_component.dart
index cc094b2fc..a4ef4ffca 100644
--- a/lib/components/parallax_component.dart
+++ b/lib/components/parallax_component.dart
@@ -14,7 +14,7 @@ class ParallaxRenderer {
double scroll = 0.0;
ParallaxRenderer(this.filename) {
- this.future = _load();
+ future = _load();
}
Future _load() {
@@ -30,12 +30,12 @@ class ParallaxRenderer {
return;
}
- var imageHeight = image.height / window.devicePixelRatio;
- var imageWidth =
+ final imageHeight = image.height / window.devicePixelRatio;
+ final imageWidth =
(rect.height / imageHeight) * (image.width / window.devicePixelRatio);
- var count = rect.width / imageWidth;
+ final count = rect.width / imageWidth;
- Rect fullRect = Rect.fromLTWH(
+ final 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 _layers = [];
+ final List _layers = [];
Size _size;
bool _loaded = false;
@@ -96,7 +96,7 @@ abstract class ParallaxComponent extends PositionComponent {
}
void _drawLayers(Canvas canvas) {
- Rect rect = Rect.fromPoints(
+ final Rect rect = Rect.fromPoints(
const Offset(0.0, 0.0), Offset(_size.width, _size.height));
_layers.forEach((layer) => layer.render(canvas, rect));
}
diff --git a/lib/components/text_box_component.dart b/lib/components/text_box_component.dart
index 3e5c15ded..c617e2c1f 100644
--- a/lib/components/text_box_component.dart
+++ b/lib/components/text_box_component.dart
@@ -16,10 +16,10 @@ class TextBoxConfig {
final double dismissDelay;
const TextBoxConfig({
- this.maxWidth: 200.0,
- this.margin: 8.0,
- this.timePerChar: 0.0,
- this.dismissDelay: 0.0,
+ this.maxWidth= 200.0,
+ this.margin= 8.0,
+ this.timePerChar= 0.0,
+ this.dismissDelay= 0.0,
});
}
@@ -51,11 +51,9 @@ class TextBoxComponent extends PositionComponent with Resizable {
_text = text;
_lines = [''];
text.split(' ').forEach((word) {
- String possibleLine = _lines.last + ' ' + word;
- widgets.TextPainter p = config.toTextPainter(possibleLine);
- if (_lineHeight == null) {
- _lineHeight = p.height;
- }
+ final String possibleLine = _lines.last + ' ' + word;
+ final widgets.TextPainter p = config.toTextPainter(possibleLine);
+ _lineHeight ??= p.height;
if (p.width <= _boxConfig.maxWidth - 2 * _boxConfig.margin) {
_lines.last = possibleLine;
_updateMaxWidth(p.width);
@@ -84,7 +82,7 @@ class TextBoxComponent extends PositionComponent with Resizable {
int get currentLine {
int totalCharCount = 0;
- int _currentChar = currentChar;
+ final int _currentChar = currentChar;
for (int i = 0; i < _lines.length; i++) {
totalCharCount += _lines[i].length;
if (totalCharCount > _currentChar) {
@@ -115,10 +113,10 @@ class TextBoxComponent extends PositionComponent with Resizable {
double get currentWidth {
int i = 0;
int totalCharCount = 0;
- int _currentChar = currentChar;
- int _currentLine = currentLine;
+ final int _currentChar = currentChar;
+ final int _currentLine = currentLine;
return _lines.sublist(0, _currentLine + 1).map((line) {
- int charCount =
+ final int charCount =
(i < _currentLine) ? line.length : (_currentChar - totalCharCount);
totalCharCount += line.length;
i++;
@@ -137,8 +135,8 @@ class TextBoxComponent extends PositionComponent with Resizable {
}
Future _redrawCache() {
- PictureRecorder recorder = PictureRecorder();
- Canvas c = Canvas(
+ final PictureRecorder recorder = PictureRecorder();
+ final Canvas c = Canvas(
recorder, Rect.fromLTWH(0.0, 0.0, width.toDouble(), height.toDouble()));
_fullRender(c);
return recorder.endRecording().toImage(width.toInt(), height.toInt());
@@ -149,7 +147,7 @@ class TextBoxComponent extends PositionComponent with Resizable {
void _fullRender(Canvas c) {
drawBackground(c);
- int _currentLine = currentLine;
+ final int _currentLine = currentLine;
int charCount = 0;
double dy = _boxConfig.margin;
for (int line = 0; line < _currentLine; line++) {
@@ -159,7 +157,7 @@ class TextBoxComponent extends PositionComponent with Resizable {
.paint(c, Offset(_boxConfig.margin, dy));
dy += _lineHeight;
}
- int max = math.min(currentChar - charCount, _lines[_currentLine].length);
+ final int max = math.min(currentChar - charCount, _lines[_currentLine].length);
_config
.toTextPainter(_lines[_currentLine].substring(0, max))
.paint(c, Offset(_boxConfig.margin, dy));
@@ -170,7 +168,7 @@ class TextBoxComponent extends PositionComponent with Resizable {
}
void update(double dt) {
- int prevCurrentChar = currentChar;
+ final int prevCurrentChar = currentChar;
_lifeTime += dt;
if (prevCurrentChar != currentChar) {
redrawLater();
diff --git a/lib/components/text_component.dart b/lib/components/text_component.dart
index e8ebfed3f..62f34f98e 100644
--- a/lib/components/text_component.dart
+++ b/lib/components/text_component.dart
@@ -30,7 +30,7 @@ class TextComponent extends PositionComponent {
}
void _updateBox() {
- TextPainter tp = config.toTextPainter(text);
+ final TextPainter tp = config.toTextPainter(text);
width = tp.width;
height = tp.height;
}
diff --git a/lib/components/tiled_component.dart b/lib/components/tiled_component.dart
index 0b5de2413..e9f574f42 100644
--- a/lib/components/tiled_component.dart
+++ b/lib/components/tiled_component.dart
@@ -29,13 +29,13 @@ class TiledComponent extends Component {
Future _loadMap() {
return Flame.bundle.loadString('assets/tiles/' + filename).then((contents) {
- var parser = TileMapParser();
+ final parser = TileMapParser();
return parser.parse(contents);
});
}
Future