mirror of https://github.com/flutter/pinball.git
refactor: removed `ControlledFlipper` (#343)
* feat: implemented Flippers behaviors * test: tested Flipper behaviors * refactor: fixed typo * refactor: removed unecessary import * feat: removed and added behavior depending on game status * test: renamed test * test: changed golden path * refactor: removed TODOspull/388/head
parent
eb8cc4bb6f
commit
0b22b712b0
@ -0,0 +1,2 @@
|
||||
export 'flipper_jointing_behavior.dart';
|
||||
export 'flipper_key_controlling_behavior.dart';
|
@ -0,0 +1,103 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
/// Joints the [Flipper] to allow pivoting around one end.
|
||||
class FlipperJointingBehavior extends Component
|
||||
with ParentIsA<Flipper>, HasGameRef {
|
||||
late final RevoluteJoint _joint;
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
|
||||
final anchor = _FlipperAnchor(flipper: parent);
|
||||
await add(anchor);
|
||||
|
||||
final jointDef = _FlipperAnchorRevoluteJointDef(
|
||||
flipper: parent,
|
||||
anchor: anchor,
|
||||
);
|
||||
_joint = _FlipperJoint(jointDef);
|
||||
parent.world.createJoint(_joint);
|
||||
}
|
||||
|
||||
@override
|
||||
void onMount() {
|
||||
gameRef.ready().whenComplete(
|
||||
() => parent.body.joints.whereType<_FlipperJoint>().first.unlock(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template flipper_anchor}
|
||||
/// [JointAnchor] positioned at the end of a [Flipper].
|
||||
///
|
||||
/// The end of a [Flipper] depends on its [Flipper.side].
|
||||
/// {@endtemplate}
|
||||
class _FlipperAnchor extends JointAnchor {
|
||||
/// {@macro flipper_anchor}
|
||||
_FlipperAnchor({
|
||||
required Flipper flipper,
|
||||
}) {
|
||||
initialPosition = Vector2(
|
||||
(Flipper.size.x * flipper.side.direction) / 2 -
|
||||
(1.65 * flipper.side.direction),
|
||||
-0.15,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template flipper_anchor_revolute_joint_def}
|
||||
/// Hinges one end of [Flipper] to a [_FlipperAnchor] to achieve a potivoting
|
||||
/// motion.
|
||||
/// {@endtemplate}
|
||||
class _FlipperAnchorRevoluteJointDef extends RevoluteJointDef {
|
||||
/// {@macro flipper_anchor_revolute_joint_def}
|
||||
_FlipperAnchorRevoluteJointDef({
|
||||
required Flipper flipper,
|
||||
required _FlipperAnchor anchor,
|
||||
}) : side = flipper.side {
|
||||
enableLimit = true;
|
||||
initialize(
|
||||
flipper.body,
|
||||
anchor.body,
|
||||
flipper.body.position + anchor.body.position,
|
||||
);
|
||||
}
|
||||
|
||||
final BoardSide side;
|
||||
}
|
||||
|
||||
/// {@template flipper_joint}
|
||||
/// [RevoluteJoint] that controls the pivoting motion of a [Flipper].
|
||||
/// {@endtemplate}
|
||||
class _FlipperJoint extends RevoluteJoint {
|
||||
/// {@macro flipper_joint}
|
||||
_FlipperJoint(_FlipperAnchorRevoluteJointDef def)
|
||||
: side = def.side,
|
||||
super(def) {
|
||||
lock();
|
||||
}
|
||||
|
||||
/// Half the angle of the arc motion.
|
||||
static const _halfSweepingAngle = 0.611;
|
||||
|
||||
final BoardSide side;
|
||||
|
||||
/// Locks the [Flipper] to its resting position.
|
||||
///
|
||||
/// The joint is locked when initialized in order to force the [Flipper]
|
||||
/// at its resting position.
|
||||
void lock() {
|
||||
final angle = _halfSweepingAngle * side.direction;
|
||||
setLimits(angle, angle);
|
||||
}
|
||||
|
||||
/// Unlocks the [Flipper] from its resting position.
|
||||
void unlock() {
|
||||
const angle = _halfSweepingAngle;
|
||||
setLimits(-angle, angle);
|
||||
}
|
||||
}
|
@ -1,49 +1,33 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
/// {@template controlled_flipper}
|
||||
/// A [Flipper] with a [FlipperController] attached.
|
||||
/// {@endtemplate}
|
||||
class ControlledFlipper extends Flipper with Controls<FlipperController> {
|
||||
/// {@macro controlled_flipper}
|
||||
ControlledFlipper({
|
||||
required BoardSide side,
|
||||
}) : super(side: side) {
|
||||
controller = FlipperController(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template flipper_controller}
|
||||
/// A [ComponentController] that controls a [Flipper]s movement.
|
||||
/// {@endtemplate}
|
||||
class FlipperController extends ComponentController<Flipper>
|
||||
with KeyboardHandler, FlameBlocReader<GameBloc, GameState> {
|
||||
/// {@macro flipper_controller}
|
||||
FlipperController(Flipper flipper)
|
||||
: _keys = flipper.side.flipperKeys,
|
||||
super(flipper);
|
||||
|
||||
/// Allows controlling the [Flipper]'s movement with keyboard input.
|
||||
class FlipperKeyControllingBehavior extends Component
|
||||
with KeyboardHandler, ParentIsA<Flipper> {
|
||||
/// The [LogicalKeyboardKey]s that will control the [Flipper].
|
||||
///
|
||||
/// [onKeyEvent] method listens to when one of these keys is pressed.
|
||||
final List<LogicalKeyboardKey> _keys;
|
||||
late final List<LogicalKeyboardKey> _keys;
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
_keys = parent.side.flipperKeys;
|
||||
}
|
||||
|
||||
@override
|
||||
bool onKeyEvent(
|
||||
RawKeyEvent event,
|
||||
Set<LogicalKeyboardKey> keysPressed,
|
||||
) {
|
||||
if (!bloc.state.status.isPlaying) return true;
|
||||
if (!_keys.contains(event.logicalKey)) return true;
|
||||
|
||||
if (event is RawKeyDownEvent) {
|
||||
component.moveUp();
|
||||
parent.moveUp();
|
||||
} else if (event is RawKeyUpEvent) {
|
||||
component.moveDown();
|
||||
parent.moveDown();
|
||||
}
|
||||
|
||||
return false;
|
@ -0,0 +1,38 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_components/src/components/components.dart';
|
||||
|
||||
import '../../../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
group('FlipperJointingBehavior', () {
|
||||
final flameTester = FlameTester(TestGame.new);
|
||||
|
||||
test('can be instantiated', () {
|
||||
expect(
|
||||
FlipperJointingBehavior(),
|
||||
isA<FlipperJointingBehavior>(),
|
||||
);
|
||||
});
|
||||
|
||||
flameTester.test('can be loaded', (game) async {
|
||||
final behavior = FlipperJointingBehavior();
|
||||
final parent = Flipper.test(side: BoardSide.left);
|
||||
await game.ensureAdd(parent);
|
||||
await parent.ensureAdd(behavior);
|
||||
expect(parent.contains(behavior), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('creates a joint', (game) async {
|
||||
final behavior = FlipperJointingBehavior();
|
||||
final parent = Flipper.test(side: BoardSide.left);
|
||||
await game.ensureAdd(parent);
|
||||
await parent.ensureAdd(behavior);
|
||||
|
||||
expect(parent.body.joints, isNotEmpty);
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,357 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../../../helpers/helpers.dart';
|
||||
|
||||
class _MockRawKeyDownEvent extends Mock implements RawKeyDownEvent {
|
||||
@override
|
||||
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class _MockRawKeyUpEvent extends Mock implements RawKeyUpEvent {
|
||||
@override
|
||||
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
group('FlipperKeyControllingBehavior', () {
|
||||
final flameTester = FlameTester(TestGame.new);
|
||||
|
||||
group(
|
||||
'onKeyEvent',
|
||||
() {
|
||||
late Flipper rightFlipper;
|
||||
late Flipper leftFlipper;
|
||||
|
||||
setUp(() {
|
||||
rightFlipper = Flipper.test(side: BoardSide.right);
|
||||
leftFlipper = Flipper.test(side: BoardSide.left);
|
||||
});
|
||||
|
||||
group('on right Flipper', () {
|
||||
flameTester.test(
|
||||
'moves upwards when right arrow is pressed',
|
||||
(game) async {
|
||||
await game.ensureAdd(rightFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await rightFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyDownEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.arrowRight,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(rightFlipper.body.linearVelocity.y, isNegative);
|
||||
expect(rightFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'moves downwards when right arrow is released',
|
||||
(game) async {
|
||||
await game.ensureAdd(rightFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await rightFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyUpEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.arrowRight,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(rightFlipper.body.linearVelocity.y, isPositive);
|
||||
expect(rightFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'moves upwards when D is pressed',
|
||||
(game) async {
|
||||
await game.ensureAdd(rightFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await rightFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyDownEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.keyD,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(rightFlipper.body.linearVelocity.y, isNegative);
|
||||
expect(rightFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'moves downwards when D is released',
|
||||
(game) async {
|
||||
await game.ensureAdd(rightFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await rightFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyUpEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.keyD,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(rightFlipper.body.linearVelocity.y, isPositive);
|
||||
expect(rightFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
group("doesn't move when", () {
|
||||
flameTester.test(
|
||||
'left arrow is pressed',
|
||||
(game) async {
|
||||
await game.ensureAdd(rightFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await rightFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyDownEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.arrowLeft,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(rightFlipper.body.linearVelocity.y, isZero);
|
||||
expect(rightFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'left arrow is released',
|
||||
(game) async {
|
||||
await game.ensureAdd(rightFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await rightFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyUpEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.arrowLeft,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(rightFlipper.body.linearVelocity.y, isZero);
|
||||
expect(rightFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'A is pressed',
|
||||
(game) async {
|
||||
await game.ensureAdd(rightFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await rightFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyDownEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.keyA,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(rightFlipper.body.linearVelocity.y, isZero);
|
||||
expect(rightFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'A is released',
|
||||
(game) async {
|
||||
await game.ensureAdd(rightFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await rightFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyUpEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.keyA,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(rightFlipper.body.linearVelocity.y, isZero);
|
||||
expect(rightFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('on left Flipper', () {
|
||||
flameTester.test(
|
||||
'moves upwards when left arrow is pressed',
|
||||
(game) async {
|
||||
await game.ensureAdd(leftFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await leftFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyDownEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.arrowLeft,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(leftFlipper.body.linearVelocity.y, isNegative);
|
||||
expect(leftFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'moves downwards when left arrow is released',
|
||||
(game) async {
|
||||
await game.ensureAdd(leftFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await leftFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyUpEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.arrowLeft,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(leftFlipper.body.linearVelocity.y, isPositive);
|
||||
expect(leftFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'moves upwards when A is pressed',
|
||||
(game) async {
|
||||
await game.ensureAdd(leftFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await leftFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyDownEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.keyA,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(leftFlipper.body.linearVelocity.y, isNegative);
|
||||
expect(leftFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'moves downwards when A is released',
|
||||
(game) async {
|
||||
await game.ensureAdd(leftFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await leftFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyUpEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.keyA,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(leftFlipper.body.linearVelocity.y, isPositive);
|
||||
expect(leftFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
group("doesn't move when", () {
|
||||
flameTester.test(
|
||||
'right arrow is pressed',
|
||||
(game) async {
|
||||
await game.ensureAdd(leftFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await leftFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyDownEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.arrowRight,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(leftFlipper.body.linearVelocity.y, isZero);
|
||||
expect(leftFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'right arrow is released',
|
||||
(game) async {
|
||||
await game.ensureAdd(leftFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await leftFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyUpEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.arrowRight,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(leftFlipper.body.linearVelocity.y, isZero);
|
||||
expect(leftFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'D is pressed',
|
||||
(game) async {
|
||||
await game.ensureAdd(leftFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await leftFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyDownEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.keyD,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(leftFlipper.body.linearVelocity.y, isZero);
|
||||
expect(leftFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'D is released',
|
||||
(game) async {
|
||||
await game.ensureAdd(leftFlipper);
|
||||
final behavior = FlipperKeyControllingBehavior();
|
||||
await leftFlipper.ensureAdd(behavior);
|
||||
|
||||
final event = _MockRawKeyUpEvent();
|
||||
when(() => event.logicalKey).thenReturn(
|
||||
LogicalKeyboardKey.keyD,
|
||||
);
|
||||
|
||||
behavior.onKeyEvent(event, {});
|
||||
|
||||
expect(leftFlipper.body.linearVelocity.y, isZero);
|
||||
expect(leftFlipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
@ -1,260 +0,0 @@
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flame/input.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/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
class _TestGame extends Forge2DGame with HasKeyboardHandlerComponents {
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
images.prefix = '';
|
||||
await images.loadAll([
|
||||
Assets.images.flipper.left.keyName,
|
||||
Assets.images.flipper.right.keyName,
|
||||
]);
|
||||
}
|
||||
|
||||
Future<void> pump(Flipper flipper, {required GameBloc gameBloc}) {
|
||||
return ensureAdd(
|
||||
FlameBlocProvider<GameBloc, GameState>.value(
|
||||
value: gameBloc,
|
||||
children: [flipper],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MockGameBloc extends Mock implements GameBloc {}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(_TestGame.new);
|
||||
|
||||
group('FlipperController', () {
|
||||
late GameBloc gameBloc;
|
||||
|
||||
setUp(() {
|
||||
gameBloc = _MockGameBloc();
|
||||
});
|
||||
|
||||
group('onKeyEvent', () {
|
||||
final leftKeys = UnmodifiableListView([
|
||||
LogicalKeyboardKey.arrowLeft,
|
||||
LogicalKeyboardKey.keyA,
|
||||
]);
|
||||
final rightKeys = UnmodifiableListView([
|
||||
LogicalKeyboardKey.arrowRight,
|
||||
LogicalKeyboardKey.keyD,
|
||||
]);
|
||||
|
||||
group('and Flipper is left', () {
|
||||
late Flipper flipper;
|
||||
late FlipperController controller;
|
||||
|
||||
setUp(() {
|
||||
flipper = Flipper(side: BoardSide.left);
|
||||
controller = FlipperController(flipper);
|
||||
flipper.add(controller);
|
||||
});
|
||||
|
||||
testRawKeyDownEvents(leftKeys, (event) {
|
||||
flameTester.test(
|
||||
'moves upwards '
|
||||
'when ${event.logicalKey.keyLabel} is pressed',
|
||||
(game) async {
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial().copyWith(
|
||||
status: GameStatus.playing,
|
||||
),
|
||||
);
|
||||
|
||||
await game.ready();
|
||||
await game.pump(flipper, gameBloc: gameBloc);
|
||||
controller.onKeyEvent(event, {});
|
||||
|
||||
expect(flipper.body.linearVelocity.y, isNegative);
|
||||
expect(flipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
testRawKeyDownEvents(leftKeys, (event) {
|
||||
flameTester.test(
|
||||
'does nothing when is game over',
|
||||
(game) async {
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial().copyWith(
|
||||
status: GameStatus.gameOver,
|
||||
),
|
||||
);
|
||||
|
||||
await game.pump(flipper, gameBloc: gameBloc);
|
||||
controller.onKeyEvent(event, {});
|
||||
|
||||
expect(flipper.body.linearVelocity.y, isZero);
|
||||
expect(flipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
testRawKeyUpEvents(leftKeys, (event) {
|
||||
flameTester.test(
|
||||
'moves downwards '
|
||||
'when ${event.logicalKey.keyLabel} is released',
|
||||
(game) async {
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial().copyWith(
|
||||
status: GameStatus.playing,
|
||||
),
|
||||
);
|
||||
|
||||
await game.ready();
|
||||
await game.pump(flipper, gameBloc: gameBloc);
|
||||
controller.onKeyEvent(event, {});
|
||||
|
||||
expect(flipper.body.linearVelocity.y, isPositive);
|
||||
expect(flipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
testRawKeyUpEvents(rightKeys, (event) {
|
||||
flameTester.test(
|
||||
'does nothing '
|
||||
'when ${event.logicalKey.keyLabel} is released',
|
||||
(game) async {
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial(),
|
||||
);
|
||||
|
||||
await game.ready();
|
||||
await game.pump(flipper, gameBloc: gameBloc);
|
||||
controller.onKeyEvent(event, {});
|
||||
|
||||
expect(flipper.body.linearVelocity.y, isZero);
|
||||
expect(flipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('and Flipper is right', () {
|
||||
late Flipper flipper;
|
||||
late FlipperController controller;
|
||||
|
||||
setUp(() {
|
||||
flipper = Flipper(side: BoardSide.right);
|
||||
controller = FlipperController(flipper);
|
||||
flipper.add(controller);
|
||||
});
|
||||
|
||||
testRawKeyDownEvents(rightKeys, (event) {
|
||||
flameTester.test(
|
||||
'moves upwards '
|
||||
'when ${event.logicalKey.keyLabel} is pressed',
|
||||
(game) async {
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial().copyWith(
|
||||
status: GameStatus.playing,
|
||||
),
|
||||
);
|
||||
|
||||
await game.ready();
|
||||
await game.pump(flipper, gameBloc: gameBloc);
|
||||
controller.onKeyEvent(event, {});
|
||||
|
||||
expect(flipper.body.linearVelocity.y, isNegative);
|
||||
expect(flipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
testRawKeyUpEvents(rightKeys, (event) {
|
||||
flameTester.test(
|
||||
'moves downwards '
|
||||
'when ${event.logicalKey.keyLabel} is released',
|
||||
(game) async {
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial().copyWith(
|
||||
status: GameStatus.playing,
|
||||
),
|
||||
);
|
||||
|
||||
await game.ready();
|
||||
await game.pump(flipper, gameBloc: gameBloc);
|
||||
controller.onKeyEvent(event, {});
|
||||
|
||||
expect(flipper.body.linearVelocity.y, isPositive);
|
||||
expect(flipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
testRawKeyDownEvents(rightKeys, (event) {
|
||||
flameTester.test(
|
||||
'does nothing when is game over',
|
||||
(game) async {
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial().copyWith(
|
||||
status: GameStatus.gameOver,
|
||||
),
|
||||
);
|
||||
|
||||
await game.pump(flipper, gameBloc: gameBloc);
|
||||
controller.onKeyEvent(event, {});
|
||||
|
||||
expect(flipper.body.linearVelocity.y, isZero);
|
||||
expect(flipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
testRawKeyUpEvents(leftKeys, (event) {
|
||||
flameTester.test(
|
||||
'does nothing '
|
||||
'when ${event.logicalKey.keyLabel} is released',
|
||||
(game) async {
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial().copyWith(
|
||||
status: GameStatus.playing,
|
||||
),
|
||||
);
|
||||
|
||||
await game.ready();
|
||||
await game.pump(flipper, gameBloc: gameBloc);
|
||||
controller.onKeyEvent(event, {});
|
||||
|
||||
expect(flipper.body.linearVelocity.y, isZero);
|
||||
expect(flipper.body.linearVelocity.x, isZero);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue