refactor: removed RampScoringSensor (#395)

pull/401/head
Alejandro Santiago 3 years ago committed by GitHub
parent 28a3c15fe2
commit 8920689de4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,7 +9,7 @@ import 'package:pinball_flame/pinball_flame.dart';
/// the [SpaceshipRamp].
/// {@endtemplate}
class RampBallAscendingContactBehavior
extends ContactBehavior<RampScoringSensor> {
extends ContactBehavior<SpaceshipRampBoardOpening> {
@override
void beginContact(Object other, Contact contact) {
super.beginContact(other, contact);

@ -27,11 +27,6 @@ class SpaceshipRamp extends Component {
required this.bloc,
}) : super(
children: [
RampScoringSensor(
children: [
RampBallAscendingContactBehavior(),
],
)..initialPosition = Vector2(1.7, -20.4),
_SpaceshipRampOpening(
outsideLayer: Layer.spaceship,
outsidePriority: ZIndexes.ballOnSpaceship,
@ -40,7 +35,7 @@ class SpaceshipRamp extends Component {
..initialPosition = Vector2(-13.7, -18.6)
..layer = Layer.spaceshipEntranceRamp,
_SpaceshipRampBackground(),
_SpaceshipRampBoardOpening()..initialPosition = Vector2(3.4, -39.5),
SpaceshipRampBoardOpening()..initialPosition = Vector2(3.4, -39.5),
_SpaceshipRampForegroundRailing(),
SpaceshipRampBase()..initialPosition = Vector2(3.4, -42.5),
_SpaceshipRampBackgroundRailingSpriteComponent(),
@ -246,13 +241,14 @@ extension on SpaceshipRampArrowSpriteState {
}
}
class _SpaceshipRampBoardOpening extends BodyComponent
with Layered, ZIndex, InitialPosition {
_SpaceshipRampBoardOpening()
class SpaceshipRampBoardOpening extends BodyComponent
with Layered, ZIndex, InitialPosition, ParentIsA<SpaceshipRamp> {
SpaceshipRampBoardOpening()
: super(
renderBody: false,
children: [
_SpaceshipRampBoardOpeningSpriteComponent(),
RampBallAscendingContactBehavior()..applyTo(['inside']),
LayerContactBehavior(layer: Layer.spaceshipEntranceRamp)
..applyTo(['inside']),
LayerContactBehavior(
@ -271,6 +267,13 @@ class _SpaceshipRampBoardOpening extends BodyComponent
layer = Layer.opening;
}
/// Creates a [SpaceshipRampBoardOpening] without any children.
///
/// This can be used for testing [SpaceshipRampBoardOpening]'s behaviors in
/// isolation.
@visibleForTesting
SpaceshipRampBoardOpening.test();
List<FixtureDef> _createFixtureDefs() {
final topEdge = EdgeShape()
..set(
@ -495,46 +498,3 @@ class _SpaceshipRampOpening extends LayerSensor {
);
}
}
/// {@template ramp_scoring_sensor}
/// Small sensor body used to detect when a ball has entered the
/// [SpaceshipRamp].
/// {@endtemplate}
class RampScoringSensor extends BodyComponent
with ParentIsA<SpaceshipRamp>, InitialPosition, Layered {
/// {@macro ramp_scoring_sensor}
RampScoringSensor({
Iterable<Component>? children,
}) : super(
children: children,
renderBody: false,
) {
layer = Layer.spaceshipEntranceRamp;
}
/// Creates a [RampScoringSensor] without any children.
@visibleForTesting
RampScoringSensor.test();
@override
Body createBody() {
final shape = PolygonShape()
..setAsBox(
2.6,
.5,
initialPosition,
-5 * math.pi / 180,
);
final fixtureDef = FixtureDef(
shape,
isSensor: true,
);
final bodyDef = BodyDef(
position: initialPosition,
userData: this,
);
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}

@ -67,16 +67,16 @@ void main() {
initialState: const SpaceshipRampState.initial(),
);
final rampSensor = RampScoringSensor.test();
final parent = SpaceshipRampBoardOpening.test();
final spaceshipRamp = SpaceshipRamp.test(
bloc: bloc,
);
when(() => body.linearVelocity).thenReturn(Vector2(0, -1));
await spaceshipRamp.add(rampSensor);
await spaceshipRamp.add(parent);
await game.ensureAddAll([spaceshipRamp, ball]);
await rampSensor.add(behavior);
await parent.add(behavior);
behavior.beginContact(ball, _MockContact());
@ -95,16 +95,16 @@ void main() {
initialState: const SpaceshipRampState.initial(),
);
final rampSensor = RampScoringSensor.test();
final parent = SpaceshipRampBoardOpening.test();
final spaceshipRamp = SpaceshipRamp.test(
bloc: bloc,
);
when(() => body.linearVelocity).thenReturn(Vector2(0, 1));
await spaceshipRamp.add(rampSensor);
await spaceshipRamp.add(parent);
await game.ensureAddAll([spaceshipRamp, ball]);
await rampSensor.add(behavior);
await parent.add(behavior);
behavior.beginContact(ball, _MockContact());

@ -7,6 +7,7 @@ import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_components/src/components/spaceship_ramp/behavior/behavior.dart';
import 'package:pinball_flame/pinball_flame.dart';
import '../../../helpers/helpers.dart';
@ -324,4 +325,29 @@ void main() {
},
);
});
group('SpaceshipRampBoardOpening', () {
test('can be instantiated', () {
expect(SpaceshipRampBoardOpening(), isA<SpaceshipRampBoardOpening>());
});
flameTester.test('can be loaded', (game) async {
final parent = SpaceshipRamp.test(bloc: _MockSpaceshipRampCubit());
final component = SpaceshipRampBoardOpening();
await game.ensureAdd(parent);
await parent.ensureAdd(component);
expect(parent.children, contains(component));
});
flameTester.test('adds a RampBallAscendingContactBehavior', (game) async {
final parent = SpaceshipRamp.test(bloc: _MockSpaceshipRampCubit());
final component = SpaceshipRampBoardOpening();
await game.ensureAdd(parent);
await parent.ensureAdd(component);
expect(
component.children.whereType<RampBallAscendingContactBehavior>().length,
equals(1),
);
});
});
}

Loading…
Cancel
Save