mirror of https://github.com/flutter/pinball.git
commit
11d1086787
@ -0,0 +1,87 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
const _attachedErrorMessage = "Can't add to attached Blueprints";
|
||||
|
||||
// TODO(erickzanardo): Keeping this inside our code base
|
||||
// so we can experiment with the idea, but this is a
|
||||
// potential upstream change on Flame.
|
||||
|
||||
/// A [Blueprint] is a virtual way of grouping [Component]s
|
||||
/// that are related, but they need to be added directly on
|
||||
/// the [FlameGame] level.
|
||||
abstract class Blueprint {
|
||||
final List<Component> _components = [];
|
||||
bool _isAttached = false;
|
||||
|
||||
/// Called before the the [Component]s managed
|
||||
/// by this blueprint is added to the [FlameGame]
|
||||
void build();
|
||||
|
||||
/// Attach the [Component]s built on [build] to the [game]
|
||||
/// instance
|
||||
@mustCallSuper
|
||||
Future<void> attach(FlameGame game) async {
|
||||
build();
|
||||
await game.addAll(_components);
|
||||
_isAttached = true;
|
||||
}
|
||||
|
||||
/// Adds a list of [Component]s to this blueprint.
|
||||
void addAll(List<Component> components) {
|
||||
assert(!_isAttached, _attachedErrorMessage);
|
||||
_components.addAll(components);
|
||||
}
|
||||
|
||||
/// Adds a single [Component] to this blueprint.
|
||||
void add(Component component) {
|
||||
assert(!_isAttached, _attachedErrorMessage);
|
||||
_components.add(component);
|
||||
}
|
||||
|
||||
/// Returns a copy of the components built by this blueprint
|
||||
List<Component> get components => List.unmodifiable(_components);
|
||||
}
|
||||
|
||||
/// A [Blueprint] that provides additional
|
||||
/// structures specific to flame_forge2d
|
||||
abstract class Forge2DBlueprint extends Blueprint {
|
||||
final List<ContactCallback> _callbacks = [];
|
||||
|
||||
/// Adds a single [ContactCallback] to this blueprint
|
||||
void addContactCallback(ContactCallback callback) {
|
||||
assert(!_isAttached, _attachedErrorMessage);
|
||||
_callbacks.add(callback);
|
||||
}
|
||||
|
||||
/// Adds a collection of [ContactCallback]s to this blueprint
|
||||
void addAllContactCallback(List<ContactCallback> callbacks) {
|
||||
assert(!_isAttached, _attachedErrorMessage);
|
||||
_callbacks.addAll(callbacks);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> attach(FlameGame game) async {
|
||||
await super.attach(game);
|
||||
|
||||
assert(game is Forge2DGame, 'Forge2DBlueprint used outside a Forge2DGame');
|
||||
|
||||
for (final callback in _callbacks) {
|
||||
(game as Forge2DGame).addContactCallback(callback);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a copy of the callbacks built by this blueprint
|
||||
List<ContactCallback> get callbacks => List.unmodifiable(_callbacks);
|
||||
}
|
||||
|
||||
/// Adds helper methods regardin [Blueprint]s to [FlameGame]
|
||||
extension FlameGameBlueprint on FlameGame {
|
||||
/// Shortcut to attach a [Blueprint] instance to this game
|
||||
/// equivalent to `MyBluepinrt().attach(game)`
|
||||
Future<void> addFromBlueprint(Blueprint blueprint) async {
|
||||
await blueprint.attach(this);
|
||||
}
|
||||
}
|
@ -0,0 +1,142 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:geometry/geometry.dart' as geometry show centroid;
|
||||
import 'package:pinball/game/game.dart';
|
||||
|
||||
/// {@template kicker}
|
||||
/// Triangular [BodyType.static] body that propels the [Ball] towards the
|
||||
/// opposite side.
|
||||
///
|
||||
/// [Kicker]s are usually positioned above each [Flipper].
|
||||
/// {@endtemplate kicker}
|
||||
class Kicker extends BodyComponent with InitialPosition {
|
||||
/// {@macro kicker}
|
||||
Kicker({
|
||||
required BoardSide side,
|
||||
}) : _side = side {
|
||||
// TODO(alestiago): Use sprite instead of color when provided.
|
||||
paint = Paint()
|
||||
..color = const Color(0xFF00FF00)
|
||||
..style = PaintingStyle.fill;
|
||||
}
|
||||
|
||||
/// Whether the [Kicker] is on the left or right side of the board.
|
||||
///
|
||||
/// A [Kicker] with [BoardSide.left] propels the [Ball] to the right,
|
||||
/// whereas a [Kicker] with [BoardSide.right] propels the [Ball] to the
|
||||
/// left.
|
||||
final BoardSide _side;
|
||||
|
||||
/// The size of the [Kicker] body.
|
||||
// TODO(alestiago): Use size from PositionedBodyComponent instead,
|
||||
// once a sprite is given.
|
||||
static final Vector2 size = Vector2(4, 10);
|
||||
|
||||
List<FixtureDef> _createFixtureDefs() {
|
||||
final fixturesDefs = <FixtureDef>[];
|
||||
final direction = _side.direction;
|
||||
const quarterPi = math.pi / 4;
|
||||
|
||||
final upperCircle = CircleShape()..radius = 1.45;
|
||||
upperCircle.position.setValues(0, -upperCircle.radius / 2);
|
||||
final upperCircleFixtureDef = FixtureDef(upperCircle)..friction = 0;
|
||||
fixturesDefs.add(upperCircleFixtureDef);
|
||||
|
||||
final lowerCircle = CircleShape()..radius = 1.45;
|
||||
lowerCircle.position.setValues(
|
||||
size.x * -direction,
|
||||
-size.y,
|
||||
);
|
||||
final lowerCircleFixtureDef = FixtureDef(lowerCircle)..friction = 0;
|
||||
fixturesDefs.add(lowerCircleFixtureDef);
|
||||
|
||||
final wallFacingEdge = EdgeShape()
|
||||
..set(
|
||||
upperCircle.position +
|
||||
Vector2(
|
||||
upperCircle.radius * direction,
|
||||
0,
|
||||
),
|
||||
// TODO(alestiago): Use values from design.
|
||||
Vector2(2.0 * direction, -size.y + 2),
|
||||
);
|
||||
final wallFacingLineFixtureDef = FixtureDef(wallFacingEdge)..friction = 0;
|
||||
fixturesDefs.add(wallFacingLineFixtureDef);
|
||||
|
||||
final bottomEdge = EdgeShape()
|
||||
..set(
|
||||
wallFacingEdge.vertex2,
|
||||
lowerCircle.position +
|
||||
Vector2(
|
||||
lowerCircle.radius * math.cos(quarterPi) * direction,
|
||||
-lowerCircle.radius * math.sin(quarterPi),
|
||||
),
|
||||
);
|
||||
final bottomLineFixtureDef = FixtureDef(bottomEdge)..friction = 0;
|
||||
fixturesDefs.add(bottomLineFixtureDef);
|
||||
|
||||
final bouncyEdge = EdgeShape()
|
||||
..set(
|
||||
upperCircle.position +
|
||||
Vector2(
|
||||
upperCircle.radius * math.cos(quarterPi) * -direction,
|
||||
upperCircle.radius * math.sin(quarterPi),
|
||||
),
|
||||
lowerCircle.position +
|
||||
Vector2(
|
||||
lowerCircle.radius * math.cos(quarterPi) * -direction,
|
||||
lowerCircle.radius * math.sin(quarterPi),
|
||||
),
|
||||
);
|
||||
|
||||
final bouncyFixtureDef = FixtureDef(bouncyEdge)
|
||||
// TODO(alestiago): Play with restitution value once game is bundled.
|
||||
..restitution = 10.0
|
||||
..friction = 0;
|
||||
fixturesDefs.add(bouncyFixtureDef);
|
||||
|
||||
// TODO(alestiago): Evaluate if there is value on centering the fixtures.
|
||||
final centroid = geometry.centroid(
|
||||
[
|
||||
upperCircle.position + Vector2(0, -upperCircle.radius),
|
||||
lowerCircle.position +
|
||||
Vector2(
|
||||
lowerCircle.radius * math.cos(quarterPi) * -direction,
|
||||
-lowerCircle.radius * math.sin(quarterPi),
|
||||
),
|
||||
wallFacingEdge.vertex2,
|
||||
],
|
||||
);
|
||||
for (final fixtureDef in fixturesDefs) {
|
||||
fixtureDef.shape.moveBy(-centroid);
|
||||
}
|
||||
|
||||
return fixturesDefs;
|
||||
}
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
final bodyDef = BodyDef()..position = initialPosition;
|
||||
final body = world.createBody(bodyDef);
|
||||
_createFixtureDefs().forEach(body.createFixture);
|
||||
|
||||
return body;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(alestiago): Evaluate if there's value on generalising this to
|
||||
// all shapes.
|
||||
extension on Shape {
|
||||
void moveBy(Vector2 offset) {
|
||||
if (this is CircleShape) {
|
||||
final circle = this as CircleShape;
|
||||
circle.position.setFrom(circle.position + offset);
|
||||
} else if (this is EdgeShape) {
|
||||
final edge = this as EdgeShape;
|
||||
edge.set(edge.vertex1 + offset, edge.vertex2 + offset);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:geometry/geometry.dart' show centroid;
|
||||
import 'package:pinball/game/game.dart';
|
||||
|
||||
/// {@template sling_shot}
|
||||
/// Triangular [BodyType.static] body that propels the [Ball] towards the
|
||||
/// opposite side.
|
||||
///
|
||||
/// [SlingShot]s are usually positioned above each [Flipper].
|
||||
/// {@endtemplate sling_shot}
|
||||
class SlingShot extends BodyComponent with InitialPosition {
|
||||
/// {@macro sling_shot}
|
||||
SlingShot({
|
||||
required BoardSide side,
|
||||
}) : _side = side {
|
||||
// TODO(alestiago): Use sprite instead of color when provided.
|
||||
paint = Paint()
|
||||
..color = const Color(0xFF00FF00)
|
||||
..style = PaintingStyle.fill;
|
||||
}
|
||||
|
||||
/// Whether the [SlingShot] is on the left or right side of the board.
|
||||
///
|
||||
/// A [SlingShot] with [BoardSide.left] propels the [Ball] to the right,
|
||||
/// whereas a [SlingShot] with [BoardSide.right] propels the [Ball] to the
|
||||
/// left.
|
||||
final BoardSide _side;
|
||||
|
||||
/// The size of the [SlingShot] body.
|
||||
// TODO(alestiago): Use size from PositionedBodyComponent instead,
|
||||
// once a sprite is given.
|
||||
static final Vector2 size = Vector2(6, 8);
|
||||
|
||||
List<FixtureDef> _createFixtureDefs() {
|
||||
final fixturesDef = <FixtureDef>[];
|
||||
|
||||
// TODO(alestiago): This magic number can be deduced by specifying the
|
||||
// angle and using polar coordinate system to place the bottom right
|
||||
// vertex.
|
||||
// Something as: y = -size.y * math.cos(angle)
|
||||
const additionalIncrement = 3;
|
||||
final triangleVertices = _side.isLeft
|
||||
? [
|
||||
Vector2(0, 0),
|
||||
Vector2(0, -size.y),
|
||||
Vector2(
|
||||
size.x,
|
||||
-size.y - additionalIncrement,
|
||||
),
|
||||
]
|
||||
: [
|
||||
Vector2(size.x, 0),
|
||||
Vector2(size.x, -size.y),
|
||||
Vector2(
|
||||
0,
|
||||
-size.y - additionalIncrement,
|
||||
),
|
||||
];
|
||||
final triangleCentroid = centroid(triangleVertices);
|
||||
for (final vertex in triangleVertices) {
|
||||
vertex.setFrom(vertex - triangleCentroid);
|
||||
}
|
||||
|
||||
final triangle = PolygonShape()..set(triangleVertices);
|
||||
final triangleFixtureDef = FixtureDef(triangle)..friction = 0;
|
||||
fixturesDef.add(triangleFixtureDef);
|
||||
|
||||
final kicker = EdgeShape()
|
||||
..set(
|
||||
triangleVertices.first,
|
||||
triangleVertices.last,
|
||||
);
|
||||
// TODO(alestiago): Play with restitution value once game is bundled.
|
||||
final kickerFixtureDef = FixtureDef(kicker)
|
||||
..restitution = 10.0
|
||||
..friction = 0;
|
||||
fixturesDef.add(kickerFixtureDef);
|
||||
|
||||
return fixturesDef;
|
||||
}
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
final bodyDef = BodyDef()..position = initialPosition;
|
||||
final body = world.createBody(bodyDef);
|
||||
_createFixtureDefs().forEach(body.createFixture);
|
||||
|
||||
return body;
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:leaderboard_repository/leaderboard_repository.dart';
|
||||
|
||||
part 'leaderboard_event.dart';
|
||||
part 'leaderboard_state.dart';
|
||||
|
||||
/// {@template leaderboard_bloc}
|
||||
/// Manages leaderboard events.
|
||||
///
|
||||
/// Uses a [LeaderboardRepository] to request and update players participations.
|
||||
/// {@endtemplate}
|
||||
class LeaderboardBloc extends Bloc<LeaderboardEvent, LeaderboardState> {
|
||||
/// {@macro leaderboard_bloc}
|
||||
LeaderboardBloc(this._leaderboardRepository)
|
||||
: super(const LeaderboardState.initial()) {
|
||||
on<Top10Fetched>(_onTop10Fetched);
|
||||
on<LeaderboardEntryAdded>(_onLeaderboardEntryAdded);
|
||||
}
|
||||
|
||||
final LeaderboardRepository _leaderboardRepository;
|
||||
|
||||
Future<void> _onTop10Fetched(
|
||||
Top10Fetched event,
|
||||
Emitter<LeaderboardState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: LeaderboardStatus.loading));
|
||||
try {
|
||||
final top10Leaderboard =
|
||||
await _leaderboardRepository.fetchTop10Leaderboard();
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: LeaderboardStatus.success,
|
||||
leaderboard: top10Leaderboard,
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
emit(state.copyWith(status: LeaderboardStatus.error));
|
||||
addError(error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onLeaderboardEntryAdded(
|
||||
LeaderboardEntryAdded event,
|
||||
Emitter<LeaderboardState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: LeaderboardStatus.loading));
|
||||
try {
|
||||
final ranking =
|
||||
await _leaderboardRepository.addLeaderboardEntry(event.entry);
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: LeaderboardStatus.success,
|
||||
ranking: ranking,
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
emit(state.copyWith(status: LeaderboardStatus.error));
|
||||
addError(error);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
part of 'leaderboard_bloc.dart';
|
||||
|
||||
/// {@template leaderboard_event}
|
||||
/// Represents the events available for [LeaderboardBloc].
|
||||
/// {endtemplate}
|
||||
abstract class LeaderboardEvent extends Equatable {
|
||||
/// {@macro leaderboard_event}
|
||||
const LeaderboardEvent();
|
||||
}
|
||||
|
||||
/// {@template top_10_fetched}
|
||||
/// Request the top 10 [LeaderboardEntry]s.
|
||||
/// {endtemplate}
|
||||
class Top10Fetched extends LeaderboardEvent {
|
||||
/// {@macro top_10_fetched}
|
||||
const Top10Fetched();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
/// {@template leaderboard_entry_added}
|
||||
/// Writes a new [LeaderboardEntry].
|
||||
///
|
||||
/// Should be added when a player finishes a game.
|
||||
/// {endtemplate}
|
||||
class LeaderboardEntryAdded extends LeaderboardEvent {
|
||||
/// {@macro leaderboard_entry_added}
|
||||
const LeaderboardEntryAdded({required this.entry});
|
||||
|
||||
/// [LeaderboardEntry] to be written to the remote storage.
|
||||
final LeaderboardEntry entry;
|
||||
|
||||
@override
|
||||
List<Object?> get props => [entry];
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
// ignore_for_file: public_member_api_docs
|
||||
|
||||
part of 'leaderboard_bloc.dart';
|
||||
|
||||
/// Defines the request status.
|
||||
enum LeaderboardStatus {
|
||||
/// Request is being loaded.
|
||||
loading,
|
||||
|
||||
/// Request was processed successfully and received a valid response.
|
||||
success,
|
||||
|
||||
/// Request was processed unsuccessfully and received an error.
|
||||
error,
|
||||
}
|
||||
|
||||
/// {@template leaderboard_state}
|
||||
/// Represents the state of the leaderboard.
|
||||
/// {@endtemplate}
|
||||
class LeaderboardState extends Equatable {
|
||||
/// {@macro leaderboard_state}
|
||||
const LeaderboardState({
|
||||
required this.status,
|
||||
required this.ranking,
|
||||
required this.leaderboard,
|
||||
});
|
||||
|
||||
const LeaderboardState.initial()
|
||||
: status = LeaderboardStatus.loading,
|
||||
ranking = const LeaderboardRanking(
|
||||
ranking: 0,
|
||||
outOf: 0,
|
||||
),
|
||||
leaderboard = const [];
|
||||
|
||||
/// The current [LeaderboardStatus] of the state.
|
||||
final LeaderboardStatus status;
|
||||
|
||||
/// Rank of the current player.
|
||||
final LeaderboardRanking ranking;
|
||||
|
||||
/// List of top-ranked players.
|
||||
final List<LeaderboardEntry> leaderboard;
|
||||
|
||||
@override
|
||||
List<Object> get props => [status, ranking, leaderboard];
|
||||
|
||||
LeaderboardState copyWith({
|
||||
LeaderboardStatus? status,
|
||||
LeaderboardRanking? ranking,
|
||||
List<LeaderboardEntry>? leaderboard,
|
||||
}) {
|
||||
return LeaderboardState(
|
||||
status: status ?? this.status,
|
||||
ranking: ranking ?? this.ranking,
|
||||
leaderboard: leaderboard ?? this.leaderboard,
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
export 'bloc/leaderboard_bloc.dart';
|
@ -0,0 +1,103 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball/flame/blueprint.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
|
||||
import '../helpers/helpers.dart';
|
||||
|
||||
class MyBlueprint extends Blueprint {
|
||||
@override
|
||||
void build() {
|
||||
add(Component());
|
||||
addAll([Component(), Component()]);
|
||||
}
|
||||
}
|
||||
|
||||
class MyForge2dBlueprint extends Forge2DBlueprint {
|
||||
@override
|
||||
void build() {
|
||||
addContactCallback(MockContactCallback());
|
||||
addAllContactCallback([MockContactCallback(), MockContactCallback()]);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('Blueprint', () {
|
||||
test('components can be added to it', () {
|
||||
final blueprint = MyBlueprint()..build();
|
||||
|
||||
expect(blueprint.components.length, equals(3));
|
||||
});
|
||||
|
||||
test('adds the components to a game on attach', () {
|
||||
final mockGame = MockPinballGame();
|
||||
when(() => mockGame.addAll(any())).thenAnswer((_) async {});
|
||||
MyBlueprint().attach(mockGame);
|
||||
|
||||
verify(() => mockGame.addAll(any())).called(1);
|
||||
});
|
||||
|
||||
test(
|
||||
'throws assertion error when adding to an already attached blueprint',
|
||||
() async {
|
||||
final mockGame = MockPinballGame();
|
||||
when(() => mockGame.addAll(any())).thenAnswer((_) async {});
|
||||
final blueprint = MyBlueprint();
|
||||
await blueprint.attach(mockGame);
|
||||
|
||||
expect(() => blueprint.add(Component()), throwsAssertionError);
|
||||
expect(() => blueprint.addAll([Component()]), throwsAssertionError);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('Forge2DBlueprint', () {
|
||||
setUpAll(() {
|
||||
registerFallbackValue(SpaceshipHoleBallContactCallback());
|
||||
});
|
||||
|
||||
test('callbacks can be added to it', () {
|
||||
final blueprint = MyForge2dBlueprint()..build();
|
||||
|
||||
expect(blueprint.callbacks.length, equals(3));
|
||||
});
|
||||
|
||||
test('adds the callbacks to a game on attach', () async {
|
||||
final mockGame = MockPinballGame();
|
||||
when(() => mockGame.addAll(any())).thenAnswer((_) async {});
|
||||
when(() => mockGame.addContactCallback(any())).thenAnswer((_) async {});
|
||||
await MyForge2dBlueprint().attach(mockGame);
|
||||
|
||||
verify(() => mockGame.addContactCallback(any())).called(3);
|
||||
});
|
||||
|
||||
test(
|
||||
'throws assertion error when adding to an already attached blueprint',
|
||||
() async {
|
||||
final mockGame = MockPinballGame();
|
||||
when(() => mockGame.addAll(any())).thenAnswer((_) async {});
|
||||
when(() => mockGame.addContactCallback(any())).thenAnswer((_) async {});
|
||||
final blueprint = MyForge2dBlueprint();
|
||||
await blueprint.attach(mockGame);
|
||||
|
||||
expect(
|
||||
() => blueprint.addContactCallback(MockContactCallback()),
|
||||
throwsAssertionError,
|
||||
);
|
||||
expect(
|
||||
() => blueprint.addAllContactCallback([MockContactCallback()]),
|
||||
throwsAssertionError,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('throws assertion error when used on a non Forge2dGame', () {
|
||||
expect(
|
||||
() => MyForge2dBlueprint().attach(FlameGame()),
|
||||
throwsAssertionError,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
// 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:pinball/game/game.dart';
|
||||
|
||||
void main() {
|
||||
group('Kicker', () {
|
||||
// TODO(alestiago): Include golden tests for left and right.
|
||||
final flameTester = FlameTester(Forge2DGame.new);
|
||||
|
||||
flameTester.test(
|
||||
'loads correctly',
|
||||
(game) async {
|
||||
final kicker = Kicker(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(kicker);
|
||||
|
||||
expect(game.contains(kicker), isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'body is static',
|
||||
(game) async {
|
||||
final kicker = Kicker(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(kicker);
|
||||
|
||||
expect(kicker.body.bodyType, equals(BodyType.static));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'has restitution',
|
||||
(game) async {
|
||||
final kicker = Kicker(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(kicker);
|
||||
|
||||
final totalRestitution = kicker.body.fixtures.fold<double>(
|
||||
0,
|
||||
(total, fixture) => total + fixture.restitution,
|
||||
);
|
||||
expect(totalRestitution, greaterThan(0));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'has no friction',
|
||||
(game) async {
|
||||
final kicker = Kicker(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(kicker);
|
||||
|
||||
final totalFriction = kicker.body.fixtures.fold<double>(
|
||||
0,
|
||||
(total, fixture) => total + fixture.friction,
|
||||
);
|
||||
expect(totalFriction, equals(0));
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
@ -1,155 +0,0 @@
|
||||
// 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:pinball/game/game.dart';
|
||||
|
||||
void main() {
|
||||
group('SlingShot', () {
|
||||
final flameTester = FlameTester(Forge2DGame.new);
|
||||
|
||||
flameTester.test(
|
||||
'loads correctly',
|
||||
(game) async {
|
||||
final slingShot = SlingShot(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(slingShot);
|
||||
|
||||
expect(game.contains(slingShot), isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
group('body', () {
|
||||
flameTester.test(
|
||||
'is static',
|
||||
(game) async {
|
||||
final slingShot = SlingShot(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(slingShot);
|
||||
|
||||
expect(slingShot.body.bodyType, equals(BodyType.static));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('first fixture', () {
|
||||
flameTester.test(
|
||||
'exists',
|
||||
(game) async {
|
||||
final slingShot = SlingShot(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(slingShot);
|
||||
|
||||
expect(slingShot.body.fixtures[0], isA<Fixture>());
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'shape is triangular',
|
||||
(game) async {
|
||||
final slingShot = SlingShot(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(slingShot);
|
||||
|
||||
final fixture = slingShot.body.fixtures[0];
|
||||
expect(fixture.shape.shapeType, equals(ShapeType.polygon));
|
||||
expect((fixture.shape as PolygonShape).vertices.length, equals(3));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'triangular shapes are different '
|
||||
'when side is left or right',
|
||||
(game) async {
|
||||
final leftSlingShot = SlingShot(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
final rightSlingShot = SlingShot(
|
||||
side: BoardSide.right,
|
||||
);
|
||||
|
||||
await game.ensureAdd(leftSlingShot);
|
||||
await game.ensureAdd(rightSlingShot);
|
||||
|
||||
final rightShape =
|
||||
rightSlingShot.body.fixtures[0].shape as PolygonShape;
|
||||
final leftShape =
|
||||
leftSlingShot.body.fixtures[0].shape as PolygonShape;
|
||||
|
||||
expect(rightShape.vertices, isNot(equals(leftShape.vertices)));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'has no friction',
|
||||
(game) async {
|
||||
final slingShot = SlingShot(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(slingShot);
|
||||
|
||||
final fixture = slingShot.body.fixtures[0];
|
||||
expect(fixture.friction, equals(0));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('second fixture', () {
|
||||
flameTester.test(
|
||||
'exists',
|
||||
(game) async {
|
||||
final slingShot = SlingShot(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(slingShot);
|
||||
|
||||
expect(slingShot.body.fixtures[1], isA<Fixture>());
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'shape is edge',
|
||||
(game) async {
|
||||
final slingShot = SlingShot(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(slingShot);
|
||||
|
||||
final fixture = slingShot.body.fixtures[1];
|
||||
expect(fixture.shape.shapeType, equals(ShapeType.edge));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'has restitution',
|
||||
(game) async {
|
||||
final slingShot = SlingShot(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(slingShot);
|
||||
|
||||
final fixture = slingShot.body.fixtures[1];
|
||||
expect(fixture.restitution, greaterThan(0));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'has no friction',
|
||||
(game) async {
|
||||
final slingShot = SlingShot(
|
||||
side: BoardSide.left,
|
||||
);
|
||||
await game.ensureAdd(slingShot);
|
||||
|
||||
final fixture = slingShot.body.fixtures[1];
|
||||
expect(fixture.friction, equals(0));
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,166 @@
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:leaderboard_repository/leaderboard_repository.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball/leaderboard/leaderboard.dart';
|
||||
|
||||
class MockLeaderboardRepository extends Mock implements LeaderboardRepository {}
|
||||
|
||||
void main() {
|
||||
group('LeaderboardBloc', () {
|
||||
late LeaderboardRepository leaderboardRepository;
|
||||
|
||||
setUp(() {
|
||||
leaderboardRepository = MockLeaderboardRepository();
|
||||
});
|
||||
|
||||
test('initial state has state loading no ranking and empty leaderboard',
|
||||
() {
|
||||
final bloc = LeaderboardBloc(leaderboardRepository);
|
||||
expect(bloc.state.status, equals(LeaderboardStatus.loading));
|
||||
expect(bloc.state.ranking.ranking, equals(0));
|
||||
expect(bloc.state.ranking.outOf, equals(0));
|
||||
expect(bloc.state.leaderboard.isEmpty, isTrue);
|
||||
});
|
||||
|
||||
group('Top10Fetched', () {
|
||||
const top10Scores = [
|
||||
2500,
|
||||
2200,
|
||||
2200,
|
||||
2000,
|
||||
1800,
|
||||
1400,
|
||||
1300,
|
||||
1000,
|
||||
600,
|
||||
300,
|
||||
100,
|
||||
];
|
||||
|
||||
final top10Leaderboard = top10Scores
|
||||
.map(
|
||||
(score) => LeaderboardEntry(
|
||||
playerInitials: 'user$score',
|
||||
score: score,
|
||||
character: CharacterType.dash,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
|
||||
blocTest<LeaderboardBloc, LeaderboardState>(
|
||||
'emits [loading, success] statuses '
|
||||
'when fetchTop10Leaderboard succeeds',
|
||||
setUp: () {
|
||||
when(() => leaderboardRepository.fetchTop10Leaderboard()).thenAnswer(
|
||||
(_) async => top10Leaderboard,
|
||||
);
|
||||
},
|
||||
build: () => LeaderboardBloc(leaderboardRepository),
|
||||
act: (bloc) => bloc.add(Top10Fetched()),
|
||||
expect: () => [
|
||||
LeaderboardState.initial(),
|
||||
isA<LeaderboardState>()
|
||||
..having(
|
||||
(element) => element.status,
|
||||
'status',
|
||||
equals(LeaderboardStatus.success),
|
||||
)
|
||||
..having(
|
||||
(element) => element.leaderboard.length,
|
||||
'leaderboard',
|
||||
equals(top10Leaderboard.length),
|
||||
)
|
||||
],
|
||||
verify: (_) =>
|
||||
verify(() => leaderboardRepository.fetchTop10Leaderboard())
|
||||
.called(1),
|
||||
);
|
||||
|
||||
blocTest<LeaderboardBloc, LeaderboardState>(
|
||||
'emits [loading, error] statuses '
|
||||
'when fetchTop10Leaderboard fails',
|
||||
setUp: () {
|
||||
when(() => leaderboardRepository.fetchTop10Leaderboard()).thenThrow(
|
||||
Exception(),
|
||||
);
|
||||
},
|
||||
build: () => LeaderboardBloc(leaderboardRepository),
|
||||
act: (bloc) => bloc.add(Top10Fetched()),
|
||||
expect: () => <LeaderboardState>[
|
||||
LeaderboardState.initial(),
|
||||
LeaderboardState.initial().copyWith(status: LeaderboardStatus.error),
|
||||
],
|
||||
verify: (_) =>
|
||||
verify(() => leaderboardRepository.fetchTop10Leaderboard())
|
||||
.called(1),
|
||||
errors: () => [isA<Exception>()],
|
||||
);
|
||||
});
|
||||
|
||||
group('LeaderboardEntryAdded', () {
|
||||
final leaderboardEntry = LeaderboardEntry(
|
||||
playerInitials: 'ABC',
|
||||
score: 1500,
|
||||
character: CharacterType.dash,
|
||||
);
|
||||
|
||||
final ranking = LeaderboardRanking(ranking: 3, outOf: 4);
|
||||
|
||||
blocTest<LeaderboardBloc, LeaderboardState>(
|
||||
'emits [loading, success] statuses '
|
||||
'when addLeaderboardEntry succeeds',
|
||||
setUp: () {
|
||||
when(
|
||||
() => leaderboardRepository.addLeaderboardEntry(leaderboardEntry),
|
||||
).thenAnswer(
|
||||
(_) async => ranking,
|
||||
);
|
||||
},
|
||||
build: () => LeaderboardBloc(leaderboardRepository),
|
||||
act: (bloc) => bloc.add(LeaderboardEntryAdded(entry: leaderboardEntry)),
|
||||
expect: () => [
|
||||
LeaderboardState.initial(),
|
||||
isA<LeaderboardState>()
|
||||
..having(
|
||||
(element) => element.status,
|
||||
'status',
|
||||
equals(LeaderboardStatus.success),
|
||||
)
|
||||
..having(
|
||||
(element) => element.ranking,
|
||||
'ranking',
|
||||
equals(ranking),
|
||||
)
|
||||
],
|
||||
verify: (_) => verify(
|
||||
() => leaderboardRepository.addLeaderboardEntry(leaderboardEntry),
|
||||
).called(1),
|
||||
);
|
||||
|
||||
blocTest<LeaderboardBloc, LeaderboardState>(
|
||||
'emits [loading, error] statuses '
|
||||
'when addLeaderboardEntry fails',
|
||||
setUp: () {
|
||||
when(
|
||||
() => leaderboardRepository.addLeaderboardEntry(leaderboardEntry),
|
||||
).thenThrow(
|
||||
Exception(),
|
||||
);
|
||||
},
|
||||
build: () => LeaderboardBloc(leaderboardRepository),
|
||||
act: (bloc) => bloc.add(LeaderboardEntryAdded(entry: leaderboardEntry)),
|
||||
expect: () => <LeaderboardState>[
|
||||
LeaderboardState.initial(),
|
||||
LeaderboardState.initial().copyWith(status: LeaderboardStatus.error),
|
||||
],
|
||||
verify: (_) => verify(
|
||||
() => leaderboardRepository.addLeaderboardEntry(leaderboardEntry),
|
||||
).called(1),
|
||||
errors: () => [isA<Exception>()],
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:leaderboard_repository/leaderboard_repository.dart';
|
||||
import 'package:pinball/leaderboard/leaderboard.dart';
|
||||
|
||||
void main() {
|
||||
group('GameEvent', () {
|
||||
group('Top10Fetched', () {
|
||||
test('can be instantiated', () {
|
||||
expect(const Top10Fetched(), isNotNull);
|
||||
});
|
||||
|
||||
test('supports value equality', () {
|
||||
expect(
|
||||
Top10Fetched(),
|
||||
equals(const Top10Fetched()),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('LeaderboardEntryAdded', () {
|
||||
const leaderboardEntry = LeaderboardEntry(
|
||||
playerInitials: 'ABC',
|
||||
score: 1500,
|
||||
character: CharacterType.dash,
|
||||
);
|
||||
|
||||
test('can be instantiated', () {
|
||||
expect(const LeaderboardEntryAdded(entry: leaderboardEntry), isNotNull);
|
||||
});
|
||||
|
||||
test('supports value equality', () {
|
||||
expect(
|
||||
LeaderboardEntryAdded(entry: leaderboardEntry),
|
||||
equals(const LeaderboardEntryAdded(entry: leaderboardEntry)),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:leaderboard_repository/leaderboard_repository.dart';
|
||||
import 'package:pinball/leaderboard/leaderboard.dart';
|
||||
|
||||
void main() {
|
||||
group('LeaderboardState', () {
|
||||
test('supports value equality', () {
|
||||
expect(
|
||||
LeaderboardState.initial(),
|
||||
equals(
|
||||
LeaderboardState.initial(),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
group('constructor', () {
|
||||
test('can be instantiated', () {
|
||||
expect(
|
||||
LeaderboardState.initial(),
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('copyWith', () {
|
||||
const leaderboardEntry = LeaderboardEntry(
|
||||
playerInitials: 'ABC',
|
||||
score: 1500,
|
||||
character: CharacterType.dash,
|
||||
);
|
||||
|
||||
test(
|
||||
'copies correctly '
|
||||
'when no argument specified',
|
||||
() {
|
||||
const leaderboardState = LeaderboardState.initial();
|
||||
expect(
|
||||
leaderboardState.copyWith(),
|
||||
equals(leaderboardState),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'copies correctly '
|
||||
'when all arguments specified',
|
||||
() {
|
||||
const leaderboardState = LeaderboardState.initial();
|
||||
final otherLeaderboardState = LeaderboardState(
|
||||
status: LeaderboardStatus.success,
|
||||
ranking: LeaderboardRanking(ranking: 0, outOf: 0),
|
||||
leaderboard: const [leaderboardEntry],
|
||||
);
|
||||
expect(leaderboardState, isNot(equals(otherLeaderboardState)));
|
||||
|
||||
expect(
|
||||
leaderboardState.copyWith(
|
||||
status: otherLeaderboardState.status,
|
||||
ranking: otherLeaderboardState.ranking,
|
||||
leaderboard: otherLeaderboardState.leaderboard,
|
||||
),
|
||||
equals(otherLeaderboardState),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue