test: joint tests

pull/10/head
Allison Ryan 3 years ago
parent 902d3092ee
commit 85936df74e

@ -1,41 +0,0 @@
import 'package:flame_forge2d/body_component.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_forge2d/forge2d_game.dart';
import 'package:forge2d/forge2d.dart';
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(topLeft, topRight),
Wall(topRight, bottomRight),
Wall(bottomRight, bottomLeft),
Wall(bottomLeft, topLeft),
];
}
class Wall extends BodyComponent {
Wall(this.start, this.end);
final Vector2 start;
final Vector2 end;
@override
Body createBody() {
final shape = EdgeShape()..set(start, end);
final fixtureDef = FixtureDef(shape)
..restitution = 0.0
..friction = 0.3;
final bodyDef = BodyDef()
..userData = this
..position = Vector2.zero()
..type = BodyType.static;
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}

@ -1,4 +1,3 @@
export 'ball.dart';
export 'boundaries.dart';
export 'plunger.dart';
export 'score_points.dart';

@ -1,43 +1,12 @@
import 'package:flame/input.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pinball/game/game.dart';
class PinballGame extends Forge2DGame with FlameBloc, KeyboardEvents {
late Plunger plunger;
import 'package:pinball/game/game.dart';
class PinballGame extends Forge2DGame with FlameBloc {
@override
Future<void> onLoad() async {
await super.onLoad();
addContactCallback(BallScorePointsCallback());
final boundaries = createBoundaries(this)..forEach(add);
final bottomWall = boundaries[2];
final center = screenToWorld(camera.viewport.effectiveSize / 2);
await add(plunger = Plunger(Vector2(center.x, center.y)));
world.createJoint(
PlungerAnchorPrismaticJointDef(plunger: plunger, anchor: bottomWall),
);
}
@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
if (event is RawKeyUpEvent &&
event.data.logicalKey == LogicalKeyboardKey.space) {
plunger.release();
}
if (event is RawKeyDownEvent &&
event.data.logicalKey == LogicalKeyboardKey.space) {
plunger.pull();
}
return KeyEventResult.handled;
}
}

@ -125,12 +125,12 @@ void main() {
setUp(() {
plunger = Plunger(Vector2.zero());
anchor = Plunger(Vector2(0, -5));
anchor = Plunger(Vector2(0, -1));
});
group('initializes with', () {
flameTester.test(
'plunger as bodyA',
'plunger body as bodyA',
(game) async {
await game.ensureAddAll([plunger, anchor]);
@ -139,12 +139,12 @@ void main() {
anchor: anchor,
);
expect(jointDef.bodyA, equals(plunger));
expect(jointDef.bodyA, equals(plunger.body));
},
);
flameTester.test(
'anchor as bodyB',
'anchor body as bodyB',
(game) async {
await game.ensureAddAll([plunger, anchor]);
@ -154,7 +154,7 @@ void main() {
);
game.world.createJoint(jointDef);
expect(jointDef.bodyB, equals(anchor));
expect(jointDef.bodyB, equals(anchor.body));
},
);
@ -203,5 +203,44 @@ void main() {
},
);
});
flameTester.widgetTest(
'plunger cannot go below anchor',
(game, tester) async {
await game.ensureAddAll([plunger, anchor]);
final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger,
anchor: anchor,
);
game.world.createJoint(jointDef);
plunger.pull();
await tester.pump(const Duration(seconds: 1));
expect(plunger.body.position.y > anchor.body.position.y, isTrue);
},
);
flameTester.widgetTest(
'plunger cannot excessively exceed starting position',
(game, tester) async {
await game.ensureAddAll([plunger, anchor]);
final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger,
anchor: anchor,
);
game.world.createJoint(jointDef);
plunger.pull();
await tester.pump(const Duration(seconds: 1));
plunger.release();
await tester.pump(const Duration(seconds: 1));
expect(plunger.body.position.y < 1, isTrue);
},
);
});
}

Loading…
Cancel
Save