After Width: | Height: | Size: 7.8 KiB |
@ -1,27 +1,39 @@
|
|||||||
import 'package:bloc/bloc.dart';
|
import 'package:bloc/bloc.dart';
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball/select_character/select_character.dart';
|
||||||
|
import 'package:pinball_audio/pinball_audio.dart';
|
||||||
|
|
||||||
part 'assets_manager_state.dart';
|
part 'assets_manager_state.dart';
|
||||||
|
|
||||||
/// {@template assets_manager_cubit}
|
|
||||||
/// Cubit responsable for pre loading any game assets
|
|
||||||
/// {@endtemplate}
|
|
||||||
class AssetsManagerCubit extends Cubit<AssetsManagerState> {
|
class AssetsManagerCubit extends Cubit<AssetsManagerState> {
|
||||||
/// {@macro assets_manager_cubit}
|
AssetsManagerCubit(this._game, this._player)
|
||||||
AssetsManagerCubit(List<Future> loadables)
|
: super(const AssetsManagerState.initial());
|
||||||
: super(
|
|
||||||
AssetsManagerState.initial(
|
final PinballGame _game;
|
||||||
loadables: loadables,
|
final PinballPlayer _player;
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
/// Loads the assets
|
|
||||||
Future<void> load() async {
|
Future<void> load() async {
|
||||||
|
/// Assigning loadables is a very expensive operation. With this purposeful
|
||||||
|
/// delay here, which is a bit random in duration but enough to let the UI
|
||||||
|
/// do its job without adding too much delay for the user, we are letting
|
||||||
|
/// the UI paint first, and then we start loading the assets.
|
||||||
|
await Future<void>.delayed(const Duration(milliseconds: 300));
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
loadables: [
|
||||||
|
_game.preFetchLeaderboard(),
|
||||||
|
..._game.preLoadAssets(),
|
||||||
|
..._player.load(),
|
||||||
|
...BonusAnimation.loadAssets(),
|
||||||
|
...SelectedCharacter.loadAssets(),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
final all = state.loadables.map((loadable) async {
|
final all = state.loadables.map((loadable) async {
|
||||||
await loadable;
|
await loadable;
|
||||||
emit(state.copyWith(loaded: [...state.loaded, loadable]));
|
emit(state.copyWith(loaded: [...state.loaded, loadable]));
|
||||||
}).toList();
|
}).toList();
|
||||||
|
|
||||||
await Future.wait(all);
|
await Future.wait(all);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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(),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Before Width: | Height: | Size: 270 KiB After Width: | Height: | Size: 266 KiB |
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 29 KiB |
@ -1,14 +1,18 @@
|
|||||||
|
// ignore_for_file: public_member_api_docs
|
||||||
|
|
||||||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
import 'package:flame_forge2d/flame_forge2d.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';
|
||||||
|
|
||||||
class AndroidSpaceshipEntranceBallContactBehavior
|
class AndroidSpaceshipEntranceBallContactBehavior
|
||||||
extends ContactBehavior<AndroidSpaceshipEntrance> {
|
extends ContactBehavior<AndroidSpaceshipEntrance>
|
||||||
|
with FlameBlocReader<AndroidSpaceshipCubit, AndroidSpaceshipState> {
|
||||||
@override
|
@override
|
||||||
void beginContact(Object other, Contact contact) {
|
void beginContact(Object other, Contact contact) {
|
||||||
super.beginContact(other, contact);
|
super.beginContact(other, contact);
|
||||||
if (other is! Ball) return;
|
if (other is! Ball) return;
|
||||||
|
|
||||||
parent.parent.bloc.onBallEntered();
|
bloc.onBallEntered();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 @@
|
|||||||
|
export 'dash_bumper_ball_contact_behavior.dart';
|
@ -0,0 +1,17 @@
|
|||||||
|
import 'package:bloc/bloc.dart';
|
||||||
|
|
||||||
|
part 'dash_bumper_state.dart';
|
||||||
|
|
||||||
|
class DashBumperCubit extends Cubit<DashBumperState> {
|
||||||
|
DashBumperCubit() : super(DashBumperState.inactive);
|
||||||
|
|
||||||
|
/// Event added when the bumper contacts with a ball.
|
||||||
|
void onBallContacted() {
|
||||||
|
emit(DashBumperState.active);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Event added when the bumper should return to its initial configuration.
|
||||||
|
void onReset() {
|
||||||
|
emit(DashBumperState.inactive);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
part of 'dash_bumper_cubit.dart';
|
||||||
|
|
||||||
|
/// Indicates the [DashBumperCubit]'s current state.
|
||||||
|
enum DashBumperState {
|
||||||
|
/// A lit up bumper.
|
||||||
|
active,
|
||||||
|
|
||||||
|
/// A dimmed bumper.
|
||||||
|
inactive,
|
||||||
|
}
|
@ -1 +0,0 @@
|
|||||||
export 'dash_nest_bumper_contact_behavior.dart';
|
|
@ -1,17 +0,0 @@
|
|||||||
import 'package:bloc/bloc.dart';
|
|
||||||
|
|
||||||
part 'dash_nest_bumper_state.dart';
|
|
||||||
|
|
||||||
class DashNestBumperCubit extends Cubit<DashNestBumperState> {
|
|
||||||
DashNestBumperCubit() : super(DashNestBumperState.inactive);
|
|
||||||
|
|
||||||
/// Event added when the bumper contacts with a ball.
|
|
||||||
void onBallContacted() {
|
|
||||||
emit(DashNestBumperState.active);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Event added when the bumper should return to its initial configuration.
|
|
||||||
void onReset() {
|
|
||||||
emit(DashNestBumperState.inactive);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
part of 'dash_nest_bumper_cubit.dart';
|
|
||||||
|
|
||||||
/// Indicates the [DashNestBumperCubit]'s current state.
|
|
||||||
enum DashNestBumperState {
|
|
||||||
/// A lit up bumper.
|
|
||||||
active,
|
|
||||||
|
|
||||||
/// A dimmed bumper.
|
|
||||||
inactive,
|
|
||||||
}
|
|
@ -0,0 +1,2 @@
|
|||||||
|
export 'flipper_jointing_behavior.dart';
|
||||||
|
export 'flipper_key_controlling_behavior.dart';
|
@ -0,0 +1,62 @@
|
|||||||
|
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 {
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
|
||||||
|
final anchor = _FlipperAnchor(flipper: parent);
|
||||||
|
await add(anchor);
|
||||||
|
|
||||||
|
final jointDef = _FlipperAnchorRevoluteJointDef(
|
||||||
|
flipper: parent,
|
||||||
|
anchor: anchor,
|
||||||
|
);
|
||||||
|
parent.world.createJoint(RevoluteJoint(jointDef));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@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,
|
||||||
|
}) {
|
||||||
|
initialize(
|
||||||
|
flipper.body,
|
||||||
|
anchor.body,
|
||||||
|
flipper.body.position + anchor.body.position,
|
||||||
|
);
|
||||||
|
|
||||||
|
enableLimit = true;
|
||||||
|
upperAngle = 0.611;
|
||||||
|
lowerAngle = -upperAngle;
|
||||||
|
}
|
||||||
|
}
|
@ -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 @@
|
|||||||
|
export 'score_component_scaling_behavior.dart';
|
@ -0,0 +1,24 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
/// Scales a [ScoreComponent] according to its position on the board.
|
||||||
|
class ScoreComponentScalingBehavior extends Component
|
||||||
|
with ParentIsA<SpriteComponent> {
|
||||||
|
@override
|
||||||
|
void update(double dt) {
|
||||||
|
super.update(dt);
|
||||||
|
final boardHeight = BoardDimensions.bounds.height;
|
||||||
|
const maxShrinkValue = 0.83;
|
||||||
|
|
||||||
|
final augmentedPosition = parent.position.y * 3;
|
||||||
|
final standardizedYPosition = augmentedPosition + (boardHeight / 2);
|
||||||
|
final scaleFactor = maxShrinkValue +
|
||||||
|
((standardizedYPosition / boardHeight) * (1 - maxShrinkValue));
|
||||||
|
|
||||||
|
parent.scale.setValues(
|
||||||
|
scaleFactor,
|
||||||
|
scaleFactor,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -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,139 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:bloc_test/bloc_test.dart';
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_components/src/components/bumping_behavior.dart';
|
||||||
|
import 'package:pinball_components/src/components/dash_bumper/behaviors/behaviors.dart';
|
||||||
|
|
||||||
|
import '../../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
class _MockDashBumperCubit extends Mock implements DashBumperCubit {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
group('DashBumper', () {
|
||||||
|
final flameTester = FlameTester(
|
||||||
|
() => TestGame(
|
||||||
|
[
|
||||||
|
Assets.images.dash.bumper.main.active.keyName,
|
||||||
|
Assets.images.dash.bumper.main.inactive.keyName,
|
||||||
|
Assets.images.dash.bumper.a.active.keyName,
|
||||||
|
Assets.images.dash.bumper.a.inactive.keyName,
|
||||||
|
Assets.images.dash.bumper.b.active.keyName,
|
||||||
|
Assets.images.dash.bumper.b.inactive.keyName,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test('"main" loads correctly', (game) async {
|
||||||
|
final bumper = DashBumper.main();
|
||||||
|
await game.ensureAdd(bumper);
|
||||||
|
expect(game.contains(bumper), isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test('"a" loads correctly', (game) async {
|
||||||
|
final bumper = DashBumper.a();
|
||||||
|
await game.ensureAdd(bumper);
|
||||||
|
|
||||||
|
expect(game.contains(bumper), isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test('"b" loads correctly', (game) async {
|
||||||
|
final bumper = DashBumper.b();
|
||||||
|
await game.ensureAdd(bumper);
|
||||||
|
expect(game.contains(bumper), isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ignore: public_member_api_docs
|
||||||
|
flameTester.test('closes bloc when removed', (game) async {
|
||||||
|
final bloc = _MockDashBumperCubit();
|
||||||
|
whenListen(
|
||||||
|
bloc,
|
||||||
|
const Stream<DashBumperState>.empty(),
|
||||||
|
initialState: DashBumperState.inactive,
|
||||||
|
);
|
||||||
|
when(bloc.close).thenAnswer((_) async {});
|
||||||
|
final bumper = DashBumper.test(bloc: bloc);
|
||||||
|
|
||||||
|
await game.ensureAdd(bumper);
|
||||||
|
game.remove(bumper);
|
||||||
|
await game.ready();
|
||||||
|
|
||||||
|
verify(bloc.close).called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test('adds a bumperBallContactBehavior', (game) async {
|
||||||
|
final bumper = DashBumper.a();
|
||||||
|
await game.ensureAdd(bumper);
|
||||||
|
expect(
|
||||||
|
bumper.children.whereType<DashBumperBallContactBehavior>().single,
|
||||||
|
isNotNull,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
group("'main' adds", () {
|
||||||
|
flameTester.test('new children', (game) async {
|
||||||
|
final component = Component();
|
||||||
|
final bumper = DashBumper.main(
|
||||||
|
children: [component],
|
||||||
|
);
|
||||||
|
await game.ensureAdd(bumper);
|
||||||
|
expect(bumper.children, contains(component));
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test('a BumpingBehavior', (game) async {
|
||||||
|
final bumper = DashBumper.main();
|
||||||
|
await game.ensureAdd(bumper);
|
||||||
|
expect(
|
||||||
|
bumper.children.whereType<BumpingBehavior>().single,
|
||||||
|
isNotNull,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group("'a' adds", () {
|
||||||
|
flameTester.test('new children', (game) async {
|
||||||
|
final component = Component();
|
||||||
|
final bumper = DashBumper.a(
|
||||||
|
children: [component],
|
||||||
|
);
|
||||||
|
await game.ensureAdd(bumper);
|
||||||
|
expect(bumper.children, contains(component));
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test('a BumpingBehavior', (game) async {
|
||||||
|
final bumper = DashBumper.a();
|
||||||
|
await game.ensureAdd(bumper);
|
||||||
|
expect(
|
||||||
|
bumper.children.whereType<BumpingBehavior>().single,
|
||||||
|
isNotNull,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group("'b' adds", () {
|
||||||
|
flameTester.test('new children', (game) async {
|
||||||
|
final component = Component();
|
||||||
|
final bumper = DashBumper.b(
|
||||||
|
children: [component],
|
||||||
|
);
|
||||||
|
await game.ensureAdd(bumper);
|
||||||
|
expect(bumper.children, contains(component));
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test('a BumpingBehavior', (game) async {
|
||||||
|
final bumper = DashBumper.b();
|
||||||
|
await game.ensureAdd(bumper);
|
||||||
|
expect(
|
||||||
|
bumper.children.whereType<BumpingBehavior>().single,
|
||||||
|
isNotNull,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -1,137 +0,0 @@
|
|||||||
// ignore_for_file: cascade_invocations
|
|
||||||
|
|
||||||
import 'package:bloc_test/bloc_test.dart';
|
|
||||||
import 'package:flame/components.dart';
|
|
||||||
import 'package:flame_test/flame_test.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:mocktail/mocktail.dart';
|
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
|
||||||
import 'package:pinball_components/src/components/bumping_behavior.dart';
|
|
||||||
import 'package:pinball_components/src/components/dash_nest_bumper/behaviors/behaviors.dart';
|
|
||||||
|
|
||||||
import '../../../helpers/helpers.dart';
|
|
||||||
|
|
||||||
class _MockDashNestBumperCubit extends Mock implements DashNestBumperCubit {}
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
TestWidgetsFlutterBinding.ensureInitialized();
|
|
||||||
|
|
||||||
group('DashNestBumper', () {
|
|
||||||
final assets = [
|
|
||||||
Assets.images.dash.bumper.main.active.keyName,
|
|
||||||
Assets.images.dash.bumper.main.inactive.keyName,
|
|
||||||
Assets.images.dash.bumper.a.active.keyName,
|
|
||||||
Assets.images.dash.bumper.a.inactive.keyName,
|
|
||||||
Assets.images.dash.bumper.b.active.keyName,
|
|
||||||
Assets.images.dash.bumper.b.inactive.keyName,
|
|
||||||
];
|
|
||||||
final flameTester = FlameTester(() => TestGame(assets));
|
|
||||||
|
|
||||||
flameTester.test('"main" loads correctly', (game) async {
|
|
||||||
final bumper = DashNestBumper.main();
|
|
||||||
await game.ensureAdd(bumper);
|
|
||||||
expect(game.contains(bumper), isTrue);
|
|
||||||
});
|
|
||||||
|
|
||||||
flameTester.test('"a" loads correctly', (game) async {
|
|
||||||
final bumper = DashNestBumper.a();
|
|
||||||
await game.ensureAdd(bumper);
|
|
||||||
|
|
||||||
expect(game.contains(bumper), isTrue);
|
|
||||||
});
|
|
||||||
|
|
||||||
flameTester.test('"b" loads correctly', (game) async {
|
|
||||||
final bumper = DashNestBumper.b();
|
|
||||||
await game.ensureAdd(bumper);
|
|
||||||
expect(game.contains(bumper), isTrue);
|
|
||||||
});
|
|
||||||
|
|
||||||
flameTester.test('closes bloc when removed', (game) async {
|
|
||||||
final bloc = _MockDashNestBumperCubit();
|
|
||||||
whenListen(
|
|
||||||
bloc,
|
|
||||||
const Stream<DashNestBumperState>.empty(),
|
|
||||||
initialState: DashNestBumperState.inactive,
|
|
||||||
);
|
|
||||||
when(bloc.close).thenAnswer((_) async {});
|
|
||||||
final dashNestBumper = DashNestBumper.test(bloc: bloc);
|
|
||||||
|
|
||||||
await game.ensureAdd(dashNestBumper);
|
|
||||||
game.remove(dashNestBumper);
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
verify(bloc.close).called(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
flameTester.test('adds a DashNestBumperBallContactBehavior', (game) async {
|
|
||||||
final dashNestBumper = DashNestBumper.a();
|
|
||||||
await game.ensureAdd(dashNestBumper);
|
|
||||||
expect(
|
|
||||||
dashNestBumper.children
|
|
||||||
.whereType<DashNestBumperBallContactBehavior>()
|
|
||||||
.single,
|
|
||||||
isNotNull,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
group("'main' adds", () {
|
|
||||||
flameTester.test('new children', (game) async {
|
|
||||||
final component = Component();
|
|
||||||
final dashNestBumper = DashNestBumper.main(
|
|
||||||
children: [component],
|
|
||||||
);
|
|
||||||
await game.ensureAdd(dashNestBumper);
|
|
||||||
expect(dashNestBumper.children, contains(component));
|
|
||||||
});
|
|
||||||
|
|
||||||
flameTester.test('a BumpingBehavior', (game) async {
|
|
||||||
final dashNestBumper = DashNestBumper.main();
|
|
||||||
await game.ensureAdd(dashNestBumper);
|
|
||||||
expect(
|
|
||||||
dashNestBumper.children.whereType<BumpingBehavior>().single,
|
|
||||||
isNotNull,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
group("'a' adds", () {
|
|
||||||
flameTester.test('new children', (game) async {
|
|
||||||
final component = Component();
|
|
||||||
final dashNestBumper = DashNestBumper.a(
|
|
||||||
children: [component],
|
|
||||||
);
|
|
||||||
await game.ensureAdd(dashNestBumper);
|
|
||||||
expect(dashNestBumper.children, contains(component));
|
|
||||||
});
|
|
||||||
|
|
||||||
flameTester.test('a BumpingBehavior', (game) async {
|
|
||||||
final dashNestBumper = DashNestBumper.a();
|
|
||||||
await game.ensureAdd(dashNestBumper);
|
|
||||||
expect(
|
|
||||||
dashNestBumper.children.whereType<BumpingBehavior>().single,
|
|
||||||
isNotNull,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
group("'b' adds", () {
|
|
||||||
flameTester.test('new children', (game) async {
|
|
||||||
final component = Component();
|
|
||||||
final dashNestBumper = DashNestBumper.b(
|
|
||||||
children: [component],
|
|
||||||
);
|
|
||||||
await game.ensureAdd(dashNestBumper);
|
|
||||||
expect(dashNestBumper.children, contains(component));
|
|
||||||
});
|
|
||||||
|
|
||||||
flameTester.test('a BumpingBehavior', (game) async {
|
|
||||||
final dashNestBumper = DashNestBumper.b();
|
|
||||||
await game.ensureAdd(dashNestBumper);
|
|
||||||
expect(
|
|
||||||
dashNestBumper.children.whereType<BumpingBehavior>().single,
|
|
||||||
isNotNull,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
@ -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 |
@ -0,0 +1,74 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame/effects.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_components/src/components/score_component/behaviors/behaviors.dart';
|
||||||
|
|
||||||
|
import '../../../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('ScoreComponentScalingBehavior', () {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(
|
||||||
|
() => TestGame([
|
||||||
|
Assets.images.score.fiveThousand.keyName,
|
||||||
|
Assets.images.score.twentyThousand.keyName,
|
||||||
|
Assets.images.score.twoHundredThousand.keyName,
|
||||||
|
Assets.images.score.oneMillion.keyName,
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
|
test('can be instantiated', () {
|
||||||
|
expect(
|
||||||
|
ScoreComponentScalingBehavior(),
|
||||||
|
isA<ScoreComponentScalingBehavior>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test('can be loaded', (game) async {
|
||||||
|
final parent = ScoreComponent.test(
|
||||||
|
points: Points.fiveThousand,
|
||||||
|
position: Vector2.zero(),
|
||||||
|
effectController: EffectController(duration: 1),
|
||||||
|
);
|
||||||
|
final behavior = ScoreComponentScalingBehavior();
|
||||||
|
await game.ensureAdd(parent);
|
||||||
|
await parent.ensureAdd(behavior);
|
||||||
|
|
||||||
|
expect(parent.children, contains(behavior));
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'scales the sprite',
|
||||||
|
(game) async {
|
||||||
|
final parent1 = ScoreComponent.test(
|
||||||
|
points: Points.fiveThousand,
|
||||||
|
position: Vector2(0, 10),
|
||||||
|
effectController: EffectController(duration: 1),
|
||||||
|
);
|
||||||
|
final parent2 = ScoreComponent.test(
|
||||||
|
points: Points.fiveThousand,
|
||||||
|
position: Vector2(0, -10),
|
||||||
|
effectController: EffectController(duration: 1),
|
||||||
|
);
|
||||||
|
await game.ensureAddAll([parent1, parent2]);
|
||||||
|
|
||||||
|
await parent1.ensureAdd(ScoreComponentScalingBehavior());
|
||||||
|
await parent2.ensureAdd(ScoreComponentScalingBehavior());
|
||||||
|
game.update(1);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
parent1.scale.x,
|
||||||
|
greaterThan(parent2.scale.x),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
parent1.scale.y,
|
||||||
|
greaterThan(parent2.scale.y),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
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,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|