feat: implement blinking `Kicker` assets (#283)
* refactor: simplified Fixtures creation * feat: add blinking assets * test: blinking and asset updates * docs: clean kicker docs * refactor: adjusted Kicker constructor * refactor: moved Mock Co-authored-by: alestiago <dev@alestiago.com>pull/287/head
Before Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 17 KiB |
@ -0,0 +1,2 @@
|
|||||||
|
export 'kicker_ball_contact_behavior.dart';
|
||||||
|
export 'kicker_blinking_behavior.dart';
|
@ -0,0 +1,14 @@
|
|||||||
|
// ignore_for_file: public_member_api_docs
|
||||||
|
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
class KickerBallContactBehavior extends ContactBehavior<Kicker> {
|
||||||
|
@override
|
||||||
|
void beginContact(Object other, Contact contact) {
|
||||||
|
super.beginContact(other, contact);
|
||||||
|
if (other is! Ball) return;
|
||||||
|
parent.bloc.onBallContacted();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
/// {@template kicker_blinking_behavior}
|
||||||
|
/// Makes a [Kicker] blink back to [KickerState.lit] when [KickerState.dimmed].
|
||||||
|
/// {@endtemplate}
|
||||||
|
class KickerBlinkingBehavior extends TimerComponent with ParentIsA<Kicker> {
|
||||||
|
/// {@macro kicker_blinking_behavior}
|
||||||
|
KickerBlinkingBehavior() : super(period: 0.05);
|
||||||
|
|
||||||
|
void _onNewState(KickerState state) {
|
||||||
|
switch (state) {
|
||||||
|
case KickerState.lit:
|
||||||
|
break;
|
||||||
|
case KickerState.dimmed:
|
||||||
|
timer
|
||||||
|
..reset()
|
||||||
|
..start();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
timer.stop();
|
||||||
|
parent.bloc.stream.listen(_onNewState);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onTick() {
|
||||||
|
super.onTick();
|
||||||
|
timer.stop();
|
||||||
|
parent.bloc.onBlinked();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
// ignore_for_file: public_member_api_docs
|
||||||
|
|
||||||
|
import 'package:bloc/bloc.dart';
|
||||||
|
|
||||||
|
part 'kicker_state.dart';
|
||||||
|
|
||||||
|
class KickerCubit extends Cubit<KickerState> {
|
||||||
|
KickerCubit() : super(KickerState.lit);
|
||||||
|
|
||||||
|
void onBallContacted() {
|
||||||
|
emit(KickerState.dimmed);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onBlinked() {
|
||||||
|
emit(KickerState.lit);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
// ignore_for_file: public_member_api_docs
|
||||||
|
|
||||||
|
part of 'kicker_cubit.dart';
|
||||||
|
|
||||||
|
enum KickerState {
|
||||||
|
lit,
|
||||||
|
dimmed,
|
||||||
|
}
|
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 70 KiB |
@ -0,0 +1,53 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:bloc_test/bloc_test.dart';
|
||||||
|
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/kicker/behaviors/behaviors.dart';
|
||||||
|
|
||||||
|
import '../../../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
class _MockKickerCubit extends Mock implements KickerCubit {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(TestGame.new);
|
||||||
|
|
||||||
|
group(
|
||||||
|
'KickerBallContactBehavior',
|
||||||
|
() {
|
||||||
|
test('can be instantiated', () {
|
||||||
|
expect(
|
||||||
|
KickerBallContactBehavior(),
|
||||||
|
isA<KickerBallContactBehavior>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'beginContact emits onBallContacted when contacts with a ball',
|
||||||
|
(game) async {
|
||||||
|
final behavior = KickerBallContactBehavior();
|
||||||
|
final bloc = _MockKickerCubit();
|
||||||
|
whenListen(
|
||||||
|
bloc,
|
||||||
|
const Stream<KickerState>.empty(),
|
||||||
|
initialState: KickerState.lit,
|
||||||
|
);
|
||||||
|
|
||||||
|
final kicker = Kicker.test(
|
||||||
|
side: BoardSide.left,
|
||||||
|
bloc: bloc,
|
||||||
|
);
|
||||||
|
await kicker.add(behavior);
|
||||||
|
await game.ensureAdd(kicker);
|
||||||
|
|
||||||
|
behavior.beginContact(MockBall(), MockContact());
|
||||||
|
|
||||||
|
verify(kicker.bloc.onBallContacted).called(1);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:bloc_test/bloc_test.dart';
|
||||||
|
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/kicker/behaviors/behaviors.dart';
|
||||||
|
|
||||||
|
import '../../../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
class _MockKickerCubit extends Mock implements KickerCubit {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(TestGame.new);
|
||||||
|
|
||||||
|
group(
|
||||||
|
'KickerBlinkingBehavior',
|
||||||
|
() {
|
||||||
|
flameTester.testGameWidget(
|
||||||
|
'calls onBlinked after 0.05 seconds when dimmed',
|
||||||
|
setUp: (game, tester) async {
|
||||||
|
final behavior = KickerBlinkingBehavior();
|
||||||
|
final bloc = _MockKickerCubit();
|
||||||
|
final streamController = StreamController<KickerState>();
|
||||||
|
whenListen(
|
||||||
|
bloc,
|
||||||
|
streamController.stream,
|
||||||
|
initialState: KickerState.lit,
|
||||||
|
);
|
||||||
|
|
||||||
|
final kicker = Kicker.test(
|
||||||
|
side: BoardSide.left,
|
||||||
|
bloc: bloc,
|
||||||
|
);
|
||||||
|
await kicker.add(behavior);
|
||||||
|
await game.ensureAdd(kicker);
|
||||||
|
|
||||||
|
streamController.add(KickerState.dimmed);
|
||||||
|
await tester.pump();
|
||||||
|
game.update(0.05);
|
||||||
|
|
||||||
|
await streamController.close();
|
||||||
|
verify(bloc.onBlinked).called(1);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
import 'package:bloc_test/bloc_test.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group(
|
||||||
|
'KickerCubit',
|
||||||
|
() {
|
||||||
|
blocTest<KickerCubit, KickerState>(
|
||||||
|
'onBallContacted emits dimmed',
|
||||||
|
build: KickerCubit.new,
|
||||||
|
act: (bloc) => bloc.onBallContacted(),
|
||||||
|
expect: () => [KickerState.dimmed],
|
||||||
|
);
|
||||||
|
|
||||||
|
blocTest<KickerCubit, KickerState>(
|
||||||
|
'onBlinked emits lit',
|
||||||
|
build: KickerCubit.new,
|
||||||
|
act: (bloc) => bloc.onBlinked(),
|
||||||
|
expect: () => [KickerState.lit],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|