mirror of https://github.com/flutter/pinball.git
parent
be299bcd51
commit
5fdc628f47
@ -0,0 +1,28 @@
|
||||
import 'package:flame_forge2d/body_component.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:forge2d/forge2d.dart';
|
||||
|
||||
class Ball extends BodyComponent {
|
||||
Ball({
|
||||
required Vector2 position,
|
||||
}) : _position = position {
|
||||
// TODO(alestiago): Use asset instead of color when provided.
|
||||
paint = Paint()..color = const Color(0xFFFFFFFF);
|
||||
}
|
||||
|
||||
final Vector2 _position;
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
final shape = CircleShape()..radius = 2;
|
||||
|
||||
final fixtureDef = FixtureDef(shape)..density = 1;
|
||||
|
||||
final bodyDef = BodyDef()
|
||||
..userData = this
|
||||
..position = _position
|
||||
..type = BodyType.dynamic;
|
||||
|
||||
return world.createBody(bodyDef)..createFixture(fixtureDef);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
export 'ball.dart';
|
@ -1,2 +1,3 @@
|
||||
export 'components/components.dart';
|
||||
export 'pinball_game.dart';
|
||||
export 'view/pinball_game_page.dart';
|
||||
|
@ -0,0 +1,72 @@
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('Ball', () {
|
||||
group('body', () {
|
||||
FlameTester(PinballGame.new).test(
|
||||
'positions correctly',
|
||||
(game) async {
|
||||
final position = Vector2(10, 10);
|
||||
final ball = Ball(position: position);
|
||||
await game.ensureAdd(ball);
|
||||
|
||||
expect(ball.body.position, position);
|
||||
},
|
||||
);
|
||||
|
||||
FlameTester(PinballGame.new).test(
|
||||
'is dynamic',
|
||||
(game) async {
|
||||
final position = Vector2(10, 10);
|
||||
final ball = Ball(position: position);
|
||||
await game.ensureAdd(ball);
|
||||
|
||||
expect(ball.body.bodyType, equals(BodyType.dynamic));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('first fixture', () {
|
||||
FlameTester(PinballGame.new).test(
|
||||
'exists',
|
||||
(game) async {
|
||||
final position = Vector2(10, 10);
|
||||
final ball = Ball(position: position);
|
||||
await game.ensureAdd(ball);
|
||||
|
||||
expect(ball.body.fixtures.length, equals(1));
|
||||
},
|
||||
);
|
||||
|
||||
FlameTester(PinballGame.new).test(
|
||||
'is dense',
|
||||
(game) async {
|
||||
final position = Vector2(10, 10);
|
||||
final ball = Ball(position: position);
|
||||
await game.ensureAdd(ball);
|
||||
|
||||
final fixture = ball.body.fixtures[0];
|
||||
expect(fixture.density, greaterThan(0));
|
||||
},
|
||||
);
|
||||
|
||||
FlameTester(PinballGame.new).test(
|
||||
'shape is circular',
|
||||
(game) async {
|
||||
final position = Vector2(10, 10);
|
||||
final ball = Ball(position: position);
|
||||
await game.ensureAdd(ball);
|
||||
|
||||
final fixture = ball.body.fixtures[0];
|
||||
expect(fixture.shape.shapeType, equals(ShapeType.circle));
|
||||
expect(fixture.shape.radius, equals(2));
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue