feat: add plunger to board

pull/25/head
Allison Ryan 4 years ago
parent 360b5876cf
commit bf48a81ef9

@ -1,23 +1,29 @@
import 'package:flame/input.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/services.dart';
import 'package:pinball/game/game.dart' show Anchor; import 'package:pinball/game/game.dart' show Anchor;
/// {@template plunger} /// {@template plunger}
/// [Plunger] serves as a spring, that shoots the ball on the right side of the /// [Plunger] serves as a spring, that shoots the ball on the right side of the
/// playfield. /// playfield.
/// ///
/// [Plunger] ignores gravity so the player controls its downward [pull]. /// [Plunger] ignores gravity so the player controls its downward [_pull].
/// {@endtemplate} /// {@endtemplate}
class Plunger extends BodyComponent { class Plunger extends BodyComponent with KeyboardHandler {
/// {@macro plunger} /// {@macro plunger}
Plunger({required Vector2 position}) : _position = position; Plunger({required Vector2 position}) : _position = position;
/// The initial position of the [Plunger] body.
final Vector2 _position; final Vector2 _position;
/// Distance the plunger can lower.
static const compressionDistance = 120.0;
@override @override
Body createBody() { Body createBody() {
final shape = PolygonShape()..setAsBoxXY(2.5, 1.5); final shape = PolygonShape()..setAsBoxXY(2, 0.75);
final fixtureDef = FixtureDef(shape); final fixtureDef = FixtureDef(shape)..density = 5;
final bodyDef = BodyDef() final bodyDef = BodyDef()
..userData = this ..userData = this
@ -29,18 +35,57 @@ class Plunger extends BodyComponent {
} }
/// Set a constant downward velocity on the [Plunger]. /// Set a constant downward velocity on the [Plunger].
void pull() { void _pull() {
body.linearVelocity = Vector2(0, -7); body.linearVelocity = Vector2(0, -3);
} }
/// Set an upward velocity on the [Plunger]. /// Set an upward velocity on the [Plunger].
/// ///
/// The velocity's magnitude depends on how far the [Plunger] has been pulled /// The velocity's magnitude depends on how far the [Plunger] has been pulled
/// from its original [_position]. /// from its original [_position].
void release() { void _release() {
final velocity = (_position.y - body.position.y) * 9; final velocity = (_position.y - body.position.y) * 9;
body.linearVelocity = Vector2(0, velocity); body.linearVelocity = Vector2(0, velocity);
} }
@override
bool onKeyEvent(
RawKeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
final keys = [
LogicalKeyboardKey.space,
LogicalKeyboardKey.arrowDown,
LogicalKeyboardKey.keyS,
];
// TODO(alestiago): Check why false cancels the event for other components.
// Investigate why return is of type [bool] expected instead of a type
// [KeyEventResult].
if (!keys.contains(event.logicalKey)) return true;
if (event is RawKeyDownEvent) {
_pull();
} else if (event is RawKeyUpEvent) {
_release();
}
return true;
}
}
/// {@template plunger_anchor}
/// [Anchor] positioned below a [Plunger].
/// {@endtemplate}
class PlungerAnchor extends Anchor {
/// {@macro plunger_anchor}
PlungerAnchor({
required Plunger plunger,
}) : super(
position: Vector2(
plunger.body.position.x,
plunger.body.position.y - Plunger.compressionDistance,
),
);
} }
/// {@template plunger_anchor_prismatic_joint_def} /// {@template plunger_anchor_prismatic_joint_def}
@ -67,6 +112,9 @@ class PlungerAnchorPrismaticJointDef extends PrismaticJointDef {
); );
enableLimit = true; enableLimit = true;
lowerTranslation = double.negativeInfinity; lowerTranslation = double.negativeInfinity;
enableMotor = true;
motorSpeed = 50;
maxMotorForce = motorSpeed;
collideConnected = true; collideConnected = true;
} }
} }

@ -4,9 +4,10 @@ import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/game/components/components.dart'; import 'package:pinball/game/components/components.dart';
/// {@template wall} /// {@template wall}
/// A continuos generic and [BodyType.static] barrier that divides a game area. /// A continuous generic and [BodyType.static] barrier that divides a game area.
/// {@endtemplate} /// {@endtemplate}
class Wall extends BodyComponent { class Wall extends BodyComponent {
/// {@macro wall}
Wall({ Wall({
required this.start, required this.start,
required this.end, required this.end,
@ -20,7 +21,7 @@ class Wall extends BodyComponent {
final shape = EdgeShape()..set(start, end); final shape = EdgeShape()..set(start, end);
final fixtureDef = FixtureDef(shape) final fixtureDef = FixtureDef(shape)
..restitution = 0.0 ..restitution = 0.1
..friction = 0.3; ..friction = 0.3;
final bodyDef = BodyDef() final bodyDef = BodyDef()
@ -32,6 +33,19 @@ class Wall extends BodyComponent {
} }
} }
List<Wall> createBoundaries(Forge2DGame game) {
final topLeft = Vector2.zero();
final bottomRight = game.screenToWorld(game.camera.viewport.effectiveSize);
final topRight = Vector2(bottomRight.x, topLeft.y);
final bottomLeft = Vector2(topLeft.x, bottomRight.y);
return [
Wall(start: topLeft, end: topRight),
Wall(start: topRight, end: bottomRight),
Wall(start: bottomLeft, end: topLeft),
];
}
/// {@template bottom_wall} /// {@template bottom_wall}
/// [Wall] located at the bottom of the board. /// [Wall] located at the bottom of the board.
/// ///
@ -39,6 +53,7 @@ class Wall extends BodyComponent {
/// [BottomWallBallContactCallback]. /// [BottomWallBallContactCallback].
/// {@endtemplate} /// {@endtemplate}
class BottomWall extends Wall { class BottomWall extends Wall {
/// {@macro bottom_wall}
BottomWall(Forge2DGame game) BottomWall(Forge2DGame game)
: super( : super(
start: game.screenToWorld(game.camera.viewport.effectiveSize), start: game.screenToWorld(game.camera.viewport.effectiveSize),

@ -7,17 +7,7 @@ import 'package:pinball/game/game.dart';
class PinballGame extends Forge2DGame class PinballGame extends Forge2DGame
with FlameBloc, HasKeyboardHandlerComponents { with FlameBloc, HasKeyboardHandlerComponents {
// TODO(erickzanardo): Change to the plumber position late Plunger plunger;
late final ballStartingPosition = screenToWorld(
Vector2(
camera.viewport.effectiveSize.x / 2,
camera.viewport.effectiveSize.y - 20,
),
) -
Vector2(0, -20);
// TODO(alestiago): Change to the design position.
late final flippersPosition = ballStartingPosition - Vector2(0, 5);
@override @override
void onAttach() { void onAttach() {
@ -25,21 +15,63 @@ class PinballGame extends Forge2DGame
spawnBall(); spawnBall();
} }
void spawnBall() {
add(Ball(position: ballStartingPosition));
}
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
addContactCallback(BallScorePointsCallback()); _addContactCallbacks();
await add(BottomWall(this)); await _addGameBoundaries();
unawaited(_addFlippers());
await _addPlunger();
// Corner wall above plunger so the ball deflects into the rest of the
// board.
// TODO(allisonryan0002): remove once we have the launch track for the ball.
await add(
Wall(
start: screenToWorld(
Vector2(
camera.viewport.effectiveSize.x,
100,
),
),
end: screenToWorld(
Vector2(
camera.viewport.effectiveSize.x - 100,
0,
),
),
),
);
}
Future<void> spawnBall() async {
await add(
Ball(
position: Vector2(
plunger.body.position.x,
plunger.body.position.y + Ball.ballSize.y,
),
),
);
}
void _addContactCallbacks() {
addContactCallback(BallScorePointsCallback());
addContactCallback(BottomWallBallContactCallback()); addContactCallback(BottomWallBallContactCallback());
}
unawaited(_addFlippers()); Future<void> _addGameBoundaries() async {
await add(BottomWall(this));
createBoundaries(this).forEach(add);
} }
Future<void> _addFlippers() async { Future<void> _addFlippers() async {
final flippersPosition = screenToWorld(
Vector2(
camera.viewport.effectiveSize.x / 2,
camera.viewport.effectiveSize.y - 120,
),
);
const spaceBetweenFlippers = 2; const spaceBetweenFlippers = 2;
final leftFlipper = Flipper.left( final leftFlipper = Flipper.left(
position: Vector2( position: Vector2(
@ -98,4 +130,27 @@ class PinballGame extends Forge2DGame
), ),
); );
} }
Future<void> _addPlunger() async {
late Anchor plungerAnchor;
await add(
plunger = Plunger(
position: screenToWorld(
Vector2(
camera.viewport.effectiveSize.x - 30,
camera.viewport.effectiveSize.y - Plunger.compressionDistance,
),
),
),
);
await add(plungerAnchor = PlungerAnchor(plunger: plunger));
world.createJoint(
PlungerAnchorPrismaticJointDef(
plunger: plunger,
anchor: plungerAnchor,
),
);
}
} }

@ -49,7 +49,7 @@ void main() {
); );
}); });
group('first fixture', () { group('fixture', () {
flameTester.test( flameTester.test(
'exists', 'exists',
(game) async { (game) async {

@ -255,36 +255,33 @@ void main() {
}, },
); );
group( group('FlipperAnchor', () {
'FlipperAnchor', flameTester.test(
() { 'position is at the left of the left Flipper',
flameTester.test( (game) async {
'position is at the left of the left Flipper', final flipper = Flipper.left(position: Vector2.zero());
(game) async { await game.ensureAdd(flipper);
final flipper = Flipper.left(position: Vector2.zero());
await game.ensureAdd(flipper);
final flipperAnchor = FlipperAnchor(flipper: flipper); final flipperAnchor = FlipperAnchor(flipper: flipper);
await game.ensureAdd(flipperAnchor); await game.ensureAdd(flipperAnchor);
expect(flipperAnchor.body.position.x, equals(-Flipper.width / 2)); expect(flipperAnchor.body.position.x, equals(-Flipper.width / 2));
}, },
); );
flameTester.test( flameTester.test(
'position is at the right of the right Flipper', 'position is at the right of the right Flipper',
(game) async { (game) async {
final flipper = Flipper.right(position: Vector2.zero()); final flipper = Flipper.right(position: Vector2.zero());
await game.ensureAdd(flipper); await game.ensureAdd(flipper);
final flipperAnchor = FlipperAnchor(flipper: flipper); final flipperAnchor = FlipperAnchor(flipper: flipper);
await game.ensureAdd(flipperAnchor); await game.ensureAdd(flipperAnchor);
expect(flipperAnchor.body.position.x, equals(Flipper.width / 2)); expect(flipperAnchor.body.position.x, equals(Flipper.width / 2));
}, },
); );
}, });
);
group('FlipperAnchorRevoluteJointDef', () { group('FlipperAnchorRevoluteJointDef', () {
group('initializes with', () { group('initializes with', () {

@ -1,8 +1,11 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'dart:collection';
import 'package:bloc_test/bloc_test.dart'; import 'package:bloc_test/bloc_test.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart'; import 'package:flame_test/flame_test.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
@ -57,7 +60,7 @@ void main() {
); );
}); });
group('first fixture', () { group('fixture', () {
flameTester.test( flameTester.test(
'exists', 'exists',
(game) async { (game) async {
@ -78,51 +81,101 @@ void main() {
expect(fixture.shape.shapeType, equals(ShapeType.polygon)); expect(fixture.shape.shapeType, equals(ShapeType.polygon));
}, },
); );
});
flameTester.test(
'pull sets a negative linear velocity',
(game) async {
final plunger = Plunger(position: Vector2.zero());
await game.ensureAdd(plunger);
plunger.pull();
expect(plunger.body.linearVelocity.y, isNegative);
expect(plunger.body.linearVelocity.x, isZero);
},
);
group('release', () {
flameTester.test( flameTester.test(
'does not set a linear velocity ' 'has density',
'when plunger is in starting position',
(game) async { (game) async {
final plunger = Plunger(position: Vector2.zero()); final plunger = Plunger(position: Vector2.zero());
await game.ensureAdd(plunger); await game.ensureAdd(plunger);
plunger.release(); final fixture = plunger.body.fixtures[0];
expect(fixture.density, greaterThan(0));
expect(plunger.body.linearVelocity.y, isZero);
expect(plunger.body.linearVelocity.x, isZero);
}, },
); );
});
flameTester.test( group('onKeyEvent', () {
'sets a positive linear velocity ' final keys = UnmodifiableListView([
'when plunger is below starting position', LogicalKeyboardKey.space,
(game) async { LogicalKeyboardKey.arrowDown,
final plunger = Plunger(position: Vector2.zero()); LogicalKeyboardKey.keyS,
await game.ensureAdd(plunger); ]);
late Plunger plunger;
setUp(() {
plunger = Plunger(position: Vector2.zero());
});
testRawKeyUpEvents(keys, (event) {
final keyLabel = (event.logicalKey != LogicalKeyboardKey.space)
? event.logicalKey.keyLabel
: 'Space';
flameTester.test(
'moves upwards when $keyLabel is released '
'and plunger is below its starting position',
(game) async {
await game.ensureAdd(plunger);
plunger.body.setTransform(Vector2(0, -1), 0);
plunger.onKeyEvent(event, {});
expect(plunger.body.linearVelocity.y, isPositive);
expect(plunger.body.linearVelocity.x, isZero);
},
);
});
testRawKeyUpEvents(keys, (event) {
final keyLabel = (event.logicalKey != LogicalKeyboardKey.space)
? event.logicalKey.keyLabel
: 'Space';
flameTester.test(
'does not move when $keyLabel is released '
'and plunger is in its starting position',
(game) async {
await game.ensureAdd(plunger);
plunger.onKeyEvent(event, {});
expect(plunger.body.linearVelocity.y, isZero);
expect(plunger.body.linearVelocity.x, isZero);
},
);
});
testRawKeyDownEvents(keys, (event) {
final keyLabel = (event.logicalKey != LogicalKeyboardKey.space)
? event.logicalKey.keyLabel
: 'Space';
flameTester.test(
'moves downwards when $keyLabel is pressed',
(game) async {
await game.ensureAdd(plunger);
plunger.onKeyEvent(event, {});
expect(plunger.body.linearVelocity.y, isNegative);
expect(plunger.body.linearVelocity.x, isZero);
},
);
});
});
});
plunger.body.setTransform(Vector2(0, -1), 0); group('PlungerAnchor', () {
plunger.release(); flameTester.test(
'position is a compression distance below the Plunger',
(game) async {
final plunger = Plunger(position: Vector2.zero());
await game.ensureAdd(plunger);
expect(plunger.body.linearVelocity.y, isPositive); final plungerAnchor = PlungerAnchor(plunger: plunger);
expect(plunger.body.linearVelocity.x, isZero); await game.ensureAdd(plungerAnchor);
},
); expect(
}); plungerAnchor.body.position.y,
equals(plunger.body.position.y - Plunger.compressionDistance),
);
},
);
}); });
group('PlungerAnchorPrismaticJointDef', () { group('PlungerAnchorPrismaticJointDef', () {
@ -257,46 +310,47 @@ void main() {
); );
}); });
flameTester.widgetTest( testRawKeyUpEvents([LogicalKeyboardKey.space], (event) {
'plunger cannot go below anchor', flameTester.widgetTest(
(game, tester) async { 'plunger cannot go below anchor',
await game.ensureAddAll([plunger, anchor]); (game, tester) async {
await game.ensureAddAll([plunger, anchor]);
// Giving anchor a shape for the plunger to collide with. // Giving anchor a shape for the plunger to collide with.
anchor.body.createFixtureFromShape(PolygonShape()..setAsBoxXY(2, 1)); anchor.body.createFixtureFromShape(PolygonShape()..setAsBoxXY(2, 1));
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger, plunger: plunger,
anchor: anchor, anchor: anchor,
); );
game.world.createJoint(jointDef); game.world.createJoint(jointDef);
plunger.pull(); await tester.pump(const Duration(seconds: 1));
await tester.pump(const Duration(seconds: 1));
expect(plunger.body.position.y > anchor.body.position.y, isTrue); expect(plunger.body.position.y > anchor.body.position.y, isTrue);
}, },
); );
});
flameTester.widgetTest( testRawKeyUpEvents([LogicalKeyboardKey.space], (event) {
'plunger cannot excessively exceed starting position', flameTester.widgetTest(
(game, tester) async { 'plunger cannot excessively exceed starting position',
await game.ensureAddAll([plunger, anchor]); (game, tester) async {
await game.ensureAddAll([plunger, anchor]);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger, plunger: plunger,
anchor: anchor, anchor: anchor,
); );
game.world.createJoint(jointDef); game.world.createJoint(jointDef);
plunger.pull(); plunger.body.setTransform(Vector2(0, -1), 0);
await tester.pump(const Duration(seconds: 1));
plunger.release(); await tester.pump(const Duration(seconds: 1));
await tester.pump(const Duration(seconds: 1));
expect(plunger.body.position.y < 1, isTrue); expect(plunger.body.position.y < 1, isTrue);
}, },
); );
});
}); });
} }

@ -76,7 +76,7 @@ void main() {
); );
}); });
group('first fixture', () { group('fixture', () {
flameTester.test( flameTester.test(
'exists', 'exists',
(game) async { (game) async {
@ -91,7 +91,7 @@ void main() {
); );
flameTester.test( flameTester.test(
'has restitution equals 0', 'has restitution',
(game) async { (game) async {
final wall = Wall( final wall = Wall(
start: Vector2.zero(), start: Vector2.zero(),
@ -100,7 +100,7 @@ void main() {
await game.ensureAdd(wall); await game.ensureAdd(wall);
final fixture = wall.body.fixtures[0]; final fixture = wall.body.fixtures[0];
expect(fixture.restitution, equals(0)); expect(fixture.restitution, greaterThan(0));
}, },
); );

@ -13,42 +13,84 @@ void main() {
// TODO(alestiago): test if [PinballGame] registers // TODO(alestiago): test if [PinballGame] registers
// [BallScorePointsCallback] once the following issue is resolved: // [BallScorePointsCallback] once the following issue is resolved:
// https://github.com/flame-engine/flame/issues/1416 // https://github.com/flame-engine/flame/issues/1416
group( group('components', () {
'components', group('Walls', () {
() { flameTester.test(
group('Flippers', () { 'has three Walls',
bool Function(Component) flipperSelector(BoardSide side) => (game) async {
(component) => component is Flipper && component.side == side; await game.ready();
final walls = game.children
flameTester.test( .where(
'has only one left Flipper', (component) => component is Wall && component is! BottomWall,
(game) async { )
await game.ready(); .toList();
// TODO(allisonryan0002): expect 3 when launch track is added and
expect( // temporary wall is removed.
() => game.children.singleWhere( expect(walls.length, 4);
flipperSelector(BoardSide.left), },
), );
returnsNormally,
); flameTester.test(
}, 'has only one BottomWall',
); (game) async {
await game.ready();
expect(
() => game.children.singleWhere(
(component) => component is BottomWall,
),
returnsNormally,
);
},
);
});
group('Flippers', () {
bool Function(Component) flipperSelector(BoardSide side) =>
(component) => component is Flipper && component.side == side;
flameTester.test(
'has only one left Flipper',
(game) async {
await game.ready();
expect(
() => game.children.singleWhere(
flipperSelector(BoardSide.left),
),
returnsNormally,
);
},
);
flameTester.test(
'has only one right Flipper',
(game) async {
await game.ready();
expect(
() => game.children.singleWhere(
flipperSelector(BoardSide.right),
),
returnsNormally,
);
},
);
});
flameTester.test(
'Plunger has only one Plunger',
(game) async {
await game.ready();
flameTester.test( expect(
'has only one right Flipper', () => game.children.singleWhere(
(game) async { (component) => component is Plunger,
await game.ready(); ),
returnsNormally,
expect(
() => game.children.singleWhere(
flipperSelector(BoardSide.right),
),
returnsNormally,
);
},
); );
}); },
}, );
); });
}); });
} }

Loading…
Cancel
Save