After Width: | Height: | Size: 9.5 MiB |
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,168 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart' hide Timer;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
/// {@template chrome_dino}
|
||||
/// Dinosaur that gobbles up a [Ball], swivel his head around, and shoots it
|
||||
/// back out.
|
||||
/// {@endtemplate}
|
||||
class ChromeDino extends BodyComponent with InitialPosition {
|
||||
/// {@macro chrome_dino}
|
||||
ChromeDino() {
|
||||
// TODO(alestiago): Remove once sprites are defined.
|
||||
paint = Paint()..color = Colors.blue;
|
||||
}
|
||||
|
||||
/// The size of the dinosaur mouth.
|
||||
static final size = Vector2(5, 2.5);
|
||||
|
||||
/// Anchors the [ChromeDino] to the [RevoluteJoint] that controls its arc
|
||||
/// motion.
|
||||
Future<_ChromeDinoJoint> _anchorToJoint() async {
|
||||
final anchor = _ChromeDinoAnchor(chromeDino: this);
|
||||
await add(anchor);
|
||||
|
||||
final jointDef = _ChromeDinoAnchorRevoluteJointDef(
|
||||
chromeDino: this,
|
||||
anchor: anchor,
|
||||
);
|
||||
final joint = _ChromeDinoJoint(jointDef);
|
||||
world.createJoint2(joint);
|
||||
|
||||
return joint;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
final joint = await _anchorToJoint();
|
||||
await add(
|
||||
TimerComponent(
|
||||
period: 1,
|
||||
onTick: joint.swivel,
|
||||
repeat: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<FixtureDef> _createFixtureDefs() {
|
||||
final fixtureDefs = <FixtureDef>[];
|
||||
|
||||
// TODO(alestiago): Subject to change when sprites are added.
|
||||
final box = PolygonShape()..setAsBoxXY(size.x / 2, size.y / 2);
|
||||
final fixtureDef = FixtureDef(box)
|
||||
..shape = box
|
||||
..density = 999
|
||||
..friction = 0.3
|
||||
..restitution = 0.1
|
||||
..isSensor = true;
|
||||
fixtureDefs.add(fixtureDef);
|
||||
|
||||
// FIXME(alestiago): Investigate why adding these fixtures is considered as
|
||||
// an invalid contact type.
|
||||
// final upperEdge = EdgeShape()
|
||||
// ..set(
|
||||
// Vector2(-size.x / 2, -size.y / 2),
|
||||
// Vector2(size.x / 2, -size.y / 2),
|
||||
// );
|
||||
// final upperEdgeDef = FixtureDef(upperEdge)..density = 0.5;
|
||||
// fixtureDefs.add(upperEdgeDef);
|
||||
|
||||
// final lowerEdge = EdgeShape()
|
||||
// ..set(
|
||||
// Vector2(-size.x / 2, size.y / 2),
|
||||
// Vector2(size.x / 2, size.y / 2),
|
||||
// );
|
||||
// final lowerEdgeDef = FixtureDef(lowerEdge)..density = 0.5;
|
||||
// fixtureDefs.add(lowerEdgeDef);
|
||||
|
||||
// final rightEdge = EdgeShape()
|
||||
// ..set(
|
||||
// Vector2(size.x / 2, -size.y / 2),
|
||||
// Vector2(size.x / 2, size.y / 2),
|
||||
// );
|
||||
// final rightEdgeDef = FixtureDef(rightEdge)..density = 0.5;
|
||||
// fixtureDefs.add(rightEdgeDef);
|
||||
|
||||
return fixtureDefs;
|
||||
}
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
final bodyDef = BodyDef()
|
||||
..gravityScale = 0
|
||||
..position = initialPosition
|
||||
..type = BodyType.dynamic;
|
||||
|
||||
final body = world.createBody(bodyDef);
|
||||
_createFixtureDefs().forEach(body.createFixture);
|
||||
|
||||
return body;
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template flipper_anchor}
|
||||
/// [JointAnchor] positioned at the end of a [ChromeDino].
|
||||
/// {@endtemplate}
|
||||
class _ChromeDinoAnchor extends JointAnchor {
|
||||
/// {@macro flipper_anchor}
|
||||
_ChromeDinoAnchor({
|
||||
required ChromeDino chromeDino,
|
||||
}) {
|
||||
initialPosition = Vector2(
|
||||
chromeDino.body.position.x + ChromeDino.size.x / 2,
|
||||
chromeDino.body.position.y,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template chrome_dino_anchor_revolute_joint_def}
|
||||
/// Hinges a [ChromeDino] to a [_ChromeDinoAnchor].
|
||||
/// {@endtemplate}
|
||||
class _ChromeDinoAnchorRevoluteJointDef extends RevoluteJointDef {
|
||||
/// {@macro chrome_dino_anchor_revolute_joint_def}
|
||||
_ChromeDinoAnchorRevoluteJointDef({
|
||||
required ChromeDino chromeDino,
|
||||
required _ChromeDinoAnchor anchor,
|
||||
}) {
|
||||
initialize(
|
||||
chromeDino.body,
|
||||
anchor.body,
|
||||
anchor.body.position,
|
||||
);
|
||||
enableLimit = true;
|
||||
// TODO(alestiago): Apply design angle value.
|
||||
const angle = math.pi / 3.5;
|
||||
lowerAngle = -angle / 2;
|
||||
upperAngle = angle / 2;
|
||||
|
||||
enableMotor = true;
|
||||
// TODO(alestiago): Tune this values.
|
||||
maxMotorTorque = motorSpeed = chromeDino.body.mass * 30;
|
||||
}
|
||||
}
|
||||
|
||||
class _ChromeDinoJoint extends RevoluteJoint {
|
||||
_ChromeDinoJoint(_ChromeDinoAnchorRevoluteJointDef def) : super(def);
|
||||
|
||||
/// Sweeps the [ChromeDino] up and down repeatedly.
|
||||
void swivel() {
|
||||
setMotorSpeed(-motorSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
extension on World {
|
||||
// TODO(alestiago): Remove once Forge2D supports custom joints.
|
||||
void createJoint2(Joint joint) {
|
||||
assert(!isLocked, '');
|
||||
|
||||
joints.add(joint);
|
||||
|
||||
joint.bodyA.joints.add(joint);
|
||||
joint.bodyB.joints.add(joint);
|
||||
}
|
||||
}
|
@ -1,16 +1,14 @@
|
||||
export 'ball.dart';
|
||||
export 'baseboard.dart';
|
||||
export 'board.dart';
|
||||
export 'board_side.dart';
|
||||
export 'bonus_word.dart';
|
||||
export 'flipper.dart';
|
||||
export 'chrome_dino.dart';
|
||||
export 'flipper_controller.dart';
|
||||
export 'flutter_forest.dart';
|
||||
export 'jetpack_ramp.dart';
|
||||
export 'joint_anchor.dart';
|
||||
export 'kicker.dart';
|
||||
export 'launcher_ramp.dart';
|
||||
export 'plunger.dart';
|
||||
export 'ramp_opening.dart';
|
||||
export 'score_points.dart';
|
||||
export 'spaceship.dart';
|
||||
export 'spaceship_exit_rail.dart';
|
||||
export 'wall.dart';
|
||||
|
@ -0,0 +1,52 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
/// {@template flipper_controller}
|
||||
/// A [Component] that controls the [Flipper]s movement.
|
||||
/// {@endtemplate}
|
||||
class FlipperController extends Component with KeyboardHandler {
|
||||
/// {@macro flipper_controller}
|
||||
FlipperController(this.flipper) : _keys = flipper.side.flipperKeys;
|
||||
|
||||
/// The [Flipper] this controller is controlling.
|
||||
final Flipper 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) {
|
||||
flipper.moveUp();
|
||||
} else if (event is RawKeyUpEvent) {
|
||||
flipper.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;
|
||||
}
|
||||
}
|
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 |
@ -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.
|
||||
///
|
@ -1,5 +1,11 @@
|
||||
export 'ball.dart';
|
||||
export 'board_side.dart';
|
||||
export 'fire_effect.dart';
|
||||
export 'flipper.dart';
|
||||
export 'flutter_sign_post.dart';
|
||||
export 'initial_position.dart';
|
||||
export 'joint_anchor.dart';
|
||||
export 'layer.dart';
|
||||
export 'ramp_opening.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
|
||||
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
/// {@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}
|
||||
enum RampOrientation {
|
||||
/// Facing up on the [Board].
|
||||
/// Facing up on the Board.
|
||||
up,
|
||||
|
||||
/// Facing down on the [Board].
|
||||
/// Facing down on the Board.
|
||||
down,
|
||||
}
|
||||
|
@ -0,0 +1,2 @@
|
||||
export 'blueprint.dart';
|
||||
export 'priority.dart';
|
@ -1 +1,2 @@
|
||||
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 'flipper/flipper.dart';
|
||||
export 'layer/layer.dart';
|
||||
|
@ -1,5 +1,26 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
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> {}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
void main() {
|
||||
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,102 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_forge2d/flame_forge2d.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 '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
group('Spaceship', () {
|
||||
late Filter filterData;
|
||||
late Fixture fixture;
|
||||
late Body body;
|
||||
late Ball ball;
|
||||
late SpaceshipEntrance entrance;
|
||||
late SpaceshipHole hole;
|
||||
late Forge2DGame game;
|
||||
|
||||
setUp(() {
|
||||
filterData = MockFilter();
|
||||
|
||||
fixture = MockFixture();
|
||||
when(() => fixture.filterData).thenReturn(filterData);
|
||||
|
||||
body = MockBody();
|
||||
when(() => body.fixtures).thenReturn([fixture]);
|
||||
|
||||
game = MockGame();
|
||||
|
||||
ball = MockBall();
|
||||
when(() => ball.gameRef).thenReturn(game);
|
||||
when(() => ball.body).thenReturn(body);
|
||||
|
||||
entrance = MockSpaceshipEntrance();
|
||||
hole = MockSpaceshipHole();
|
||||
});
|
||||
|
||||
group('Spaceship', () {
|
||||
final tester = FlameTester(TestGame.new);
|
||||
|
||||
tester.testGameWidget(
|
||||
'renders correctly',
|
||||
setUp: (game, tester) async {
|
||||
await game.addFromBlueprint(Spaceship(position: Vector2(30, -30)));
|
||||
await game.ready();
|
||||
await tester.pump();
|
||||
},
|
||||
verify: (game, tester) async {
|
||||
await expectLater(
|
||||
find.byGame<Forge2DGame>(),
|
||||
matchesGoldenFile('golden/spaceship.png'),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('SpaceshipEntranceBallContactCallback', () {
|
||||
test('changes the ball priority on contact', () {
|
||||
when(() => ball.priority).thenReturn(2);
|
||||
when(() => entrance.priority).thenReturn(3);
|
||||
|
||||
SpaceshipEntranceBallContactCallback().begin(
|
||||
entrance,
|
||||
ball,
|
||||
MockContact(),
|
||||
);
|
||||
|
||||
verify(() => ball.showInFrontOf(entrance)).called(1);
|
||||
});
|
||||
});
|
||||
|
||||
group('SpaceshipHoleBallContactCallback', () {
|
||||
test('changes the ball priority on contact', () {
|
||||
when(() => hole.outsideLayer).thenReturn(Layer.board);
|
||||
when(() => hole.onExitElevation).thenReturn(1);
|
||||
|
||||
SpaceshipHoleBallContactCallback().begin(
|
||||
hole,
|
||||
ball,
|
||||
MockContact(),
|
||||
);
|
||||
|
||||
verify(() => ball.priority = hole.onExitElevation).called(1);
|
||||
});
|
||||
|
||||
test('re order the game children', () {
|
||||
when(() => hole.outsideLayer).thenReturn(Layer.board);
|
||||
when(() => hole.onExitElevation).thenReturn(1);
|
||||
|
||||
SpaceshipHoleBallContactCallback().begin(
|
||||
hole,
|
||||
ball,
|
||||
MockContact(),
|
||||
);
|
||||
|
||||
verify(() => ball.sendToBack()).called(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(Forge2DGame.new);
|
||||
|
||||
group('ChromeDino', () {
|
||||
flameTester.test(
|
||||
'loads correctly',
|
||||
(game) async {
|
||||
final chromeDino = ChromeDino();
|
||||
|
||||
await game.ready();
|
||||
await game.ensureAdd(chromeDino);
|
||||
|
||||
expect(game.contains(chromeDino), isTrue);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
@ -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);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
@ -1,67 +1,59 @@
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball/flame/priority.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
group('Spaceship', () {
|
||||
late Filter filterData;
|
||||
late Fixture fixture;
|
||||
late Body body;
|
||||
group('SpaceshipExitRail', () {
|
||||
late PinballGame game;
|
||||
late SpaceshipExitRailEnd exitRailEnd;
|
||||
late Ball ball;
|
||||
late SpaceshipEntrance entrance;
|
||||
late SpaceshipHole hole;
|
||||
late Body body;
|
||||
late Fixture fixture;
|
||||
late Filter filterData;
|
||||
|
||||
setUp(() {
|
||||
filterData = MockFilter();
|
||||
|
||||
fixture = MockFixture();
|
||||
when(() => fixture.filterData).thenReturn(filterData);
|
||||
|
||||
body = MockBody();
|
||||
when(() => body.fixtures).thenReturn([fixture]);
|
||||
|
||||
game = MockPinballGame();
|
||||
|
||||
exitRailEnd = MockSpaceshipExitRailEnd();
|
||||
|
||||
ball = MockBall();
|
||||
body = MockBody();
|
||||
when(() => ball.gameRef).thenReturn(game);
|
||||
when(() => ball.body).thenReturn(body);
|
||||
|
||||
entrance = MockSpaceshipEntrance();
|
||||
hole = MockSpaceshipHole();
|
||||
fixture = MockFixture();
|
||||
filterData = MockFilter();
|
||||
when(() => body.fixtures).thenReturn([fixture]);
|
||||
when(() => fixture.filterData).thenReturn(filterData);
|
||||
});
|
||||
|
||||
group('SpaceshipEntranceBallContactCallback', () {
|
||||
group('SpaceshipExitHoleBallContactCallback', () {
|
||||
test('changes the ball priority on contact', () {
|
||||
when(() => ball.priority).thenReturn(1);
|
||||
when(() => entrance.priority).thenReturn(2);
|
||||
when(() => exitRailEnd.outsideLayer).thenReturn(Layer.board);
|
||||
|
||||
SpaceshipEntranceBallContactCallback().begin(
|
||||
entrance,
|
||||
SpaceshipExitRailEndBallContactCallback().begin(
|
||||
exitRailEnd,
|
||||
ball,
|
||||
MockContact(),
|
||||
);
|
||||
|
||||
verify(() => ball.showInFrontOf(entrance)).called(1);
|
||||
});
|
||||
verify(() => ball.priority = 1).called(1);
|
||||
});
|
||||
|
||||
group('SpaceshipHoleBallContactCallback', () {
|
||||
test('changes the ball priority on contact', () {
|
||||
when(() => ball.priority).thenReturn(1);
|
||||
test('reorders the game children', () {
|
||||
when(() => exitRailEnd.outsideLayer).thenReturn(Layer.board);
|
||||
|
||||
SpaceshipHoleBallContactCallback().begin(
|
||||
hole,
|
||||
SpaceshipExitRailEndBallContactCallback().begin(
|
||||
exitRailEnd,
|
||||
ball,
|
||||
MockContact(),
|
||||
);
|
||||
|
||||
verify(() => ball.sendToBack()).called(1);
|
||||
verify(game.reorderChildren).called(1);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,44 +1,195 @@
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:leaderboard_repository/leaderboard_repository.dart';
|
||||
import 'package:mockingjay/mockingjay.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball/l10n/l10n.dart';
|
||||
import 'package:pinball/leaderboard/leaderboard.dart';
|
||||
import 'package:pinball_theme/pinball_theme.dart';
|
||||
|
||||
import '../../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
group('GameOverDialog', () {
|
||||
testWidgets('renders correctly', (tester) async {
|
||||
final l10n = await AppLocalizations.delegate.load(Locale('en'));
|
||||
testWidgets('renders GameOverDialogView', (tester) async {
|
||||
await tester.pumpApp(
|
||||
const GameOverDialog(
|
||||
GameOverDialog(
|
||||
score: 1000,
|
||||
theme: DashTheme(),
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.text(l10n.gameOver), findsOneWidget);
|
||||
expect(find.text(l10n.leaderboard), findsOneWidget);
|
||||
expect(find.byType(GameOverDialogView), 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 {
|
||||
final l10n = await AppLocalizations.delegate.load(Locale('en'));
|
||||
final navigator = MockNavigator();
|
||||
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(
|
||||
const GameOverDialog(
|
||||
theme: DashTheme(),
|
||||
BlocProvider.value(
|
||||
value: leaderboardBloc,
|
||||
child: GameOverDialogView(
|
||||
score: entryData.score,
|
||||
theme: entryData.character.toTheme,
|
||||
),
|
||||
),
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|