mirror of https://github.com/flutter/pinball.git
parent
552bf040c4
commit
73e9dc5fbc
@ -1,23 +1,30 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
class PlungerReleasingBehavior extends Component
|
||||
with ParentIsA<Plunger>, FlameBlocListenable<PlungerCubit, PlungerState> {
|
||||
with FlameBlocListenable<PlungerCubit, PlungerState> {
|
||||
PlungerReleasingBehavior({
|
||||
required double strength,
|
||||
}) : _strength = strength;
|
||||
|
||||
final double _strength; // 11
|
||||
final double _strength;
|
||||
|
||||
late final Plunger _plunger;
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
_plunger = parent!.parent! as Plunger;
|
||||
}
|
||||
|
||||
@override
|
||||
void onNewState(PlungerState state) {
|
||||
super.onNewState(state);
|
||||
if (state.isReleasing) {
|
||||
final velocity =
|
||||
(parent.initialPosition.y - parent.body.position.y) * _strength;
|
||||
parent.body.linearVelocity = Vector2(0, velocity);
|
||||
final velocity = (_plunger.initialPosition.y - _plunger.body.position.y) *
|
||||
_strength.abs();
|
||||
_plunger.body.linearVelocity = Vector2(0, velocity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 42 KiB |
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 42 KiB |
@ -1,5 +1,72 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:flame_forge2d/forge2d_game.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';
|
||||
|
||||
class _TestGame extends Forge2DGame {
|
||||
Future<void> pump(
|
||||
PlungerReleasingBehavior behavior, {
|
||||
PlungerCubit? plugerBloc,
|
||||
}) async {
|
||||
final plunger = Plunger.test();
|
||||
await ensureAdd(plunger);
|
||||
return plunger.ensureAdd(
|
||||
FlameBlocProvider<PlungerCubit, PlungerState>.value(
|
||||
value: plugerBloc ?? PlungerCubit(),
|
||||
children: [behavior],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MockPlungerCubit extends Mock implements PlungerCubit {}
|
||||
|
||||
void main() {
|
||||
group('PlungerReleasingBehavior', () {});
|
||||
group('PlungerReleasingBehavior', () {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(_TestGame.new);
|
||||
|
||||
test('can be instantiated', () {
|
||||
expect(
|
||||
PlungerReleasingBehavior(strength: 0),
|
||||
isA<PlungerReleasingBehavior>(),
|
||||
);
|
||||
});
|
||||
|
||||
flameTester.test('can be loaded', (game) async {
|
||||
final behavior = PlungerReleasingBehavior(strength: 0);
|
||||
await game.pump(behavior);
|
||||
expect(game.descendants(), contains(behavior));
|
||||
});
|
||||
|
||||
flameTester.test('applies vertical linear velocity', (game) async {
|
||||
final plungerBloc = _MockPlungerCubit();
|
||||
final streamController = StreamController<PlungerState>();
|
||||
whenListen<PlungerState>(
|
||||
plungerBloc,
|
||||
streamController.stream,
|
||||
initialState: PlungerState.pulling,
|
||||
);
|
||||
|
||||
final behavior = PlungerReleasingBehavior(strength: 2);
|
||||
await game.pump(
|
||||
behavior,
|
||||
plugerBloc: plungerBloc,
|
||||
);
|
||||
|
||||
streamController.add(PlungerState.releasing);
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
|
||||
final plunger = behavior.ancestors().whereType<Plunger>().single;
|
||||
expect(plunger.body.linearVelocity.x, equals(0));
|
||||
expect(plunger.body.linearVelocity.y, isNot(greaterThan(0)));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in new issue