fix: fixed mere conflict on components barrel

pull/40/head
RuiAlonso 4 years ago
commit 4ea90bc46e

@ -3,7 +3,7 @@ import 'package:pinball/game/game.dart';
/// Indicates a side of the board.
///
/// Usually used to position or mirror elements of a [PinballGame]; such as a
/// [Flipper].
/// [Flipper] or [SlingShot].
enum BoardSide {
/// The left side of the board.
left,
@ -19,4 +19,9 @@ extension BoardSideX on BoardSide {
/// Whether this side is [BoardSide.right].
bool get isRight => this == BoardSide.right;
/// Direction of the [BoardSide].
///
/// Represents the path which the [BoardSide] moves along.
int get direction => isLeft ? -1 : 1;
}

@ -8,6 +8,8 @@ export 'jetpack_ramp.dart';
export 'joint_anchor.dart';
export 'pathway.dart';
export 'plunger.dart';
export 'round_bumper.dart';
export 'score_points.dart';
export 'sling_shot.dart';
export 'sparky_ramp.dart';
export 'wall.dart';

@ -0,0 +1,41 @@
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/game/game.dart';
/// {@template round_bumper}
/// Circular body that repels a [Ball] on contact, increasing the score.
/// {@endtemplate}
class RoundBumper extends BodyComponent with ScorePoints {
/// {@macro round_bumper}
RoundBumper({
required Vector2 position,
required double radius,
required int points,
}) : _position = position,
_radius = radius,
_points = points;
/// The position of the [RoundBumper] body.
final Vector2 _position;
/// The radius of the [RoundBumper].
final double _radius;
/// Points awarded from hitting this [RoundBumper].
final int _points;
@override
int get points => _points;
@override
Body createBody() {
final shape = CircleShape()..radius = _radius;
final fixtureDef = FixtureDef(shape)..restitution = 1;
final bodyDef = BodyDef()
..position = _position
..type = BodyType.static;
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}

@ -0,0 +1,96 @@
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 {
/// {@macro sling_shot}
SlingShot({
required Vector2 position,
required BoardSide side,
}) : _position = position,
_side = side {
// TODO(alestiago): Use sprite instead of color when provided.
paint = Paint()
..color = const Color(0xFF00FF00)
..style = PaintingStyle.fill;
}
/// The initial position of the [SlingShot] body.
final Vector2 _position;
/// 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;
List<FixtureDef> _createFixtureDefs() {
final fixtures = <FixtureDef>[];
// TODO(alestiago): Use size from PositionedBodyComponent instead,
// once a sprite is given.
final size = Vector2(10, 10);
// 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 = 2;
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;
fixtures.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 = 20.0
..friction = 0;
fixtures.add(kickerFixtureDef);
return fixtures;
}
@override
Body createBody() {
final bodyDef = BodyDef()..position = _position;
final body = world.createBody(bodyDef);
_createFixtureDefs().forEach(body.createFixture);
return body;
}
}

@ -23,5 +23,12 @@ void main() {
expect(side.isLeft, isFalse);
expect(side.isRight, isTrue);
});
test('direction is correct', () {
const side = BoardSide.left;
expect(side.direction, equals(-1));
const side2 = BoardSide.right;
expect(side2.direction, equals(1));
});
});
}

@ -0,0 +1,124 @@
// 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() {
TestWidgetsFlutterBinding.ensureInitialized();
group('RoundBumper', () {
final flameTester = FlameTester(Forge2DGame.new);
const radius = 1.0;
const points = 1;
flameTester.test(
'loads correctly',
(game) async {
await game.ready();
final roundBumper = RoundBumper(
position: Vector2.zero(),
radius: radius,
points: points,
);
await game.ensureAdd(roundBumper);
expect(game.contains(roundBumper), isTrue);
},
);
flameTester.test(
'has points',
(game) async {
final roundBumper = RoundBumper(
position: Vector2.zero(),
radius: radius,
points: points,
);
await game.ensureAdd(roundBumper);
expect(roundBumper.points, equals(points));
},
);
group('body', () {
flameTester.test(
'positions correctly',
(game) async {
final position = Vector2.all(10);
final roundBumper = RoundBumper(
position: position,
radius: radius,
points: points,
);
await game.ensureAdd(roundBumper);
game.contains(roundBumper);
expect(roundBumper.body.position, equals(position));
},
);
flameTester.test(
'is static',
(game) async {
final roundBumper = RoundBumper(
position: Vector2.zero(),
radius: radius,
points: points,
);
await game.ensureAdd(roundBumper);
expect(roundBumper.body.bodyType, equals(BodyType.static));
},
);
});
group('fixture', () {
flameTester.test(
'exists',
(game) async {
final roundBumper = RoundBumper(
position: Vector2.zero(),
radius: radius,
points: points,
);
await game.ensureAdd(roundBumper);
expect(roundBumper.body.fixtures[0], isA<Fixture>());
},
);
flameTester.test(
'has restitution',
(game) async {
final roundBumper = RoundBumper(
position: Vector2.zero(),
radius: radius,
points: points,
);
await game.ensureAdd(roundBumper);
final fixture = roundBumper.body.fixtures[0];
expect(fixture.restitution, greaterThan(0));
},
);
flameTester.test(
'shape is circular',
(game) async {
final roundBumper = RoundBumper(
position: Vector2.zero(),
radius: radius,
points: points,
);
await game.ensureAdd(roundBumper);
final fixture = roundBumper.body.fixtures[0];
expect(fixture.shape.shapeType, equals(ShapeType.circle));
expect(fixture.shape.radius, equals(1));
},
);
});
});
}

@ -0,0 +1,180 @@
// 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(
position: Vector2.zero(),
side: BoardSide.left,
);
await game.ensureAdd(slingShot);
expect(game.contains(slingShot), isTrue);
},
);
group('body', () {
flameTester.test(
'positions correctly',
(game) async {
final position = Vector2.all(10);
final slingShot = SlingShot(
position: position,
side: BoardSide.left,
);
await game.ensureAdd(slingShot);
expect(slingShot.body.position, equals(position));
},
);
flameTester.test(
'is static',
(game) async {
final slingShot = SlingShot(
position: Vector2.zero(),
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(
position: Vector2.zero(),
side: BoardSide.left,
);
await game.ensureAdd(slingShot);
expect(slingShot.body.fixtures[0], isA<Fixture>());
},
);
flameTester.test(
'shape is triangular',
(game) async {
final slingShot = SlingShot(
position: Vector2.zero(),
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(
position: Vector2.zero(),
side: BoardSide.left,
);
final rightSlingShot = SlingShot(
position: Vector2.zero(),
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(
position: Vector2.zero(),
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(
position: Vector2.zero(),
side: BoardSide.left,
);
await game.ensureAdd(slingShot);
expect(slingShot.body.fixtures[1], isA<Fixture>());
},
);
flameTester.test(
'shape is edge',
(game) async {
final slingShot = SlingShot(
position: Vector2.zero(),
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(
position: Vector2.zero(),
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(
position: Vector2.zero(),
side: BoardSide.left,
);
await game.ensureAdd(slingShot);
final fixture = slingShot.body.fixtures[1];
expect(fixture.friction, equals(0));
},
);
});
});
}
Loading…
Cancel
Save