chore: modified doc for Layered and added one test for nested

pull/87/head
RuiAlonso 4 years ago
parent 780e205dc0
commit c786d730fd

@ -8,6 +8,9 @@ import 'package:flutter/material.dart';
/// [BodyComponent]s with compatible [Layer]s can collide with each other, /// [BodyComponent]s with compatible [Layer]s can collide with each other,
/// ignoring others. This compatibility depends on bit masking operation /// ignoring others. This compatibility depends on bit masking operation
/// between layers. For more information read: https://en.wikipedia.org/wiki/Mask_(computing). /// between layers. For more information read: https://en.wikipedia.org/wiki/Mask_(computing).
///
/// A parent [Layered] have priority against its children's layer. Them won't be
/// changed but will be ignored.
/// {@endtemplate} /// {@endtemplate}
mixin Layered<T extends Forge2DGame> on BodyComponent<T> { mixin Layered<T extends Forge2DGame> on BodyComponent<T> {
Layer _layer = Layer.all; Layer _layer = Layer.all;

@ -14,6 +14,24 @@ class TestBodyComponent extends BodyComponent with Layered {
} }
} }
class TestNestedBodyComponent extends BodyComponent with Layered {
TestNestedBodyComponent({required this.childLayer});
final Layer childLayer;
@override
Body createBody() {
final fixtureDef = FixtureDef(CircleShape());
return world.createBody(BodyDef())..createFixture(fixtureDef);
}
@override
Future<void> onLoad() async {
await super.onLoad();
await add(TestBodyComponent()..layer = childLayer);
}
}
void main() { void main() {
final flameTester = FlameTester(Forge2DGame.new); final flameTester = FlameTester(Forge2DGame.new);
@ -121,6 +139,24 @@ void main() {
expect(component.layer, equals(Layer.all)); expect(component.layer, equals(Layer.all));
}, },
); );
flameTester.test(
'nested children will keep their layer',
(game) async {
const parentLayer = Layer.jetpack;
const childLayer = Layer.board;
final component = TestNestedBodyComponent(childLayer: childLayer)
..layer = parentLayer;
await game.ensureAdd(component);
expect(childLayer, isNot(equals(parentLayer)));
for (final child in component.children) {
expect((child as TestBodyComponent).layer, equals(childLayer));
}
},
);
}); });
group('LayerMaskBits', () { group('LayerMaskBits', () {

Loading…
Cancel
Save