mirror of https://github.com/flutter/pinball.git
commit
7e439975f6
@ -0,0 +1,26 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
class AnimatronicLoopingBehavior extends TimerComponent
|
||||||
|
with ParentIsA<SpriteAnimationComponent> {
|
||||||
|
AnimatronicLoopingBehavior({
|
||||||
|
required double animationCoolDown,
|
||||||
|
}) : super(period: animationCoolDown);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
parent.animation?.onComplete = () {
|
||||||
|
parent.animation?.reset();
|
||||||
|
parent.playing = false;
|
||||||
|
timer
|
||||||
|
..reset()
|
||||||
|
..start();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onTick() {
|
||||||
|
parent.playing = true;
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
@ -1,3 +1,4 @@
|
|||||||
export 'flipper_jointing_behavior.dart';
|
export 'flipper_jointing_behavior.dart';
|
||||||
export 'flipper_key_controlling_behavior.dart';
|
export 'flipper_key_controlling_behavior.dart';
|
||||||
export 'flipper_moving_behavior.dart';
|
export 'flipper_moving_behavior.dart';
|
||||||
|
export 'flipper_noise_behavior.dart';
|
||||||
|
@ -0,0 +1,18 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||||||
|
import 'package:pinball_audio/pinball_audio.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
class FlipperNoiseBehavior extends Component
|
||||||
|
with
|
||||||
|
FlameBlocListenable<FlipperCubit, FlipperState>,
|
||||||
|
FlameBlocReader<FlipperCubit, FlipperState> {
|
||||||
|
@override
|
||||||
|
void onNewState(FlipperState state) {
|
||||||
|
super.onNewState(state);
|
||||||
|
if (bloc.state.isMovingUp) {
|
||||||
|
readProvider<PinballAudioPlayer>().play(PinballAudio.flipper);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
// ignore_for_file: avoid_dynamic_calls, cascade_invocations
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:bloc_test/bloc_test.dart';
|
||||||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball_audio/pinball_audio.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
class _TestGame extends Forge2DGame {
|
||||||
|
Future<void> pump(
|
||||||
|
FlipperNoiseBehavior behavior, {
|
||||||
|
FlipperCubit? flipperBloc,
|
||||||
|
PinballAudioPlayer? audioPlayer,
|
||||||
|
}) async {
|
||||||
|
final flipper = Flipper.test(side: BoardSide.left);
|
||||||
|
await ensureAdd(
|
||||||
|
FlameProvider<PinballAudioPlayer>.value(
|
||||||
|
audioPlayer ?? _MockPinballAudioPlayer(),
|
||||||
|
children: [
|
||||||
|
flipper,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await flipper.ensureAdd(
|
||||||
|
FlameBlocProvider<FlipperCubit, FlipperState>.value(
|
||||||
|
value: flipperBloc ?? FlipperCubit(),
|
||||||
|
children: [behavior],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _MockPinballAudioPlayer extends Mock implements PinballAudioPlayer {}
|
||||||
|
|
||||||
|
class _MockFlipperCubit extends Mock implements FlipperCubit {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(_TestGame.new);
|
||||||
|
|
||||||
|
group('FlipperNoiseBehavior', () {
|
||||||
|
test('can be instantiated', () {
|
||||||
|
expect(
|
||||||
|
FlipperNoiseBehavior(),
|
||||||
|
isA<FlipperNoiseBehavior>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'plays the flipper sound when moving up',
|
||||||
|
(game) async {
|
||||||
|
final audioPlayer = _MockPinballAudioPlayer();
|
||||||
|
final bloc = _MockFlipperCubit();
|
||||||
|
whenListen(
|
||||||
|
bloc,
|
||||||
|
Stream.fromIterable([FlipperState.movingUp]),
|
||||||
|
initialState: FlipperState.movingUp,
|
||||||
|
);
|
||||||
|
|
||||||
|
final behavior = FlipperNoiseBehavior();
|
||||||
|
await game.pump(
|
||||||
|
behavior,
|
||||||
|
flipperBloc: bloc,
|
||||||
|
audioPlayer: audioPlayer,
|
||||||
|
);
|
||||||
|
behavior.onNewState(FlipperState.movingUp);
|
||||||
|
game.update(0);
|
||||||
|
|
||||||
|
verify(() => audioPlayer.play(PinballAudio.flipper)).called(1);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball/game/behaviors/behaviors.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
class _TestGame extends Forge2DGame {
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
images.prefix = '';
|
||||||
|
await images.load(Assets.images.dash.animatronic.keyName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _TestSpriteAnimationComponent extends SpriteAnimationComponent {}
|
||||||
|
|
||||||
|
class _MockSpriteAnimation extends Mock implements SpriteAnimation {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
final flameTester = FlameTester(_TestGame.new);
|
||||||
|
|
||||||
|
group('AnimatronicLoopingBehavior', () {
|
||||||
|
test('can be instantiated', () {
|
||||||
|
expect(
|
||||||
|
AnimatronicLoopingBehavior(animationCoolDown: 1),
|
||||||
|
isA<AnimatronicLoopingBehavior>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'can be added',
|
||||||
|
(game) async {
|
||||||
|
final behavior = AnimatronicLoopingBehavior(animationCoolDown: 1);
|
||||||
|
final animation = _MockSpriteAnimation();
|
||||||
|
final spriteAnimationComponent = _TestSpriteAnimationComponent()
|
||||||
|
..animation = animation;
|
||||||
|
await game.ensureAdd(spriteAnimationComponent);
|
||||||
|
await spriteAnimationComponent.add(behavior);
|
||||||
|
await game.ready();
|
||||||
|
|
||||||
|
expect(game.contains(spriteAnimationComponent), isTrue);
|
||||||
|
expect(spriteAnimationComponent.contains(behavior), isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'onTick starts playing the animation',
|
||||||
|
(game) async {
|
||||||
|
final behavior = AnimatronicLoopingBehavior(animationCoolDown: 1);
|
||||||
|
final spriteAnimationComponent = _TestSpriteAnimationComponent();
|
||||||
|
await game.ensureAdd(spriteAnimationComponent);
|
||||||
|
await spriteAnimationComponent.add(behavior);
|
||||||
|
|
||||||
|
spriteAnimationComponent.playing = false;
|
||||||
|
game.update(behavior.timer.limit);
|
||||||
|
|
||||||
|
expect(spriteAnimationComponent.playing, isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'animation onComplete resets and stops playing the animation',
|
||||||
|
(game) async {
|
||||||
|
final behavior = AnimatronicLoopingBehavior(animationCoolDown: 1);
|
||||||
|
final spriteAnimationComponent = DashAnimatronic();
|
||||||
|
|
||||||
|
await game.ensureAdd(spriteAnimationComponent);
|
||||||
|
await spriteAnimationComponent.add(behavior);
|
||||||
|
|
||||||
|
game.update(1);
|
||||||
|
expect(spriteAnimationComponent.playing, isTrue);
|
||||||
|
|
||||||
|
spriteAnimationComponent.animation!.onComplete!.call();
|
||||||
|
|
||||||
|
expect(spriteAnimationComponent.playing, isFalse);
|
||||||
|
expect(spriteAnimationComponent.animation!.currentIndex, equals(0));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'animation onComplete resets and starts the timer',
|
||||||
|
(game) async {
|
||||||
|
final behavior = AnimatronicLoopingBehavior(animationCoolDown: 1);
|
||||||
|
final spriteAnimationComponent = DashAnimatronic();
|
||||||
|
|
||||||
|
await game.ensureAdd(spriteAnimationComponent);
|
||||||
|
await spriteAnimationComponent.add(behavior);
|
||||||
|
|
||||||
|
game.update(0.5);
|
||||||
|
expect(behavior.timer.current, equals(0.5));
|
||||||
|
|
||||||
|
spriteAnimationComponent.animation!.onComplete!.call();
|
||||||
|
|
||||||
|
expect(behavior.timer.current, equals(0));
|
||||||
|
expect(behavior.timer.isRunning(), isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in new issue