refactor: moved SparkyComputerSensor

pull/194/head
alestiago 3 years ago
parent 47d0ff651b
commit 7bbc46acd9

@ -21,7 +21,7 @@ class SparkyFireZone extends Blueprint with HasGameRef<Forge2DGame> {
_SparkyBumper.a()..initialPosition = Vector2(-22.9, -41.65),
_SparkyBumper.b()..initialPosition = Vector2(-21.25, -57.9),
_SparkyBumper.c()..initialPosition = Vector2(-3.3, -52.55),
SparkyTurboChargeSensor()..initialPosition = Vector2(-13, -49.8),
SparkyComputerSensor()..initialPosition = Vector2(-13, -49.8),
SparkyAnimatronic()..position = Vector2(-13.8, -58.2),
],
blueprints: [
@ -47,9 +47,7 @@ class _SparkyBumper extends SparkyBumper with ScorePoints {
await super.onLoad();
// TODO(alestiago): Revisit once this has been merged:
// https://github.com/flame-engine/flame/pull/1547
gameRef
..removeContactCallback(SparkyBumperBallContactCallback())
..addContactCallback(SparkyBumperBallContactCallback());
gameRef.addContactCallback(SparkyBumperBallContactCallback());
}
}
@ -67,59 +65,47 @@ class SparkyBumperBallContactCallback
}
}
/// {@template sparky_turbo_charge_sensor}
/// {@template sparky_computer_sensor}
/// Small sensor body used to detect when a ball has entered the
/// [SparkyComputer] with the [SparkyTurboChargeSensorBallContactCallback].
/// [SparkyComputer].
/// {@endtemplate}
@visibleForTesting
// TODO(alestiago): Revisit once this has been merged:
// https://github.com/flame-engine/flame/pull/1547
class SparkyTurboChargeSensor extends BodyComponent with InitialPosition {
/// {@macro sparky_turbo_charge_sensor}
SparkyTurboChargeSensor() {
class SparkyComputerSensor extends BodyComponent with InitialPosition {
/// {@macro sparky_computer_sensor}
SparkyComputerSensor() {
renderBody = false;
}
@override
Body createBody() {
final shape = CircleShape()..radius = 0.1;
final fixtureDef = FixtureDef(
shape,
isSensor: true,
);
final fixtureDef = FixtureDef(shape, isSensor: true);
final bodyDef = BodyDef(
position: initialPosition,
userData: this,
);
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
@override
Future<void> onLoad() async {
await super.onLoad();
gameRef.addContactCallback(SparkyTurboChargeSensorBallContactCallback());
// TODO(alestiago): Revisit once this has been merged:
// https://github.com/flame-engine/flame/pull/1547
gameRef.addContactCallback(SparkyComputerSensorBallContactCallback());
}
}
/// {@template sparky_turbo_charge_sensor_ball_contact_callback}
/// Turbo charges the [Ball] on contact with [SparkyTurboChargeSensor].
/// {@endtemplate}
@visibleForTesting
class SparkyTurboChargeSensorBallContactCallback
extends ContactCallback<SparkyTurboChargeSensor, ControlledBall> {
/// {@macro sparky_turbo_charge_sensor_ball_contact_callback}
SparkyTurboChargeSensorBallContactCallback();
// TODO(alestiago): Revisit once this has been merged:
// https://github.com/flame-engine/flame/pull/1547
// ignore: public_member_api_docs
class SparkyComputerSensorBallContactCallback
extends ContactCallback<SparkyComputerSensor, ControlledBall> {
@override
void begin(
SparkyTurboChargeSensor sparkyTurboChargeSensor,
ControlledBall ball,
_,
) {
// TODO(alestiago): Revisit once this has been merged:
// https://github.com/flame-engine/flame/pull/1547
ball.controller.turboCharge();
ball.gameRef.firstChild<SparkyAnimatronic>()?.playing = true;
void begin(_, ControlledBall controlledBall, __) {
controlledBall.controller.turboCharge();
// ball.gameRef.firstChild<SparkyAnimatronic>()?.playing = true;
}
}

@ -7,9 +7,6 @@ import 'package:pinball_flame/pinball_flame.dart';
/// {@template sparky_computer}
/// A computer owned by Sparky.
///
/// Register a [ContactCallback] for [SparkyComputerSensor] to listen when
/// something enters the [SparkyComputer].
/// {@endtemplate}
class SparkyComputer extends Blueprint {
/// {@macro sparky_computer}
@ -18,7 +15,6 @@ class SparkyComputer extends Blueprint {
components: [
_ComputerBase(),
_ComputerTopSpriteComponent(),
SparkyComputerSensor(),
],
);
}
@ -104,24 +100,3 @@ class _ComputerTopSpriteComponent extends SpriteComponent with HasGameRef {
size = sprite.originalSize / 10;
}
}
/// {@template sparky_computer_sensor}
/// Small sensor body used to detect when a ball has entered the
/// [SparkyComputer].
/// {@endtemplate}
class SparkyComputerSensor extends BodyComponent with InitialPosition {
/// {@macro sparky_computer_sensor}
SparkyComputerSensor() {
renderBody = false;
}
@override
Body createBody() {
final shape = CircleShape()..radius = 0.1;
final fixtureDef = FixtureDef(shape, isSensor: true);
final bodyDef = BodyDef()
..position = initialPosition
..userData = this;
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}

@ -127,4 +127,37 @@ void main() {
);
});
});
group(
'SparkyTurboChargeSensorBallContactCallback',
() {
// TODO(alestiago): Make tests pass.
flameTester.test('calls turboCharge', (game) async {
final callback = SparkyComputerSensorBallContactCallback();
final ball = MockControlledBall();
when(() => ball.controller).thenReturn(MockBallController());
when(() => ball.gameRef).thenReturn(game);
await game.ready();
callback.begin(MockSparkyComputerSensor(), ball, MockContact());
verify(() => ball.controller.turboCharge()).called(1);
});
flameTester.test('plays DashAnimatronic', (game) async {
final callback = SparkyComputerSensorBallContactCallback();
final ball = MockControlledBall();
when(() => ball.gameRef).thenReturn(game);
when(() => ball.controller).thenReturn(MockBallController());
final dashAnimatronic = DashAnimatronic();
await game.ensureAdd(dashAnimatronic);
expect(dashAnimatronic.playing, isFalse);
callback.begin(MockSparkyComputerSensor(), ball, MockContact());
await game.ready();
expect(dashAnimatronic.playing, isTrue);
});
},
);
}

Loading…
Cancel
Save