feat: pr suggestions

pull/62/head
Erick Zanardo 4 years ago
parent da1e227922
commit 681a20c698

@ -23,9 +23,9 @@ abstract class Blueprint {
/// Attach the [Component]s built on [build] to the [game]
/// instance
@mustCallSuper
void attach(FlameGame game) {
Future<void> attach(FlameGame game) async {
build();
game.addAll(_components);
await game.addAll(_components);
_isAttached = true;
}
@ -63,8 +63,8 @@ abstract class Forge2DBlueprint extends Blueprint {
}
@override
void attach(FlameGame game) {
super.attach(game);
Future<void> attach(FlameGame game) async {
await super.attach(game);
assert(game is Forge2DGame, 'Forge2DBlueprint used outside a Forge2DGame');
@ -81,7 +81,7 @@ abstract class Forge2DBlueprint extends Blueprint {
extension FlameGameBlueprint on FlameGame {
/// Shortcut to attach a [Blueprint] instance to this game
/// equivalent to `MyBluepinrt().attach(game)`
void addFromBlueprint(Blueprint blueprint) {
blueprint.attach(this);
Future<void> addFromBlueprint(Blueprint blueprint) async {
await blueprint.attach(this);
}
}

@ -5,7 +5,7 @@ import 'dart:math';
import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/flame/extensions.dart';
import 'package:pinball/flame/blueprint.dart';
import 'package:pinball/game/game.dart';
// TODO(erickzanardo): change this to use the layer class
@ -20,6 +20,12 @@ class Spaceship extends Forge2DBlueprint {
@override
void build() {
final position = Vector2(20, -24);
addAllContactCallback([
SpaceshipHoleBallContactCallback(),
SpaceshipEntranceBallContactCallback(),
]);
addAll([
SpaceshipSaucer()..initialPosition = position,
SpaceshipEntrance()..initialPosition = position,
@ -29,11 +35,6 @@ class Spaceship extends Forge2DBlueprint {
SpaceshipHole()..initialPosition = position - Vector2(-5, 4),
SpaceshipWall()..initialPosition = position,
]);
addAllContactCallback([
SpaceshipHoleBallContactCallback(),
SpaceshipEntranceBallContactCallback(),
]);
}
}

@ -4,7 +4,7 @@ import 'dart:async';
import 'package:flame/input.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/flame/extensions.dart';
import 'package:pinball/flame/blueprint.dart';
import 'package:pinball/game/game.dart';
import 'package:pinball_theme/pinball_theme.dart';
@ -30,7 +30,7 @@ class PinballGame extends Forge2DGame
unawaited(_addPlunger());
unawaited(_addPaths());
addFromBlueprint(Spaceship());
unawaited(addFromBlueprint(Spaceship()));
// Corner wall above plunger so the ball deflects into the rest of the
// board.

@ -2,7 +2,7 @@ import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pinball/flame/extensions.dart';
import 'package:pinball/flame/blueprint.dart';
import 'package:pinball/game/game.dart';
import '../helpers/helpers.dart';
@ -41,10 +41,11 @@ void main() {
test(
'throws assertion error when adding to an already attached blueprint',
() {
() async {
final mockGame = MockPinballGame();
when(() => mockGame.addAll(any())).thenAnswer((_) async {});
final blueprint = MyBlueprint()..attach(mockGame);
final blueprint = MyBlueprint();
await blueprint.attach(mockGame);
expect(() => blueprint.add(Component()), throwsAssertionError);
expect(() => blueprint.addAll([Component()]), throwsAssertionError);
@ -63,22 +64,23 @@ void main() {
expect(blueprint.callbacks.length, equals(3));
});
test('adds the callbacks to a game on attach', () {
test('adds the callbacks to a game on attach', () async {
final mockGame = MockPinballGame();
when(() => mockGame.addAll(any())).thenAnswer((_) async {});
when(() => mockGame.addContactCallback(any())).thenAnswer((_) async {});
MyForge2dBlueprint().attach(mockGame);
await MyForge2dBlueprint().attach(mockGame);
verify(() => mockGame.addContactCallback(any())).called(3);
});
test(
'throws assertion error when adding to an already attached blueprint',
() {
() async {
final mockGame = MockPinballGame();
when(() => mockGame.addAll(any())).thenAnswer((_) async {});
when(() => mockGame.addContactCallback(any())).thenAnswer((_) async {});
final blueprint = MyForge2dBlueprint()..attach(mockGame);
final blueprint = MyForge2dBlueprint();
await blueprint.attach(mockGame);
expect(
() => blueprint.addContactCallback(MockContactCallback()),
Loading…
Cancel
Save