mirror of https://github.com/flutter/pinball.git
parent
58e2998b97
commit
9cf4214c2f
@ -1,46 +0,0 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/game.dart';
|
||||
|
||||
// TODO(erickzanardo): Keeping this inside our code base so we can experiment
|
||||
// with the idea, but this is a potential upstream change on Flame.
|
||||
|
||||
/// {@template blueprint}
|
||||
/// A [Blueprint] is a virtual way of grouping [Component]s that are related.
|
||||
/// {@endtemplate blueprint}
|
||||
class Blueprint {
|
||||
/// {@macro blueprint}
|
||||
Blueprint({
|
||||
Iterable<Component>? components,
|
||||
Iterable<Blueprint>? blueprints,
|
||||
}) {
|
||||
if (components != null) _components.addAll(components);
|
||||
if (blueprints != null) {
|
||||
_blueprints.addAll(blueprints);
|
||||
for (final blueprint in blueprints) {
|
||||
_components.addAll(blueprint.components);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final List<Component> _components = [];
|
||||
|
||||
final List<Blueprint> _blueprints = [];
|
||||
|
||||
Future<void> _addToParent(Component parent) async {
|
||||
await parent.addAll(_components);
|
||||
}
|
||||
|
||||
/// Returns a copy of the components built by this blueprint.
|
||||
List<Component> get components => List.unmodifiable(_components);
|
||||
|
||||
/// Returns a copy of the blueprints built by this blueprint.
|
||||
List<Blueprint> get blueprints => List.unmodifiable(_blueprints);
|
||||
}
|
||||
|
||||
/// Adds helper methods regarding [Blueprint]s to [FlameGame].
|
||||
extension FlameGameBlueprint on Component {
|
||||
/// Shortcut to add a [Blueprint]s components to its parent.
|
||||
Future<void> addFromBlueprint(Blueprint blueprint) async {
|
||||
await blueprint._addToParent(this);
|
||||
}
|
||||
}
|
@ -1,86 +0,0 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('Blueprint', () {
|
||||
final flameTester = FlameTester(FlameGame.new);
|
||||
|
||||
test('correctly sets and gets components', () {
|
||||
final component1 = Component();
|
||||
final component2 = Component();
|
||||
final blueprint = Blueprint(
|
||||
components: [
|
||||
component1,
|
||||
component2,
|
||||
],
|
||||
);
|
||||
|
||||
expect(blueprint.components.length, 2);
|
||||
expect(blueprint.components, contains(component1));
|
||||
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 {
|
||||
final blueprint = Blueprint(
|
||||
components: [
|
||||
Component(),
|
||||
Component(),
|
||||
],
|
||||
);
|
||||
await game.addFromBlueprint(blueprint);
|
||||
await game.ready();
|
||||
|
||||
for (final component in blueprint.components) {
|
||||
expect(game.children.contains(component), isTrue);
|
||||
}
|
||||
});
|
||||
|
||||
flameTester.test('adds components from a child Blueprint', (game) async {
|
||||
final childBlueprint = Blueprint(
|
||||
components: [
|
||||
Component(),
|
||||
Component(),
|
||||
],
|
||||
);
|
||||
final parentBlueprint = Blueprint(
|
||||
components: [
|
||||
Component(),
|
||||
Component(),
|
||||
],
|
||||
blueprints: [
|
||||
childBlueprint,
|
||||
],
|
||||
);
|
||||
|
||||
await game.addFromBlueprint(parentBlueprint);
|
||||
await game.ready();
|
||||
|
||||
for (final component in childBlueprint.components) {
|
||||
expect(game.children, contains(component));
|
||||
expect(parentBlueprint.components, contains(component));
|
||||
}
|
||||
for (final component in parentBlueprint.components) {
|
||||
expect(game.children, contains(component));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue