mirror of https://github.com/flutter/pinball.git
feat: including black and white Flipper assets (#102)
* feat: included left and right flipper assets * feat: loaded left and right flipper assets * feat: moved Flipper to components and defined controller * refactor: moved lock to FlipperJoint * refactor(sandbox): rephrased description * feat(sanbox): including Flipper example * feat: included Tracing story * feat: defined trace method * feat(sandbox): included right flipper * feat: sized Flipper to match asset * refactor: moved tests * feat: tested flipper_controller * feat: adjusted flipper size * feat(sandbox): removed TapDetector * refactor: removed unused import * fix: increased test coverage * docs: improved doc comments * fix: changing merge imports * refactor: simplified loading asset logic Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * chore: removed old Flipper asset * fix: used correct variable * feat: enchanced tracing example * refactor: renamed circleAssetShadow to assetShadow Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>pull/108/head
parent
c4012b1c65
commit
72b8213f74
Before Width: | Height: | Size: 8.1 KiB |
@ -0,0 +1,52 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
/// {@template flipper_controller}
|
||||||
|
/// A [Component] that controls the [Flipper]s movement.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class FlipperController extends Component with KeyboardHandler {
|
||||||
|
/// {@macro flipper_controller}
|
||||||
|
FlipperController(this.flipper) : _keys = flipper.side.flipperKeys;
|
||||||
|
|
||||||
|
/// The [Flipper] this controller is controlling.
|
||||||
|
final Flipper flipper;
|
||||||
|
|
||||||
|
/// The [LogicalKeyboardKey]s that will control the [Flipper].
|
||||||
|
///
|
||||||
|
/// [onKeyEvent] method listens to when one of these keys is pressed.
|
||||||
|
final List<LogicalKeyboardKey> _keys;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool onKeyEvent(
|
||||||
|
RawKeyEvent event,
|
||||||
|
Set<LogicalKeyboardKey> keysPressed,
|
||||||
|
) {
|
||||||
|
if (!_keys.contains(event.logicalKey)) return true;
|
||||||
|
|
||||||
|
if (event is RawKeyDownEvent) {
|
||||||
|
flipper.moveUp();
|
||||||
|
} else if (event is RawKeyUpEvent) {
|
||||||
|
flipper.moveDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension on BoardSide {
|
||||||
|
List<LogicalKeyboardKey> get flipperKeys {
|
||||||
|
switch (this) {
|
||||||
|
case BoardSide.left:
|
||||||
|
return [
|
||||||
|
LogicalKeyboardKey.arrowLeft,
|
||||||
|
LogicalKeyboardKey.keyA,
|
||||||
|
];
|
||||||
|
case BoardSide.right:
|
||||||
|
return [
|
||||||
|
LogicalKeyboardKey.arrowRight,
|
||||||
|
LogicalKeyboardKey.keyD,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
After Width: | Height: | Size: 3.7 KiB |
After Width: | Height: | Size: 3.6 KiB |
@ -1,4 +1,8 @@
|
|||||||
import 'package:pinball/game/game.dart';
|
// ignore_for_file: comment_references
|
||||||
|
// TODO(alestiago): Revisit ignore lint rule once Kicker is moved to this
|
||||||
|
// package.
|
||||||
|
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
/// Indicates a side of the board.
|
/// Indicates a side of the board.
|
||||||
///
|
///
|
@ -1,5 +1,8 @@
|
|||||||
export 'ball.dart';
|
export 'ball.dart';
|
||||||
|
export 'board_side.dart';
|
||||||
export 'fire_effect.dart';
|
export 'fire_effect.dart';
|
||||||
|
export 'flipper.dart';
|
||||||
export 'initial_position.dart';
|
export 'initial_position.dart';
|
||||||
|
export 'joint_anchor.dart';
|
||||||
export 'layer.dart';
|
export 'layer.dart';
|
||||||
export 'shapes/shapes.dart';
|
export 'shapes/shapes.dart';
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:sandbox/common/common.dart';
|
||||||
|
|
||||||
|
class BasicFlipperGame extends BasicGame {
|
||||||
|
static const info = '''
|
||||||
|
Basic example of how a Flipper works.
|
||||||
|
''';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
|
||||||
|
final center = screenToWorld(camera.viewport.canvasSize! / 2);
|
||||||
|
|
||||||
|
final leftFlipper = Flipper(side: BoardSide.left)
|
||||||
|
..initialPosition = center - Vector2(Flipper.size.x, 0);
|
||||||
|
final rightFlipper = Flipper(side: BoardSide.right)
|
||||||
|
..initialPosition = center + Vector2(Flipper.size.x, 0);
|
||||||
|
|
||||||
|
await addAll([
|
||||||
|
leftFlipper,
|
||||||
|
rightFlipper,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
import 'package:dashbook/dashbook.dart';
|
||||||
|
import 'package:flame/game.dart';
|
||||||
|
import 'package:sandbox/common/common.dart';
|
||||||
|
import 'package:sandbox/stories/flipper/basic.dart';
|
||||||
|
import 'package:sandbox/stories/flipper/tracing.dart';
|
||||||
|
|
||||||
|
void addFlipperStories(Dashbook dashbook) {
|
||||||
|
dashbook.storiesOf('Flipper')
|
||||||
|
..add(
|
||||||
|
'Basic',
|
||||||
|
(context) => GameWidget(
|
||||||
|
game: BasicFlipperGame(),
|
||||||
|
),
|
||||||
|
codeLink: buildSourceLink('flipper/basic.dart'),
|
||||||
|
info: BasicFlipperGame.info,
|
||||||
|
)
|
||||||
|
..add(
|
||||||
|
'Tracing',
|
||||||
|
(context) => GameWidget(
|
||||||
|
game: FlipperTracingGame(),
|
||||||
|
),
|
||||||
|
codeLink: buildSourceLink('flipper/tracing.dart'),
|
||||||
|
info: FlipperTracingGame.info,
|
||||||
|
);
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:sandbox/common/common.dart';
|
||||||
|
|
||||||
|
class FlipperTracingGame extends BasicGame {
|
||||||
|
static const info = '''
|
||||||
|
Basic example of how the Flipper body overlays the sprite.
|
||||||
|
''';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
final center = screenToWorld(camera.viewport.canvasSize! / 2);
|
||||||
|
|
||||||
|
final leftFlipper = Flipper(side: BoardSide.left)
|
||||||
|
..initialPosition = center - Vector2(Flipper.size.x, 0);
|
||||||
|
final rightFlipper = Flipper(side: BoardSide.right)
|
||||||
|
..initialPosition = center + Vector2(Flipper.size.x, 0);
|
||||||
|
|
||||||
|
await addAll([
|
||||||
|
leftFlipper,
|
||||||
|
rightFlipper,
|
||||||
|
]);
|
||||||
|
leftFlipper.trace();
|
||||||
|
rightFlipper.trace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension on BodyComponent {
|
||||||
|
void trace({Color color = Colors.red}) {
|
||||||
|
paint = Paint()..color = color;
|
||||||
|
renderBody = true;
|
||||||
|
body.joints.whereType<RevoluteJoint>().forEach(
|
||||||
|
(joint) => joint.setLimits(0, 0),
|
||||||
|
);
|
||||||
|
body.setType(BodyType.static);
|
||||||
|
|
||||||
|
unawaited(
|
||||||
|
mounted.whenComplete(() {
|
||||||
|
final sprite = children.whereType<SpriteComponent>().first;
|
||||||
|
sprite.paint.color = sprite.paint.color.withOpacity(0.5);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,3 @@
|
|||||||
export 'ball/ball.dart';
|
export 'ball/ball.dart';
|
||||||
|
export 'flipper/flipper.dart';
|
||||||
export 'layer/layer.dart';
|
export 'layer/layer.dart';
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:pinball/game/game.dart';
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group(
|
group(
|
@ -0,0 +1,133 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
import '../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(TestGame.new);
|
||||||
|
|
||||||
|
group('Flipper', () {
|
||||||
|
// TODO(alestiago): Add golden tests.
|
||||||
|
// TODO(alestiago): Consider testing always both left and right Flipper.
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'loads correctly',
|
||||||
|
(game) async {
|
||||||
|
final leftFlipper = Flipper(side: BoardSide.left);
|
||||||
|
final rightFlipper = Flipper(side: BoardSide.right);
|
||||||
|
await game.ready();
|
||||||
|
await game.ensureAddAll([leftFlipper, rightFlipper]);
|
||||||
|
|
||||||
|
expect(game.contains(leftFlipper), isTrue);
|
||||||
|
expect(game.contains(rightFlipper), isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
group('constructor', () {
|
||||||
|
test('sets BoardSide', () {
|
||||||
|
final leftFlipper = Flipper(side: BoardSide.left);
|
||||||
|
expect(leftFlipper.side, equals(leftFlipper.side));
|
||||||
|
|
||||||
|
final rightFlipper = Flipper(side: BoardSide.right);
|
||||||
|
expect(rightFlipper.side, equals(rightFlipper.side));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('body', () {
|
||||||
|
flameTester.test(
|
||||||
|
'is dynamic',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
expect(flipper.body.bodyType, equals(BodyType.dynamic));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'ignores gravity',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
|
||||||
|
expect(flipper.body.gravityScale, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'has greater mass than Ball',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
final ball = Ball(baseColor: Colors.white);
|
||||||
|
|
||||||
|
await game.ready();
|
||||||
|
await game.ensureAddAll([flipper, ball]);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
flipper.body.getMassData().mass,
|
||||||
|
greaterThan(ball.body.getMassData().mass),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('fixtures', () {
|
||||||
|
flameTester.test(
|
||||||
|
'has three',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
|
||||||
|
expect(flipper.body.fixtures.length, equals(3));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'has density',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
|
||||||
|
final fixtures = flipper.body.fixtures;
|
||||||
|
final density = fixtures.fold<double>(
|
||||||
|
0,
|
||||||
|
(sum, fixture) => sum + fixture.density,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(density, greaterThan(0));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'moveDown applies downward velocity',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity, equals(Vector2.zero()));
|
||||||
|
flipper.moveDown();
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, lessThan(0));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'moveUp applies upward velocity',
|
||||||
|
(game) async {
|
||||||
|
final flipper = Flipper(side: BoardSide.left);
|
||||||
|
await game.ensureAdd(flipper);
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity, equals(Vector2.zero()));
|
||||||
|
flipper.moveUp();
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, greaterThan(0));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,169 @@
|
|||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
import '../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
final flameTester = FlameTester(PinballGameTest.create);
|
||||||
|
|
||||||
|
group('FlipperController', () {
|
||||||
|
group('onKeyEvent', () {
|
||||||
|
final leftKeys = UnmodifiableListView([
|
||||||
|
LogicalKeyboardKey.arrowLeft,
|
||||||
|
LogicalKeyboardKey.keyA,
|
||||||
|
]);
|
||||||
|
final rightKeys = UnmodifiableListView([
|
||||||
|
LogicalKeyboardKey.arrowRight,
|
||||||
|
LogicalKeyboardKey.keyD,
|
||||||
|
]);
|
||||||
|
|
||||||
|
group('and Flipper is left', () {
|
||||||
|
late Flipper flipper;
|
||||||
|
late FlipperController controller;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
flipper = Flipper(side: BoardSide.left);
|
||||||
|
controller = FlipperController(flipper);
|
||||||
|
flipper.add(controller);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyDownEvents(leftKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'moves upwards '
|
||||||
|
'when ${event.logicalKey.keyLabel} is pressed',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isPositive);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyUpEvents(leftKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'moves downwards '
|
||||||
|
'when ${event.logicalKey.keyLabel} is released',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isNegative);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyUpEvents(rightKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'does nothing '
|
||||||
|
'when ${event.logicalKey.keyLabel} is released',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isZero);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyDownEvents(rightKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'does nothing '
|
||||||
|
'when ${event.logicalKey.keyLabel} is pressed',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isZero);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('and Flipper is right', () {
|
||||||
|
late Flipper flipper;
|
||||||
|
late FlipperController controller;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
flipper = Flipper(side: BoardSide.right);
|
||||||
|
controller = FlipperController(flipper);
|
||||||
|
flipper.add(controller);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyDownEvents(rightKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'moves upwards '
|
||||||
|
'when ${event.logicalKey.keyLabel} is pressed',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isPositive);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyUpEvents(rightKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'moves downwards '
|
||||||
|
'when ${event.logicalKey.keyLabel} is released',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isNegative);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyUpEvents(leftKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'does nothing '
|
||||||
|
'when ${event.logicalKey.keyLabel} is released',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isZero);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testRawKeyDownEvents(leftKeys, (event) {
|
||||||
|
flameTester.test(
|
||||||
|
'does nothing '
|
||||||
|
'when ${event.logicalKey.keyLabel} is pressed',
|
||||||
|
(game) async {
|
||||||
|
await game.ready();
|
||||||
|
await game.add(flipper);
|
||||||
|
controller.onKeyEvent(event, {});
|
||||||
|
|
||||||
|
expect(flipper.body.linearVelocity.y, isZero);
|
||||||
|
expect(flipper.body.linearVelocity.x, isZero);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -1,275 +0,0 @@
|
|||||||
// ignore_for_file: cascade_invocations
|
|
||||||
|
|
||||||
import 'dart:collection';
|
|
||||||
|
|
||||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
|
||||||
import 'package:flame_test/flame_test.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter/services.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:pinball/game/game.dart';
|
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
|
||||||
|
|
||||||
import '../../helpers/helpers.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
TestWidgetsFlutterBinding.ensureInitialized();
|
|
||||||
final flameTester = FlameTester(PinballGameTest.create);
|
|
||||||
|
|
||||||
group(
|
|
||||||
'Flipper',
|
|
||||||
() {
|
|
||||||
// TODO(alestiago): Add golden tests.
|
|
||||||
flameTester.test(
|
|
||||||
'loads correctly',
|
|
||||||
(game) async {
|
|
||||||
final leftFlipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
final rightFlipper = Flipper(
|
|
||||||
side: BoardSide.right,
|
|
||||||
);
|
|
||||||
await game.ready();
|
|
||||||
await game.ensureAddAll([leftFlipper, rightFlipper]);
|
|
||||||
|
|
||||||
expect(game.contains(leftFlipper), isTrue);
|
|
||||||
expect(game.contains(rightFlipper), isTrue);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
group('constructor', () {
|
|
||||||
test('sets BoardSide', () {
|
|
||||||
final leftFlipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(leftFlipper.side, equals(leftFlipper.side));
|
|
||||||
|
|
||||||
final rightFlipper = Flipper(
|
|
||||||
side: BoardSide.right,
|
|
||||||
);
|
|
||||||
expect(rightFlipper.side, equals(rightFlipper.side));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
group('body', () {
|
|
||||||
flameTester.test(
|
|
||||||
'is dynamic',
|
|
||||||
(game) async {
|
|
||||||
final flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
|
|
||||||
expect(flipper.body.bodyType, equals(BodyType.dynamic));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'ignores gravity',
|
|
||||||
(game) async {
|
|
||||||
final flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
|
|
||||||
expect(flipper.body.gravityScale, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'has greater mass than Ball',
|
|
||||||
(game) async {
|
|
||||||
final flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
final ball = Ball(baseColor: Colors.white);
|
|
||||||
|
|
||||||
await game.ready();
|
|
||||||
await game.ensureAddAll([flipper, ball]);
|
|
||||||
|
|
||||||
expect(
|
|
||||||
flipper.body.getMassData().mass,
|
|
||||||
greaterThan(ball.body.getMassData().mass),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
group('fixtures', () {
|
|
||||||
flameTester.test(
|
|
||||||
'has three',
|
|
||||||
(game) async {
|
|
||||||
final flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
|
|
||||||
expect(flipper.body.fixtures.length, equals(3));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'has density',
|
|
||||||
(game) async {
|
|
||||||
final flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
|
|
||||||
final fixtures = flipper.body.fixtures;
|
|
||||||
final density = fixtures.fold<double>(
|
|
||||||
0,
|
|
||||||
(sum, fixture) => sum + fixture.density,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(density, greaterThan(0));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
group('onKeyEvent', () {
|
|
||||||
final leftKeys = UnmodifiableListView([
|
|
||||||
LogicalKeyboardKey.arrowLeft,
|
|
||||||
LogicalKeyboardKey.keyA,
|
|
||||||
]);
|
|
||||||
final rightKeys = UnmodifiableListView([
|
|
||||||
LogicalKeyboardKey.arrowRight,
|
|
||||||
LogicalKeyboardKey.keyD,
|
|
||||||
]);
|
|
||||||
|
|
||||||
group('and Flipper is left', () {
|
|
||||||
late Flipper flipper;
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
flipper = Flipper(
|
|
||||||
side: BoardSide.left,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyDownEvents(leftKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'moves upwards '
|
|
||||||
'when ${event.logicalKey.keyLabel} is pressed',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isPositive);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyUpEvents(leftKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'moves downwards '
|
|
||||||
'when ${event.logicalKey.keyLabel} is released',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isNegative);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyUpEvents(rightKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'does nothing '
|
|
||||||
'when ${event.logicalKey.keyLabel} is released',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isZero);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyDownEvents(rightKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'does nothing '
|
|
||||||
'when ${event.logicalKey.keyLabel} is pressed',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isZero);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
group('and Flipper is right', () {
|
|
||||||
late Flipper flipper;
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
flipper = Flipper(
|
|
||||||
side: BoardSide.right,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyDownEvents(rightKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'moves upwards '
|
|
||||||
'when ${event.logicalKey.keyLabel} is pressed',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isPositive);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyUpEvents(rightKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'moves downwards '
|
|
||||||
'when ${event.logicalKey.keyLabel} is released',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isNegative);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyUpEvents(leftKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'does nothing '
|
|
||||||
'when ${event.logicalKey.keyLabel} is released',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isZero);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testRawKeyDownEvents(leftKeys, (event) {
|
|
||||||
flameTester.test(
|
|
||||||
'does nothing '
|
|
||||||
'when ${event.logicalKey.keyLabel} is pressed',
|
|
||||||
(game) async {
|
|
||||||
await game.ensureAdd(flipper);
|
|
||||||
flipper.onKeyEvent(event, {});
|
|
||||||
|
|
||||||
expect(flipper.body.linearVelocity.y, isZero);
|
|
||||||
expect(flipper.body.linearVelocity.x, isZero);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
Loading…
Reference in new issue