test: slingshot

pull/148/head
Allison Ryan 4 years ago
parent 06c737ca3c
commit fefeb245d3

@ -17,16 +17,19 @@ class Slingshots extends Forge2DBlueprint {
angle: -1.5 * (math.pi / 180), angle: -1.5 * (math.pi / 180),
spritePath: Assets.images.slingshot.leftUpper.keyName, spritePath: Assets.images.slingshot.leftUpper.keyName,
)..initialPosition = Vector2(-29, 1.5); )..initialPosition = Vector2(-29, 1.5);
final leftLowerSlingshot = Slingshot( final leftLowerSlingshot = Slingshot(
length: 3.54, length: 3.54,
angle: -29.1 * (math.pi / 180), angle: -29.1 * (math.pi / 180),
spritePath: Assets.images.slingshot.leftLower.keyName, spritePath: Assets.images.slingshot.leftLower.keyName,
)..initialPosition = Vector2(-31, -6.2); )..initialPosition = Vector2(-31, -6.2);
final rightUpperSlingshot = Slingshot( final rightUpperSlingshot = Slingshot(
length: 5.64, length: 5.64,
angle: 1 * (math.pi / 180), angle: 1 * (math.pi / 180),
spritePath: Assets.images.slingshot.rightUpper.keyName, spritePath: Assets.images.slingshot.rightUpper.keyName,
)..initialPosition = Vector2(22.3, 1.58); )..initialPosition = Vector2(22.3, 1.58);
final rightLowerSlingshot = Slingshot( final rightLowerSlingshot = Slingshot(
length: 3.46, length: 3.46,
angle: 26.8 * (math.pi / 180), angle: 26.8 * (math.pi / 180),
@ -68,12 +71,12 @@ class Slingshot extends BodyComponent with InitialPosition {
final topCircleShape = CircleShape()..radius = circleRadius; final topCircleShape = CircleShape()..radius = circleRadius;
topCircleShape.position.setValues(0, _length / 2); topCircleShape.position.setValues(0, _length / 2);
final topCircleFixtureDef = FixtureDef(topCircleShape); final topCircleFixtureDef = FixtureDef(topCircleShape)..friction = 0;
fixturesDef.add(topCircleFixtureDef); fixturesDef.add(topCircleFixtureDef);
final bottomCircleShape = CircleShape()..radius = circleRadius; final bottomCircleShape = CircleShape()..radius = circleRadius;
bottomCircleShape.position.setValues(0, -_length / 2); bottomCircleShape.position.setValues(0, -_length / 2);
final bottomCircleFixtureDef = FixtureDef(bottomCircleShape); final bottomCircleFixtureDef = FixtureDef(bottomCircleShape)..friction = 0;
fixturesDef.add(bottomCircleFixtureDef); fixturesDef.add(bottomCircleFixtureDef);
final leftEdgeShape = EdgeShape() final leftEdgeShape = EdgeShape()
@ -81,17 +84,20 @@ class Slingshot extends BodyComponent with InitialPosition {
Vector2(circleRadius, _length / 2), Vector2(circleRadius, _length / 2),
Vector2(circleRadius, -_length / 2), Vector2(circleRadius, -_length / 2),
); );
final leftEdgeShapeFixtureDef = FixtureDef(leftEdgeShape)..restitution = 5; final leftEdgeShapeFixtureDef = FixtureDef(leftEdgeShape)
..friction = 0
..restitution = 5;
fixturesDef.add(leftEdgeShapeFixtureDef); fixturesDef.add(leftEdgeShapeFixtureDef);
final righttEdgeShape = EdgeShape() final rightEdgeShape = EdgeShape()
..set( ..set(
Vector2(-circleRadius, _length / 2), Vector2(-circleRadius, _length / 2),
Vector2(-circleRadius, -_length / 2), Vector2(-circleRadius, -_length / 2),
); );
final righttEdgeShapeFixtureDef = FixtureDef(righttEdgeShape) final rightEdgeShapeFixtureDef = FixtureDef(rightEdgeShape)
..friction = 0
..restitution = 5; ..restitution = 5;
fixturesDef.add(righttEdgeShapeFixtureDef); fixturesDef.add(rightEdgeShapeFixtureDef);
return fixturesDef; return fixturesDef;
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

@ -0,0 +1,97 @@
// 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_components/pinball_components.dart';
import '../../helpers/helpers.dart';
void main() {
group('Slingshot', () {
final flameTester = FlameTester(TestGame.new);
const length = 2.0;
const angle = 0.0;
final spritePath = Assets.images.slingshot.leftUpper.keyName;
flameTester.testGameWidget(
'renders correctly',
setUp: (game, tester) async {
await game.addFromBlueprint(Slingshots());
await game.ready();
game.camera.followVector2(Vector2.zero());
},
// TODO(allisonryan0002): enable test when workflows are fixed.
// verify: (game, tester) async {
// await expectLater(
// find.byGame<Forge2DGame>(),
// matchesGoldenFile('golden/slingshots.png'),
// );
// },
);
flameTester.test(
'loads correctly',
(game) async {
final slingshot = Slingshot(
length: length,
angle: angle,
spritePath: spritePath,
);
await game.ensureAdd(slingshot);
expect(game.contains(slingshot), isTrue);
},
);
flameTester.test(
'body is static',
(game) async {
final slingshot = Slingshot(
length: length,
angle: angle,
spritePath: spritePath,
);
await game.ensureAdd(slingshot);
expect(slingshot.body.bodyType, equals(BodyType.static));
},
);
flameTester.test(
'has restitution',
(game) async {
final slingshot = Slingshot(
length: length,
angle: angle,
spritePath: spritePath,
);
await game.ensureAdd(slingshot);
final totalRestitution = slingshot.body.fixtures.fold<double>(
0,
(total, fixture) => total + fixture.restitution,
);
expect(totalRestitution, greaterThan(0));
},
);
flameTester.test(
'has no friction',
(game) async {
final slingshot = Slingshot(
length: length,
angle: angle,
spritePath: spritePath,
);
await game.ensureAdd(slingshot);
final totalFriction = slingshot.body.fixtures.fold<double>(
0,
(total, fixture) => total + fixture.friction,
);
expect(totalFriction, equals(0));
},
);
});
}
Loading…
Cancel
Save