From c880884b5c92a9032e83264514a427aebea9e67b Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Tue, 15 Mar 2022 11:17:58 +0100 Subject: [PATCH] chore: ramp area name changed to opening --- lib/game/components/crossing_ramp.dart | 43 ++++++++------- lib/game/components/jetpack_ramp.dart | 55 ++++++++++--------- lib/game/components/sparky_ramp.dart | 56 ++++++++++---------- test/game/components/crossing_ramp_test.dart | 5 +- test/game/components/jetpack_ramp_test.dart | 12 +++-- test/game/components/sparky_ramp_test.dart | 12 +++-- test/helpers/mocks.dart | 5 +- 7 files changed, 101 insertions(+), 87 deletions(-) diff --git a/lib/game/components/crossing_ramp.dart b/lib/game/components/crossing_ramp.dart index 184a3d9d..847e12f0 100644 --- a/lib/game/components/crossing_ramp.dart +++ b/lib/game/components/crossing_ramp.dart @@ -14,7 +14,7 @@ enum RampOrientation { down, } -/// Indicates the type of the ramp. +/// Indicates a type of the ramp. /// /// Used to set the maskBits of the ramp to determine their possible collisions. enum RampType { @@ -46,15 +46,17 @@ extension RampTypeX on RampType { } } -/// {@template ramp_area} +/// {@template ramp_opening} /// [BodyComponent] located at the entrance and exit of a ramp. /// -/// Detects when a [Ball] goes through it, -/// Collisions with [RampArea] are listened by [RampAreaCallback]. +/// [RampOpeningBallContactCallback] detects when a [Ball] passes +/// through this opening. +/// Collisions with [RampOpening] are listened +/// by [RampOpeningBallContactCallback]. /// {@endtemplate} -abstract class RampArea extends BodyComponent { - /// {@macro ramp_area} - RampArea({ +abstract class RampOpening extends BodyComponent { + /// {@macro ramp_opening} + RampOpening({ required Vector2 position, required int categoryBits, }) : _position = position, @@ -66,37 +68,38 @@ abstract class RampArea extends BodyComponent { final Vector2 _position; final int _categoryBits; - /// Mask of category bits for collision with [RampArea] + /// Mask of category bits for collision with [RampOpening] int get categoryBits => _categoryBits; - /// The [Shape] of the [RampArea] + /// The [Shape] of the [RampOpening] Shape get shape; - /// Orientation of the [RampArea] entrance/exit + /// Orientation of the [RampOpening] entrance/exit RampOrientation get orientation; @override Body createBody() { - final fixtureDef = FixtureDef(shape) - ..isSensor = true - ..filter.categoryBits = _categoryBits; + final fixtureDef = FixtureDef(shape)..isSensor = true; final bodyDef = BodyDef() ..userData = this ..position = _position ..type = BodyType.static; - return world.createBody(bodyDef)..createFixture(fixtureDef); + final body = world.createBody(bodyDef); + body.createFixture(fixtureDef).filterData.categoryBits = _categoryBits; + + return body; } } -/// {@template ramp_area_callback} -/// Listens when a [Ball] goes through a [RampArea] and gets into a [Pathway] -/// ramp, in order to avoid collisions with other crossing ramps. -/// Modifies [Ball]'s maskBits while is inside the ramp. When exits sets [Ball] -/// maskBits to collide with all elements. +/// {@template ramp_opening_ball_contact_callback} +/// Detects when a [Ball] enters or exits a [Pathway] ramp through a +/// [RampOpening]. +/// Modifies [Ball]'s maskBits while is inside the ramp. When [Ball] exits, +/// sets maskBits to collide with all elements. /// {@endtemplate} -abstract class RampAreaCallback +abstract class RampOpeningBallContactCallback extends ContactCallback { /// Collection of balls inside ramp pathway. Set get ballsInside; diff --git a/lib/game/components/jetpack_ramp.dart b/lib/game/components/jetpack_ramp.dart index caaef312..2ffd6c32 100644 --- a/lib/game/components/jetpack_ramp.dart +++ b/lib/game/components/jetpack_ramp.dart @@ -7,13 +7,13 @@ import 'package:pinball/game/game.dart'; /// Represents the upper left blue ramp for the game. /// /// Composed of a [Pathway.arc] defining the ramp, and two -/// [JetpackRampArea]s at the entrance and exit of the ramp. +/// [JetpackRampOpening]s at the entrance and exit of the ramp. /// {@endtemplate} -class JetpackRamp extends PositionComponent with HasGameRef { +class JetpackRamp extends Component with HasGameRef { /// {@macro jetpack_ramp} JetpackRamp({ - required Vector2 position, - }) : _position = position; + required this.position, + }); final double _radius = 200; final double _width = 80; @@ -21,14 +21,16 @@ class JetpackRamp extends PositionComponent with HasGameRef { final double _rotation = radians(-10); final double _entranceRotation = radians(15); final double _exitRotation = radians(-5); - final Vector2 _position; + + /// The position of this [JetpackRamp] + final Vector2 position; @override Future onLoad() async { await add( Pathway.arc( color: const Color.fromARGB(255, 8, 218, 241), - position: _position, + position: position, width: _width, radius: _radius, angle: _angle, @@ -38,33 +40,33 @@ class JetpackRamp extends PositionComponent with HasGameRef { ); await add( - JetpackRampArea( - position: _position + Vector2(-10.5, 0), + JetpackRampOpening( + position: position + Vector2(-10.5, 0), rotation: _entranceRotation, orientation: RampOrientation.down, ), ); await add( - JetpackRampArea( - position: _position + Vector2(20.5, 3), + JetpackRampOpening( + position: position + Vector2(20.5, 3), rotation: _exitRotation, orientation: RampOrientation.down, ), ); - gameRef.addContactCallback(JetpackRampAreaCallback()); + gameRef.addContactCallback(JetpackRampOpeningBallContactCallback()); } } -/// {@template jetpack_ramp_area} -/// Implementation of [RampArea] for sensors in [JetpackRamp]. +/// {@template jetpack_ramp_opening} +/// Implementation of [RampOpening] for sensors in [JetpackRamp]. /// -/// [RampArea] with [RampType.jetpack] to filter [Ball]s collisions +/// [RampOpening] with [RampType.jetpack] to filter [Ball]s collisions /// inside [JetpackRamp]. /// {@endtemplate} -class JetpackRampArea extends RampArea { - /// {@macro jetpack_ramp_area} - JetpackRampArea({ +class JetpackRampOpening extends RampOpening { + /// {@macro jetpack_ramp_opening} + JetpackRampOpening({ required Vector2 position, double rotation = 0, required RampOrientation orientation, @@ -76,14 +78,14 @@ class JetpackRampArea extends RampArea { ); /// Orientation of entrance/exit of [JetpackRamp] where - /// this [JetpackRampArea] is placed. + /// this [JetpackRampOpening] is placed. final RampOrientation _orientation; - /// Rotation of the [RampArea] to place it right at the + /// Rotation of the [RampOpening] to place it right at the /// entrance/exit of [JetpackRamp]. final double _rotation; - /// Size of the [RampArea] placed at the entrance/exit of [JetpackRamp]. + /// Size of the [RampOpening] placed at the entrance/exit of [JetpackRamp]. final int _size = 7; @override @@ -99,13 +101,14 @@ class JetpackRampArea extends RampArea { ]); } -/// {@template jetpack_ramp_area_callback} -/// Implementation of [RampAreaCallback] to listen when a [Ball] -/// gets into a [JetpackRampArea]. +/// {@template jetpack_ramp_opening_ball_contact_callback} +/// Implementation of [RampOpeningBallContactCallback] to listen when a [Ball] +/// gets into a [JetpackRampOpening]. /// {@endtemplate} -class JetpackRampAreaCallback extends RampAreaCallback { - /// {@macro jetpack_ramp_area_callback} - JetpackRampAreaCallback() : super(); +class JetpackRampOpeningBallContactCallback + extends RampOpeningBallContactCallback { + /// {@macro jetpack_ramp_opening_ball_contact_callback} + JetpackRampOpeningBallContactCallback() : super(); /// Collection of balls inside [JetpackRamp]. final _ballsInsideJetpack = {}; diff --git a/lib/game/components/sparky_ramp.dart b/lib/game/components/sparky_ramp.dart index 19690371..2a976787 100644 --- a/lib/game/components/sparky_ramp.dart +++ b/lib/game/components/sparky_ramp.dart @@ -7,27 +7,28 @@ import 'package:pinball/game/game.dart'; /// Represent the upper right yellow ramp for the game. /// /// Group of [Component]s composed by a [Pathway.arc] as the ramp, and two -/// [SparkyRampArea] at the entrance and exit of the ramp, to detect when +/// [SparkyRampOpening] at the entrance and exit of the ramp, to detect when /// a ball gets into/out of the ramp. /// {@endtemplate} -class SparkyRamp extends PositionComponent with HasGameRef { +class SparkyRamp extends Component with HasGameRef { /// {@macro sparky_ramp} SparkyRamp({ - required Vector2 position, - }) : _position = position, - super(); + required this.position, + }); final double _radius = 300; final double _width = 80; final double _angle = radians(200); - final Vector2 _position; + + /// The position of this [SparkyRamp] + final Vector2 position; @override Future onLoad() async { await add( Pathway.arc( color: const Color.fromARGB(255, 251, 255, 0), - position: _position, + position: position, radius: _radius, angle: _angle, width: _width, @@ -35,32 +36,32 @@ class SparkyRamp extends PositionComponent with HasGameRef { ), ); await add( - SparkyRampArea( - position: _position + Vector2(-18, -2), + SparkyRampOpening( + position: position + Vector2(-18, -2), orientation: RampOrientation.down, rotation: radians(13), ), ); await add( - SparkyRampArea( - position: _position + Vector2(33, 6), + SparkyRampOpening( + position: position + Vector2(33, 6), orientation: RampOrientation.down, ), ); - gameRef.addContactCallback(SparkyRampAreaCallback()); + gameRef.addContactCallback(SparkyRampOpeningBallContactCallback()); } } -/// {@template sparky_ramp_area} -/// Implementation of [RampArea] for sensors in [SparkyRamp]. +/// {@template sparky_ramp_opening} +/// Implementation of [RampOpening] for sensors in [SparkyRamp]. /// -/// [RampArea] with [RampType.sparky] to filter [Ball]s collisions +/// [RampOpening] with [RampType.sparky] to filter [Ball]s collisions /// inside [SparkyRamp]. /// {@endtemplate} -class SparkyRampArea extends RampArea { - /// {@macro sparky_ramp_area} - SparkyRampArea({ +class SparkyRampOpening extends RampOpening { + /// {@macro sparky_ramp_opening} + SparkyRampOpening({ required Vector2 position, double rotation = 0, required RampOrientation orientation, @@ -72,14 +73,14 @@ class SparkyRampArea extends RampArea { ); /// Orientation of entrance/exit of [SparkyRamp] where - /// this [SparkyRampArea] is placed. + /// this [SparkyRampOpening] is placed. final RampOrientation _orientation; - /// Rotation of the [RampArea] to place it right at the + /// Rotation of the [RampOpening] to place it right at the /// entrance/exit of [SparkyRamp]. final double _rotation; - /// Size of the [RampArea] placed at the entrance/exit of [SparkyRamp]. + /// Size of the [RampOpening] placed at the entrance/exit of [SparkyRamp]. final int _size = 7; @override @@ -95,13 +96,14 @@ class SparkyRampArea extends RampArea { ]); } -/// {@template sparky_ramp_area_callback} -/// Implementation of [RampAreaCallback] to listen when a [Ball] -/// gets into a [SparkyRampArea]. +/// {@template sparky_ramp_opening_ball_contact_callback} +/// Implementation of [RampOpeningBallContactCallback] to listen when a [Ball] +/// gets into a [SparkyRampOpening]. /// {@endtemplate} -class SparkyRampAreaCallback extends RampAreaCallback { - /// {@macro sparky_ramp_area_callback} - SparkyRampAreaCallback() : super(); +class SparkyRampOpeningBallContactCallback + extends RampOpeningBallContactCallback { + /// {@macro sparky_ramp_opening_ball_contact_callback} + SparkyRampOpeningBallContactCallback() : super(); /// Collection of balls inside [SparkyRamp]. final _ballsInsideSparky = {}; diff --git a/test/game/components/crossing_ramp_test.dart b/test/game/components/crossing_ramp_test.dart index af2b84e6..e98daac1 100644 --- a/test/game/components/crossing_ramp_test.dart +++ b/test/game/components/crossing_ramp_test.dart @@ -7,7 +7,7 @@ import 'package:pinball/game/game.dart'; import '../../helpers/helpers.dart'; -class TestRampArea extends RampArea { +class TestRampArea extends RampOpening { TestRampArea({ required Vector2 position, required RampOrientation orientation, @@ -30,7 +30,8 @@ class TestRampArea extends RampArea { ]); } -class TestRampAreaCallback extends RampAreaCallback { +class TestRampAreaCallback + extends RampOpeningBallContactCallback { TestRampAreaCallback() : super(); final _ballsInside = {}; diff --git a/test/game/components/jetpack_ramp_test.dart b/test/game/components/jetpack_ramp_test.dart index 8b6e3321..434ab9bf 100644 --- a/test/game/components/jetpack_ramp_test.dart +++ b/test/game/components/jetpack_ramp_test.dart @@ -9,7 +9,7 @@ import 'package:pinball/game/game.dart'; import '../../helpers/helpers.dart'; -class MockJetpackRampArea extends Mock implements JetpackRampArea {} +class MockJetpackRampArea extends Mock implements JetpackRampOpening {} void main() { TestWidgetsFlutterBinding.ensureInitialized(); @@ -33,7 +33,7 @@ void main() { flameTester.test( 'positions correctly', (game) async { - final position = Vector2.zero(); + final position = Vector2.all(10); final ramp = JetpackRamp( position: position, ); @@ -72,7 +72,8 @@ void main() { await game.ready(); await game.ensureAdd(ramp); - final rampAreas = ramp.children.whereType().toList(); + final rampAreas = + ramp.children.whereType().toList(); expect(rampAreas.length, 2); }, ); @@ -84,7 +85,7 @@ void main() { 'orientation is down', (game) async { final position = Vector2.all(10); - final ramp = JetpackRampArea( + final ramp = JetpackRampOpening( position: position, orientation: RampOrientation.down, ); @@ -98,7 +99,8 @@ void main() { group('JetpackRampAreaCallback', () { test('has no ball inside on creation', () { - expect(JetpackRampAreaCallback().ballsInside, equals({})); + expect(JetpackRampOpeningBallContactCallback().ballsInside, + equals({})); }); }); } diff --git a/test/game/components/sparky_ramp_test.dart b/test/game/components/sparky_ramp_test.dart index fd24bd48..84cb3a7f 100644 --- a/test/game/components/sparky_ramp_test.dart +++ b/test/game/components/sparky_ramp_test.dart @@ -9,7 +9,7 @@ import 'package:pinball/game/game.dart'; import '../../helpers/helpers.dart'; -class MockSparkyRampArea extends Mock implements SparkyRampArea {} +class MockSparkyRampArea extends Mock implements SparkyRampOpening {} void main() { TestWidgetsFlutterBinding.ensureInitialized(); @@ -33,7 +33,7 @@ void main() { flameTester.test( 'positions correctly', (game) async { - final position = Vector2.zero(); + final position = Vector2.all(10); final ramp = SparkyRamp( position: position, ); @@ -72,7 +72,8 @@ void main() { await game.ready(); await game.ensureAdd(ramp); - final rampAreas = ramp.children.whereType().toList(); + final rampAreas = + ramp.children.whereType().toList(); expect(rampAreas.length, 2); }, ); @@ -84,7 +85,7 @@ void main() { 'orientation is down', (game) async { final position = Vector2.all(10); - final ramp = SparkyRampArea( + final ramp = SparkyRampOpening( position: position, orientation: RampOrientation.down, ); @@ -98,7 +99,8 @@ void main() { group('SparkyRampAreaCallback', () { test('has no ball inside on creation', () { - expect(SparkyRampAreaCallback().ballsInside, equals({})); + expect( + SparkyRampOpeningBallContactCallback().ballsInside, equals({})); }); }); } diff --git a/test/helpers/mocks.dart b/test/helpers/mocks.dart index 2c6c4264..cace878a 100644 --- a/test/helpers/mocks.dart +++ b/test/helpers/mocks.dart @@ -18,9 +18,10 @@ class MockBall extends Mock implements Ball {} class MockContact extends Mock implements Contact {} -class MockArea extends Mock implements RampArea {} +class MockRampOpening extends Mock implements RampOpening {} -class MockRampAreaCallback extends Mock implements RampAreaCallback {} +class MockRampOpeningBallContactCallback extends Mock + implements RampOpeningBallContactCallback {} class MockGameBloc extends Mock implements GameBloc {}