feat: included default value

pull/50/head
alestiago 4 years ago
parent daede67b67
commit 619badd89f

@ -2,9 +2,24 @@ 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 been loaded.',
);
if (value == initialPosition) return;
_initialPosition.setFrom(value);
}
/// The initial position of the [body].
late final Vector2 initialPosition;
Vector2 get initialPosition => _initialPosition;
@override
Future<void> onLoad() async {

@ -28,11 +28,39 @@ void main() {
);
});
flameTester.test('positions correctly', (game) async {
final position = Vector2.all(10);
final component = TestBodyComponent()..initialPosition = position;
await game.ensureAdd(component);
expect(component.body.position, equals(position));
});
flameTester.test(
'positions correctly',
(game) async {
final position = Vector2.all(10);
final component = TestBodyComponent()..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…
Cancel
Save