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 /// Forces a given [BodyComponent] to position their [body] to an
/// [initialPosition]. /// [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> { 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]. /// The initial position of the [body].
late final Vector2 initialPosition; Vector2 get initialPosition => _initialPosition;
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {

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