refactor: convert to mixin

pull/157/head
Allison Ryan 4 years ago
parent d06c505ca1
commit d0947fe9aa

@ -18,12 +18,17 @@ extension BodyTrace on BodyComponent {
}
}
extension Forge2DGameTrace on Forge2DGame {
void traceAllBodies({
mixin Traceable on Forge2DGame {
bool get trace;
Future<void> traceAllBodies({
Color color = const Color(0xFFFF0000),
}) {
children
.whereType<BodyComponent>()
.forEach((bodyComponent) => bodyComponent.trace());
}) async {
if (trace) {
await ready();
children
.whereType<BodyComponent>()
.forEach((bodyComponent) => bodyComponent.trace());
}
}
}

@ -5,10 +5,11 @@ import 'package:pinball_components/pinball_components.dart';
import 'package:sandbox/common/common.dart';
import 'package:sandbox/stories/ball/basic_ball_game.dart';
class FlipperGame extends BasicBallGame with KeyboardEvents {
class FlipperGame extends BasicBallGame with KeyboardEvents, Traceable {
FlipperGame({
required this.trace,
}) : super(color: const Color(0xFFFF0000));
required bool trace,
}) : _trace = trace,
super(color: const Color(0xFFFF0000));
static const info = '''
Shows how a Flipper works.
@ -17,7 +18,10 @@ class FlipperGame extends BasicBallGame with KeyboardEvents {
- Tap anywhere on the screen to spawn a ball into the game.
''';
final bool trace;
final bool _trace;
@override
bool get trace => _trace;
static const _leftFlipperKeys = [
LogicalKeyboardKey.arrowLeft,
@ -47,9 +51,8 @@ class FlipperGame extends BasicBallGame with KeyboardEvents {
leftFlipper,
rightFlipper,
]);
await ready();
if (trace) traceAllBodies();
await traceAllBodies();
}
@override

@ -3,10 +3,11 @@ import 'package:pinball_components/pinball_components.dart';
import 'package:sandbox/common/common.dart';
import 'package:sandbox/stories/ball/basic_ball_game.dart';
class KickerGame extends BasicBallGame {
class KickerGame extends BasicBallGame with Traceable {
KickerGame({
required this.trace,
}) : super(color: const Color(0xFFFF0000));
required bool trace,
}) : _trace = trace,
super(color: const Color(0xFFFF0000));
static const info = '''
Shows how Kickers are rendered.
@ -15,7 +16,10 @@ class KickerGame extends BasicBallGame {
- Tap anywhere on the screen to spawn a ball into the game.
''';
final bool trace;
final bool _trace;
@override
bool get trace => _trace;
@override
Future<void> onLoad() async {
@ -30,8 +34,7 @@ class KickerGame extends BasicBallGame {
final rightKicker = Kicker(side: BoardSide.right)
..initialPosition = Vector2(center.x + (Kicker.size.x * 2), center.y);
await add(rightKicker);
await ready();
if (trace) traceAllBodies();
await traceAllBodies();
}
}

@ -3,10 +3,11 @@ import 'package:pinball_components/pinball_components.dart';
import 'package:sandbox/common/common.dart';
import 'package:sandbox/stories/ball/basic_ball_game.dart';
class SlingshotGame extends BasicBallGame {
class SlingshotGame extends BasicBallGame with Traceable {
SlingshotGame({
required this.trace,
}) : super(color: const Color(0xFFFF0000));
required bool trace,
}) : _trace = trace,
super(color: const Color(0xFFFF0000));
static const info = '''
Shows how Slingshots are rendered.
@ -15,17 +16,16 @@ class SlingshotGame extends BasicBallGame {
- Tap anywhere on the screen to spawn a ball into the game.
''';
final bool trace;
final bool _trace;
@override
bool get trace => _trace;
@override
Future<void> onLoad() async {
await super.onLoad();
await addFromBlueprint(Slingshots());
await ready();
camera.followVector2(Vector2.zero());
if (trace) traceAllBodies();
await traceAllBodies();
}
}

@ -5,10 +5,11 @@ import 'package:pinball_components/pinball_components.dart';
import 'package:sandbox/common/common.dart';
import 'package:sandbox/stories/ball/basic_ball_game.dart';
class SparkyBumperGame extends BasicBallGame {
class SparkyBumperGame extends BasicBallGame with Traceable {
SparkyBumperGame({
required this.trace,
}) : super(color: const Color(0xFF0000FF));
required bool trace,
}) : _trace = trace,
super(color: const Color(0xFF0000FF));
static const info = '''
Shows how a SparkyBumper is rendered.
@ -16,7 +17,10 @@ class SparkyBumperGame extends BasicBallGame {
Activate the "trace" parameter to overlay the body.
''';
final bool trace;
final bool _trace;
@override
bool get trace => _trace;
@override
Future<void> onLoad() async {
@ -37,8 +41,7 @@ class SparkyBumperGame extends BasicBallGame {
sparkyBumperB,
sparkyBumperC,
]);
await ready();
if (trace) traceAllBodies();
await traceAllBodies();
}
}

Loading…
Cancel
Save