refactor: named SpaceshipExitRail

pull/79/head
RuiAlonso 4 years ago
parent c0863d2694
commit 9c04c418e3

@ -16,5 +16,5 @@ export 'ramp_opening.dart';
export 'round_bumper.dart'; export 'round_bumper.dart';
export 'score_points.dart'; export 'score_points.dart';
export 'spaceship.dart'; export 'spaceship.dart';
export 'spaceship_drop_ramp.dart'; export 'spaceship_exit_rail.dart';
export 'wall.dart'; export 'wall.dart';

@ -32,7 +32,8 @@ class Spaceship extends Forge2DBlueprint {
SpaceshipBridge()..initialPosition = position, SpaceshipBridge()..initialPosition = position,
SpaceshipBridgeTop()..initialPosition = position + Vector2(0, 5.5), SpaceshipBridgeTop()..initialPosition = position + Vector2(0, 5.5),
SpaceshipHole( SpaceshipHole(
onExitLayer: Layer.spaceship_drop, onExitLayer: Layer.spaceshipDrop,
onExitElevation: 2,
)..initialPosition = position - Vector2(5, 4), )..initialPosition = position - Vector2(5, 4),
SpaceshipHole()..initialPosition = position - Vector2(-5, 4), SpaceshipHole()..initialPosition = position - Vector2(-5, 4),
SpaceshipWall()..initialPosition = position, SpaceshipWall()..initialPosition = position,
@ -45,6 +46,7 @@ class Spaceship extends Forge2DBlueprint {
/// {@endtemplate} /// {@endtemplate}
class SpaceshipSaucer extends BodyComponent with InitialPosition, Layered { class SpaceshipSaucer extends BodyComponent with InitialPosition, Layered {
/// {@macro spaceship_saucer} /// {@macro spaceship_saucer}
// TODO(ruimiguel): apply Elevated when PR merged.
SpaceshipSaucer() : super(priority: 2) { SpaceshipSaucer() : super(priority: 2) {
layer = Layer.spaceship; layer = Layer.spaceship;
} }
@ -99,6 +101,7 @@ class SpaceshipSaucer extends BodyComponent with InitialPosition, Layered {
/// {@endtemplate} /// {@endtemplate}
class SpaceshipBridgeTop extends BodyComponent with InitialPosition { class SpaceshipBridgeTop extends BodyComponent with InitialPosition {
/// {@macro spaceship_bridge_top} /// {@macro spaceship_bridge_top}
// TODO(ruimiguel): apply Elevated when PR merged.
SpaceshipBridgeTop() : super(priority: 6); SpaceshipBridgeTop() : super(priority: 6);
@override @override
@ -134,6 +137,7 @@ class SpaceshipBridgeTop extends BodyComponent with InitialPosition {
/// {@endtemplate} /// {@endtemplate}
class SpaceshipBridge extends BodyComponent with InitialPosition, Layered { class SpaceshipBridge extends BodyComponent with InitialPosition, Layered {
/// {@macro spaceship_bridge} /// {@macro spaceship_bridge}
// TODO(ruimiguel): apply Elevated when PR merged.
SpaceshipBridge() : super(priority: 3) { SpaceshipBridge() : super(priority: 3) {
layer = Layer.spaceship; layer = Layer.spaceship;
} }
@ -192,6 +196,10 @@ class SpaceshipEntrance extends RampOpening {
layer = Layer.spaceship; layer = Layer.spaceship;
} }
/// Priority order for [SpaceshipHole] on enter.
// TODO(ruimiguel): apply Elevated when PR merged.
final int onEnterElevation = 3;
@override @override
Shape get shape { Shape get shape {
const radius = Spaceship.radius * 2; const radius = Spaceship.radius * 2;
@ -211,11 +219,11 @@ class SpaceshipEntrance extends RampOpening {
/// {@template spaceship_hole} /// {@template spaceship_hole}
/// A sensor [BodyComponent] responsible for sending the [Ball] /// A sensor [BodyComponent] responsible for sending the [Ball]
/// back to the board. /// out from the [Spaceship].
/// {@endtemplate} /// {@endtemplate}
class SpaceshipHole extends RampOpening { class SpaceshipHole extends RampOpening {
/// {@macro spaceship_hole} /// {@macro spaceship_hole}
SpaceshipHole({Layer? onExitLayer}) SpaceshipHole({Layer? onExitLayer, this.onExitElevation = 1})
: super( : super(
pathwayLayer: Layer.spaceship, pathwayLayer: Layer.spaceship,
outsideLayer: onExitLayer, outsideLayer: onExitLayer,
@ -224,6 +232,10 @@ class SpaceshipHole extends RampOpening {
layer = Layer.spaceship; layer = Layer.spaceship;
} }
/// Priority order for [SpaceshipHole] on exit.
// TODO(ruimiguel): apply Elevated when PR merged.
final int onExitElevation;
@override @override
Shape get shape { Shape get shape {
return CircleShape()..radius = Spaceship.radius / 40; return CircleShape()..radius = Spaceship.radius / 40;
@ -238,6 +250,7 @@ class SpaceshipHole extends RampOpening {
/// {@endtemplate} /// {@endtemplate}
class SpaceshipWall extends BodyComponent with InitialPosition, Layered { class SpaceshipWall extends BodyComponent with InitialPosition, Layered {
/// {@macro spaceship_wall} /// {@macro spaceship_wall}
// TODO(ruimiguel): apply Elevated when PR merged
SpaceshipWall() : super(priority: 4) { SpaceshipWall() : super(priority: 4) {
layer = Layer.spaceship; layer = Layer.spaceship;
} }
@ -299,7 +312,8 @@ class SpaceshipEntranceBallContactCallback
@override @override
void begin(SpaceshipEntrance entrance, Ball ball, _) { void begin(SpaceshipEntrance entrance, Ball ball, _) {
ball ball
..priority = 3 // TODO(ruimiguel): apply Elevated when PR merged.
..priority = entrance.onEnterElevation
..gameRef.reorderChildren() ..gameRef.reorderChildren()
..layer = Layer.spaceship; ..layer = Layer.spaceship;
} }
@ -308,14 +322,15 @@ class SpaceshipEntranceBallContactCallback
/// [ContactCallback] that handles the contact between the [Ball] /// [ContactCallback] that handles the contact between the [Ball]
/// and a [SpaceshipHole]. /// and a [SpaceshipHole].
/// ///
/// It resets the [Ball] priority and filter data so it will "be back" on the /// It sets the [Ball] priority and filter data so it will "be back" on the
/// board. /// board.
class SpaceshipHoleBallContactCallback class SpaceshipHoleBallContactCallback
extends ContactCallback<SpaceshipHole, Ball> { extends ContactCallback<SpaceshipHole, Ball> {
@override @override
void begin(SpaceshipHole hole, Ball ball, _) { void begin(SpaceshipHole hole, Ball ball, _) {
ball ball
..priority = 1 // TODO(ruimiguel): apply Elevated when PR merged.
..priority = hole.onExitElevation
..gameRef.reorderChildren() ..gameRef.reorderChildren()
..layer = hole.outsideLayer; ..layer = hole.outsideLayer;
} }

@ -7,7 +7,7 @@ import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/flame/blueprint.dart'; import 'package:pinball/flame/blueprint.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
class SpaceshipDropRamp extends Forge2DBlueprint { class SpaceshipExitRail extends Forge2DBlueprint {
@override @override
void build() { void build() {
final position = Vector2( final position = Vector2(
@ -16,10 +16,10 @@ class SpaceshipDropRamp extends Forge2DBlueprint {
); );
addAllContactCallback([ addAllContactCallback([
SpaceshipDropHoleBallContactCallback(), SpaceshipExitHoleBallContactCallback(),
]); ]);
final curvedPath = Pathway.bezierCurve( final pathway = Pathway.bezierCurve(
color: const Color.fromARGB(255, 226, 226, 218), color: const Color.fromARGB(255, 226, 226, 218),
width: 4, width: 4,
rotation: 230 * math.pi / 180, rotation: 230 * math.pi / 180,
@ -31,7 +31,7 @@ class SpaceshipDropRamp extends Forge2DBlueprint {
], ],
)..layer = Layer.spaceshipDrop; )..layer = Layer.spaceshipDrop;
final curvedEntrance = Pathway.arc( final entrance = Pathway.arc(
color: const Color.fromARGB(255, 226, 226, 218), color: const Color.fromARGB(255, 226, 226, 218),
center: position, center: position,
radius: 4, radius: 4,
@ -41,7 +41,7 @@ class SpaceshipDropRamp extends Forge2DBlueprint {
singleWall: true, singleWall: true,
)..layer = Layer.spaceshipDrop; )..layer = Layer.spaceshipDrop;
final curvedExit = Pathway.arc( final exit = Pathway.arc(
color: const Color.fromARGB(255, 226, 226, 218), color: const Color.fromARGB(255, 226, 226, 218),
center: position, center: position,
radius: 4, radius: 4,
@ -52,21 +52,21 @@ class SpaceshipDropRamp extends Forge2DBlueprint {
)..layer = Layer.spaceshipDrop; )..layer = Layer.spaceshipDrop;
addAll([ addAll([
curvedPath..initialPosition = position, pathway..initialPosition = position,
curvedEntrance..initialPosition = position + Vector2(26.5, -30), entrance..initialPosition = position + Vector2(26.5, -30),
curvedExit..initialPosition = position + Vector2(29, -66.5), exit..initialPosition = position + Vector2(29, -66.5),
SpaceshipDropHole()..initialPosition = position + Vector2(0, -42), SpaceshipExitHole()..initialPosition = position + Vector2(0, -42),
]); ]);
} }
} }
/// {@template spaceship_drop_hole} /// {@template spaceship_exit_hole}
/// A sensor [BodyComponent] responsible for sending the [Ball] /// A sensor [BodyComponent] responsible for sending the [Ball]
/// back to the board. /// back to the board.
/// {@endtemplate} /// {@endtemplate}
class SpaceshipDropHole extends RampOpening { class SpaceshipExitHole extends RampOpening {
/// {@macro spaceship_drop_hole} /// {@macro spaceship_exit_hole}
SpaceshipDropHole() SpaceshipExitHole()
: super( : super(
pathwayLayer: Layer.spaceshipDrop, pathwayLayer: Layer.spaceshipDrop,
orientation: RampOrientation.down, orientation: RampOrientation.down,
@ -81,14 +81,14 @@ class SpaceshipDropHole extends RampOpening {
} }
/// [ContactCallback] that handles the contact between the [Ball] /// [ContactCallback] that handles the contact between the [Ball]
/// and a [SpaceshipDropHole]. /// and a [SpaceshipExitHole].
/// ///
/// It resets the [Ball] priority and filter data so it will "be back" on the /// It resets the [Ball] priority and filter data so it will "be back" on the
/// board. /// board.
class SpaceshipDropHoleBallContactCallback class SpaceshipExitHoleBallContactCallback
extends ContactCallback<SpaceshipDropHole, Ball> { extends ContactCallback<SpaceshipExitHole, Ball> {
@override @override
void begin(SpaceshipDropHole hole, Ball ball, _) { void begin(SpaceshipExitHole hole, Ball ball, _) {
ball ball
..priority = 1 ..priority = 1
..gameRef.reorderChildren() ..gameRef.reorderChildren()

@ -42,6 +42,7 @@ class PinballGame extends Forge2DGame
unawaited(_addBonusWord()); unawaited(_addBonusWord());
unawaited(_addPaths()); unawaited(_addPaths());
unawaited(addFromBlueprint(Spaceship())); unawaited(addFromBlueprint(Spaceship()));
unawaited(addFromBlueprint(SpaceshipExitRail()));
// Fix camera on the center of the board. // Fix camera on the center of the board.
camera camera
@ -83,8 +84,6 @@ class PinballGame extends Forge2DGame
} }
Future<void> _addPaths() async { Future<void> _addPaths() async {
unawaited(addFromBlueprint(SpaceshipDropRamp()));
final jetpackRamp = JetpackRamp( final jetpackRamp = JetpackRamp(
position: Vector2( position: Vector2(
PinballGame.boardBounds.left + 40.5, PinballGame.boardBounds.left + 40.5,

@ -12,7 +12,7 @@ void main() {
late Body body; late Body body;
late PinballGame game; late PinballGame game;
late Ball ball; late Ball ball;
late SpaceshipDropHole hole; late SpaceshipExitHole hole;
setUp(() { setUp(() {
filterData = MockFilter(); filterData = MockFilter();
@ -29,14 +29,14 @@ void main() {
when(() => ball.gameRef).thenReturn(game); when(() => ball.gameRef).thenReturn(game);
when(() => ball.body).thenReturn(body); when(() => ball.body).thenReturn(body);
hole = MockSpaceshipDropHole(); hole = MockSpaceshipExitHole();
}); });
group('SpaceshipDropHoleBallContactCallback', () { group('SpaceshipExitHoleBallContactCallback', () {
test('changes the ball priority on contact', () { test('changes the ball priority on contact', () {
when(() => hole.outsideLayer).thenReturn(Layer.board); when(() => hole.outsideLayer).thenReturn(Layer.board);
SpaceshipDropHoleBallContactCallback().begin( SpaceshipExitHoleBallContactCallback().begin(
hole, hole,
ball, ball,
MockContact(), MockContact(),
@ -48,7 +48,7 @@ void main() {
test('re order the game children', () { test('re order the game children', () {
when(() => hole.outsideLayer).thenReturn(Layer.board); when(() => hole.outsideLayer).thenReturn(Layer.board);
SpaceshipDropHoleBallContactCallback().begin( SpaceshipExitHoleBallContactCallback().begin(
hole, hole,
ball, ball,
MockContact(), MockContact(),

@ -36,16 +36,20 @@ void main() {
group('SpaceshipEntranceBallContactCallback', () { group('SpaceshipEntranceBallContactCallback', () {
test('changes the ball priority on contact', () { test('changes the ball priority on contact', () {
when(() => entrance.onEnterElevation).thenReturn(3);
SpaceshipEntranceBallContactCallback().begin( SpaceshipEntranceBallContactCallback().begin(
entrance, entrance,
ball, ball,
MockContact(), MockContact(),
); );
verify(() => ball.priority = 3).called(1); verify(() => ball.priority = entrance.onEnterElevation).called(1);
}); });
test('re order the game children', () { test('re order the game children', () {
when(() => entrance.onEnterElevation).thenReturn(3);
SpaceshipEntranceBallContactCallback().begin( SpaceshipEntranceBallContactCallback().begin(
entrance, entrance,
ball, ball,
@ -59,6 +63,7 @@ void main() {
group('SpaceshipHoleBallContactCallback', () { group('SpaceshipHoleBallContactCallback', () {
test('changes the ball priority on contact', () { test('changes the ball priority on contact', () {
when(() => hole.outsideLayer).thenReturn(Layer.board); when(() => hole.outsideLayer).thenReturn(Layer.board);
when(() => hole.onExitElevation).thenReturn(1);
SpaceshipHoleBallContactCallback().begin( SpaceshipHoleBallContactCallback().begin(
hole, hole,
@ -66,11 +71,12 @@ void main() {
MockContact(), MockContact(),
); );
verify(() => ball.priority = 1).called(1); verify(() => ball.priority = hole.onExitElevation).called(1);
}); });
test('re order the game children', () { test('re order the game children', () {
when(() => hole.outsideLayer).thenReturn(Layer.board); when(() => hole.outsideLayer).thenReturn(Layer.board);
when(() => hole.onExitElevation).thenReturn(1);
SpaceshipHoleBallContactCallback().begin( SpaceshipHoleBallContactCallback().begin(
hole, hole,

@ -60,4 +60,4 @@ class MockSpaceshipEntrance extends Mock implements SpaceshipEntrance {}
class MockSpaceshipHole extends Mock implements SpaceshipHole {} class MockSpaceshipHole extends Mock implements SpaceshipHole {}
class MockSpaceshipDropHole extends Mock implements SpaceshipDropHole {} class MockSpaceshipExitHole extends Mock implements SpaceshipExitHole {}

Loading…
Cancel
Save