Before Width: | Height: | Size: 8.1 KiB |
Before Width: | Height: | Size: 9.5 KiB |
Before Width: | Height: | Size: 3.4 KiB |
Before Width: | Height: | Size: 9.8 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 7.7 KiB |
@ -0,0 +1,37 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||||||
|
|
||||||
|
/// {@template component_controller}
|
||||||
|
/// A [ComponentController] is a [Component] in charge of handling the logic
|
||||||
|
/// associated with another [Component].
|
||||||
|
///
|
||||||
|
/// [ComponentController]s usually implement [BlocComponent].
|
||||||
|
/// {@endtemplate}
|
||||||
|
abstract class ComponentController<T extends Component> extends Component {
|
||||||
|
/// {@macro component_controller}
|
||||||
|
ComponentController(this.component);
|
||||||
|
|
||||||
|
/// The [Component] controlled by this [ComponentController].
|
||||||
|
final T component;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> addToParent(Component parent) async {
|
||||||
|
assert(
|
||||||
|
parent == component,
|
||||||
|
'ComponentController should be child of $component.',
|
||||||
|
);
|
||||||
|
await super.addToParent(parent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Mixin that attaches a single [ComponentController] to a [Component].
|
||||||
|
mixin Controls<T extends ComponentController> on Component {
|
||||||
|
/// The [ComponentController] attached to this [Component].
|
||||||
|
late final T controller;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
await add(controller);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
export 'component_controller.dart';
|
@ -1,88 +0,0 @@
|
|||||||
import 'package:flame/components.dart';
|
|
||||||
import 'package:pinball/flame/blueprint.dart';
|
|
||||||
import 'package:pinball/game/game.dart';
|
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
|
||||||
|
|
||||||
/// {@template ball_type}
|
|
||||||
/// Specifies the type of [Ball].
|
|
||||||
///
|
|
||||||
/// Different [BallType]s are affected by different game mechanics.
|
|
||||||
/// {@endtemplate}
|
|
||||||
enum BallType {
|
|
||||||
/// A [Ball] spawned from the [Plunger].
|
|
||||||
///
|
|
||||||
/// [normal] balls decrease the [GameState.balls] when they fall through the
|
|
||||||
/// the [BottomWall].
|
|
||||||
normal,
|
|
||||||
|
|
||||||
/// A [Ball] that does not alter [GameState.balls].
|
|
||||||
///
|
|
||||||
/// For example, a [Ball] spawned by Dash in the [FlutterForest].
|
|
||||||
extra,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// {@template ball_blueprint}
|
|
||||||
/// [Blueprint] which cretes a ball game object.
|
|
||||||
/// {@endtemplate}
|
|
||||||
class BallBlueprint extends Blueprint<PinballGame> {
|
|
||||||
/// {@macro ball_blueprint}
|
|
||||||
BallBlueprint({
|
|
||||||
required this.position,
|
|
||||||
required this.type,
|
|
||||||
});
|
|
||||||
|
|
||||||
/// The initial position of the [Ball].
|
|
||||||
final Vector2 position;
|
|
||||||
|
|
||||||
/// {@macro ball_type}
|
|
||||||
final BallType type;
|
|
||||||
|
|
||||||
@override
|
|
||||||
void build(PinballGame gameRef) {
|
|
||||||
final baseColor = gameRef.theme.characterTheme.ballColor;
|
|
||||||
final ball = Ball(baseColor: baseColor)
|
|
||||||
..add(
|
|
||||||
BallController(type: type),
|
|
||||||
);
|
|
||||||
|
|
||||||
add(ball..initialPosition = position + Vector2(0, ball.size.y / 2));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// {@template ball_controller}
|
|
||||||
/// Controller attached to a [Ball] that handles its game related logic.
|
|
||||||
/// {@endtemplate}
|
|
||||||
class BallController extends Component with HasGameRef<PinballGame> {
|
|
||||||
/// {@macro ball_controller}
|
|
||||||
BallController({required this.type});
|
|
||||||
|
|
||||||
/// {@macro ball_type}
|
|
||||||
final BallType type;
|
|
||||||
|
|
||||||
/// Removes the [Ball] from a [PinballGame]; spawning a new [Ball] if
|
|
||||||
/// any are left.
|
|
||||||
///
|
|
||||||
/// Triggered by [BottomWallBallContactCallback] when the [Ball] falls into
|
|
||||||
/// a [BottomWall].
|
|
||||||
void lost() {
|
|
||||||
parent?.shouldRemove = true;
|
|
||||||
// TODO(alestiago): Consider adding test for this logic once we remove the
|
|
||||||
// BallX extension.
|
|
||||||
if (type != BallType.normal) return;
|
|
||||||
|
|
||||||
final bloc = gameRef.read<GameBloc>()..add(const BallLost());
|
|
||||||
final shouldBallRespwan = !bloc.state.isLastBall && !bloc.state.isGameOver;
|
|
||||||
if (shouldBallRespwan) {
|
|
||||||
gameRef.spawnBall();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds helper methods to the [Ball]
|
|
||||||
extension BallX on Ball {
|
|
||||||
/// Returns the controller instance of the ball
|
|
||||||
// TODO(erickzanardo): Remove the need of an extension.
|
|
||||||
BallController get controller {
|
|
||||||
return children.whereType<BallController>().first;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +1,14 @@
|
|||||||
export 'ball.dart';
|
|
||||||
export 'baseboard.dart';
|
export 'baseboard.dart';
|
||||||
export 'board.dart';
|
export 'board.dart';
|
||||||
export 'board_side.dart';
|
|
||||||
export 'bonus_word.dart';
|
export 'bonus_word.dart';
|
||||||
export 'chrome_dino.dart';
|
export 'chrome_dino.dart';
|
||||||
export 'flipper.dart';
|
export 'controlled_ball.dart';
|
||||||
|
export 'flipper_controller.dart';
|
||||||
export 'flutter_forest.dart';
|
export 'flutter_forest.dart';
|
||||||
export 'jetpack_ramp.dart';
|
export 'jetpack_ramp.dart';
|
||||||
export 'joint_anchor.dart';
|
|
||||||
export 'kicker.dart';
|
export 'kicker.dart';
|
||||||
export 'launcher_ramp.dart';
|
export 'launcher_ramp.dart';
|
||||||
export 'plunger.dart';
|
export 'plunger.dart';
|
||||||
export 'ramp_opening.dart';
|
|
||||||
export 'score_points.dart';
|
export 'score_points.dart';
|
||||||
export 'spaceship.dart';
|
export 'spaceship_exit_rail.dart';
|
||||||
export 'wall.dart';
|
export 'wall.dart';
|
||||||
|
@ -0,0 +1,102 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||||||
|
import 'package:flame_forge2d/forge2d_game.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball/flame/flame.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_theme/pinball_theme.dart';
|
||||||
|
|
||||||
|
/// {@template controlled_ball}
|
||||||
|
/// A [Ball] with a [BallController] attached.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class ControlledBall extends Ball with Controls<BallController> {
|
||||||
|
/// A [Ball] that launches from the [Plunger].
|
||||||
|
///
|
||||||
|
/// When a launched [Ball] is lost, it will decrease the [GameState.balls]
|
||||||
|
/// count, and a new [Ball] is spawned.
|
||||||
|
ControlledBall.launch({
|
||||||
|
required PinballTheme theme,
|
||||||
|
}) : super(baseColor: theme.characterTheme.ballColor) {
|
||||||
|
controller = LaunchedBallController(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template bonus_ball}
|
||||||
|
/// {@macro controlled_ball}
|
||||||
|
///
|
||||||
|
/// When a bonus [Ball] is lost, the [GameState.balls] doesn't change.
|
||||||
|
/// {@endtemplate}
|
||||||
|
ControlledBall.bonus({
|
||||||
|
required PinballTheme theme,
|
||||||
|
}) : super(baseColor: theme.characterTheme.ballColor) {
|
||||||
|
controller = BonusBallController(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// [Ball] used in [DebugPinballGame].
|
||||||
|
ControlledBall.debug() : super(baseColor: const Color(0xFFFF0000)) {
|
||||||
|
controller = BonusBallController(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template ball_controller}
|
||||||
|
/// Controller attached to a [Ball] that handles its game related logic.
|
||||||
|
/// {@endtemplate}
|
||||||
|
abstract class BallController extends ComponentController<Ball> {
|
||||||
|
/// {@macro ball_controller}
|
||||||
|
BallController(Ball ball) : super(ball);
|
||||||
|
|
||||||
|
/// Removes the [Ball] from a [PinballGame].
|
||||||
|
///
|
||||||
|
/// {@template ball_controller_lost}
|
||||||
|
/// Triggered by [BottomWallBallContactCallback] when the [Ball] falls into
|
||||||
|
/// a [BottomWall].
|
||||||
|
/// {@endtemplate}
|
||||||
|
void lost();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template bonus_ball_controller}
|
||||||
|
/// {@macro ball_controller}
|
||||||
|
///
|
||||||
|
/// A [BonusBallController] doesn't change the [GameState.balls] count.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class BonusBallController extends BallController {
|
||||||
|
/// {@macro bonus_ball_controller}
|
||||||
|
BonusBallController(Ball<Forge2DGame> component) : super(component);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void lost() {
|
||||||
|
component.shouldRemove = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template launched_ball_controller}
|
||||||
|
/// {@macro ball_controller}
|
||||||
|
///
|
||||||
|
/// A [LaunchedBallController] changes the [GameState.balls] count.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class LaunchedBallController extends BallController
|
||||||
|
with HasGameRef<PinballGame>, BlocComponent<GameBloc, GameState> {
|
||||||
|
/// {@macro launched_ball_controller}
|
||||||
|
LaunchedBallController(Ball<Forge2DGame> ball) : super(ball);
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool listenWhen(GameState? previousState, GameState newState) {
|
||||||
|
return (previousState?.balls ?? 0) > newState.balls;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onNewState(GameState state) {
|
||||||
|
super.onNewState(state);
|
||||||
|
component.shouldRemove = true;
|
||||||
|
if (state.balls > 1) gameRef.spawnBall();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes the [Ball] from a [PinballGame]; spawning a new [Ball] if
|
||||||
|
/// any are left.
|
||||||
|
///
|
||||||
|
/// {@macro ball_controller_lost}
|
||||||
|
@override
|
||||||
|
void lost() {
|
||||||
|
gameRef.read<GameBloc>().add(const BallLost());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:pinball/flame/flame.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
/// {@template controlled_flipper}
|
||||||
|
/// A [Flipper] with a [FlipperController] attached.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class ControlledFlipper extends Flipper with Controls<FlipperController> {
|
||||||
|
/// {@macro controlled_flipper}
|
||||||
|
ControlledFlipper({
|
||||||
|
required BoardSide side,
|
||||||
|
}) : super(side: side) {
|
||||||
|
controller = FlipperController(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template flipper_controller}
|
||||||
|
/// A [ComponentController] that controls a [Flipper]s movement.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class FlipperController extends ComponentController<Flipper>
|
||||||
|
with KeyboardHandler {
|
||||||
|
/// {@macro flipper_controller}
|
||||||
|
FlipperController(Flipper flipper)
|
||||||
|
: _keys = flipper.side.flipperKeys,
|
||||||
|
super(flipper);
|
||||||
|
|
||||||
|
/// The [LogicalKeyboardKey]s that will control the [Flipper].
|
||||||
|
///
|
||||||
|
/// [onKeyEvent] method listens to when one of these keys is pressed.
|
||||||
|
final List<LogicalKeyboardKey> _keys;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool onKeyEvent(
|
||||||
|
RawKeyEvent event,
|
||||||
|
Set<LogicalKeyboardKey> keysPressed,
|
||||||
|
) {
|
||||||
|
if (!_keys.contains(event.logicalKey)) return true;
|
||||||
|
|
||||||
|
if (event is RawKeyDownEvent) {
|
||||||
|
component.moveUp();
|
||||||
|
} else if (event is RawKeyUpEvent) {
|
||||||
|
component.moveDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension on BoardSide {
|
||||||
|
List<LogicalKeyboardKey> get flipperKeys {
|
||||||
|
switch (this) {
|
||||||
|
case BoardSide.left:
|
||||||
|
return [
|
||||||
|
LogicalKeyboardKey.arrowLeft,
|
||||||
|
LogicalKeyboardKey.keyA,
|
||||||
|
];
|
||||||
|
case BoardSide.right:
|
||||||
|
return [
|
||||||
|
LogicalKeyboardKey.arrowRight,
|
||||||
|
LogicalKeyboardKey.keyD,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,198 @@
|
|||||||
|
// ignore_for_file: avoid_renaming_method_parameters
|
||||||
|
|
||||||
|
import 'dart:math' as math;
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
import 'package:flame/extensions.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart' hide Assets;
|
||||||
|
|
||||||
|
/// {@template spaceship_exit_rail}
|
||||||
|
/// A [Blueprint] for the spaceship drop tube.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class SpaceshipExitRail extends Forge2DBlueprint {
|
||||||
|
/// {@macro spaceship_exit_rail}
|
||||||
|
SpaceshipExitRail({required this.position});
|
||||||
|
|
||||||
|
/// The [position] where the elements will be created
|
||||||
|
final Vector2 position;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void build(_) {
|
||||||
|
addAllContactCallback([
|
||||||
|
SpaceshipExitRailEndBallContactCallback(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
final spaceshipExitRailRamp = _SpaceshipExitRailRamp()
|
||||||
|
..initialPosition = position;
|
||||||
|
final exitRail = SpaceshipExitRailEnd()
|
||||||
|
..initialPosition = position + _SpaceshipExitRailRamp.exitPoint;
|
||||||
|
|
||||||
|
addAll([
|
||||||
|
spaceshipExitRailRamp,
|
||||||
|
exitRail,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SpaceshipExitRailRamp extends BodyComponent
|
||||||
|
with InitialPosition, Layered {
|
||||||
|
_SpaceshipExitRailRamp() : super(priority: 2) {
|
||||||
|
layer = Layer.spaceshipExitRail;
|
||||||
|
// TODO(ruimiguel): remove color once asset is placed.
|
||||||
|
paint = Paint()
|
||||||
|
..color = const Color.fromARGB(255, 249, 65, 3)
|
||||||
|
..style = PaintingStyle.stroke;
|
||||||
|
}
|
||||||
|
|
||||||
|
static final exitPoint = Vector2(9.2, -48.5);
|
||||||
|
|
||||||
|
List<FixtureDef> _createFixtureDefs() {
|
||||||
|
const entranceRotationAngle = 175 * math.pi / 180;
|
||||||
|
const curveRotationAngle = 275 * math.pi / 180;
|
||||||
|
const exitRotationAngle = 340 * math.pi / 180;
|
||||||
|
const width = 5.5;
|
||||||
|
|
||||||
|
final fixturesDefs = <FixtureDef>[];
|
||||||
|
|
||||||
|
final entranceWall = ArcShape(
|
||||||
|
center: Vector2(width / 2, 0),
|
||||||
|
arcRadius: width / 2,
|
||||||
|
angle: math.pi,
|
||||||
|
rotation: entranceRotationAngle,
|
||||||
|
);
|
||||||
|
final entranceFixtureDef = FixtureDef(entranceWall);
|
||||||
|
fixturesDefs.add(entranceFixtureDef);
|
||||||
|
|
||||||
|
final topLeftControlPoints = [
|
||||||
|
Vector2(0, 0),
|
||||||
|
Vector2(10, .5),
|
||||||
|
Vector2(7, 4),
|
||||||
|
Vector2(15.5, 8.3),
|
||||||
|
];
|
||||||
|
final topLeftCurveShape = BezierCurveShape(
|
||||||
|
controlPoints: topLeftControlPoints,
|
||||||
|
)..rotate(curveRotationAngle);
|
||||||
|
final topLeftFixtureDef = FixtureDef(topLeftCurveShape);
|
||||||
|
fixturesDefs.add(topLeftFixtureDef);
|
||||||
|
|
||||||
|
final topRightControlPoints = [
|
||||||
|
Vector2(0, width),
|
||||||
|
Vector2(10, 6.5),
|
||||||
|
Vector2(7, 10),
|
||||||
|
Vector2(15.5, 13.2),
|
||||||
|
];
|
||||||
|
final topRightCurveShape = BezierCurveShape(
|
||||||
|
controlPoints: topRightControlPoints,
|
||||||
|
)..rotate(curveRotationAngle);
|
||||||
|
final topRightFixtureDef = FixtureDef(topRightCurveShape);
|
||||||
|
fixturesDefs.add(topRightFixtureDef);
|
||||||
|
|
||||||
|
final mediumLeftControlPoints = [
|
||||||
|
topLeftControlPoints.last,
|
||||||
|
Vector2(21, 12.9),
|
||||||
|
Vector2(30, 7.1),
|
||||||
|
Vector2(32, 4.8),
|
||||||
|
];
|
||||||
|
final mediumLeftCurveShape = BezierCurveShape(
|
||||||
|
controlPoints: mediumLeftControlPoints,
|
||||||
|
)..rotate(curveRotationAngle);
|
||||||
|
final mediumLeftFixtureDef = FixtureDef(mediumLeftCurveShape);
|
||||||
|
fixturesDefs.add(mediumLeftFixtureDef);
|
||||||
|
|
||||||
|
final mediumRightControlPoints = [
|
||||||
|
topRightControlPoints.last,
|
||||||
|
Vector2(21, 17.2),
|
||||||
|
Vector2(30, 12.1),
|
||||||
|
Vector2(32, 10.2),
|
||||||
|
];
|
||||||
|
final mediumRightCurveShape = BezierCurveShape(
|
||||||
|
controlPoints: mediumRightControlPoints,
|
||||||
|
)..rotate(curveRotationAngle);
|
||||||
|
final mediumRightFixtureDef = FixtureDef(mediumRightCurveShape);
|
||||||
|
fixturesDefs.add(mediumRightFixtureDef);
|
||||||
|
|
||||||
|
final bottomLeftControlPoints = [
|
||||||
|
mediumLeftControlPoints.last,
|
||||||
|
Vector2(40, -1),
|
||||||
|
Vector2(48, 1.9),
|
||||||
|
Vector2(50.5, 2.5),
|
||||||
|
];
|
||||||
|
final bottomLeftCurveShape = BezierCurveShape(
|
||||||
|
controlPoints: bottomLeftControlPoints,
|
||||||
|
)..rotate(curveRotationAngle);
|
||||||
|
final bottomLeftFixtureDef = FixtureDef(bottomLeftCurveShape);
|
||||||
|
fixturesDefs.add(bottomLeftFixtureDef);
|
||||||
|
|
||||||
|
final bottomRightControlPoints = [
|
||||||
|
mediumRightControlPoints.last,
|
||||||
|
Vector2(40, 4),
|
||||||
|
Vector2(46, 6.5),
|
||||||
|
Vector2(48.8, 7.6),
|
||||||
|
];
|
||||||
|
final bottomRightCurveShape = BezierCurveShape(
|
||||||
|
controlPoints: bottomRightControlPoints,
|
||||||
|
)..rotate(curveRotationAngle);
|
||||||
|
final bottomRightFixtureDef = FixtureDef(bottomRightCurveShape);
|
||||||
|
fixturesDefs.add(bottomRightFixtureDef);
|
||||||
|
|
||||||
|
final exitWall = ArcShape(
|
||||||
|
center: exitPoint,
|
||||||
|
arcRadius: width / 2,
|
||||||
|
angle: math.pi,
|
||||||
|
rotation: exitRotationAngle,
|
||||||
|
);
|
||||||
|
final exitFixtureDef = FixtureDef(exitWall);
|
||||||
|
fixturesDefs.add(exitFixtureDef);
|
||||||
|
|
||||||
|
return fixturesDefs;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Body createBody() {
|
||||||
|
final bodyDef = BodyDef()
|
||||||
|
..userData = this
|
||||||
|
..position = initialPosition;
|
||||||
|
|
||||||
|
final body = world.createBody(bodyDef);
|
||||||
|
_createFixtureDefs().forEach(body.createFixture);
|
||||||
|
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template spaceship_exit_rail_end}
|
||||||
|
/// A sensor [BodyComponent] responsible for sending the [Ball]
|
||||||
|
/// back to the board.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class SpaceshipExitRailEnd extends RampOpening {
|
||||||
|
/// {@macro spaceship_exit_rail_end}
|
||||||
|
SpaceshipExitRailEnd()
|
||||||
|
: super(
|
||||||
|
pathwayLayer: Layer.spaceshipExitRail,
|
||||||
|
orientation: RampOrientation.down,
|
||||||
|
) {
|
||||||
|
layer = Layer.spaceshipExitRail;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Shape get shape {
|
||||||
|
return CircleShape()..radius = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// [ContactCallback] that handles the contact between the [Ball]
|
||||||
|
/// and a [SpaceshipExitRailEnd].
|
||||||
|
///
|
||||||
|
/// It resets the [Ball] priority and filter data so it will "be back" on the
|
||||||
|
/// board.
|
||||||
|
class SpaceshipExitRailEndBallContactCallback
|
||||||
|
extends ContactCallback<SpaceshipExitRailEnd, Ball> {
|
||||||
|
@override
|
||||||
|
void begin(SpaceshipExitRailEnd exitRail, Ball ball, _) {
|
||||||
|
ball
|
||||||
|
..priority = 1
|
||||||
|
..gameRef.reorderChildren()
|
||||||
|
..layer = exitRail.outsideLayer;
|
||||||
|
}
|
||||||
|
}
|
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 3.1 KiB |
After Width: | Height: | Size: 3.7 KiB |
After Width: | Height: | Size: 3.6 KiB |
After Width: | Height: | Size: 8.2 KiB |
After Width: | Height: | Size: 231 KiB |
After Width: | Height: | Size: 38 KiB |
@ -0,0 +1,29 @@
|
|||||||
|
import 'dart:math' as math;
|
||||||
|
|
||||||
|
import 'package:flame/extensions.dart';
|
||||||
|
|
||||||
|
/// {@template board_dimensions}
|
||||||
|
/// Contains various board properties and dimensions for global use.
|
||||||
|
/// {@endtemplate}
|
||||||
|
// TODO(allisonryan0002): consider alternatives for global dimensions.
|
||||||
|
class BoardDimensions {
|
||||||
|
/// Width and height of the board.
|
||||||
|
static final size = Vector2(101.6, 143.8);
|
||||||
|
|
||||||
|
/// [Rect] for easier access to board boundaries.
|
||||||
|
static final bounds = Rect.fromCenter(
|
||||||
|
center: Offset.zero,
|
||||||
|
width: size.x,
|
||||||
|
height: -size.y,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// 3D perspective angle of the board in radians.
|
||||||
|
static final perspectiveAngle = -math.atan(18.6 / bounds.height);
|
||||||
|
|
||||||
|
/// Factor the board shrinks by from the closest point to the farthest.
|
||||||
|
static const perspectiveShrinkFactor = 0.63;
|
||||||
|
|
||||||
|
/// Board height based on the [perspectiveShrinkFactor].
|
||||||
|
static final shrinkAdjustedHeight =
|
||||||
|
(1 / (1 - perspectiveShrinkFactor)) * size.y;
|
||||||
|
}
|
@ -1,4 +1,8 @@
|
|||||||
import 'package:pinball/game/game.dart';
|
// ignore_for_file: comment_references
|
||||||
|
// TODO(alestiago): Revisit ignore lint rule once Kicker is moved to this
|
||||||
|
// package.
|
||||||
|
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
/// Indicates a side of the board.
|
/// Indicates a side of the board.
|
||||||
///
|
///
|
@ -1,5 +1,12 @@
|
|||||||
export 'ball.dart';
|
export 'ball.dart';
|
||||||
|
export 'board_dimensions.dart';
|
||||||
|
export 'board_side.dart';
|
||||||
export 'fire_effect.dart';
|
export 'fire_effect.dart';
|
||||||
|
export 'flipper.dart';
|
||||||
|
export 'flutter_sign_post.dart';
|
||||||
export 'initial_position.dart';
|
export 'initial_position.dart';
|
||||||
|
export 'joint_anchor.dart';
|
||||||
export 'layer.dart';
|
export 'layer.dart';
|
||||||
|
export 'ramp_opening.dart';
|
||||||
export 'shapes/shapes.dart';
|
export 'shapes/shapes.dart';
|
||||||
|
export 'spaceship.dart';
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
/// {@template flutter_sign_post}
|
||||||
|
/// A sign, found in the FlutterForest.
|
||||||
|
/// {@endtemplate}
|
||||||
|
// TODO(alestiago): Revisit doc comment if FlutterForest is moved to package.
|
||||||
|
class FlutterSignPost extends BodyComponent with InitialPosition {
|
||||||
|
Future<void> _loadSprite() async {
|
||||||
|
final sprite = await gameRef.loadSprite(
|
||||||
|
Assets.images.flutterSignPost.keyName,
|
||||||
|
);
|
||||||
|
final spriteComponent = SpriteComponent(
|
||||||
|
sprite: sprite,
|
||||||
|
size: sprite.originalSize / 10,
|
||||||
|
anchor: Anchor.bottomCenter,
|
||||||
|
position: Vector2(0.65, 0.45),
|
||||||
|
);
|
||||||
|
await add(spriteComponent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
paint = Paint()
|
||||||
|
..color = Colors.blue.withOpacity(0.5)
|
||||||
|
..style = PaintingStyle.fill;
|
||||||
|
await _loadSprite();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Body createBody() {
|
||||||
|
final shape = CircleShape()..radius = 0.25;
|
||||||
|
final fixtureDef = FixtureDef(shape);
|
||||||
|
final bodyDef = BodyDef()..position = initialPosition;
|
||||||
|
|
||||||
|
return world.createBody(bodyDef)..createFixture(fixtureDef);
|
||||||
|
}
|
||||||
|
}
|
@ -1,17 +1,16 @@
|
|||||||
// ignore_for_file: avoid_renaming_method_parameters
|
// ignore_for_file: avoid_renaming_method_parameters
|
||||||
|
|
||||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
import 'package:pinball/game/game.dart';
|
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
/// {@template ramp_orientation}
|
/// {@template ramp_orientation}
|
||||||
/// Determines if a ramp is facing [up] or [down] on the [Board].
|
/// Determines if a ramp is facing [up] or [down] on the Board.
|
||||||
/// {@endtemplate}
|
/// {@endtemplate}
|
||||||
enum RampOrientation {
|
enum RampOrientation {
|
||||||
/// Facing up on the [Board].
|
/// Facing up on the Board.
|
||||||
up,
|
up,
|
||||||
|
|
||||||
/// Facing down on the [Board].
|
/// Facing down on the Board.
|
||||||
down,
|
down,
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
|||||||
|
export 'blueprint.dart';
|
@ -1 +1,2 @@
|
|||||||
export 'components/components.dart';
|
export 'components/components.dart';
|
||||||
|
export 'flame/flame.dart';
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:sandbox/common/common.dart';
|
||||||
|
|
||||||
|
class BasicFlipperGame extends BasicGame {
|
||||||
|
static const info = '''
|
||||||
|
Basic example of how a Flipper works.
|
||||||
|
''';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
|
||||||
|
final center = screenToWorld(camera.viewport.canvasSize! / 2);
|
||||||
|
|
||||||
|
final leftFlipper = Flipper(side: BoardSide.left)
|
||||||
|
..initialPosition = center - Vector2(Flipper.size.x, 0);
|
||||||
|
final rightFlipper = Flipper(side: BoardSide.right)
|
||||||
|
..initialPosition = center + Vector2(Flipper.size.x, 0);
|
||||||
|
|
||||||
|
await addAll([
|
||||||
|
leftFlipper,
|
||||||
|
rightFlipper,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
import 'package:dashbook/dashbook.dart';
|
||||||
|
import 'package:flame/game.dart';
|
||||||
|
import 'package:sandbox/common/common.dart';
|
||||||
|
import 'package:sandbox/stories/flipper/basic.dart';
|
||||||
|
import 'package:sandbox/stories/flipper/tracing.dart';
|
||||||
|
|
||||||
|
void addFlipperStories(Dashbook dashbook) {
|
||||||
|
dashbook.storiesOf('Flipper')
|
||||||
|
..add(
|
||||||
|
'Basic',
|
||||||
|
(context) => GameWidget(
|
||||||
|
game: BasicFlipperGame(),
|
||||||
|
),
|
||||||
|
codeLink: buildSourceLink('flipper/basic.dart'),
|
||||||
|
info: BasicFlipperGame.info,
|
||||||
|
)
|
||||||
|
..add(
|
||||||
|
'Tracing',
|
||||||
|
(context) => GameWidget(
|
||||||
|
game: FlipperTracingGame(),
|
||||||
|
),
|
||||||
|
codeLink: buildSourceLink('flipper/tracing.dart'),
|
||||||
|
info: FlipperTracingGame.info,
|
||||||
|
);
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:sandbox/common/common.dart';
|
||||||
|
|
||||||
|
class FlipperTracingGame extends BasicGame {
|
||||||
|
static const info = '''
|
||||||
|
Basic example of how the Flipper body overlays the sprite.
|
||||||
|
''';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
final center = screenToWorld(camera.viewport.canvasSize! / 2);
|
||||||
|
|
||||||
|
final leftFlipper = Flipper(side: BoardSide.left)
|
||||||
|
..initialPosition = center - Vector2(Flipper.size.x, 0);
|
||||||
|
final rightFlipper = Flipper(side: BoardSide.right)
|
||||||
|
..initialPosition = center + Vector2(Flipper.size.x, 0);
|
||||||
|
|
||||||
|
await addAll([
|
||||||
|
leftFlipper,
|
||||||
|
rightFlipper,
|
||||||
|
]);
|
||||||
|
leftFlipper.trace();
|
||||||
|
rightFlipper.trace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension on BodyComponent {
|
||||||
|
void trace({Color color = Colors.red}) {
|
||||||
|
paint = Paint()..color = color;
|
||||||
|
renderBody = true;
|
||||||
|
body.joints.whereType<RevoluteJoint>().forEach(
|
||||||
|
(joint) => joint.setLimits(0, 0),
|
||||||
|
);
|
||||||
|
body.setType(BodyType.static);
|
||||||
|
|
||||||
|
unawaited(
|
||||||
|
mounted.whenComplete(() {
|
||||||
|
final sprite = children.whereType<SpriteComponent>().first;
|
||||||
|
sprite.paint.color = sprite.paint.color.withOpacity(0.5);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flame/input.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:sandbox/common/common.dart';
|
||||||
|
|
||||||
|
class BasicSpaceship extends BasicGame with TapDetector {
|
||||||
|
static String info = 'Renders a spaceship and allows balls to be '
|
||||||
|
'spawned upon click to test their interactions';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
|
||||||
|
camera.followVector2(Vector2.zero());
|
||||||
|
|
||||||
|
unawaited(
|
||||||
|
addFromBlueprint(Spaceship(position: Vector2.zero())),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onTapUp(TapUpInfo info) {
|
||||||
|
add(
|
||||||
|
Ball(baseColor: Colors.blue)
|
||||||
|
..initialPosition = info.eventPosition.game
|
||||||
|
..layer = Layer.jetpack,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
import 'package:dashbook/dashbook.dart';
|
||||||
|
import 'package:flame/game.dart';
|
||||||
|
import 'package:sandbox/common/common.dart';
|
||||||
|
import 'package:sandbox/stories/spaceship/basic.dart';
|
||||||
|
|
||||||
|
void addSpaceshipStories(Dashbook dashbook) {
|
||||||
|
dashbook.storiesOf('Spaceship').add(
|
||||||
|
'Basic',
|
||||||
|
(context) => GameWidget(game: BasicSpaceship()),
|
||||||
|
codeLink: buildSourceLink('spaceship/basic.dart'),
|
||||||
|
info: BasicSpaceship.info,
|
||||||
|
);
|
||||||
|
}
|
@ -1,2 +1,3 @@
|
|||||||
export 'ball/ball.dart';
|
export 'ball/ball.dart';
|
||||||
|
export 'flipper/flipper.dart';
|
||||||
export 'layer/layer.dart';
|
export 'layer/layer.dart';
|
||||||
|
@ -1,5 +1,26 @@
|
|||||||
import 'dart:ui';
|
import 'dart:ui';
|
||||||
|
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
import 'package:mocktail/mocktail.dart';
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
class MockCanvas extends Mock implements Canvas {}
|
class MockCanvas extends Mock implements Canvas {}
|
||||||
|
|
||||||
|
class MockFilter extends Mock implements Filter {}
|
||||||
|
|
||||||
|
class MockFixture extends Mock implements Fixture {}
|
||||||
|
|
||||||
|
class MockBody extends Mock implements Body {}
|
||||||
|
|
||||||
|
class MockBall extends Mock implements Ball {}
|
||||||
|
|
||||||
|
class MockGame extends Mock implements Forge2DGame {}
|
||||||
|
|
||||||
|
class MockSpaceshipEntrance extends Mock implements SpaceshipEntrance {}
|
||||||
|
|
||||||
|
class MockSpaceshipHole extends Mock implements SpaceshipHole {}
|
||||||
|
|
||||||
|
class MockContact extends Mock implements Contact {}
|
||||||
|
|
||||||
|
class MockContactCallback extends Mock
|
||||||
|
implements ContactCallback<Object, Object> {}
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
import 'package:flame/extensions.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('BoardDimensions', () {
|
||||||
|
test('has size', () {
|
||||||
|
expect(BoardDimensions.size, equals(Vector2(101.6, 143.8)));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('has bounds', () {
|
||||||
|
expect(BoardDimensions.bounds, isNotNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('has perspectiveAngle', () {
|
||||||
|
expect(BoardDimensions.perspectiveAngle, isNotNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('has perspectiveShrinkFactor', () {
|
||||||
|
expect(BoardDimensions.perspectiveShrinkFactor, equals(0.63));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('has shrinkAdjustedHeight', () {
|
||||||
|
expect(BoardDimensions.shrinkAdjustedHeight, isNotNull);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:pinball/game/game.dart';
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group(
|
group(
|
@ -0,0 +1,133 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
import '../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(TestGame.new);
|
||||||
|
|
||||||
|
group('Flipper', () {
|
||||||
|
// TODO(alestiago): Add golden tests.
|
||||||
|
// TODO(alestiago): Consider testing always both left and right Flipper.
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'loads correctly',
|
||||||
|
(game) async {
|
||||||
|
final leftFlipper = Flipper(side: BoardSide.left);
|
||||||
|
final rightFlipper = Flipper(side: BoardSide.right);
|
||||||
|
await game.ready();
|
||||||
|
await game.ensureAddAll([leftFlipper, rightFlipper]);
|
||||||
|
|
||||||
|
expect(game.contains(leftFlipper), isTrue);
|
||||||
|
expect(game.contains(rightFlipper), isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
group('constructor', () {
|
||||||
|
test('sets BoardSide', () {
|
||||||
|
final leftFlipper = Flipper(side: BoardSide.left);
|
||||||
|
expect(leftFlipper.side, equals(leftFlipper.side));
|
||||||
|
|
||||||
|
final rightFlipper = Flipper(side: BoardSide.right);
|
||||||
|
expect(rightFlipper.side, equals(rightFlipper.side));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('body', () {
|
||||||
|
flameTester.test(
|
||||||
|
'is dynamic',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
expect(flipper.body.bodyType, equals(BodyType.dynamic));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'ignores gravity',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
|
||||||
|
expect(flipper.body.gravityScale, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'has greater mass than Ball',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
final ball = Ball(baseColor: Colors.white);
|
||||||
|
|
||||||
|
await game.ready();
|
||||||
|
await game.ensureAddAll([flipper, ball]);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
flipper.body.getMassData().mass,
|
||||||
|
greaterThan(ball.body.getMassData().mass),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('fixtures', () {
|
||||||
|
flameTester.test(
|
||||||
|
'has three',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
|
||||||
|
expect(flipper.body.fixtures.length, equals(3));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'has density',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
|
||||||
|
final fixtures = flipper.body.fixtures;
|
||||||
|
final density = fixtures.fold<double>(
|
||||||
|
0,
|
||||||
|
(sum, fixture) => sum + fixture.density,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(density, greaterThan(0));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'moveDown applies downward velocity',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity, equals(Vector2.zero()));
|
||||||
|
flipper.moveDown();
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, lessThan(0));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'moveUp applies upward velocity',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity, equals(Vector2.zero()));
|
||||||
|
flipper.moveUp();
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, greaterThan(0));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
import '../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(TestGame.new);
|
||||||
|
|
||||||
|
group('FlutterSignPost', () {
|
||||||
|
flameTester.test(
|
||||||
|
'loads correctly',
|
||||||
|
(game) async {
|
||||||
|
final flutterSignPost = FlutterSignPost();
|
||||||
|
await game.ready();
|
||||||
|
await game.ensureAdd(flutterSignPost);
|
||||||
|
|
||||||
|
expect(game.contains(flutterSignPost), isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
After Width: | Height: | Size: 78 KiB |
@ -0,0 +1,66 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame/game.dart';
|
||||||
|
import 'package:flame/src/components/component.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pinball/flame/flame.dart';
|
||||||
|
|
||||||
|
class TestComponentController extends ComponentController {
|
||||||
|
TestComponentController(Component component) : super(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
class ControlledComponent extends Component
|
||||||
|
with Controls<TestComponentController> {
|
||||||
|
ControlledComponent() : super() {
|
||||||
|
controller = TestComponentController(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(FlameGame.new);
|
||||||
|
|
||||||
|
group('ComponentController', () {
|
||||||
|
flameTester.test(
|
||||||
|
'can be instantiated',
|
||||||
|
(game) async {
|
||||||
|
expect(
|
||||||
|
TestComponentController(Component()),
|
||||||
|
isA<ComponentController>(),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
flameTester.test(
|
||||||
|
'throws AssertionError when not attached to controlled component',
|
||||||
|
(game) async {
|
||||||
|
final component = Component();
|
||||||
|
final controller = TestComponentController(component);
|
||||||
|
|
||||||
|
final anotherComponet = Component();
|
||||||
|
await expectLater(
|
||||||
|
() async => await anotherComponet.add(controller),
|
||||||
|
throwsAssertionError,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('Controls', () {
|
||||||
|
flameTester.test(
|
||||||
|
'can be instantiated',
|
||||||
|
(game) async {
|
||||||
|
expect(ControlledComponent(), isA<Component>());
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test('adds controller', (game) async {
|
||||||
|
final component = ControlledComponent();
|
||||||
|
|
||||||
|
await game.add(component);
|
||||||
|
await game.ready();
|
||||||
|
|
||||||
|
expect(component.contains(component.controller), isTrue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -1,84 +0,0 @@
|
|||||||
// ignore_for_file: cascade_invocations
|
|
||||||
|
|
||||||
import 'package:bloc_test/bloc_test.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';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
TestWidgetsFlutterBinding.ensureInitialized();
|
|
||||||
|
|
||||||
group('Ball', () {
|
|
||||||
group('lost', () {
|
|
||||||
late GameBloc gameBloc;
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
gameBloc = MockGameBloc();
|
|
||||||
whenListen(
|
|
||||||
gameBloc,
|
|
||||||
const Stream<GameState>.empty(),
|
|
||||||
initialState: const GameState.initial(),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
final tester = flameBlocTester(gameBloc: () => gameBloc);
|
|
||||||
|
|
||||||
tester.widgetTest(
|
|
||||||
'adds BallLost to GameBloc',
|
|
||||||
(game, tester) async {
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
game.children.whereType<Ball>().first.controller.lost();
|
|
||||||
await tester.pump();
|
|
||||||
|
|
||||||
verify(() => gameBloc.add(const BallLost())).called(1);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
tester.widgetTest(
|
|
||||||
'resets the ball if the game is not over',
|
|
||||||
(game, tester) async {
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
game.children.whereType<Ball>().first.controller.lost();
|
|
||||||
await game.ready(); // Making sure that all additions are done
|
|
||||||
|
|
||||||
expect(
|
|
||||||
game.children.whereType<Ball>().length,
|
|
||||||
equals(1),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
tester.widgetTest(
|
|
||||||
'no ball is added on game over',
|
|
||||||
(game, tester) async {
|
|
||||||
whenListen(
|
|
||||||
gameBloc,
|
|
||||||
const Stream<GameState>.empty(),
|
|
||||||
initialState: const GameState(
|
|
||||||
score: 10,
|
|
||||||
balls: 1,
|
|
||||||
activatedBonusLetters: [],
|
|
||||||
activatedDashNests: {},
|
|
||||||
bonusHistory: [],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
game.children.whereType<Ball>().first.controller.lost();
|
|
||||||
await tester.pump();
|
|
||||||
|
|
||||||
expect(
|
|
||||||
game.children.whereType<Ball>().length,
|
|
||||||
equals(0),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
@ -0,0 +1,197 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:bloc_test/bloc_test.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter/painting.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';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(PinballGameTest.create);
|
||||||
|
|
||||||
|
group('BonusBallController', () {
|
||||||
|
late Ball ball;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
ball = Ball(baseColor: const Color(0xFF00FFFF));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can be instantiated', () {
|
||||||
|
expect(
|
||||||
|
BonusBallController(ball),
|
||||||
|
isA<BonusBallController>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'lost removes ball',
|
||||||
|
(game) async {
|
||||||
|
await game.add(ball);
|
||||||
|
final controller = BonusBallController(ball);
|
||||||
|
await ball.ensureAdd(controller);
|
||||||
|
|
||||||
|
controller.lost();
|
||||||
|
await game.ready();
|
||||||
|
|
||||||
|
expect(game.contains(ball), isFalse);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('LaunchedBallController', () {
|
||||||
|
test('can be instantiated', () {
|
||||||
|
expect(
|
||||||
|
LaunchedBallController(MockBall()),
|
||||||
|
isA<LaunchedBallController>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('description', () {
|
||||||
|
late Ball ball;
|
||||||
|
late GameBloc gameBloc;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
ball = Ball(baseColor: const Color(0xFF00FFFF));
|
||||||
|
gameBloc = MockGameBloc();
|
||||||
|
whenListen(
|
||||||
|
gameBloc,
|
||||||
|
const Stream<GameState>.empty(),
|
||||||
|
initialState: const GameState.initial(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
final tester = flameBlocTester<PinballGame>(
|
||||||
|
game: PinballGameTest.create,
|
||||||
|
gameBloc: () => gameBloc,
|
||||||
|
);
|
||||||
|
|
||||||
|
tester.testGameWidget(
|
||||||
|
'lost adds BallLost to GameBloc',
|
||||||
|
setUp: (game, tester) async {
|
||||||
|
final controller = LaunchedBallController(ball);
|
||||||
|
await ball.add(controller);
|
||||||
|
await game.ensureAdd(ball);
|
||||||
|
|
||||||
|
controller.lost();
|
||||||
|
},
|
||||||
|
verify: (game, tester) async {
|
||||||
|
verify(() => gameBloc.add(const BallLost())).called(1);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
group('listenWhen', () {
|
||||||
|
tester.testGameWidget(
|
||||||
|
'listens when a ball has been lost',
|
||||||
|
setUp: (game, tester) async {
|
||||||
|
final controller = LaunchedBallController(ball);
|
||||||
|
|
||||||
|
await ball.add(controller);
|
||||||
|
await game.ensureAdd(ball);
|
||||||
|
},
|
||||||
|
verify: (game, tester) async {
|
||||||
|
final controller =
|
||||||
|
game.descendants().whereType<LaunchedBallController>().first;
|
||||||
|
|
||||||
|
final previousState = MockGameState();
|
||||||
|
final newState = MockGameState();
|
||||||
|
when(() => previousState.balls).thenReturn(3);
|
||||||
|
when(() => newState.balls).thenReturn(2);
|
||||||
|
|
||||||
|
expect(controller.listenWhen(previousState, newState), isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
tester.testGameWidget(
|
||||||
|
'does not listen when a ball has not been lost',
|
||||||
|
setUp: (game, tester) async {
|
||||||
|
final controller = LaunchedBallController(ball);
|
||||||
|
|
||||||
|
await ball.add(controller);
|
||||||
|
await game.ensureAdd(ball);
|
||||||
|
},
|
||||||
|
verify: (game, tester) async {
|
||||||
|
final controller =
|
||||||
|
game.descendants().whereType<LaunchedBallController>().first;
|
||||||
|
|
||||||
|
final previousState = MockGameState();
|
||||||
|
final newState = MockGameState();
|
||||||
|
when(() => previousState.balls).thenReturn(3);
|
||||||
|
when(() => newState.balls).thenReturn(3);
|
||||||
|
|
||||||
|
expect(controller.listenWhen(previousState, newState), isFalse);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('onNewState', () {
|
||||||
|
tester.testGameWidget(
|
||||||
|
'removes ball',
|
||||||
|
setUp: (game, tester) async {
|
||||||
|
final controller = LaunchedBallController(ball);
|
||||||
|
await ball.add(controller);
|
||||||
|
await game.ensureAdd(ball);
|
||||||
|
|
||||||
|
final state = MockGameState();
|
||||||
|
when(() => state.balls).thenReturn(1);
|
||||||
|
controller.onNewState(state);
|
||||||
|
await game.ready();
|
||||||
|
},
|
||||||
|
verify: (game, tester) async {
|
||||||
|
expect(game.contains(ball), isFalse);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
tester.testGameWidget(
|
||||||
|
'spawns a new ball when the ball is not the last one',
|
||||||
|
setUp: (game, tester) async {
|
||||||
|
final controller = LaunchedBallController(ball);
|
||||||
|
await ball.add(controller);
|
||||||
|
await game.ensureAdd(ball);
|
||||||
|
|
||||||
|
final state = MockGameState();
|
||||||
|
when(() => state.balls).thenReturn(2);
|
||||||
|
|
||||||
|
final previousBalls = game.descendants().whereType<Ball>().toList();
|
||||||
|
controller.onNewState(state);
|
||||||
|
await game.ready();
|
||||||
|
|
||||||
|
final currentBalls = game.descendants().whereType<Ball>();
|
||||||
|
|
||||||
|
expect(currentBalls.contains(ball), isFalse);
|
||||||
|
expect(currentBalls.length, equals(previousBalls.length));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
tester.testGameWidget(
|
||||||
|
'does not spawn a new ball is the last one',
|
||||||
|
setUp: (game, tester) async {
|
||||||
|
final controller = LaunchedBallController(ball);
|
||||||
|
await ball.add(controller);
|
||||||
|
await game.ensureAdd(ball);
|
||||||
|
|
||||||
|
final state = MockGameState();
|
||||||
|
when(() => state.balls).thenReturn(1);
|
||||||
|
|
||||||
|
final previousBalls = game.descendants().whereType<Ball>().toList();
|
||||||
|
controller.onNewState(state);
|
||||||
|
await game.ready();
|
||||||
|
|
||||||
|
final currentBalls = game.descendants().whereType<Ball>();
|
||||||
|
|
||||||
|
expect(currentBalls.contains(ball), isFalse);
|
||||||
|
expect(
|
||||||
|
currentBalls.length,
|
||||||
|
equals((previousBalls..remove(ball)).length),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,169 @@
|
|||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
import '../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(PinballGameTest.create);
|
||||||
|
|
||||||
|
group('FlipperController', () {
|
||||||
|
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 {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isPositive);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyUpEvents(leftKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'moves downwards '
|
||||||
|
'when ${event.logicalKey.keyLabel} is released',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isNegative);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyUpEvents(rightKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'does nothing '
|
||||||
|
'when ${event.logicalKey.keyLabel} is released',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isZero);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyDownEvents(rightKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'does nothing '
|
||||||
|
'when ${event.logicalKey.keyLabel} is pressed',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
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 {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isPositive);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyUpEvents(rightKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'moves downwards '
|
||||||
|
'when ${event.logicalKey.keyLabel} is released',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isNegative);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyUpEvents(leftKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'does nothing '
|
||||||
|
'when ${event.logicalKey.keyLabel} is released',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isZero);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyDownEvents(leftKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'does nothing '
|
||||||
|
'when ${event.logicalKey.keyLabel} is pressed',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isZero);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -1,275 +0,0 @@
|
|||||||
// ignore_for_file: cascade_invocations
|
|
||||||
|
|
||||||
import 'dart:collection';
|
|
||||||
|
|
||||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
|
||||||
import 'package:flame_test/flame_test.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter/services.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:pinball/game/game.dart';
|
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
|
||||||
|
|
||||||
import '../../helpers/helpers.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
TestWidgetsFlutterBinding.ensureInitialized();
|
|
||||||
final flameTester = FlameTester(PinballGameTest.create);
|
|
||||||
|
|
||||||
group(
|
|
||||||
'Flipper',
|
|
||||||
() {
|
|
||||||
// TODO(alestiago): Add golden tests.
|
|
||||||
flameTester.test(
|
|
||||||
'loads correctly',
|
|
||||||
(game) async {
|
|
||||||
final leftFlipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
final rightFlipper = Flipper(
|
|
||||||
side: BoardSide.right,
|
|
||||||
);
|
|
||||||
await game.ready();
|
|
||||||
await game.ensureAddAll([leftFlipper, rightFlipper]);
|
|
||||||
|
|
||||||
expect(game.contains(leftFlipper), isTrue);
|
|
||||||
expect(game.contains(rightFlipper), isTrue);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
group('constructor', () {
|
|
||||||
test('sets BoardSide', () {
|
|
||||||
final leftFlipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(leftFlipper.side, equals(leftFlipper.side));
|
|
||||||
|
|
||||||
final rightFlipper = Flipper(
|
|
||||||
side: BoardSide.right,
|
|
||||||
);
|
|
||||||
expect(rightFlipper.side, equals(rightFlipper.side));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
group('body', () {
|
|
||||||
flameTester.test(
|
|
||||||
'is dynamic',
|
|
||||||
(game) async {
|
|
||||||
final flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
|
|
||||||
expect(flipper.body.bodyType, equals(BodyType.dynamic));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'ignores gravity',
|
|
||||||
(game) async {
|
|
||||||
final flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
|
|
||||||
expect(flipper.body.gravityScale, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'has greater mass than Ball',
|
|
||||||
(game) async {
|
|
||||||
final flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
final ball = Ball(baseColor: Colors.white);
|
|
||||||
|
|
||||||
await game.ready();
|
|
||||||
await game.ensureAddAll([flipper, ball]);
|
|
||||||
|
|
||||||
expect(
|
|
||||||
flipper.body.getMassData().mass,
|
|
||||||
greaterThan(ball.body.getMassData().mass),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
group('fixtures', () {
|
|
||||||
flameTester.test(
|
|
||||||
'has three',
|
|
||||||
(game) async {
|
|
||||||
final flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
|
|
||||||
expect(flipper.body.fixtures.length, equals(3));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'has density',
|
|
||||||
(game) async {
|
|
||||||
final flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
|
|
||||||
final fixtures = flipper.body.fixtures;
|
|
||||||
final density = fixtures.fold<double>(
|
|
||||||
0,
|
|
||||||
(sum, fixture) => sum + fixture.density,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(density, greaterThan(0));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
group('onKeyEvent', () {
|
|
||||||
final leftKeys = UnmodifiableListView([
|
|
||||||
LogicalKeyboardKey.arrowLeft,
|
|
||||||
LogicalKeyboardKey.keyA,
|
|
||||||
]);
|
|
||||||
final rightKeys = UnmodifiableListView([
|
|
||||||
LogicalKeyboardKey.arrowRight,
|
|
||||||
LogicalKeyboardKey.keyD,
|
|
||||||
]);
|
|
||||||
|
|
||||||
group('and Flipper is left', () {
|
|
||||||
late Flipper flipper;
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyDownEvents(leftKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'moves upwards '
|
|
||||||
'when ${event.logicalKey.keyLabel} is pressed',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isPositive);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyUpEvents(leftKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'moves downwards '
|
|
||||||
'when ${event.logicalKey.keyLabel} is released',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isNegative);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyUpEvents(rightKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'does nothing '
|
|
||||||
'when ${event.logicalKey.keyLabel} is released',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isZero);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyDownEvents(rightKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'does nothing '
|
|
||||||
'when ${event.logicalKey.keyLabel} is pressed',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isZero);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
group('and Flipper is right', () {
|
|
||||||
late Flipper flipper;
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
flipper = Flipper(
|
|
||||||
side: BoardSide.right,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyDownEvents(rightKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'moves upwards '
|
|
||||||
'when ${event.logicalKey.keyLabel} is pressed',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isPositive);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyUpEvents(rightKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'moves downwards '
|
|
||||||
'when ${event.logicalKey.keyLabel} is released',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isNegative);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyUpEvents(leftKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'does nothing '
|
|
||||||
'when ${event.logicalKey.keyLabel} is released',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isZero);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyDownEvents(leftKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'does nothing '
|
|
||||||
'when ${event.logicalKey.keyLabel} is pressed',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isZero);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
@ -0,0 +1,60 @@
|
|||||||
|
import 'package:flame_forge2d/flame_forge2d.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';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('SpaceshipExitRail', () {
|
||||||
|
late PinballGame game;
|
||||||
|
late SpaceshipExitRailEnd exitRailEnd;
|
||||||
|
late Ball ball;
|
||||||
|
late Body body;
|
||||||
|
late Fixture fixture;
|
||||||
|
late Filter filterData;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
game = MockPinballGame();
|
||||||
|
|
||||||
|
exitRailEnd = MockSpaceshipExitRailEnd();
|
||||||
|
|
||||||
|
ball = MockBall();
|
||||||
|
body = MockBody();
|
||||||
|
when(() => ball.gameRef).thenReturn(game);
|
||||||
|
when(() => ball.body).thenReturn(body);
|
||||||
|
|
||||||
|
fixture = MockFixture();
|
||||||
|
filterData = MockFilter();
|
||||||
|
when(() => body.fixtures).thenReturn([fixture]);
|
||||||
|
when(() => fixture.filterData).thenReturn(filterData);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('SpaceshipExitHoleBallContactCallback', () {
|
||||||
|
test('changes the ball priority on contact', () {
|
||||||
|
when(() => exitRailEnd.outsideLayer).thenReturn(Layer.board);
|
||||||
|
|
||||||
|
SpaceshipExitRailEndBallContactCallback().begin(
|
||||||
|
exitRailEnd,
|
||||||
|
ball,
|
||||||
|
MockContact(),
|
||||||
|
);
|
||||||
|
|
||||||
|
verify(() => ball.priority = 1).called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('reorders the game children', () {
|
||||||
|
when(() => exitRailEnd.outsideLayer).thenReturn(Layer.board);
|
||||||
|
|
||||||
|
SpaceshipExitRailEndBallContactCallback().begin(
|
||||||
|
exitRailEnd,
|
||||||
|
ball,
|
||||||
|
MockContact(),
|
||||||
|
);
|
||||||
|
|
||||||
|
verify(game.reorderChildren).called(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -1,44 +1,195 @@
|
|||||||
// ignore_for_file: prefer_const_constructors
|
// ignore_for_file: prefer_const_constructors
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:bloc_test/bloc_test.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:leaderboard_repository/leaderboard_repository.dart';
|
||||||
import 'package:mockingjay/mockingjay.dart';
|
import 'package:mockingjay/mockingjay.dart';
|
||||||
import 'package:pinball/game/game.dart';
|
import 'package:pinball/game/game.dart';
|
||||||
import 'package:pinball/l10n/l10n.dart';
|
import 'package:pinball/l10n/l10n.dart';
|
||||||
|
import 'package:pinball/leaderboard/leaderboard.dart';
|
||||||
import 'package:pinball_theme/pinball_theme.dart';
|
import 'package:pinball_theme/pinball_theme.dart';
|
||||||
|
|
||||||
import '../../../helpers/helpers.dart';
|
import '../../../helpers/helpers.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('GameOverDialog', () {
|
group('GameOverDialog', () {
|
||||||
testWidgets('renders correctly', (tester) async {
|
testWidgets('renders GameOverDialogView', (tester) async {
|
||||||
final l10n = await AppLocalizations.delegate.load(Locale('en'));
|
|
||||||
await tester.pumpApp(
|
await tester.pumpApp(
|
||||||
const GameOverDialog(
|
GameOverDialog(
|
||||||
|
score: 1000,
|
||||||
theme: DashTheme(),
|
theme: DashTheme(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(find.text(l10n.gameOver), findsOneWidget);
|
expect(find.byType(GameOverDialogView), findsOneWidget);
|
||||||
expect(find.text(l10n.leaderboard), findsOneWidget);
|
});
|
||||||
|
|
||||||
|
group('GameOverDialogView', () {
|
||||||
|
late LeaderboardBloc leaderboardBloc;
|
||||||
|
|
||||||
|
final leaderboard = [
|
||||||
|
LeaderboardEntry(
|
||||||
|
rank: '1',
|
||||||
|
playerInitials: 'ABC',
|
||||||
|
score: 5000,
|
||||||
|
character: DashTheme().characterAsset,
|
||||||
|
),
|
||||||
|
];
|
||||||
|
final entryData = LeaderboardEntryData(
|
||||||
|
playerInitials: 'VGV',
|
||||||
|
score: 10000,
|
||||||
|
character: CharacterType.dash,
|
||||||
|
);
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
leaderboardBloc = MockLeaderboardBloc();
|
||||||
|
whenListen(
|
||||||
|
leaderboardBloc,
|
||||||
|
const Stream<LeaderboardState>.empty(),
|
||||||
|
initialState: const LeaderboardState.initial(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('renders input text view when bloc emits [loading]',
|
||||||
|
(tester) async {
|
||||||
|
final l10n = await AppLocalizations.delegate.load(Locale('en'));
|
||||||
|
|
||||||
|
await tester.pumpApp(
|
||||||
|
BlocProvider.value(
|
||||||
|
value: leaderboardBloc,
|
||||||
|
child: GameOverDialogView(
|
||||||
|
score: entryData.score,
|
||||||
|
theme: entryData.character.toTheme,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(find.widgetWithText(TextButton, l10n.addUser), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('renders error view when bloc emits [error]', (tester) async {
|
||||||
|
final l10n = await AppLocalizations.delegate.load(Locale('en'));
|
||||||
|
|
||||||
|
whenListen(
|
||||||
|
leaderboardBloc,
|
||||||
|
const Stream<LeaderboardState>.empty(),
|
||||||
|
initialState: LeaderboardState.initial()
|
||||||
|
.copyWith(status: LeaderboardStatus.error),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.pumpApp(
|
||||||
|
BlocProvider.value(
|
||||||
|
value: leaderboardBloc,
|
||||||
|
child: GameOverDialogView(
|
||||||
|
score: entryData.score,
|
||||||
|
theme: entryData.character.toTheme,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(find.text(l10n.error), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('renders success view when bloc emits [success]',
|
||||||
|
(tester) async {
|
||||||
|
final l10n = await AppLocalizations.delegate.load(Locale('en'));
|
||||||
|
|
||||||
|
whenListen(
|
||||||
|
leaderboardBloc,
|
||||||
|
const Stream<LeaderboardState>.empty(),
|
||||||
|
initialState: LeaderboardState(
|
||||||
|
status: LeaderboardStatus.success,
|
||||||
|
ranking: LeaderboardRanking(ranking: 1, outOf: 2),
|
||||||
|
leaderboard: leaderboard,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.pumpApp(
|
||||||
|
BlocProvider.value(
|
||||||
|
value: leaderboardBloc,
|
||||||
|
child: GameOverDialogView(
|
||||||
|
score: entryData.score,
|
||||||
|
theme: entryData.character.toTheme,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
find.widgetWithText(TextButton, l10n.leaderboard),
|
||||||
|
findsOneWidget,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('adds LeaderboardEntryAdded when tap on add user button',
|
||||||
|
(tester) async {
|
||||||
|
final l10n = await AppLocalizations.delegate.load(Locale('en'));
|
||||||
|
|
||||||
|
whenListen(
|
||||||
|
leaderboardBloc,
|
||||||
|
const Stream<LeaderboardState>.empty(),
|
||||||
|
initialState: LeaderboardState.initial(),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.pumpApp(
|
||||||
|
BlocProvider.value(
|
||||||
|
value: leaderboardBloc,
|
||||||
|
child: GameOverDialogView(
|
||||||
|
score: entryData.score,
|
||||||
|
theme: entryData.character.toTheme,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.enterText(
|
||||||
|
find.byKey(const Key('player_initials_text_field')),
|
||||||
|
entryData.playerInitials,
|
||||||
|
);
|
||||||
|
|
||||||
|
final button = find.widgetWithText(TextButton, l10n.addUser);
|
||||||
|
await tester.ensureVisible(button);
|
||||||
|
await tester.tap(button);
|
||||||
|
|
||||||
|
verify(
|
||||||
|
() => leaderboardBloc.add(LeaderboardEntryAdded(entry: entryData)),
|
||||||
|
).called(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('tapping on leaderboard button navigates to LeaderBoardPage',
|
testWidgets('navigates to LeaderboardPage when tap on leaderboard button',
|
||||||
(tester) async {
|
(tester) async {
|
||||||
final l10n = await AppLocalizations.delegate.load(Locale('en'));
|
final l10n = await AppLocalizations.delegate.load(Locale('en'));
|
||||||
final navigator = MockNavigator();
|
final navigator = MockNavigator();
|
||||||
when(() => navigator.push<void>(any())).thenAnswer((_) async {});
|
when(() => navigator.push<void>(any())).thenAnswer((_) async {});
|
||||||
|
whenListen(
|
||||||
|
leaderboardBloc,
|
||||||
|
const Stream<LeaderboardState>.empty(),
|
||||||
|
initialState: LeaderboardState(
|
||||||
|
status: LeaderboardStatus.success,
|
||||||
|
ranking: LeaderboardRanking(ranking: 1, outOf: 2),
|
||||||
|
leaderboard: leaderboard,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
await tester.pumpApp(
|
await tester.pumpApp(
|
||||||
const GameOverDialog(
|
BlocProvider.value(
|
||||||
theme: DashTheme(),
|
value: leaderboardBloc,
|
||||||
|
child: GameOverDialogView(
|
||||||
|
score: entryData.score,
|
||||||
|
theme: entryData.character.toTheme,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
navigator: navigator,
|
navigator: navigator,
|
||||||
);
|
);
|
||||||
|
|
||||||
await tester.tap(find.widgetWithText(TextButton, l10n.leaderboard));
|
final button = find.widgetWithText(TextButton, l10n.leaderboard);
|
||||||
|
await tester.ensureVisible(button);
|
||||||
|
await tester.tap(button);
|
||||||
|
|
||||||
verify(() => navigator.push<void>(any())).called(1);
|
verify(() => navigator.push<void>(any())).called(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
|
||||||
|
class FakeContact extends Fake implements Contact {}
|
||||||
|
|
||||||
|
class FakeGameEvent extends Fake implements GameEvent {}
|
@ -0,0 +1,8 @@
|
|||||||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
|
||||||
|
class TestGame extends Forge2DGame with FlameBloc {
|
||||||
|
TestGame() {
|
||||||
|
images.prefix = '';
|
||||||
|
}
|
||||||
|
}
|