mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 11:43:19 +08:00
formatting, linting and refactorings
This commit is contained in:
@ -13,18 +13,23 @@ class Animation {
|
||||
|
||||
Animation.spriteList(this.sprites, {this.stepTime, this.lifeTime});
|
||||
|
||||
Animation.sequenced(String imagePath, int amount,
|
||||
{double textureX = 0.0,
|
||||
Animation.sequenced(
|
||||
String imagePath,
|
||||
int amount, {
|
||||
double textureX = 0.0,
|
||||
double textureY = 0.0,
|
||||
double textureWidth = null,
|
||||
double textureHeight = null}) {
|
||||
sprites = new List<Sprite>(amount);
|
||||
double textureHeight = null,
|
||||
}) {
|
||||
this.sprites = new List<Sprite>(amount);
|
||||
for (var i = 0; i < amount; i++) {
|
||||
sprites[i] = new Sprite(imagePath,
|
||||
this.sprites[i] = new Sprite(
|
||||
imagePath,
|
||||
x: textureX + i * textureWidth,
|
||||
y: textureY,
|
||||
width: textureWidth,
|
||||
height: textureHeight);
|
||||
height: textureHeight,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ import 'package:audioplayers/audioplayer.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
class Audio {
|
||||
Map<String, File> loadedFiles = new Map();
|
||||
Map<String, File> loadedFiles = {};
|
||||
|
||||
void clear(String fileName) {
|
||||
loadedFiles.remove(fileName);
|
||||
|
||||
@ -17,20 +17,20 @@ abstract class Box2DComponent extends Component {
|
||||
int positionIterations;
|
||||
|
||||
World world;
|
||||
List<Component> components = new List();
|
||||
|
||||
List<Component> components = [];
|
||||
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,
|
||||
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));
|
||||
double scale: DEFAULT_SCALE,
|
||||
}) {
|
||||
final pool = new DefaultWorldPool(worldPoolSize, worldPoolContainerSize);
|
||||
this.world = new World.withPool(new Vector2(0.0, gravity), pool);
|
||||
this.viewport = new Viewport(dimensions, scale);
|
||||
}
|
||||
|
||||
@ -66,10 +66,16 @@ abstract class Box2DComponent extends Component {
|
||||
|
||||
void initializeWorld();
|
||||
|
||||
void cameraFollow(BodyComponent component,
|
||||
{double horizontal, double vertical}) {
|
||||
viewport.cameraFollow(component,
|
||||
horizontal: horizontal, vertical: vertical);
|
||||
void cameraFollow(
|
||||
BodyComponent component, {
|
||||
double horizontal,
|
||||
double vertical,
|
||||
}) {
|
||||
viewport.cameraFollow(
|
||||
component,
|
||||
horizontal: horizontal,
|
||||
vertical: vertical,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,7 +86,7 @@ abstract class BodyComponent extends Component {
|
||||
|
||||
Body body;
|
||||
|
||||
BodyComponent(this.box) {}
|
||||
BodyComponent(this.box);
|
||||
|
||||
World get world => box.world;
|
||||
|
||||
@ -127,7 +133,7 @@ abstract class BodyComponent extends Component {
|
||||
|
||||
void renderCircle(Canvas canvas, Offset center, double radius) {
|
||||
final Paint paint = new Paint()
|
||||
..color = new Color.fromARGB(255, 255, 255, 255);
|
||||
..color = const Color.fromARGB(255, 255, 255, 255);
|
||||
canvas.drawCircle(center, radius, paint);
|
||||
}
|
||||
|
||||
@ -152,7 +158,7 @@ abstract class BodyComponent extends Component {
|
||||
void renderPolygon(Canvas canvas, List<Offset> points) {
|
||||
final path = new Path()..addPolygon(points, true);
|
||||
final Paint paint = new Paint()
|
||||
..color = new Color.fromARGB(255, 255, 255, 255);
|
||||
..color = const Color.fromARGB(255, 255, 255, 255);
|
||||
canvas.drawPath(path, paint);
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,9 +14,7 @@ class Viewport extends ViewportTransform {
|
||||
|
||||
double worldAlignBottom(double height) => -(size.height / 2 / scale) + height;
|
||||
|
||||
/**
|
||||
* Resizes the current view port.
|
||||
*/
|
||||
/// Resizes the current view port.
|
||||
void resize(Size size) {
|
||||
this.size = size;
|
||||
this.extents =
|
||||
@ -25,24 +23,17 @@ class Viewport extends ViewportTransform {
|
||||
new Vector2.copy(new Vector2(size.width / 2, size.height / 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the number of horizontal world meters of this viewport considering a
|
||||
* percentage of its width.
|
||||
*
|
||||
* @param percent percetage of the width in [0, 1] range
|
||||
*/
|
||||
/// Computes the number of horizontal world meters of this viewport considering a percentage of its width.
|
||||
///
|
||||
/// @param percent percetage of the width in [0, 1] range.
|
||||
double worldWidth(double percent) {
|
||||
return percent * (size.width / scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the scroll percentage of total screen width of the current viwerport
|
||||
* center position.
|
||||
*
|
||||
* @param screens multiplies the visible screen with to create a bigger virtual
|
||||
* screen.
|
||||
* @return the percentage in the range of [0, 1]
|
||||
*/
|
||||
/// Computes the scroll percentage of total screen width of the current viwerport center position.
|
||||
///
|
||||
/// @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);
|
||||
@ -51,14 +42,11 @@ class Viewport extends ViewportTransform {
|
||||
return x > 0 ? scroll : 1 - scroll;
|
||||
}
|
||||
|
||||
/**
|
||||
* Follows the spececified body component using a sliding focus window
|
||||
* defined as a percentage of the total viewport.
|
||||
*
|
||||
* @param component to follow.
|
||||
* @param horizontal percentage of the horizontal viewport. Null means no horizontal following.
|
||||
* @param vertical percentage of the vertical viewport. Null means no vertical following.
|
||||
*/
|
||||
/// Follows the spececified body component using a sliding focus window defined as a percentage of the total viewport.
|
||||
///
|
||||
/// @param component to follow.
|
||||
/// @param horizontal percentage of the horizontal viewport. Null means no horizontal following.
|
||||
/// @param vertical percentage of the vertical viewport. Null means no vertical following.
|
||||
void cameraFollow(BodyComponent component,
|
||||
{double horizontal, double vertical}) {
|
||||
Vector2 position = component.center;
|
||||
|
||||
@ -11,18 +11,26 @@ class AnimationComponent extends PositionComponent {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
AnimationComponent.sequenced(width, height, String imagePath, int amount,
|
||||
{double textureX = 0.0,
|
||||
AnimationComponent.sequenced(
|
||||
width,
|
||||
height,
|
||||
String imagePath,
|
||||
int amount, {
|
||||
double textureX = 0.0,
|
||||
double textureY = 0.0,
|
||||
double textureWidth = null,
|
||||
double textureHeight = null}) {
|
||||
double textureHeight = null,
|
||||
}) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.animation = new Animation.sequenced(imagePath, amount,
|
||||
this.animation = new Animation.sequenced(
|
||||
imagePath,
|
||||
amount,
|
||||
textureX: textureX,
|
||||
textureY: textureY,
|
||||
textureWidth: textureWidth,
|
||||
textureHeight: textureHeight);
|
||||
textureHeight: textureHeight,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
@ -32,7 +40,7 @@ class AnimationComponent extends PositionComponent {
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
if (loaded() && x != null && y != null) {
|
||||
if (loaded()) {
|
||||
prepareCanvas(canvas);
|
||||
animation.getSprite().render(canvas, width, height);
|
||||
}
|
||||
|
||||
@ -5,26 +5,48 @@ import '../sprite.dart';
|
||||
import '../position.dart';
|
||||
import 'package:flutter/painting.dart';
|
||||
|
||||
/// This represents a Component for your game.
|
||||
///
|
||||
/// Components can be bullets flying on the screen, a spaship or your player's fighter.
|
||||
/// Anything that either renders or updates can be added to the list on [BaseGame]. It will deal with calling those methods for you.
|
||||
/// Components also have other methods that can help you out if you want to overwrite them.
|
||||
abstract class Component {
|
||||
/// This method is called periodically by the game engine to request that your component updates itself.
|
||||
///
|
||||
/// The time [t] in seconds (with microseconds precision provided by Flutter) since the last update cycle.
|
||||
/// This time can vary according to hardware capacity, so make sure to update your state considering this.
|
||||
/// All components on [BaseGame] are always updated by the same amount. The time each one takes to update adds up to the next update cycle.
|
||||
void update(double t);
|
||||
|
||||
/// Renders this component on the provided Canvas [c].
|
||||
void render(Canvas c);
|
||||
|
||||
/// This is a hook called by [BaseGame] to let this component know that the screen (or flame draw area) has been update.
|
||||
///
|
||||
/// It receives the new size.
|
||||
/// You can use the [Resizable] mixin if you want an implementation of this hook that keeps track of the current size.
|
||||
void resize(Size size) {}
|
||||
|
||||
bool loaded() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool destroy() {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isHud() {
|
||||
return false;
|
||||
}
|
||||
/// Wether this component has been loaded yet. If not loaded, [BaseGame] will not try to render it.
|
||||
///
|
||||
/// Sprite based components can use this to let [BaseGame] know not to try to render when the [Sprite] has not been loaded yet.
|
||||
/// Note that for a more consistent experience, you can pre-load all your assets beforehand with [Flame.images.loadAll].
|
||||
bool loaded() => true;
|
||||
|
||||
/// Wether this should be destroyed or not.
|
||||
///
|
||||
/// It will be called once per component per loop, and if it returns true, [BaseGame] will mark your component for deletion and remove it before the next loop.
|
||||
bool destroy() => false;
|
||||
|
||||
/// Wether this component is HUD object or not.
|
||||
///
|
||||
/// HUD objects ignore the [BaseGame.camera] when rendered (so their position coordinates are considered relative to the device screen).
|
||||
bool isHud() => false;
|
||||
}
|
||||
|
||||
/// A [Component] implementation that represents a component that has a specific, possibly mutatable position on the screen.
|
||||
///
|
||||
/// It represents a rectangle of dimension ([width], [height]), on the position ([x], [y]), rotate around its center with angle [angle].
|
||||
abstract class PositionComponent extends Component {
|
||||
double x = 0.0, y = 0.0, angle = 0.0;
|
||||
double width = 0.0, height = 0.0;
|
||||
|
||||
@ -8,7 +8,7 @@ import 'component.dart';
|
||||
|
||||
class ParallaxRenderer {
|
||||
String filename;
|
||||
Future future;
|
||||
Future<Image> future;
|
||||
|
||||
Image image;
|
||||
double scroll = 0.0;
|
||||
@ -17,7 +17,7 @@ class ParallaxRenderer {
|
||||
this.future = _load();
|
||||
}
|
||||
|
||||
Future _load() {
|
||||
Future<Image> _load() {
|
||||
return Flame.images.load(filename).then((image) {
|
||||
this.image = image;
|
||||
});
|
||||
@ -42,7 +42,8 @@ class ParallaxRenderer {
|
||||
canvas: canvas,
|
||||
image: image,
|
||||
rect: fullRect,
|
||||
repeat: ImageRepeat.repeatX);
|
||||
repeat: ImageRepeat.repeatX,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,23 +60,18 @@ abstract class ParallaxComponent extends PositionComponent {
|
||||
this._size = size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the images defined by this list of filenames. All images
|
||||
* are positioned at its scroll center.
|
||||
*
|
||||
* @param filenames Image filenames
|
||||
*/
|
||||
/// Loads the images defined by this list of filenames. All images are positioned at its scroll center.
|
||||
///
|
||||
/// @param filenames Image filenames
|
||||
void load(List<String> filenames) {
|
||||
var futures =
|
||||
filenames.fold(new List<Future>(), (List<Future> result, filename) {
|
||||
var layer = new ParallaxRenderer(filename);
|
||||
final futures = filenames.fold(<Future<Image>>[],
|
||||
(List<Future<Image>> result, String filename) {
|
||||
final layer = new ParallaxRenderer(filename);
|
||||
_layers.add(layer);
|
||||
result.add(layer.future);
|
||||
return result;
|
||||
});
|
||||
Future.wait(futures).then((r) {
|
||||
_loaded = true;
|
||||
});
|
||||
Future.wait(futures).then((r) => _loaded = true);
|
||||
}
|
||||
|
||||
void updateScroll(int layerIndex, scroll) {
|
||||
@ -101,10 +97,8 @@ abstract class ParallaxComponent extends PositionComponent {
|
||||
|
||||
void _drawLayers(Canvas canvas) {
|
||||
Rect rect = new Rect.fromPoints(
|
||||
new Offset(0.0, 0.0), new Offset(_size.width, _size.height));
|
||||
_layers.forEach((layer) {
|
||||
layer.render(canvas, rect);
|
||||
});
|
||||
const Offset(0.0, 0.0), new Offset(_size.width, _size.height));
|
||||
_layers.forEach((layer) => layer.render(canvas, rect));
|
||||
}
|
||||
|
||||
@override
|
||||
@ -112,8 +106,8 @@ abstract class ParallaxComponent extends PositionComponent {
|
||||
if (!this.loaded()) {
|
||||
return;
|
||||
}
|
||||
for (var i = 0; i < _layers.length; i++) {
|
||||
var scroll = _layers[i].scroll;
|
||||
for (int i = 0; i < _layers.length; i++) {
|
||||
double scroll = _layers[i].scroll;
|
||||
scroll += (BASE_SPEED + i * LAYER_DELTA) * delta / _size.width;
|
||||
if (scroll > 1) {
|
||||
scroll = scroll % 1;
|
||||
|
||||
@ -234,6 +234,7 @@ abstract class BaseGame extends Game {
|
||||
/// Returns `false` by default. Override to use the debug mode.
|
||||
/// In debug mode, the [_recordDt] method actually records every `dt` for statistics.
|
||||
/// Then, you can use the [fps] method to check the game FPS.
|
||||
/// You can also use this value to enable other debug behaviors for your game.
|
||||
bool debugMode() => false;
|
||||
|
||||
/// This is a hook that comes from the RenderBox to allow recording of render times and statistics.
|
||||
|
||||
@ -4,7 +4,7 @@ import 'dart:ui';
|
||||
import 'dart:async';
|
||||
|
||||
class Images {
|
||||
Map<String, Image> loadedFiles = new Map();
|
||||
Map<String, Image> loadedFiles = {};
|
||||
|
||||
void clear(String fileName) {
|
||||
loadedFiles.remove(fileName);
|
||||
@ -29,9 +29,7 @@ class Images {
|
||||
ByteData data = await rootBundle.load('assets/images/' + name);
|
||||
Uint8List bytes = new Uint8List.view(data.buffer);
|
||||
Completer<Image> completer = new Completer();
|
||||
decodeImageFromList(bytes, (image) {
|
||||
completer.complete(image);
|
||||
});
|
||||
decodeImageFromList(bytes, (image) => completer.complete(image));
|
||||
return completer.future;
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,11 +11,13 @@ class Sprite {
|
||||
|
||||
static final Paint paint = new Paint()..color = Colors.white;
|
||||
|
||||
Sprite(String fileName,
|
||||
{double x = 0.0,
|
||||
Sprite(
|
||||
String fileName, {
|
||||
double x = 0.0,
|
||||
double y = 0.0,
|
||||
double width = null,
|
||||
double height = null}) {
|
||||
double height = null,
|
||||
}) {
|
||||
Flame.images.load(fileName).then((img) {
|
||||
if (width == null) {
|
||||
width = img.width.toDouble();
|
||||
@ -28,11 +30,13 @@ class Sprite {
|
||||
});
|
||||
}
|
||||
|
||||
Sprite.fromImage(this.image,
|
||||
{double x = 0.0,
|
||||
Sprite.fromImage(
|
||||
this.image, {
|
||||
double x = 0.0,
|
||||
double y = 0.0,
|
||||
double width = null,
|
||||
double height = null}) {
|
||||
double height = null,
|
||||
}) {
|
||||
if (width == null) {
|
||||
width = image.width.toDouble();
|
||||
}
|
||||
@ -42,14 +46,21 @@ class Sprite {
|
||||
this.src = new Rect.fromLTWH(x, y, width, height);
|
||||
}
|
||||
|
||||
static Future<Sprite> loadSprite(String fileName,
|
||||
{double x = 0.0,
|
||||
static Future<Sprite> loadSprite(
|
||||
String fileName, {
|
||||
double x = 0.0,
|
||||
double y = 0.0,
|
||||
double width = null,
|
||||
double height = null}) async {
|
||||
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() {
|
||||
|
||||
@ -17,7 +17,7 @@ class Util {
|
||||
// "In release mode we start off at 0x0 but we don't in debug mode"
|
||||
return await new Future<Size>(() {
|
||||
if (window.physicalSize.isEmpty) {
|
||||
var completer = new Completer<Size>();
|
||||
final completer = new Completer<Size>();
|
||||
window.onMetricsChanged = () {
|
||||
if (!window.physicalSize.isEmpty) {
|
||||
completer.complete(window.physicalSize / window.devicePixelRatio);
|
||||
@ -29,17 +29,28 @@ 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}) {
|
||||
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);
|
||||
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);
|
||||
text: span,
|
||||
textAlign: textAlign,
|
||||
textDirection: textDirection,
|
||||
);
|
||||
tp.layout();
|
||||
return tp;
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@ import 'package:flame/box2d/viewport.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
var viewport = new Viewport(new Size(100.0, 100.0), 1.0);
|
||||
final viewport = new Viewport(new Size(100.0, 100.0), 1.0);
|
||||
|
||||
group("getCenterHorizontalScreenPercentage", () {
|
||||
test("center starts in the middle", () {
|
||||
|
||||
Reference in New Issue
Block a user