feat: included blueprints getter

pull/220/head
alestiago 3 years ago
parent 38602c8dba
commit f3568a6380

@ -1,5 +1,6 @@
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flutter/material.dart';
// TODO(erickzanardo): Keeping this inside our code base // TODO(erickzanardo): Keeping this inside our code base
// so we can experiment with the idea, but this is a // so we can experiment with the idea, but this is a
@ -18,6 +19,7 @@ class Blueprint extends Component {
}) { }) {
if (components != null) _components.addAll(components); if (components != null) _components.addAll(components);
if (blueprints != null) { if (blueprints != null) {
_blueprints.addAll(blueprints);
for (final blueprint in blueprints) { for (final blueprint in blueprints) {
_components.addAll(blueprint.components); _components.addAll(blueprint.components);
} }
@ -26,12 +28,17 @@ class Blueprint extends Component {
final List<Component> _components = []; final List<Component> _components = [];
final List<Component> _blueprints = [];
Future<void> _addToParent(Component parent) async { Future<void> _addToParent(Component parent) async {
await parent.addAll(_components); await parent.addAll(_components);
} }
/// Returns a copy of the components built by this blueprint. /// Returns a copy of the components built by this blueprint.
List<Component> get components => List.unmodifiable(_components); List<Component> get components => List.unmodifiable(_components);
/// Returns a copy of the blueprints built by this blueprint.
List<Component> get blueprints => List.unmodifiable(_blueprints);
} }
/// Adds helper methods regarding [Blueprint]s to [FlameGame]. /// Adds helper methods regarding [Blueprint]s to [FlameGame].

@ -27,6 +27,18 @@ void main() {
expect(blueprint.components, contains(component2)); expect(blueprint.components, contains(component2));
}); });
test('correctly sets and gets blueprints', () {
final blueprint2 = Blueprint(
components: [Component()],
);
final blueprint1 = Blueprint(
components: [Component()],
blueprints: [blueprint2],
);
expect(blueprint1.blueprints, contains(blueprint2));
});
flameTester.test('adds the components to parent on attach', (game) async { flameTester.test('adds the components to parent on attach', (game) async {
final blueprint = Blueprint( final blueprint = Blueprint(
components: [ components: [

Loading…
Cancel
Save