@ -0,0 +1,30 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:pinball/select_character/select_character.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
/// {@template bonus_ball_spawning_behavior}
|
||||||
|
/// After a duration, spawns a bonus ball from the [DinoWalls] and boosts it
|
||||||
|
/// into the middle of the board.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class BonusBallSpawningBehavior extends TimerComponent with HasGameRef {
|
||||||
|
/// {@macro bonus_ball_spawning_behavior}
|
||||||
|
BonusBallSpawningBehavior()
|
||||||
|
: super(
|
||||||
|
period: 5,
|
||||||
|
removeOnFinish: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onTick() {
|
||||||
|
final characterTheme = readBloc<CharacterThemeCubit, CharacterThemeState>()
|
||||||
|
.state
|
||||||
|
.characterTheme;
|
||||||
|
gameRef.descendants().whereType<ZCanvasComponent>().single.add(
|
||||||
|
Ball(assetPath: characterTheme.ball.keyName)
|
||||||
|
..add(BallImpulsingBehavior(impulse: Vector2(-40, 0)))
|
||||||
|
..initialPosition = Vector2(29.2, -24.5)
|
||||||
|
..zIndex = ZIndexes.ballOnBoard,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball/l10n/l10n.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
|
||||||
|
/// {@template mobile_controls}
|
||||||
|
/// Widget with the controls used to enable the user initials input on mobile.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class MobileControls extends StatelessWidget {
|
||||||
|
/// {@macro mobile_controls}
|
||||||
|
const MobileControls({
|
||||||
|
Key? key,
|
||||||
|
required this.game,
|
||||||
|
}) : super(key: key);
|
||||||
|
|
||||||
|
/// Game instance
|
||||||
|
final PinballGame game;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final l10n = AppLocalizations.of(context);
|
||||||
|
return Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||||
|
children: [
|
||||||
|
MobileDpad(
|
||||||
|
onTapUp: () => game.triggerVirtualKeyUp(LogicalKeyboardKey.arrowUp),
|
||||||
|
onTapDown: () => game.triggerVirtualKeyUp(
|
||||||
|
LogicalKeyboardKey.arrowDown,
|
||||||
|
),
|
||||||
|
onTapLeft: () => game.triggerVirtualKeyUp(
|
||||||
|
LogicalKeyboardKey.arrowLeft,
|
||||||
|
),
|
||||||
|
onTapRight: () => game.triggerVirtualKeyUp(
|
||||||
|
LogicalKeyboardKey.arrowRight,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
PinballButton(
|
||||||
|
text: l10n.enter,
|
||||||
|
onTap: () => game.triggerVirtualKeyUp(LogicalKeyboardKey.enter),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
|
||||||
|
/// {@template mobile_dpad}
|
||||||
|
/// Widget rendering 4 directional input arrows.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class MobileDpad extends StatelessWidget {
|
||||||
|
/// {@template mobile_dpad}
|
||||||
|
const MobileDpad({
|
||||||
|
Key? key,
|
||||||
|
required this.onTapUp,
|
||||||
|
required this.onTapDown,
|
||||||
|
required this.onTapLeft,
|
||||||
|
required this.onTapRight,
|
||||||
|
}) : super(key: key);
|
||||||
|
|
||||||
|
static const _size = 180.0;
|
||||||
|
|
||||||
|
/// Called when dpad up is pressed
|
||||||
|
final VoidCallback onTapUp;
|
||||||
|
|
||||||
|
/// Called when dpad down is pressed
|
||||||
|
final VoidCallback onTapDown;
|
||||||
|
|
||||||
|
/// Called when dpad left is pressed
|
||||||
|
final VoidCallback onTapLeft;
|
||||||
|
|
||||||
|
/// Called when dpad right is pressed
|
||||||
|
final VoidCallback onTapRight;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return SizedBox(
|
||||||
|
width: _size,
|
||||||
|
height: _size,
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
const Spacer(),
|
||||||
|
PinballDpadButton(
|
||||||
|
direction: PinballDpadDirection.up,
|
||||||
|
onTap: onTapUp,
|
||||||
|
),
|
||||||
|
const Spacer(),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
PinballDpadButton(
|
||||||
|
direction: PinballDpadDirection.left,
|
||||||
|
onTap: onTapLeft,
|
||||||
|
),
|
||||||
|
const Spacer(),
|
||||||
|
PinballDpadButton(
|
||||||
|
direction: PinballDpadDirection.right,
|
||||||
|
onTap: onTapRight,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
const Spacer(),
|
||||||
|
PinballDpadButton(
|
||||||
|
direction: PinballDpadDirection.down,
|
||||||
|
onTap: onTapDown,
|
||||||
|
),
|
||||||
|
const Spacer(),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
export 'bonus_animation.dart';
|
export 'bonus_animation.dart';
|
||||||
export 'game_hud.dart';
|
export 'game_hud.dart';
|
||||||
|
export 'mobile_controls.dart';
|
||||||
|
export 'mobile_dpad.dart';
|
||||||
export 'play_button_overlay.dart';
|
export 'play_button_overlay.dart';
|
||||||
export 'round_count_display.dart';
|
export 'round_count_display.dart';
|
||||||
export 'score_view.dart';
|
export 'score_view.dart';
|
||||||
|
Before Width: | Height: | Size: 270 KiB After Width: | Height: | Size: 266 KiB |
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 29 KiB |
@ -0,0 +1,22 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
/// {@template ball_impulsing_behavior}
|
||||||
|
/// Impulses the [Ball] in a given direction.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class BallImpulsingBehavior extends Component with ParentIsA<Ball> {
|
||||||
|
/// {@macro ball_impulsing_behavior}
|
||||||
|
BallImpulsingBehavior({
|
||||||
|
required Vector2 impulse,
|
||||||
|
}) : _impulse = impulse;
|
||||||
|
|
||||||
|
final Vector2 _impulse;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
parent.body.linearVelocity = _impulse;
|
||||||
|
shouldRemove = true;
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
export 'ball_gravitating_behavior.dart';
|
export 'ball_gravitating_behavior.dart';
|
||||||
|
export 'ball_impulsing_behavior.dart';
|
||||||
export 'ball_scaling_behavior.dart';
|
export 'ball_scaling_behavior.dart';
|
||||||
export 'ball_turbo_charging_behavior.dart';
|
export 'ball_turbo_charging_behavior.dart';
|
||||||
|
@ -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/components.dart';
|
||||||
import 'package:flame_bloc/flame_bloc.dart';
|
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:pinball/game/game.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 controlled_flipper}
|
/// Allows controlling the [Flipper]'s movement with keyboard input.
|
||||||
/// A [Flipper] with a [FlipperController] attached.
|
class FlipperKeyControllingBehavior extends Component
|
||||||
/// {@endtemplate}
|
with KeyboardHandler, ParentIsA<Flipper> {
|
||||||
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);
|
|
||||||
|
|
||||||
/// The [LogicalKeyboardKey]s that will control the [Flipper].
|
/// The [LogicalKeyboardKey]s that will control the [Flipper].
|
||||||
///
|
///
|
||||||
/// [onKeyEvent] method listens to when one of these keys is pressed.
|
/// [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
|
@override
|
||||||
bool onKeyEvent(
|
bool onKeyEvent(
|
||||||
RawKeyEvent event,
|
RawKeyEvent event,
|
||||||
Set<LogicalKeyboardKey> keysPressed,
|
Set<LogicalKeyboardKey> keysPressed,
|
||||||
) {
|
) {
|
||||||
if (!bloc.state.status.isPlaying) return true;
|
|
||||||
if (!_keys.contains(event.logicalKey)) return true;
|
if (!_keys.contains(event.logicalKey)) return true;
|
||||||
|
|
||||||
if (event is RawKeyDownEvent) {
|
if (event is RawKeyDownEvent) {
|
||||||
component.moveUp();
|
parent.moveUp();
|
||||||
} else if (event is RawKeyUpEvent) {
|
} else if (event is RawKeyUpEvent) {
|
||||||
component.moveDown();
|
parent.moveDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
@ -0,0 +1,53 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_theme/pinball_theme.dart' as theme;
|
||||||
|
|
||||||
|
import '../../../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
group(
|
||||||
|
'BallImpulsingBehavior',
|
||||||
|
() {
|
||||||
|
final asset = theme.Assets.images.dash.ball.keyName;
|
||||||
|
final flameTester = FlameTester(() => TestGame([asset]));
|
||||||
|
|
||||||
|
test('can be instantiated', () {
|
||||||
|
expect(
|
||||||
|
BallImpulsingBehavior(impulse: Vector2.zero()),
|
||||||
|
isA<BallImpulsingBehavior>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'impulses the ball with the given velocity when loaded '
|
||||||
|
'and then removes itself',
|
||||||
|
(game) async {
|
||||||
|
final ball = Ball.test();
|
||||||
|
await game.ensureAdd(ball);
|
||||||
|
final impulse = Vector2.all(1);
|
||||||
|
final behavior = BallImpulsingBehavior(impulse: impulse);
|
||||||
|
await ball.ensureAdd(behavior);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
ball.body.linearVelocity.x,
|
||||||
|
equals(impulse.x),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
ball.body.linearVelocity.y,
|
||||||
|
equals(impulse.y),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
game.descendants().whereType<BallImpulsingBehavior>().isEmpty,
|
||||||
|
isTrue,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
@ -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);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
Before Width: | Height: | Size: 1.2 MiB After Width: | Height: | Size: 1.2 MiB |
Before Width: | Height: | Size: 148 KiB After Width: | Height: | Size: 148 KiB |
After Width: | Height: | Size: 1.9 KiB |
After Width: | Height: | Size: 1.9 KiB |
After Width: | Height: | Size: 1.9 KiB |
After Width: | Height: | Size: 1.8 KiB |
@ -0,0 +1,66 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball_ui/gen/gen.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
|
||||||
|
/// Enum with all possibile directions of a [PinballDpadButton].
|
||||||
|
enum PinballDpadDirection {
|
||||||
|
/// Up
|
||||||
|
up,
|
||||||
|
|
||||||
|
/// Down
|
||||||
|
down,
|
||||||
|
|
||||||
|
/// Left
|
||||||
|
left,
|
||||||
|
|
||||||
|
/// Right
|
||||||
|
right,
|
||||||
|
}
|
||||||
|
|
||||||
|
extension _PinballDpadDirectionX on PinballDpadDirection {
|
||||||
|
String toAsset() {
|
||||||
|
switch (this) {
|
||||||
|
case PinballDpadDirection.up:
|
||||||
|
return Assets.images.button.dpadUp.keyName;
|
||||||
|
case PinballDpadDirection.down:
|
||||||
|
return Assets.images.button.dpadDown.keyName;
|
||||||
|
case PinballDpadDirection.left:
|
||||||
|
return Assets.images.button.dpadLeft.keyName;
|
||||||
|
case PinballDpadDirection.right:
|
||||||
|
return Assets.images.button.dpadRight.keyName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template pinball_dpad_button}
|
||||||
|
/// Widget that renders a Dpad button with a given direction.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class PinballDpadButton extends StatelessWidget {
|
||||||
|
/// {@macro pinball_dpad_button}
|
||||||
|
const PinballDpadButton({
|
||||||
|
Key? key,
|
||||||
|
required this.direction,
|
||||||
|
required this.onTap,
|
||||||
|
}) : super(key: key);
|
||||||
|
|
||||||
|
/// Which [PinballDpadDirection] this button is.
|
||||||
|
final PinballDpadDirection direction;
|
||||||
|
|
||||||
|
/// The function executed when the button is pressed.
|
||||||
|
final VoidCallback onTap;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Material(
|
||||||
|
color: PinballColors.transparent,
|
||||||
|
child: InkWell(
|
||||||
|
onTap: onTap,
|
||||||
|
child: Image.asset(
|
||||||
|
direction.toAsset(),
|
||||||
|
width: 60,
|
||||||
|
height: 60,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
export 'animated_ellipsis_text.dart';
|
export 'animated_ellipsis_text.dart';
|
||||||
export 'crt_background.dart';
|
export 'crt_background.dart';
|
||||||
export 'pinball_button.dart';
|
export 'pinball_button.dart';
|
||||||
|
export 'pinball_dpad_button.dart';
|
||||||
export 'pinball_loading_indicator.dart';
|
export 'pinball_loading_indicator.dart';
|
||||||
|
@ -0,0 +1,122 @@
|
|||||||
|
// ignore_for_file: one_member_abstracts
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball_ui/gen/gen.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
|
||||||
|
extension _WidgetTesterX on WidgetTester {
|
||||||
|
Future<void> pumpButton({
|
||||||
|
required PinballDpadDirection direction,
|
||||||
|
required VoidCallback onTap,
|
||||||
|
}) async {
|
||||||
|
await pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
home: Scaffold(
|
||||||
|
body: PinballDpadButton(
|
||||||
|
direction: direction,
|
||||||
|
onTap: onTap,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension _CommonFindersX on CommonFinders {
|
||||||
|
Finder byImagePath(String path) {
|
||||||
|
return find.byWidgetPredicate(
|
||||||
|
(widget) {
|
||||||
|
if (widget is Image) {
|
||||||
|
final image = widget.image;
|
||||||
|
|
||||||
|
if (image is AssetImage) {
|
||||||
|
return image.keyName == path;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class _VoidCallbackStubBase {
|
||||||
|
void onCall();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _VoidCallbackStub extends Mock implements _VoidCallbackStubBase {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('PinballDpadButton', () {
|
||||||
|
testWidgets('can be tapped', (tester) async {
|
||||||
|
final stub = _VoidCallbackStub();
|
||||||
|
await tester.pumpButton(
|
||||||
|
direction: PinballDpadDirection.up,
|
||||||
|
onTap: stub.onCall,
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.byType(Image));
|
||||||
|
|
||||||
|
verify(stub.onCall).called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('up', () {
|
||||||
|
testWidgets('renders the correct image', (tester) async {
|
||||||
|
await tester.pumpButton(
|
||||||
|
direction: PinballDpadDirection.up,
|
||||||
|
onTap: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
find.byImagePath(Assets.images.button.dpadUp.keyName),
|
||||||
|
findsOneWidget,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('down', () {
|
||||||
|
testWidgets('renders the correct image', (tester) async {
|
||||||
|
await tester.pumpButton(
|
||||||
|
direction: PinballDpadDirection.down,
|
||||||
|
onTap: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
find.byImagePath(Assets.images.button.dpadDown.keyName),
|
||||||
|
findsOneWidget,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('left', () {
|
||||||
|
testWidgets('renders the correct image', (tester) async {
|
||||||
|
await tester.pumpButton(
|
||||||
|
direction: PinballDpadDirection.left,
|
||||||
|
onTap: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
find.byImagePath(Assets.images.button.dpadLeft.keyName),
|
||||||
|
findsOneWidget,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('right', () {
|
||||||
|
testWidgets('renders the correct image', (tester) async {
|
||||||
|
await tester.pumpButton(
|
||||||
|
direction: PinballDpadDirection.right,
|
||||||
|
onTap: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
find.byImagePath(Assets.images.button.dpadRight.keyName),
|
||||||
|
findsOneWidget,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
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:pinball/game/behaviors/behaviors.dart';
|
||||||
|
import 'package:pinball/select_character/select_character.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
import 'package:pinball_theme/pinball_theme.dart' as theme;
|
||||||
|
|
||||||
|
class _TestGame extends Forge2DGame {
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
images.prefix = '';
|
||||||
|
await images.loadAll([
|
||||||
|
theme.Assets.images.dash.ball.keyName,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> pump(BonusBallSpawningBehavior child) async {
|
||||||
|
await ensureAdd(
|
||||||
|
FlameBlocProvider<CharacterThemeCubit, CharacterThemeState>.value(
|
||||||
|
value: CharacterThemeCubit(),
|
||||||
|
children: [
|
||||||
|
ZCanvasComponent(
|
||||||
|
children: [child],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
group('FlutterForestBonusBehavior', () {
|
||||||
|
final flameTester = FlameTester(_TestGame.new);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'adds a ball with a BallImpulsingBehavior to the game onTick '
|
||||||
|
'resulting in a -40 x impulse',
|
||||||
|
(game) async {
|
||||||
|
await game.onLoad();
|
||||||
|
final behavior = BonusBallSpawningBehavior();
|
||||||
|
|
||||||
|
await game.pump(behavior);
|
||||||
|
|
||||||
|
game.update(behavior.timer.limit);
|
||||||
|
await game.ready();
|
||||||
|
|
||||||
|
final ball = game.descendants().whereType<Ball>().single;
|
||||||
|
|
||||||
|
expect(ball.body.linearVelocity.x, equals(-40));
|
||||||
|
expect(ball.body.linearVelocity.y, equals(0));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
@ -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);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
@ -0,0 +1,131 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball/l10n/l10n.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
|
||||||
|
class _MockPinballGame extends Mock implements PinballGame {}
|
||||||
|
|
||||||
|
extension _WidgetTesterX on WidgetTester {
|
||||||
|
Future<void> pumpMobileControls(PinballGame game) async {
|
||||||
|
await pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
localizationsDelegates: const [
|
||||||
|
AppLocalizations.delegate,
|
||||||
|
GlobalMaterialLocalizations.delegate,
|
||||||
|
],
|
||||||
|
home: Scaffold(
|
||||||
|
body: MobileControls(game: game),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension _CommonFindersX on CommonFinders {
|
||||||
|
Finder byPinballDpadDirection(PinballDpadDirection direction) {
|
||||||
|
return byWidgetPredicate((widget) {
|
||||||
|
return widget is PinballDpadButton && widget.direction == direction;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('MobileControls', () {
|
||||||
|
testWidgets('renders', (tester) async {
|
||||||
|
await tester.pumpMobileControls(_MockPinballGame());
|
||||||
|
|
||||||
|
expect(find.byType(PinballButton), findsOneWidget);
|
||||||
|
expect(find.byType(MobileDpad), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('correctly triggers the arrow up', (tester) async {
|
||||||
|
var pressed = false;
|
||||||
|
final component = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowUp: () => pressed = true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final game = _MockPinballGame();
|
||||||
|
when(game.descendants).thenReturn([component]);
|
||||||
|
|
||||||
|
await tester.pumpMobileControls(game);
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.up));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(pressed, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('correctly triggers the arrow down', (tester) async {
|
||||||
|
var pressed = false;
|
||||||
|
final component = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowDown: () => pressed = true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final game = _MockPinballGame();
|
||||||
|
when(game.descendants).thenReturn([component]);
|
||||||
|
|
||||||
|
await tester.pumpMobileControls(game);
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.down));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(pressed, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('correctly triggers the arrow right', (tester) async {
|
||||||
|
var pressed = false;
|
||||||
|
final component = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowRight: () => pressed = true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final game = _MockPinballGame();
|
||||||
|
when(game.descendants).thenReturn([component]);
|
||||||
|
|
||||||
|
await tester.pumpMobileControls(game);
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.right));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(pressed, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('correctly triggers the arrow left', (tester) async {
|
||||||
|
var pressed = false;
|
||||||
|
final component = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowLeft: () => pressed = true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final game = _MockPinballGame();
|
||||||
|
when(game.descendants).thenReturn([component]);
|
||||||
|
|
||||||
|
await tester.pumpMobileControls(game);
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.left));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(pressed, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('correctly triggers the enter', (tester) async {
|
||||||
|
var pressed = false;
|
||||||
|
final component = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.enter: () => pressed = true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final game = _MockPinballGame();
|
||||||
|
when(game.descendants).thenReturn([component]);
|
||||||
|
|
||||||
|
await tester.pumpMobileControls(game);
|
||||||
|
await tester.tap(find.byType(PinballButton));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(pressed, isTrue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,113 @@
|
|||||||
|
// ignore_for_file: one_member_abstracts
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
|
||||||
|
extension _WidgetTesterX on WidgetTester {
|
||||||
|
Future<void> pumpDpad({
|
||||||
|
required VoidCallback onTapUp,
|
||||||
|
required VoidCallback onTapDown,
|
||||||
|
required VoidCallback onTapLeft,
|
||||||
|
required VoidCallback onTapRight,
|
||||||
|
}) async {
|
||||||
|
await pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
home: Scaffold(
|
||||||
|
body: MobileDpad(
|
||||||
|
onTapUp: onTapUp,
|
||||||
|
onTapDown: onTapDown,
|
||||||
|
onTapLeft: onTapLeft,
|
||||||
|
onTapRight: onTapRight,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension _CommonFindersX on CommonFinders {
|
||||||
|
Finder byPinballDpadDirection(PinballDpadDirection direction) {
|
||||||
|
return byWidgetPredicate((widget) {
|
||||||
|
return widget is PinballDpadButton && widget.direction == direction;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class _VoidCallbackStubBase {
|
||||||
|
void onCall();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _VoidCallbackStub extends Mock implements _VoidCallbackStubBase {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('MobileDpad', () {
|
||||||
|
testWidgets('renders correctly', (tester) async {
|
||||||
|
await tester.pumpDpad(
|
||||||
|
onTapUp: () {},
|
||||||
|
onTapDown: () {},
|
||||||
|
onTapLeft: () {},
|
||||||
|
onTapRight: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
find.byType(PinballDpadButton),
|
||||||
|
findsNWidgets(4),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('can tap up', (tester) async {
|
||||||
|
final stub = _VoidCallbackStub();
|
||||||
|
await tester.pumpDpad(
|
||||||
|
onTapUp: stub.onCall,
|
||||||
|
onTapDown: () {},
|
||||||
|
onTapLeft: () {},
|
||||||
|
onTapRight: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.up));
|
||||||
|
verify(stub.onCall).called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('can tap down', (tester) async {
|
||||||
|
final stub = _VoidCallbackStub();
|
||||||
|
await tester.pumpDpad(
|
||||||
|
onTapUp: () {},
|
||||||
|
onTapDown: stub.onCall,
|
||||||
|
onTapLeft: () {},
|
||||||
|
onTapRight: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.down));
|
||||||
|
verify(stub.onCall).called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('can tap left', (tester) async {
|
||||||
|
final stub = _VoidCallbackStub();
|
||||||
|
await tester.pumpDpad(
|
||||||
|
onTapUp: () {},
|
||||||
|
onTapDown: () {},
|
||||||
|
onTapLeft: stub.onCall,
|
||||||
|
onTapRight: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.left));
|
||||||
|
verify(stub.onCall).called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('can tap left', (tester) async {
|
||||||
|
final stub = _VoidCallbackStub();
|
||||||
|
await tester.pumpDpad(
|
||||||
|
onTapUp: () {},
|
||||||
|
onTapDown: () {},
|
||||||
|
onTapLeft: () {},
|
||||||
|
onTapRight: stub.onCall,
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.right));
|
||||||
|
verify(stub.onCall).called(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|