fix: both `Fipper`s should respond to multi touch taps (#330)

* fix: both flippers should respond to multi touch taps

* fix: both flippers should respond to multi touch taps

Co-authored-by: Tom Arra <tarra3@gmail.com>
pull/336/head
Jochum van der Ploeg 3 years ago committed by GitHub
parent 0a52583519
commit 94206bddaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -81,7 +81,7 @@ class PinballGame extends PinballForge2DGame
await super.onLoad();
}
BoardSide? focusedBoardSide;
final focusedBoardSide = <int, BoardSide>{};
@override
void onTapDown(int pointerId, TapDownInfo info) {
@ -94,9 +94,10 @@ class PinballGame extends PinballForge2DGame
descendants().whereType<Plunger>().single.pullFor(2);
} else {
final leftSide = info.eventPosition.widget.x < canvasSize.x / 2;
focusedBoardSide = leftSide ? BoardSide.left : BoardSide.right;
focusedBoardSide[pointerId] =
leftSide ? BoardSide.left : BoardSide.right;
final flippers = descendants().whereType<Flipper>().where((flipper) {
return flipper.side == focusedBoardSide;
return flipper.side == focusedBoardSide[pointerId];
});
flippers.first.moveUp();
}
@ -107,23 +108,23 @@ class PinballGame extends PinballForge2DGame
@override
void onTapUp(int pointerId, TapUpInfo info) {
_moveFlippersDown();
_moveFlippersDown(pointerId);
super.onTapUp(pointerId, info);
}
@override
void onTapCancel(int pointerId) {
_moveFlippersDown();
_moveFlippersDown(pointerId);
super.onTapCancel(pointerId);
}
void _moveFlippersDown() {
if (focusedBoardSide != null) {
void _moveFlippersDown(int pointerId) {
if (focusedBoardSide[pointerId] != null) {
final flippers = descendants().whereType<Flipper>().where((flipper) {
return flipper.side == focusedBoardSide;
return flipper.side == focusedBoardSide[pointerId];
});
flippers.first.moveDown();
focusedBoardSide = null;
focusedBoardSide.remove(pointerId);
}
}
}

@ -415,6 +415,51 @@ void main() {
expect(flippers.first.body.linearVelocity.y, isPositive);
});
flameTester.test(
'multiple touches control both flippers',
(game) async {
await game.ready();
final raw = _MockTapDownDetails();
when(() => raw.kind).thenReturn(PointerDeviceKind.touch);
final leftEventPosition = _MockEventPosition();
when(() => leftEventPosition.game).thenReturn(Vector2.zero());
when(() => leftEventPosition.widget).thenReturn(Vector2.zero());
final rightEventPosition = _MockEventPosition();
when(() => rightEventPosition.game).thenReturn(Vector2.zero());
when(() => rightEventPosition.widget).thenReturn(game.canvasSize);
final leftTapDownEvent = _MockTapDownInfo();
when(() => leftTapDownEvent.eventPosition)
.thenReturn(leftEventPosition);
when(() => leftTapDownEvent.raw).thenReturn(raw);
final rightTapDownEvent = _MockTapDownInfo();
when(() => rightTapDownEvent.eventPosition)
.thenReturn(rightEventPosition);
when(() => rightTapDownEvent.raw).thenReturn(raw);
final flippers = game.descendants().whereType<Flipper>();
final rightFlipper = flippers.elementAt(0);
final leftFlipper = flippers.elementAt(1);
game.onTapDown(0, leftTapDownEvent);
game.onTapDown(1, rightTapDownEvent);
expect(leftFlipper.body.linearVelocity.y, isNegative);
expect(leftFlipper.side, equals(BoardSide.left));
expect(rightFlipper.body.linearVelocity.y, isNegative);
expect(rightFlipper.side, equals(BoardSide.right));
expect(
game.focusedBoardSide,
equals({0: BoardSide.left, 1: BoardSide.right}),
);
},
);
});
group('plunger control', () {

Loading…
Cancel
Save