refactor: changed name for RampBallAscendingContactBehavior

pull/296/head
RuiAlonso 3 years ago
parent bb28113d6b
commit 069809a440

@ -1 +1 @@
export 'ramp_ball_contact_behavior.dart'; export 'ramp_ball_ascending_contact_behavior.dart';

@ -4,20 +4,21 @@ import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart'; import 'package:pinball_flame/pinball_flame.dart';
/// {@template ramp_ball_contact_behavior} /// {@template ramp_ball_ascending_contact_behavior}
/// Detects a [Ball]that enters in the [SpaceshipRamp]. /// Detects an ascending [Ball] that enters into the [SpaceshipRamp].
/// ///
/// The [Ball] can hit with sensor to recognize if [Ball] goes in or out the /// The [Ball] can hit with sensor to recognize if a [Ball] goes into or out of
/// [SpaceshipRamp]. /// the [SpaceshipRamp].
/// {@endtemplate} /// {@endtemplate}
class RampBallContactBehavior extends ContactBehavior<RampScoringSensor> { class RampBallAscendingContactBehavior
extends ContactBehavior<RampScoringSensor> {
@override @override
void beginContact(Object other, Contact contact) { void beginContact(Object other, Contact contact) {
super.beginContact(other, contact); super.beginContact(other, contact);
if (other is! Ball) return; if (other is! Ball) return;
if (other.body.linearVelocity.y < 0) { if (other.body.linearVelocity.y < 0) {
parent.parent.bloc.onBallInside(); parent.parent.bloc.onAscendingBallEntered();
} }
} }
} }

@ -8,7 +8,7 @@ part 'spaceship_ramp_state.dart';
class SpaceshipRampCubit extends Cubit<SpaceshipRampState> { class SpaceshipRampCubit extends Cubit<SpaceshipRampState> {
SpaceshipRampCubit() : super(const SpaceshipRampState.initial()); SpaceshipRampCubit() : super(const SpaceshipRampState.initial());
void onBallInside() { void onAscendingBallEntered() {
emit( emit(
state.copyWith(hits: state.hits + 1), state.copyWith(hits: state.hits + 1),
); );

@ -27,11 +27,11 @@ class SpaceshipRamp extends Component {
required this.bloc, required this.bloc,
}) : super( }) : super(
children: [ children: [
// TODO(ruimiguel): refactor RampSensor and RampOpening to be in // TODO(ruimiguel): refactor RampScoringSensor and
// only one sensor. // _SpaceshipRampOpening to be in only one sensor if possible.
RampScoringSensor( RampScoringSensor(
children: [ children: [
RampBallContactBehavior(), RampBallAscendingContactBehavior(),
], ],
)..initialPosition = Vector2(1.7, -20.4), )..initialPosition = Vector2(1.7, -20.4),
_SpaceshipRampOpening( _SpaceshipRampOpening(

@ -54,7 +54,7 @@ class SpaceshipRampGame extends BallGame with KeyboardEvents {
) { ) {
if (event is RawKeyDownEvent && if (event is RawKeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.space) { event.logicalKey == LogicalKeyboardKey.space) {
_spaceshipRamp.bloc.onBallInside(); _spaceshipRamp.bloc.onAscendingBallEntered();
return KeyEventResult.handled; return KeyEventResult.handled;
} }

@ -36,12 +36,12 @@ void main() {
final flameTester = FlameTester(() => TestGame(assets)); final flameTester = FlameTester(() => TestGame(assets));
group( group(
'RampBallContactBehavior', 'RampBallAscendingContactBehavior',
() { () {
test('can be instantiated', () { test('can be instantiated', () {
expect( expect(
RampBallContactBehavior(), RampBallAscendingContactBehavior(),
isA<RampBallContactBehavior>(), isA<RampBallAscendingContactBehavior>(),
); );
}); });
@ -57,9 +57,9 @@ void main() {
}); });
flameTester.test( flameTester.test(
"calls 'onBallInside' when a ball enters into the ramp", "calls 'onAscendingBallEntered' when a ball enters into the ramp",
(game) async { (game) async {
final behavior = RampBallContactBehavior(); final behavior = RampBallAscendingContactBehavior();
final bloc = _MockSpaceshipRampCubit(); final bloc = _MockSpaceshipRampCubit();
whenListen( whenListen(
bloc, bloc,
@ -80,14 +80,14 @@ void main() {
behavior.beginContact(ball, _MockContact()); behavior.beginContact(ball, _MockContact());
verify(bloc.onBallInside).called(1); verify(bloc.onAscendingBallEntered).called(1);
}, },
); );
flameTester.test( flameTester.test(
"doesn't call 'onBallInside' when a ball goes out the ramp", "doesn't call 'onAscendingBallEntered' when a ball goes out the ramp",
(game) async { (game) async {
final behavior = RampBallContactBehavior(); final behavior = RampBallAscendingContactBehavior();
final bloc = _MockSpaceshipRampCubit(); final bloc = _MockSpaceshipRampCubit();
whenListen( whenListen(
bloc, bloc,
@ -108,7 +108,7 @@ void main() {
behavior.beginContact(ball, _MockContact()); behavior.beginContact(ball, _MockContact());
verifyNever(bloc.onBallInside); verifyNever(bloc.onAscendingBallEntered);
}, },
); );
}); });

@ -6,14 +6,14 @@ import 'package:pinball_components/pinball_components.dart';
void main() { void main() {
group('SpaceshipRampCubit', () { group('SpaceshipRampCubit', () {
group('onBallInside', () { group('onAscendingBallEntered', () {
blocTest<SpaceshipRampCubit, SpaceshipRampState>( blocTest<SpaceshipRampCubit, SpaceshipRampState>(
'emits hits incremented and arrow goes to the next value', 'emits hits incremented and arrow goes to the next value',
build: SpaceshipRampCubit.new, build: SpaceshipRampCubit.new,
act: (bloc) => bloc act: (bloc) => bloc
..onBallInside() ..onAscendingBallEntered()
..onBallInside() ..onAscendingBallEntered()
..onBallInside(), ..onAscendingBallEntered(),
expect: () => [ expect: () => [
SpaceshipRampState(hits: 1), SpaceshipRampState(hits: 1),
SpaceshipRampState(hits: 2), SpaceshipRampState(hits: 2),

@ -79,7 +79,7 @@ void main() {
final canvas = ZCanvasComponent(children: [ramp]); final canvas = ZCanvasComponent(children: [ramp]);
await game.ensureAdd(canvas); await game.ensureAdd(canvas);
ramp.bloc.onBallInside(); ramp.bloc.onAscendingBallEntered();
await game.ready(); await game.ready();
await tester.pump(); await tester.pump();
@ -112,8 +112,8 @@ void main() {
await game.ensureAdd(canvas); await game.ensureAdd(canvas);
ramp.bloc ramp.bloc
..onBallInside() ..onAscendingBallEntered()
..onBallInside(); ..onAscendingBallEntered();
await game.ready(); await game.ready();
await tester.pump(); await tester.pump();
@ -146,9 +146,9 @@ void main() {
await game.ensureAdd(canvas); await game.ensureAdd(canvas);
ramp.bloc ramp.bloc
..onBallInside() ..onAscendingBallEntered()
..onBallInside() ..onAscendingBallEntered()
..onBallInside(); ..onAscendingBallEntered();
await game.ready(); await game.ready();
await tester.pump(); await tester.pump();
@ -181,10 +181,10 @@ void main() {
await game.ensureAdd(canvas); await game.ensureAdd(canvas);
ramp.bloc ramp.bloc
..onBallInside() ..onAscendingBallEntered()
..onBallInside() ..onAscendingBallEntered()
..onBallInside() ..onAscendingBallEntered()
..onBallInside(); ..onAscendingBallEntered();
await game.ready(); await game.ready();
await tester.pump(); await tester.pump();
@ -217,11 +217,11 @@ void main() {
await game.ensureAdd(canvas); await game.ensureAdd(canvas);
ramp.bloc ramp.bloc
..onBallInside() ..onAscendingBallEntered()
..onBallInside() ..onAscendingBallEntered()
..onBallInside() ..onAscendingBallEntered()
..onBallInside() ..onAscendingBallEntered()
..onBallInside(); ..onAscendingBallEntered();
await game.ready(); await game.ready();
await tester.pump(); await tester.pump();

Loading…
Cancel
Save