mirror of https://github.com/flutter/pinball.git
Merge branch 'feat/leaderboard-repository' of https://github.com/VGVentures/pinball into feat/leaderboard-repository
commit
721de958da
@ -0,0 +1,79 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
|
||||||
|
/// {@template bottom_group}
|
||||||
|
/// Grouping of the board's bottom [Component]s.
|
||||||
|
///
|
||||||
|
/// The bottom [Component]s are the [Flipper]s and the [Baseboard]s.
|
||||||
|
/// {@endtemplate}
|
||||||
|
// TODO(alestiago): Consider renaming once entire Board is defined.
|
||||||
|
class BottomGroup extends Component {
|
||||||
|
/// {@macro bottom_group}
|
||||||
|
BottomGroup({
|
||||||
|
required this.position,
|
||||||
|
required this.spacing,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// The amount of space between the line of symmetry.
|
||||||
|
final double spacing;
|
||||||
|
|
||||||
|
/// The position of this [BottomGroup].
|
||||||
|
final Vector2 position;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
final spacing = this.spacing + Flipper.width / 2;
|
||||||
|
final rightSide = _BottomGroupSide(
|
||||||
|
side: BoardSide.right,
|
||||||
|
position: position + Vector2(spacing, 0),
|
||||||
|
);
|
||||||
|
final leftSide = _BottomGroupSide(
|
||||||
|
side: BoardSide.left,
|
||||||
|
position: position + Vector2(-spacing, 0),
|
||||||
|
);
|
||||||
|
|
||||||
|
await addAll([rightSide, leftSide]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template bottom_group_side}
|
||||||
|
/// Group with one side of [BottomGroup]'s symmetric [Component]s.
|
||||||
|
///
|
||||||
|
/// For example, [Flipper]s are symmetric components.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class _BottomGroupSide extends Component {
|
||||||
|
/// {@macro bottom_group_side}
|
||||||
|
_BottomGroupSide({
|
||||||
|
required BoardSide side,
|
||||||
|
required Vector2 position,
|
||||||
|
}) : _side = side,
|
||||||
|
_position = position;
|
||||||
|
|
||||||
|
final BoardSide _side;
|
||||||
|
|
||||||
|
final Vector2 _position;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
final direction = _side.direction;
|
||||||
|
|
||||||
|
final flipper = Flipper.fromSide(
|
||||||
|
side: _side,
|
||||||
|
)..initialPosition = _position;
|
||||||
|
final baseboard = Baseboard(side: _side)
|
||||||
|
..initialPosition = _position +
|
||||||
|
Vector2(
|
||||||
|
(Flipper.width * direction) - direction,
|
||||||
|
Flipper.height,
|
||||||
|
);
|
||||||
|
final slingShot = SlingShot(
|
||||||
|
side: _side,
|
||||||
|
)..initialPosition = _position +
|
||||||
|
Vector2(
|
||||||
|
(Flipper.width) * direction,
|
||||||
|
Flipper.height + SlingShot.size.y,
|
||||||
|
);
|
||||||
|
|
||||||
|
await addAll([flipper, baseboard, slingShot]);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
|
||||||
|
/// Forces a given [BodyComponent] to position their [body] to an
|
||||||
|
/// [initialPosition].
|
||||||
|
///
|
||||||
|
/// Note: If the [initialPosition] is set after the [BodyComponent] has been
|
||||||
|
/// loaded it will have no effect; defaulting to [Vector2.zero].
|
||||||
|
mixin InitialPosition<T extends Forge2DGame> on BodyComponent<T> {
|
||||||
|
final Vector2 _initialPosition = Vector2.zero();
|
||||||
|
|
||||||
|
set initialPosition(Vector2 value) {
|
||||||
|
assert(
|
||||||
|
!isLoaded,
|
||||||
|
'Cannot set initialPosition after component has already loaded.',
|
||||||
|
);
|
||||||
|
if (value == initialPosition) return;
|
||||||
|
|
||||||
|
_initialPosition.setFrom(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The initial position of the [body].
|
||||||
|
Vector2 get initialPosition => _initialPosition;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
// TODO(alestiago): Investiagate why body.position.setFrom(initialPosition)
|
||||||
|
// works for some components and not others.
|
||||||
|
assert(
|
||||||
|
body.position == initialPosition,
|
||||||
|
'Body position does not match initialPosition.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
// 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';
|
||||||
|
|
||||||
|
import '../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(Forge2DGame.new);
|
||||||
|
|
||||||
|
group('BottomGroup', () {
|
||||||
|
flameTester.test(
|
||||||
|
'loads correctly',
|
||||||
|
(game) async {
|
||||||
|
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0);
|
||||||
|
await game.ready();
|
||||||
|
await game.ensureAdd(bottomGroup);
|
||||||
|
|
||||||
|
expect(game.contains(bottomGroup), isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
group('children', () {
|
||||||
|
flameTester.test(
|
||||||
|
'has one left flipper',
|
||||||
|
(game) async {
|
||||||
|
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0);
|
||||||
|
await game.ready();
|
||||||
|
await game.ensureAdd(bottomGroup);
|
||||||
|
|
||||||
|
final leftFlippers = bottomGroup.findNestedChildren<Flipper>(
|
||||||
|
condition: (flipper) => flipper.side.isLeft,
|
||||||
|
);
|
||||||
|
expect(leftFlippers.length, equals(1));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'has one right flipper',
|
||||||
|
(game) async {
|
||||||
|
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0);
|
||||||
|
await game.ready();
|
||||||
|
await game.ensureAdd(bottomGroup);
|
||||||
|
|
||||||
|
final rightFlippers = bottomGroup.findNestedChildren<Flipper>(
|
||||||
|
condition: (flipper) => flipper.side.isRight,
|
||||||
|
);
|
||||||
|
expect(rightFlippers.length, equals(1));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'has two Baseboards',
|
||||||
|
(game) async {
|
||||||
|
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0);
|
||||||
|
await game.ready();
|
||||||
|
await game.ensureAdd(bottomGroup);
|
||||||
|
|
||||||
|
final baseboards = bottomGroup.findNestedChildren<Baseboard>();
|
||||||
|
expect(baseboards.length, equals(2));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'has two SlingShots',
|
||||||
|
(game) async {
|
||||||
|
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0);
|
||||||
|
await game.ready();
|
||||||
|
await game.ensureAdd(bottomGroup);
|
||||||
|
|
||||||
|
final slingShots = bottomGroup.findNestedChildren<SlingShot>();
|
||||||
|
expect(slingShots.length, equals(2));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,81 @@
|
|||||||
|
// 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';
|
||||||
|
|
||||||
|
class TestBodyComponent extends BodyComponent with InitialPosition {
|
||||||
|
@override
|
||||||
|
Body createBody() {
|
||||||
|
return world.createBody(BodyDef());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestPositionedBodyComponent extends BodyComponent with InitialPosition {
|
||||||
|
@override
|
||||||
|
Body createBody() {
|
||||||
|
return world.createBody(BodyDef()..position = initialPosition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
final flameTester = FlameTester(Forge2DGame.new);
|
||||||
|
group('InitialPosition', () {
|
||||||
|
test('correctly sets and gets', () {
|
||||||
|
final component = TestBodyComponent()..initialPosition = Vector2(1, 2);
|
||||||
|
expect(component.initialPosition, Vector2(1, 2));
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'throws AssertionError '
|
||||||
|
'when BodyDef is not positioned with initialPosition',
|
||||||
|
(game) async {
|
||||||
|
final component = TestBodyComponent()
|
||||||
|
..initialPosition = Vector2.all(
|
||||||
|
10,
|
||||||
|
);
|
||||||
|
await expectLater(
|
||||||
|
() => game.ensureAdd(component),
|
||||||
|
throwsAssertionError,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'positions correctly',
|
||||||
|
(game) async {
|
||||||
|
final position = Vector2.all(10);
|
||||||
|
final component = TestPositionedBodyComponent()
|
||||||
|
..initialPosition = position;
|
||||||
|
await game.ensureAdd(component);
|
||||||
|
expect(component.body.position, equals(position));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'deafaults to zero '
|
||||||
|
'when no initialPosition is given',
|
||||||
|
(game) async {
|
||||||
|
final component = TestBodyComponent();
|
||||||
|
await game.ensureAdd(component);
|
||||||
|
expect(component.body.position, equals(Vector2.zero()));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'setting throws AssertionError '
|
||||||
|
'when component has loaded',
|
||||||
|
(game) async {
|
||||||
|
final component = TestBodyComponent();
|
||||||
|
await game.ensureAdd(component);
|
||||||
|
|
||||||
|
expect(component.isLoaded, isTrue);
|
||||||
|
expect(
|
||||||
|
() => component.initialPosition = Vector2.all(4),
|
||||||
|
throwsAssertionError,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in new issue