feat: made Baseboard use InitialPosition

pull/50/head
alestiago 4 years ago
parent 401244a8c0
commit 85db44a617

@ -6,13 +6,11 @@ import 'package:pinball/game/game.dart';
/// {@template baseboard} /// {@template baseboard}
/// Straight, angled board piece to corral the [Ball] towards the [Flipper]s. /// Straight, angled board piece to corral the [Ball] towards the [Flipper]s.
/// {@endtemplate} /// {@endtemplate}
class Baseboard extends BodyComponent { class Baseboard extends BodyComponent with InitialPosition {
/// {@macro baseboard} /// {@macro baseboard}
Baseboard({ Baseboard({
required BoardSide side, required BoardSide side,
required Vector2 position, }) : _side = side;
}) : _side = side,
_position = position;
/// The width of the [Baseboard]. /// The width of the [Baseboard].
static const width = 10.0; static const width = 10.0;
@ -20,9 +18,6 @@ class Baseboard extends BodyComponent {
/// The height of the [Baseboard]. /// The height of the [Baseboard].
static const height = 2.0; static const height = 2.0;
/// The position of the [Baseboard] body.
final Vector2 _position;
/// Whether the [Baseboard] is on the left or right side of the board. /// Whether the [Baseboard] is on the left or right side of the board.
final BoardSide _side; final BoardSide _side;
@ -63,7 +58,7 @@ class Baseboard extends BodyComponent {
const angle = math.pi / 7; const angle = math.pi / 7;
final bodyDef = BodyDef() final bodyDef = BodyDef()
..position = _position ..position = initialPosition
..type = BodyType.static ..type = BodyType.static
..angle = _side.isLeft ? -angle : angle; ..angle = _side.isLeft ? -angle : angle;

@ -61,14 +61,12 @@ class _BottomGroupSide extends Component {
side: _side, side: _side,
position: _position, position: _position,
); );
final baseboard = Baseboard( final baseboard = Baseboard(side: _side)
side: _side, ..initialPosition = _position +
position: _position +
Vector2( Vector2(
(Flipper.width * direction) - direction, (Flipper.width * direction) - direction,
Flipper.height, Flipper.height,
), );
);
final slingShot = SlingShot( final slingShot = SlingShot(
side: _side, side: _side,
position: _position + position: _position +

@ -15,13 +15,12 @@ void main() {
(game) async { (game) async {
await game.ready(); await game.ready();
final leftBaseboard = Baseboard( final leftBaseboard = Baseboard(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2.zero();
final rightBaseboard = Baseboard( final rightBaseboard = Baseboard(
position: Vector2.zero(),
side: BoardSide.right, side: BoardSide.right,
); )..initialPosition = Vector2.zero();
await game.ensureAddAll([leftBaseboard, rightBaseboard]); await game.ensureAddAll([leftBaseboard, rightBaseboard]);
expect(game.contains(leftBaseboard), isTrue); expect(game.contains(leftBaseboard), isTrue);
@ -35,13 +34,13 @@ void main() {
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.all(10);
final baseboard = Baseboard( final baseboard = Baseboard(
position: position,
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = position;
await game.ensureAdd(baseboard); await game.ensureAdd(baseboard);
game.contains(baseboard); game.contains(baseboard);
expect(baseboard.body.position, position); expect(baseboard.body.position, equals(position));
}, },
); );
@ -49,9 +48,9 @@ void main() {
'is static', 'is static',
(game) async { (game) async {
final baseboard = Baseboard( final baseboard = Baseboard(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(baseboard); await game.ensureAdd(baseboard);
expect(baseboard.body.bodyType, equals(BodyType.static)); expect(baseboard.body.bodyType, equals(BodyType.static));
@ -62,13 +61,11 @@ void main() {
'is at an angle', 'is at an angle',
(game) async { (game) async {
final leftBaseboard = Baseboard( final leftBaseboard = Baseboard(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2.zero();
final rightBaseboard = Baseboard( final rightBaseboard = Baseboard(
position: Vector2.zero(),
side: BoardSide.right, side: BoardSide.right,
); )..initialPosition = Vector2.zero();
await game.ensureAddAll([leftBaseboard, rightBaseboard]); await game.ensureAddAll([leftBaseboard, rightBaseboard]);
expect(leftBaseboard.body.angle, isNegative); expect(leftBaseboard.body.angle, isNegative);
@ -82,9 +79,8 @@ void main() {
'has three', 'has three',
(game) async { (game) async {
final baseboard = Baseboard( final baseboard = Baseboard(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2.zero();
await game.ensureAdd(baseboard); await game.ensureAdd(baseboard);
expect(baseboard.body.fixtures.length, equals(3)); expect(baseboard.body.fixtures.length, equals(3));

Loading…
Cancel
Save