mirror of https://github.com/flutter/pinball.git
commit
6c534c61ea
@ -0,0 +1,34 @@
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
|
||||
/// Forces a given [BodyComponent] to position their [body] to an
|
||||
/// [initialPosition].
|
||||
///
|
||||
/// Note: If the [initialPosition] is set after the [BodyComponent] has been
|
||||
/// loaded it will have no effect; defaulting to [Vector2.zero].
|
||||
mixin InitialPosition<T extends Forge2DGame> on BodyComponent<T> {
|
||||
final Vector2 _initialPosition = Vector2.zero();
|
||||
|
||||
set initialPosition(Vector2 value) {
|
||||
assert(
|
||||
!isLoaded,
|
||||
'Cannot set initialPosition after component has already loaded.',
|
||||
);
|
||||
if (value == initialPosition) return;
|
||||
|
||||
_initialPosition.setFrom(value);
|
||||
}
|
||||
|
||||
/// The initial position of the [body].
|
||||
Vector2 get initialPosition => _initialPosition;
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
// TODO(alestiago): Investiagate why body.position.setFrom(initialPosition)
|
||||
// works for some components and not others.
|
||||
assert(
|
||||
body.position == initialPosition,
|
||||
'Body position does not match initialPosition.',
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
// 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/game/game.dart';
|
||||
|
||||
class TestBodyComponent extends BodyComponent with InitialPosition {
|
||||
@override
|
||||
Body createBody() {
|
||||
return world.createBody(BodyDef());
|
||||
}
|
||||
}
|
||||
|
||||
class TestPositionedBodyComponent extends BodyComponent with InitialPosition {
|
||||
@override
|
||||
Body createBody() {
|
||||
return world.createBody(BodyDef()..position = initialPosition);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
final flameTester = FlameTester(Forge2DGame.new);
|
||||
group('InitialPosition', () {
|
||||
test('correctly sets and gets', () {
|
||||
final component = TestBodyComponent()..initialPosition = Vector2(1, 2);
|
||||
expect(component.initialPosition, Vector2(1, 2));
|
||||
});
|
||||
|
||||
flameTester.test(
|
||||
'throws AssertionError '
|
||||
'when BodyDef is not positioned with initialPosition',
|
||||
(game) async {
|
||||
final component = TestBodyComponent()
|
||||
..initialPosition = Vector2.all(
|
||||
10,
|
||||
);
|
||||
await expectLater(
|
||||
() => game.ensureAdd(component),
|
||||
throwsAssertionError,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'positions correctly',
|
||||
(game) async {
|
||||
final position = Vector2.all(10);
|
||||
final component = TestPositionedBodyComponent()
|
||||
..initialPosition = position;
|
||||
await game.ensureAdd(component);
|
||||
expect(component.body.position, equals(position));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'deafaults to zero '
|
||||
'when no initialPosition is given',
|
||||
(game) async {
|
||||
final component = TestBodyComponent();
|
||||
await game.ensureAdd(component);
|
||||
expect(component.body.position, equals(Vector2.zero()));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'setting throws AssertionError '
|
||||
'when component has loaded',
|
||||
(game) async {
|
||||
final component = TestBodyComponent();
|
||||
await game.ensureAdd(component);
|
||||
|
||||
expect(component.isLoaded, isTrue);
|
||||
expect(
|
||||
() => component.initialPosition = Vector2.all(4),
|
||||
throwsAssertionError,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
Loading…
Reference in new issue