Merge branch 'main' into feat(sandbox)/included-flipper-key-events

pull/123/head
Alejandro Santiago 4 years ago committed by GitHub
commit f15a7b8079
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

@ -23,6 +23,11 @@ abstract class ComponentController<T extends Component> extends Component {
); );
await super.addToParent(parent); await super.addToParent(parent);
} }
@override
Future<void> add(Component component) {
throw Exception('ComponentController cannot add other components.');
}
} }
/// Mixin that attaches a single [ComponentController] to a [Component]. /// Mixin that attaches a single [ComponentController] to a [Component].

@ -1,10 +1,10 @@
// ignore_for_file: avoid_renaming_method_parameters // ignore_for_file: avoid_renaming_method_parameters
import 'dart:math' as math; import 'dart:math' as math;
import 'dart:ui';
import 'package:flame/extensions.dart'; import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/gen/assets.gen.dart';
import 'package:pinball_components/pinball_components.dart' hide Assets; import 'package:pinball_components/pinball_components.dart' hide Assets;
/// {@template spaceship_exit_rail} /// {@template spaceship_exit_rail}
@ -12,10 +12,10 @@ import 'package:pinball_components/pinball_components.dart' hide Assets;
/// {@endtemplate} /// {@endtemplate}
class SpaceshipExitRail extends Forge2DBlueprint { class SpaceshipExitRail extends Forge2DBlueprint {
/// {@macro spaceship_exit_rail} /// {@macro spaceship_exit_rail}
SpaceshipExitRail({required this.position}); SpaceshipExitRail();
/// The [position] where the elements will be created /// Base priority for wall while be on jetpack ramp.
final Vector2 position; static const ballPriorityWhenOnSpaceshipExitRail = 2;
@override @override
void build(_) { void build(_) {
@ -23,127 +23,101 @@ class SpaceshipExitRail extends Forge2DBlueprint {
SpaceshipExitRailEndBallContactCallback(), SpaceshipExitRailEndBallContactCallback(),
]); ]);
final spaceshipExitRailRamp = _SpaceshipExitRailRamp() final exitRailRamp = _SpaceshipExitRailRamp();
..initialPosition = position; final exitRailEnd = SpaceshipExitRailEnd();
final exitRail = SpaceshipExitRailEnd() final topBase = _SpaceshipExitRailBase(radius: 0.55)
..initialPosition = position + _SpaceshipExitRailRamp.exitPoint; ..initialPosition = Vector2(-26.15, 18.65);
final bottomBase = _SpaceshipExitRailBase(radius: 0.8)
..initialPosition = Vector2(-25.5, -12.9);
addAll([ addAll([
spaceshipExitRailRamp, exitRailRamp,
exitRail, exitRailEnd,
topBase,
bottomBase,
]); ]);
} }
} }
class _SpaceshipExitRailRamp extends BodyComponent class _SpaceshipExitRailRamp extends BodyComponent
with InitialPosition, Layered { with InitialPosition, Layered {
_SpaceshipExitRailRamp() : super(priority: 2) { _SpaceshipExitRailRamp()
: super(
priority: SpaceshipExitRail.ballPriorityWhenOnSpaceshipExitRail - 1,
) {
renderBody = false;
layer = Layer.spaceshipExitRail; layer = Layer.spaceshipExitRail;
// TODO(ruimiguel): remove color once asset is placed.
paint = Paint()
..color = const Color.fromARGB(255, 249, 65, 3)
..style = PaintingStyle.stroke;
} }
static final exitPoint = Vector2(9.2, -48.5);
List<FixtureDef> _createFixtureDefs() { List<FixtureDef> _createFixtureDefs() {
const entranceRotationAngle = 175 * math.pi / 180;
const curveRotationAngle = 275 * math.pi / 180;
const exitRotationAngle = 340 * math.pi / 180;
const width = 5.5;
final fixturesDefs = <FixtureDef>[]; final fixturesDefs = <FixtureDef>[];
final entranceWall = ArcShape( final topArcShape = ArcShape(
center: Vector2(width / 2, 0), center: Vector2(-35.5, 30.9),
arcRadius: width / 2, arcRadius: 2.5,
angle: math.pi, angle: math.pi,
rotation: entranceRotationAngle, rotation: 2.9,
); );
final entranceFixtureDef = FixtureDef(entranceWall); final topArcFixtureDef = FixtureDef(topArcShape);
fixturesDefs.add(entranceFixtureDef); fixturesDefs.add(topArcFixtureDef);
final topLeftControlPoints = [
Vector2(0, 0),
Vector2(10, .5),
Vector2(7, 4),
Vector2(15.5, 8.3),
];
final topLeftCurveShape = BezierCurveShape( final topLeftCurveShape = BezierCurveShape(
controlPoints: topLeftControlPoints, controlPoints: [
)..rotate(curveRotationAngle); Vector2(-37.9, 30.4),
final topLeftFixtureDef = FixtureDef(topLeftCurveShape); Vector2(-38, 23.9),
fixturesDefs.add(topLeftFixtureDef); Vector2(-30.93, 18.2),
],
final topRightControlPoints = [ );
Vector2(0, width), final topLeftCurveFixtureDef = FixtureDef(topLeftCurveShape);
Vector2(10, 6.5), fixturesDefs.add(topLeftCurveFixtureDef);
Vector2(7, 10),
Vector2(15.5, 13.2), final middleLeftCurveShape = BezierCurveShape(
]; controlPoints: [
final topRightCurveShape = BezierCurveShape( Vector2(-30.93, 18.2),
controlPoints: topRightControlPoints, Vector2(-22.6, 10.3),
)..rotate(curveRotationAngle); Vector2(-30, 0.2),
final topRightFixtureDef = FixtureDef(topRightCurveShape); ],
fixturesDefs.add(topRightFixtureDef); );
final middleLeftCurveFixtureDef = FixtureDef(middleLeftCurveShape);
final mediumLeftControlPoints = [ fixturesDefs.add(middleLeftCurveFixtureDef);
topLeftControlPoints.last,
Vector2(21, 12.9),
Vector2(30, 7.1),
Vector2(32, 4.8),
];
final mediumLeftCurveShape = BezierCurveShape(
controlPoints: mediumLeftControlPoints,
)..rotate(curveRotationAngle);
final mediumLeftFixtureDef = FixtureDef(mediumLeftCurveShape);
fixturesDefs.add(mediumLeftFixtureDef);
final mediumRightControlPoints = [
topRightControlPoints.last,
Vector2(21, 17.2),
Vector2(30, 12.1),
Vector2(32, 10.2),
];
final mediumRightCurveShape = BezierCurveShape(
controlPoints: mediumRightControlPoints,
)..rotate(curveRotationAngle);
final mediumRightFixtureDef = FixtureDef(mediumRightCurveShape);
fixturesDefs.add(mediumRightFixtureDef);
final bottomLeftControlPoints = [
mediumLeftControlPoints.last,
Vector2(40, -1),
Vector2(48, 1.9),
Vector2(50.5, 2.5),
];
final bottomLeftCurveShape = BezierCurveShape( final bottomLeftCurveShape = BezierCurveShape(
controlPoints: bottomLeftControlPoints, controlPoints: [
)..rotate(curveRotationAngle); Vector2(-30, 0.2),
final bottomLeftFixtureDef = FixtureDef(bottomLeftCurveShape); Vector2(-36, -8.6),
fixturesDefs.add(bottomLeftFixtureDef); Vector2(-32.04, -18.3),
],
final bottomRightControlPoints = [ );
mediumRightControlPoints.last, final bottomLeftCurveFixtureDef = FixtureDef(bottomLeftCurveShape);
Vector2(40, 4), fixturesDefs.add(bottomLeftCurveFixtureDef);
Vector2(46, 6.5),
Vector2(48.8, 7.6), final topRightStraightShape = EdgeShape()
]; ..set(
Vector2(-33, 31.3),
Vector2(-27.2, 21.3),
);
final topRightStraightFixtureDef = FixtureDef(topRightStraightShape);
fixturesDefs.add(topRightStraightFixtureDef);
final middleRightCurveShape = BezierCurveShape(
controlPoints: [
Vector2(-27.2, 21.3),
Vector2(-16.5, 11.4),
Vector2(-25.29, -1.7),
],
);
final middleRightCurveFixtureDef = FixtureDef(middleRightCurveShape);
fixturesDefs.add(middleRightCurveFixtureDef);
final bottomRightCurveShape = BezierCurveShape( final bottomRightCurveShape = BezierCurveShape(
controlPoints: bottomRightControlPoints, controlPoints: [
)..rotate(curveRotationAngle); Vector2(-25.29, -1.7),
final bottomRightFixtureDef = FixtureDef(bottomRightCurveShape); Vector2(-29.91, -8.5),
fixturesDefs.add(bottomRightFixtureDef); Vector2(-26.8, -15.7),
],
final exitWall = ArcShape(
center: exitPoint,
arcRadius: width / 2,
angle: math.pi,
rotation: exitRotationAngle,
); );
final exitFixtureDef = FixtureDef(exitWall); final bottomRightCurveFixtureDef = FixtureDef(bottomRightCurveShape);
fixturesDefs.add(exitFixtureDef); fixturesDefs.add(bottomRightCurveFixtureDef);
return fixturesDefs; return fixturesDefs;
} }
@ -159,6 +133,52 @@ class _SpaceshipExitRailRamp extends BodyComponent
return body; return body;
} }
@override
Future<void> onLoad() async {
await super.onLoad();
await _loadSprite();
}
Future<void> _loadSprite() async {
final sprite = await gameRef.loadSprite(
Assets.images.components.spaceshipDropTube.path,
);
final spriteComponent = SpriteComponent(
sprite: sprite,
size: Vector2(17.5, 55.7),
anchor: Anchor.center,
position: Vector2(-29.4, -5.7),
);
await add(spriteComponent);
}
}
class _SpaceshipExitRailBase extends BodyComponent
with InitialPosition, Layered {
_SpaceshipExitRailBase({required this.radius})
: super(
priority: SpaceshipExitRail.ballPriorityWhenOnSpaceshipExitRail + 1,
) {
renderBody = false;
layer = Layer.board;
}
final double radius;
@override
Body createBody() {
final shape = CircleShape()..radius = radius;
final fixtureDef = FixtureDef(shape);
final bodyDef = BodyDef()
..position = initialPosition
..userData = this;
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
} }
/// {@template spaceship_exit_rail_end} /// {@template spaceship_exit_rail_end}
@ -172,12 +192,18 @@ class SpaceshipExitRailEnd extends RampOpening {
insideLayer: Layer.spaceshipExitRail, insideLayer: Layer.spaceshipExitRail,
orientation: RampOrientation.down, orientation: RampOrientation.down,
) { ) {
renderBody = false;
layer = Layer.spaceshipExitRail; layer = Layer.spaceshipExitRail;
} }
@override @override
Shape get shape { Shape get shape {
return CircleShape()..radius = 1; return ArcShape(
center: Vector2(-29, -17.8),
arcRadius: 2.5,
angle: math.pi * 0.8,
rotation: -0.16,
);
} }
} }
@ -191,8 +217,7 @@ class SpaceshipExitRailEndBallContactCallback
@override @override
void begin(SpaceshipExitRailEnd exitRail, Ball ball, _) { void begin(SpaceshipExitRailEnd exitRail, Ball ball, _) {
ball ball
..priority = 1 ..sendTo(exitRail.outsidePriority)
..gameRef.reorderChildren()
..layer = exitRail.outsideLayer; ..layer = exitRail.outsideLayer;
} }
} }

@ -22,6 +22,7 @@ extension PinballGameAssetsX on PinballGame {
images.load(components.Assets.images.dashBumper.main.active.keyName), images.load(components.Assets.images.dashBumper.main.active.keyName),
images.load(components.Assets.images.dashBumper.main.inactive.keyName), images.load(components.Assets.images.dashBumper.main.inactive.keyName),
images.load(Assets.images.components.background.path), images.load(Assets.images.components.background.path),
images.load(Assets.images.components.spaceshipDropTube.path),
]); ]);
} }
} }

@ -45,9 +45,7 @@ class PinballGame extends Forge2DGame
); );
unawaited( unawaited(
addFromBlueprint( addFromBlueprint(
SpaceshipExitRail( SpaceshipExitRail(),
position: Vector2(-34.3, 23.8),
),
), ),
); );

@ -20,6 +20,10 @@ class $AssetsImagesComponentsGen {
/// File path: assets/images/components/background.png /// File path: assets/images/components/background.png
AssetGenImage get background => AssetGenImage get background =>
const AssetGenImage('assets/images/components/background.png'); const AssetGenImage('assets/images/components/background.png');
/// File path: assets/images/components/spaceship-drop-tube.png
AssetGenImage get spaceshipDropTube =>
const AssetGenImage('assets/images/components/spaceship-drop-tube.png');
} }
class Assets { class Assets {

@ -114,9 +114,9 @@ void main() {
Vector2(0, 0), Vector2(0, 0),
Vector2(10, 10), Vector2(10, 10),
], ],
step: 0.001, step: 0.02,
); );
expect(points.length, 1000); expect(points.length, 50);
}); });
}); });

@ -195,8 +195,8 @@ class SpaceshipHole extends RampOpening {
@override @override
Shape get shape { Shape get shape {
return ArcShape( return ArcShape(
center: Vector2(0, 4.2), center: Vector2(0, 3.2),
arcRadius: 6, arcRadius: 5,
angle: 1, angle: 1,
rotation: 60 * pi / 180, rotation: 60 * pi / 180,
); );

@ -31,6 +31,7 @@ void main() {
); );
}, },
); );
flameTester.test( flameTester.test(
'throws AssertionError when not attached to controlled component', 'throws AssertionError when not attached to controlled component',
(game) async { (game) async {
@ -44,6 +45,35 @@ void main() {
); );
}, },
); );
flameTester.test(
'throws Exception when adding a component',
(game) async {
final component = ControlledComponent();
final controller = TestComponentController(component);
await expectLater(
() async => controller.add(Component()),
throwsException,
);
},
);
flameTester.test(
'throws Exception when adding multiple components',
(game) async {
final component = ControlledComponent();
final controller = TestComponentController(component);
await expectLater(
() async => controller.addAll([
Component(),
Component(),
]),
throwsException,
);
},
);
}); });
group('Controls', () { group('Controls', () {

@ -196,10 +196,10 @@ void main() {
group('bonus letter activation', () { group('bonus letter activation', () {
late GameBloc gameBloc; late GameBloc gameBloc;
final tester = flameBlocTester<PinballGame>( final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
// TODO(alestiago): Use TestGame once BonusLetter has controller. // TODO(alestiago): Use TestGame once BonusLetter has controller.
game: PinballGameTest.create, gameBuilder: PinballGameTest.create,
gameBloc: () => gameBloc, blocBuilder: () => gameBloc,
); );
setUp(() { setUp(() {
@ -211,7 +211,7 @@ void main() {
); );
}); });
tester.testGameWidget( flameBlocTester.testGameWidget(
'adds BonusLetterActivated to GameBloc when not activated', 'adds BonusLetterActivated to GameBloc when not activated',
setUp: (game, tester) async { setUp: (game, tester) async {
await game.ready(); await game.ready();
@ -225,7 +225,7 @@ void main() {
}, },
); );
tester.testGameWidget( flameBlocTester.testGameWidget(
"doesn't add BonusLetterActivated to GameBloc when already activated", "doesn't add BonusLetterActivated to GameBloc when already activated",
setUp: (game, tester) async { setUp: (game, tester) async {
const state = GameState( const state = GameState(
@ -253,7 +253,7 @@ void main() {
}, },
); );
tester.testGameWidget( flameBlocTester.testGameWidget(
'adds a ColorEffect', 'adds a ColorEffect',
setUp: (game, tester) async { setUp: (game, tester) async {
const state = GameState( const state = GameState(
@ -284,7 +284,7 @@ void main() {
}, },
); );
tester.testGameWidget( flameBlocTester.testGameWidget(
'only listens when there is a change on the letter status', 'only listens when there is a change on the letter status',
setUp: (game, tester) async { setUp: (game, tester) async {
await game.ready(); await game.ready();

@ -66,12 +66,12 @@ void main() {
); );
}); });
final tester = flameBlocTester<PinballGame>( final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
game: PinballGameTest.create, gameBuilder: PinballGameTest.create,
gameBloc: () => gameBloc, blocBuilder: () => gameBloc,
); );
tester.testGameWidget( flameBlocTester.testGameWidget(
'lost adds BallLost to GameBloc', 'lost adds BallLost to GameBloc',
setUp: (game, tester) async { setUp: (game, tester) async {
final controller = LaunchedBallController(ball); final controller = LaunchedBallController(ball);
@ -86,7 +86,7 @@ void main() {
); );
group('listenWhen', () { group('listenWhen', () {
tester.testGameWidget( flameBlocTester.testGameWidget(
'listens when a ball has been lost', 'listens when a ball has been lost',
setUp: (game, tester) async { setUp: (game, tester) async {
final controller = LaunchedBallController(ball); final controller = LaunchedBallController(ball);
@ -107,7 +107,7 @@ void main() {
}, },
); );
tester.testGameWidget( flameBlocTester.testGameWidget(
'does not listen when a ball has not been lost', 'does not listen when a ball has not been lost',
setUp: (game, tester) async { setUp: (game, tester) async {
final controller = LaunchedBallController(ball); final controller = LaunchedBallController(ball);
@ -130,7 +130,7 @@ void main() {
}); });
group('onNewState', () { group('onNewState', () {
tester.testGameWidget( flameBlocTester.testGameWidget(
'removes ball', 'removes ball',
setUp: (game, tester) async { setUp: (game, tester) async {
final controller = LaunchedBallController(ball); final controller = LaunchedBallController(ball);
@ -147,7 +147,7 @@ void main() {
}, },
); );
tester.testGameWidget( flameBlocTester.testGameWidget(
'spawns a new ball when the ball is not the last one', 'spawns a new ball when the ball is not the last one',
setUp: (game, tester) async { setUp: (game, tester) async {
final controller = LaunchedBallController(ball); final controller = LaunchedBallController(ball);
@ -168,7 +168,7 @@ void main() {
}, },
); );
tester.testGameWidget( flameBlocTester.testGameWidget(
'does not spawn a new ball is the last one', 'does not spawn a new ball is the last one',
setUp: (game, tester) async { setUp: (game, tester) async {
final controller = LaunchedBallController(ball); final controller = LaunchedBallController(ball);

@ -86,12 +86,12 @@ void main() {
group('controller', () { group('controller', () {
group('listenWhen', () { group('listenWhen', () {
final gameBloc = MockGameBloc(); final gameBloc = MockGameBloc();
final tester = flameBlocTester( final flameBlocTester = FlameBlocTester<TestGame, GameBloc>(
game: TestGame.new, gameBuilder: TestGame.new,
gameBloc: () => gameBloc, blocBuilder: () => gameBloc,
); );
tester.testGameWidget( flameBlocTester.testGameWidget(
'listens when a Bonus.dashNest is added', 'listens when a Bonus.dashNest is added',
verify: (game, tester) async { verify: (game, tester) async {
final flutterForest = FlutterForest(); final flutterForest = FlutterForest();
@ -145,12 +145,12 @@ void main() {
); );
}); });
final tester = flameBlocTester<PinballGame>( final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
game: PinballGameTest.create, gameBuilder: PinballGameTest.create,
gameBloc: () => gameBloc, blocBuilder: () => gameBloc,
); );
tester.testGameWidget( flameBlocTester.testGameWidget(
'add DashNestActivated event', 'add DashNestActivated event',
setUp: (game, tester) async { setUp: (game, tester) async {
await game.ready(); await game.ready();
@ -171,7 +171,7 @@ void main() {
}, },
); );
tester.testGameWidget( flameBlocTester.testGameWidget(
'add Scored event', 'add Scored event',
setUp: (game, tester) async { setUp: (game, tester) async {
final flutterForest = FlutterForest(); final flutterForest = FlutterForest();

@ -31,29 +31,33 @@ void main() {
when(() => fixture.filterData).thenReturn(filterData); when(() => fixture.filterData).thenReturn(filterData);
}); });
// TODO(alestiago): Make ContactCallback private and use `beginContact`
// instead.
group('SpaceshipExitHoleBallContactCallback', () { group('SpaceshipExitHoleBallContactCallback', () {
test('changes the ball priority on contact', () { setUp(() {
when(() => ball.priority).thenReturn(1);
when(() => exitRailEnd.outsideLayer).thenReturn(Layer.board); when(() => exitRailEnd.outsideLayer).thenReturn(Layer.board);
when(() => exitRailEnd.outsidePriority).thenReturn(0);
});
test('changes the ball priority on contact', () {
SpaceshipExitRailEndBallContactCallback().begin( SpaceshipExitRailEndBallContactCallback().begin(
exitRailEnd, exitRailEnd,
ball, ball,
MockContact(), MockContact(),
); );
verify(() => ball.priority = 1).called(1); verify(() => ball.sendTo(exitRailEnd.outsidePriority)).called(1);
}); });
test('reorders the game children', () { test('changes the ball layer on contact', () {
when(() => exitRailEnd.outsideLayer).thenReturn(Layer.board);
SpaceshipExitRailEndBallContactCallback().begin( SpaceshipExitRailEndBallContactCallback().begin(
exitRailEnd, exitRailEnd,
ball, ball,
MockContact(), MockContact(),
); );
verify(game.reorderChildren).called(1); verify(() => ball.layer = exitRailEnd.outsideLayer).called(1);
}); });
}); });
}); });

@ -1,21 +1,21 @@
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame/src/game/flame_game.dart';
import 'package:flame_test/flame_test.dart'; import 'package:flame_test/flame_test.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pinball/game/game.dart';
FlameTester<T> flameBlocTester<T extends Forge2DGame>({ class FlameBlocTester<T extends FlameGame, B extends Bloc<dynamic, dynamic>>
required T Function() game, extends FlameTester<T> {
required GameBloc Function() gameBloc, FlameBlocTester({
}) { required GameCreateFunction<T> gameBuilder,
return FlameTester<T>( required B Function() blocBuilder,
game, }) : super(
pumpWidget: (gameWidget, tester) async { gameBuilder,
await tester.pumpWidget( pumpWidget: (gameWidget, tester) async {
BlocProvider.value( await tester.pumpWidget(
value: gameBloc(), BlocProvider.value(
child: gameWidget, value: blocBuilder(),
), child: gameWidget,
); ),
}, );
); },
);
} }

Loading…
Cancel
Save