refactor: move JetpackRamp to pinball_components as SpaceshipEntranceRamp (#129)

* refactor: renamed JetpackRamp to SpaceshipEntranceRamp

* refactor: renamed Layer.jetpack to Layer.spaceshipEntranceRamp

* refactor: moved spaceship entrance ramp and assets to pinball components

* test: tests for spaceship ramp

* refactor: renamed SpaceshipEntranceRamp to SpaceshipRamp
pull/138/head
Rui Miguel Alonso 4 years ago committed by GitHub
parent b67b08a18d
commit 61c347c72d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,7 +3,6 @@ export 'bonus_word.dart';
export 'controlled_ball.dart'; export 'controlled_ball.dart';
export 'controlled_flipper.dart'; export 'controlled_flipper.dart';
export 'flutter_forest.dart'; export 'flutter_forest.dart';
export 'jetpack_ramp.dart';
export 'launcher_ramp.dart'; export 'launcher_ramp.dart';
export 'plunger.dart'; export 'plunger.dart';
export 'score_points.dart'; export 'score_points.dart';

@ -14,7 +14,7 @@ class SpaceshipExitRail extends Forge2DBlueprint {
/// {@macro spaceship_exit_rail} /// {@macro spaceship_exit_rail}
SpaceshipExitRail(); SpaceshipExitRail();
/// Base priority for wall while be on jetpack ramp. /// Base priority for wall while be on [_SpaceshipExitRailRamp].
static const ballPriorityWhenOnSpaceshipExitRail = 2; static const ballPriorityWhenOnSpaceshipExitRail = 2;
@override @override

@ -21,10 +21,14 @@ extension PinballGameAssetsX on PinballGame {
images.load(components.Assets.images.dashBumper.b.inactive.keyName), images.load(components.Assets.images.dashBumper.b.inactive.keyName),
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(components.Assets.images.spaceshipRamp.spaceshipRamp.keyName),
images.load(
components.Assets.images.spaceshipRamp.spaceshipRailingBg.keyName,
),
images.load(
components.Assets.images.spaceshipRamp.spaceshipRailingFg.keyName,
),
images.load(Assets.images.components.background.path), images.load(Assets.images.components.background.path),
images.load(Assets.images.components.spaceshipRamp.path),
images.load(Assets.images.components.spaceshipRailingBg.path),
images.load(Assets.images.components.spaceshipRailingFg.path),
images.load(Assets.images.components.spaceshipDropTube.path), images.load(Assets.images.components.spaceshipDropTube.path),
]); ]);
} }

@ -92,9 +92,7 @@ class PinballGame extends Forge2DGame
} }
Future<void> _addPaths() async { Future<void> _addPaths() async {
unawaited( unawaited(addFromBlueprint(SpaceshipRamp()));
addFromBlueprint(Jetpack()),
);
unawaited(addFromBlueprint(Launcher())); unawaited(addFromBlueprint(Launcher()));
} }

@ -24,18 +24,6 @@ class $AssetsImagesComponentsGen {
/// File path: assets/images/components/spaceship-drop-tube.png /// File path: assets/images/components/spaceship-drop-tube.png
AssetGenImage get spaceshipDropTube => AssetGenImage get spaceshipDropTube =>
const AssetGenImage('assets/images/components/spaceship-drop-tube.png'); const AssetGenImage('assets/images/components/spaceship-drop-tube.png');
/// File path: assets/images/components/spaceship_railing_bg.png
AssetGenImage get spaceshipRailingBg =>
const AssetGenImage('assets/images/components/spaceship_railing_bg.png');
/// File path: assets/images/components/spaceship_railing_fg.png
AssetGenImage get spaceshipRailingFg =>
const AssetGenImage('assets/images/components/spaceship_railing_fg.png');
/// File path: assets/images/components/spaceship_ramp.png
AssetGenImage get spaceshipRamp =>
const AssetGenImage('assets/images/components/spaceship_ramp.png');
} }
class Assets { class Assets {

@ -27,6 +27,9 @@ class $AssetsImagesGen {
AssetGenImage get spaceshipBridge => AssetGenImage get spaceshipBridge =>
const AssetGenImage('assets/images/spaceship_bridge.png'); const AssetGenImage('assets/images/spaceship_bridge.png');
$AssetsImagesSpaceshipRampGen get spaceshipRamp =>
const $AssetsImagesSpaceshipRampGen();
/// File path: assets/images/spaceship_saucer.png /// File path: assets/images/spaceship_saucer.png
AssetGenImage get spaceshipSaucer => AssetGenImage get spaceshipSaucer =>
const AssetGenImage('assets/images/spaceship_saucer.png'); const AssetGenImage('assets/images/spaceship_saucer.png');
@ -77,6 +80,22 @@ class $AssetsImagesFlipperGen {
const AssetGenImage('assets/images/flipper/right.png'); const AssetGenImage('assets/images/flipper/right.png');
} }
class $AssetsImagesSpaceshipRampGen {
const $AssetsImagesSpaceshipRampGen();
/// File path: assets/images/spaceship_ramp/spaceship_railing_bg.png
AssetGenImage get spaceshipRailingBg => const AssetGenImage(
'assets/images/spaceship_ramp/spaceship_railing_bg.png');
/// File path: assets/images/spaceship_ramp/spaceship_railing_fg.png
AssetGenImage get spaceshipRailingFg => const AssetGenImage(
'assets/images/spaceship_ramp/spaceship_railing_fg.png');
/// File path: assets/images/spaceship_ramp/spaceship_ramp.png
AssetGenImage get spaceshipRamp =>
const AssetGenImage('assets/images/spaceship_ramp/spaceship_ramp.png');
}
class $AssetsImagesDashBumperAGen { class $AssetsImagesDashBumperAGen {
const $AssetsImagesDashBumperAGen(); const $AssetsImagesDashBumperAGen();

@ -15,3 +15,4 @@ export 'layer.dart';
export 'ramp_opening.dart'; export 'ramp_opening.dart';
export 'shapes/shapes.dart'; export 'shapes/shapes.dart';
export 'spaceship.dart'; export 'spaceship.dart';
export 'spaceship_ramp.dart';

@ -53,8 +53,8 @@ enum Layer {
/// Collide only with ramps opening elements. /// Collide only with ramps opening elements.
opening, opening,
/// Collide only with Jetpack group elements. /// Collide only with Spaceship entrance ramp group elements.
jetpack, spaceshipEntranceRamp,
/// Collide only with Launcher group elements. /// Collide only with Launcher group elements.
launcher, launcher,
@ -86,7 +86,7 @@ extension LayerMaskBits on Layer {
return 0x0001; return 0x0001;
case Layer.opening: case Layer.opening:
return 0x0007; return 0x0007;
case Layer.jetpack: case Layer.spaceshipEntranceRamp:
return 0x0002; return 0x0002;
case Layer.launcher: case Layer.launcher:
return 0x0005; return 0x0005;

@ -1,63 +1,66 @@
// ignore_for_file: public_member_api_docs, avoid_renaming_method_parameters // ignore_for_file: avoid_renaming_method_parameters, comment_references
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball_components/gen/assets.gen.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;
/// A [Blueprint] which creates the [JetpackRamp]. /// {@template spaceship_ramp}
class Jetpack extends Forge2DBlueprint { /// A [Blueprint] which creates the [_SpaceshipRampBackground].
/// {@macro spaceship} /// {@endtemplate}
Jetpack(); class SpaceshipRamp extends Forge2DBlueprint {
/// {@macro spaceship_ramp}
SpaceshipRamp();
/// Base priority for wall while be in the ramp.
static const int ballPriorityInsideRamp = 4; static const int ballPriorityInsideRamp = 4;
@override @override
void build(_) { void build(_) {
addAllContactCallback([ addAllContactCallback([
RampOpeningBallContactCallback<_JetpackRampOpening>(), RampOpeningBallContactCallback<_SpaceshipRampOpening>(),
]); ]);
final rightOpening = _JetpackRampOpening( final rightOpening = _SpaceshipRampOpening(
// TODO(ruimiguel): set Board priority when defined. // TODO(ruimiguel): set Board priority when defined.
outsidePriority: 1, outsidePriority: 1,
rotation: math.pi, rotation: math.pi,
) )
..initialPosition = Vector2(1.7, 19) ..initialPosition = Vector2(1.7, 19)
..layer = Layer.opening; ..layer = Layer.opening;
final leftOpening = _JetpackRampOpening( final leftOpening = _SpaceshipRampOpening(
outsideLayer: Layer.spaceship, outsideLayer: Layer.spaceship,
outsidePriority: Spaceship.ballPriorityWhenOnSpaceship, outsidePriority: Spaceship.ballPriorityWhenOnSpaceship,
rotation: math.pi, rotation: math.pi,
) )
..initialPosition = Vector2(-13.7, 19) ..initialPosition = Vector2(-13.7, 19)
..layer = Layer.jetpack; ..layer = Layer.spaceshipEntranceRamp;
final jetpackRamp = JetpackRamp(); final spaceshipRamp = _SpaceshipRampBackground();
final jetpackRampWallFg = _JetpackRampForegroundRailing(); final spaceshipRampForegroundRailing = _SpaceshipRampForegroundRailing();
final baseRight = _JetpackBase()..initialPosition = Vector2(1.7, 20); final baseRight = _SpaceshipRampBase()..initialPosition = Vector2(1.7, 20);
addAll([ addAll([
rightOpening, rightOpening,
leftOpening, leftOpening,
baseRight, baseRight,
jetpackRamp, spaceshipRamp,
jetpackRampWallFg, spaceshipRampForegroundRailing,
]); ]);
} }
} }
/// {@template jetpack_ramp} /// Represents the upper left blue ramp of the [Board] with its background
/// Represents the upper left blue ramp of the [Board]. /// railing.
/// {@endtemplate} class _SpaceshipRampBackground extends BodyComponent
class JetpackRamp extends BodyComponent with InitialPosition, Layered { with InitialPosition, Layered {
JetpackRamp() : super(priority: Jetpack.ballPriorityInsideRamp - 1) { _SpaceshipRampBackground()
layer = Layer.jetpack; : super(priority: SpaceshipRamp.ballPriorityInsideRamp - 1) {
layer = Layer.spaceshipEntranceRamp;
} }
/// Width between walls of the ramp. /// Width between walls of the ramp.
@ -113,7 +116,7 @@ class JetpackRamp extends BodyComponent with InitialPosition, Layered {
Future<void> _loadSprites() async { Future<void> _loadSprites() async {
final spriteRamp = await gameRef.loadSprite( final spriteRamp = await gameRef.loadSprite(
Assets.images.components.spaceshipRamp.path, Assets.images.spaceshipRamp.spaceshipRamp.keyName,
); );
final spriteRampComponent = SpriteComponent( final spriteRampComponent = SpriteComponent(
@ -124,7 +127,7 @@ class JetpackRamp extends BodyComponent with InitialPosition, Layered {
); );
final spriteRailingBg = await gameRef.loadSprite( final spriteRailingBg = await gameRef.loadSprite(
Assets.images.components.spaceshipRailingBg.path, Assets.images.spaceshipRamp.spaceshipRailingBg.keyName,
); );
final spriteRailingBgComponent = SpriteComponent( final spriteRailingBgComponent = SpriteComponent(
sprite: spriteRailingBg, sprite: spriteRailingBg,
@ -140,11 +143,12 @@ class JetpackRamp extends BodyComponent with InitialPosition, Layered {
} }
} }
class _JetpackRampForegroundRailing extends BodyComponent /// Represents the foreground of the railing upper left blue ramp.
class _SpaceshipRampForegroundRailing extends BodyComponent
with InitialPosition, Layered { with InitialPosition, Layered {
_JetpackRampForegroundRailing() _SpaceshipRampForegroundRailing()
: super(priority: Jetpack.ballPriorityInsideRamp + 1) { : super(priority: SpaceshipRamp.ballPriorityInsideRamp + 1) {
layer = Layer.jetpack; layer = Layer.spaceshipEntranceRamp;
} }
List<FixtureDef> _createFixtureDefs() { List<FixtureDef> _createFixtureDefs() {
@ -197,7 +201,7 @@ class _JetpackRampForegroundRailing extends BodyComponent
Future<void> _loadSprites() async { Future<void> _loadSprites() async {
final spriteRailingFg = await gameRef.loadSprite( final spriteRailingFg = await gameRef.loadSprite(
Assets.images.components.spaceshipRailingFg.path, Assets.images.spaceshipRamp.spaceshipRailingFg.keyName,
); );
final spriteRailingFgComponent = SpriteComponent( final spriteRailingFgComponent = SpriteComponent(
sprite: spriteRailingFg, sprite: spriteRailingFg,
@ -210,8 +214,9 @@ class _JetpackRampForegroundRailing extends BodyComponent
} }
} }
class _JetpackBase extends BodyComponent with InitialPosition, Layered { /// Represents the ground right base of the [SpaceshipRamp].
_JetpackBase() { class _SpaceshipRampBase extends BodyComponent with InitialPosition, Layered {
_SpaceshipRampBase() {
layer = Layer.board; layer = Layer.board;
} }
@ -240,28 +245,28 @@ class _JetpackBase extends BodyComponent with InitialPosition, Layered {
} }
} }
/// {@template jetpack_ramp_opening} /// {@template spaceship_ramp_opening}
/// [RampOpening] with [Layer.jetpack] to filter [Ball] collisions /// [RampOpening] with [Layer.spaceshipEntranceRamp] to filter [Ball] collisions
/// inside [JetpackRamp]. /// inside [_SpaceshipRampBackground].
/// {@endtemplate} /// {@endtemplate}
class _JetpackRampOpening extends RampOpening { class _SpaceshipRampOpening extends RampOpening {
/// {@macro jetpack_ramp_opening} /// {@macro spaceship_ramp_opening}
_JetpackRampOpening({ _SpaceshipRampOpening({
Layer? outsideLayer, Layer? outsideLayer,
int? outsidePriority, int? outsidePriority,
required double rotation, required double rotation,
}) : _rotation = rotation, }) : _rotation = rotation,
super( super(
insideLayer: Layer.jetpack, insideLayer: Layer.spaceshipEntranceRamp,
outsideLayer: outsideLayer, outsideLayer: outsideLayer,
orientation: RampOrientation.down, orientation: RampOrientation.down,
insidePriority: Jetpack.ballPriorityInsideRamp, insidePriority: SpaceshipRamp.ballPriorityInsideRamp,
outsidePriority: outsidePriority, outsidePriority: outsidePriority,
); );
final double _rotation; final double _rotation;
static final Vector2 _size = Vector2(JetpackRamp.width / 4, .1); static final Vector2 _size = Vector2(_SpaceshipRampBackground.width / 4, .1);
@override @override
Shape get shape { Shape get shape {

@ -32,6 +32,7 @@ flutter:
- assets/images/dash_bumper/a/ - assets/images/dash_bumper/a/
- assets/images/dash_bumper/b/ - assets/images/dash_bumper/b/
- assets/images/dash_bumper/main/ - assets/images/dash_bumper/main/
- assets/images/spaceship_ramp/
flutter_gen: flutter_gen:
line_length: 80 line_length: 80

@ -35,7 +35,7 @@ class BigSquare extends BodyComponent with InitialPosition, Layered {
paint = Paint() paint = Paint()
..color = const Color.fromARGB(255, 8, 218, 241) ..color = const Color.fromARGB(255, 8, 218, 241)
..style = PaintingStyle.stroke; ..style = PaintingStyle.stroke;
layer = Layer.jetpack; layer = Layer.spaceshipEntranceRamp;
} }
@override @override

@ -25,7 +25,7 @@ class BasicSpaceship extends BasicGame with TapDetector {
add( add(
Ball(baseColor: Colors.blue) Ball(baseColor: Colors.blue)
..initialPosition = info.eventPosition.game ..initialPosition = info.eventPosition.game
..layer = Layer.jetpack, ..layer = Layer.spaceshipEntranceRamp,
); );
} }
} }

@ -46,14 +46,15 @@ void main() {
}); });
test('correctly sets and gets', () { test('correctly sets and gets', () {
final component = TestLayeredBodyComponent()..layer = Layer.jetpack; final component = TestLayeredBodyComponent()
expect(component.layer, Layer.jetpack); ..layer = Layer.spaceshipEntranceRamp;
expect(component.layer, Layer.spaceshipEntranceRamp);
}); });
flameTester.test( flameTester.test(
'layers correctly before being loaded', 'layers correctly before being loaded',
(game) async { (game) async {
const expectedLayer = Layer.jetpack; const expectedLayer = Layer.spaceshipEntranceRamp;
final component = TestLayeredBodyComponent()..layer = expectedLayer; final component = TestLayeredBodyComponent()..layer = expectedLayer;
await game.ensureAdd(component); await game.ensureAdd(component);
// TODO(alestiago): modify once component.loaded is available. // TODO(alestiago): modify once component.loaded is available.
@ -71,7 +72,8 @@ void main() {
'when multiple different sets', 'when multiple different sets',
(game) async { (game) async {
const expectedLayer = Layer.launcher; const expectedLayer = Layer.launcher;
final component = TestLayeredBodyComponent()..layer = Layer.jetpack; final component = TestLayeredBodyComponent()
..layer = Layer.spaceshipEntranceRamp;
expect(component.layer, isNot(equals(expectedLayer))); expect(component.layer, isNot(equals(expectedLayer)));
component.layer = expectedLayer; component.layer = expectedLayer;
@ -90,7 +92,7 @@ void main() {
flameTester.test( flameTester.test(
'layers correctly after being loaded', 'layers correctly after being loaded',
(game) async { (game) async {
const expectedLayer = Layer.jetpack; const expectedLayer = Layer.spaceshipEntranceRamp;
final component = TestLayeredBodyComponent(); final component = TestLayeredBodyComponent();
await game.ensureAdd(component); await game.ensureAdd(component);
component.layer = expectedLayer; component.layer = expectedLayer;
@ -109,7 +111,7 @@ void main() {
final component = TestLayeredBodyComponent(); final component = TestLayeredBodyComponent();
await game.ensureAdd(component); await game.ensureAdd(component);
component.layer = Layer.jetpack; component.layer = Layer.spaceshipEntranceRamp;
expect(component.layer, isNot(equals(expectedLayer))); expect(component.layer, isNot(equals(expectedLayer)));
component.layer = expectedLayer; component.layer = expectedLayer;
@ -133,7 +135,7 @@ void main() {
flameTester.test( flameTester.test(
'nested Layered children will keep their layer', 'nested Layered children will keep their layer',
(game) async { (game) async {
const parentLayer = Layer.jetpack; const parentLayer = Layer.spaceshipEntranceRamp;
const childLayer = Layer.board; const childLayer = Layer.board;
final component = TestLayeredBodyComponent()..layer = parentLayer; final component = TestLayeredBodyComponent()..layer = parentLayer;
@ -152,7 +154,7 @@ void main() {
flameTester.test( flameTester.test(
'nested children will keep their layer', 'nested children will keep their layer',
(game) async { (game) async {
const parentLayer = Layer.jetpack; const parentLayer = Layer.spaceshipEntranceRamp;
final component = TestLayeredBodyComponent()..layer = parentLayer; final component = TestLayeredBodyComponent()..layer = parentLayer;
final childComponent = TestBodyComponent(); final childComponent = TestBodyComponent();

@ -44,7 +44,7 @@ void main() {
(game) async { (game) async {
final ramp = TestRampOpening( final ramp = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack, pathwayLayer: Layer.spaceshipEntranceRamp,
); );
await game.ready(); await game.ready();
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -59,7 +59,7 @@ void main() {
(game) async { (game) async {
final ramp = TestRampOpening( final ramp = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack, pathwayLayer: Layer.spaceshipEntranceRamp,
); );
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -68,7 +68,7 @@ void main() {
); );
group('first fixture', () { group('first fixture', () {
const pathwayLayer = Layer.jetpack; const pathwayLayer = Layer.spaceshipEntranceRamp;
const openingLayer = Layer.opening; const openingLayer = Layer.opening;
flameTester.test( flameTester.test(
@ -124,7 +124,7 @@ void main() {
final body = MockBody(); final body = MockBody();
final area = TestRampOpening( final area = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack, pathwayLayer: Layer.spaceshipEntranceRamp,
); );
final callback = TestRampOpeningBallContactCallback(); final callback = TestRampOpeningBallContactCallback();
@ -145,7 +145,7 @@ void main() {
final body = MockBody(); final body = MockBody();
final area = TestRampOpening( final area = TestRampOpening(
orientation: RampOrientation.up, orientation: RampOrientation.up,
pathwayLayer: Layer.jetpack, pathwayLayer: Layer.spaceshipEntranceRamp,
); );
final callback = TestRampOpeningBallContactCallback(); final callback = TestRampOpeningBallContactCallback();
@ -165,7 +165,7 @@ void main() {
final body = MockBody(); final body = MockBody();
final area = TestRampOpening( final area = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack, pathwayLayer: Layer.spaceshipEntranceRamp,
)..initialPosition = Vector2(0, 10); )..initialPosition = Vector2(0, 10);
final callback = TestRampOpeningBallContactCallback(); final callback = TestRampOpeningBallContactCallback();
@ -189,7 +189,7 @@ void main() {
final body = MockBody(); final body = MockBody();
final area = TestRampOpening( final area = TestRampOpening(
orientation: RampOrientation.up, orientation: RampOrientation.up,
pathwayLayer: Layer.jetpack, pathwayLayer: Layer.spaceshipEntranceRamp,
)..initialPosition = Vector2(0, 10); )..initialPosition = Vector2(0, 10);
final callback = TestRampOpeningBallContactCallback(); final callback = TestRampOpeningBallContactCallback();
@ -213,7 +213,7 @@ void main() {
final body = MockBody(); final body = MockBody();
final area = TestRampOpening( final area = TestRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack, pathwayLayer: Layer.spaceshipEntranceRamp,
)..initialPosition = Vector2(0, 10); )..initialPosition = Vector2(0, 10);
final callback = TestRampOpeningBallContactCallback(); final callback = TestRampOpeningBallContactCallback();

@ -0,0 +1,27 @@
// ignore_for_file: cascade_invocations
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pinball_components/pinball_components.dart';
import '../../helpers/helpers.dart';
void main() {
group('SpaceshipRamp', () {
TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(TestGame.new);
flameTester.test(
'loads correctly',
(game) async {
final spaceshipEntranceRamp = SpaceshipRamp();
await game.addFromBlueprint(spaceshipEntranceRamp);
await game.ready();
for (final element in spaceshipEntranceRamp.components) {
expect(game.contains(element), isTrue);
}
},
);
});
}
Loading…
Cancel
Save