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.a()..initialPosition = Vector2(-22.9, -41.65),
_SparkyBumper.b()..initialPosition = Vector2(-21.25, -57.9), _SparkyBumper.b()..initialPosition = Vector2(-21.25, -57.9),
_SparkyBumper.c()..initialPosition = Vector2(-3.3, -52.55), _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), SparkyAnimatronic()..position = Vector2(-13.8, -58.2),
], ],
blueprints: [ blueprints: [
@ -47,9 +47,7 @@ class _SparkyBumper extends SparkyBumper with ScorePoints {
await super.onLoad(); await super.onLoad();
// TODO(alestiago): Revisit once this has been merged: // TODO(alestiago): Revisit once this has been merged:
// https://github.com/flame-engine/flame/pull/1547 // https://github.com/flame-engine/flame/pull/1547
gameRef gameRef.addContactCallback(SparkyBumperBallContactCallback());
..removeContactCallback(SparkyBumperBallContactCallback())
..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 /// Small sensor body used to detect when a ball has entered the
/// [SparkyComputer] with the [SparkyTurboChargeSensorBallContactCallback]. /// [SparkyComputer].
/// {@endtemplate} /// {@endtemplate}
@visibleForTesting
// TODO(alestiago): Revisit once this has been merged: // TODO(alestiago): Revisit once this has been merged:
// https://github.com/flame-engine/flame/pull/1547 // https://github.com/flame-engine/flame/pull/1547
class SparkyTurboChargeSensor extends BodyComponent with InitialPosition { class SparkyComputerSensor extends BodyComponent with InitialPosition {
/// {@macro sparky_turbo_charge_sensor} /// {@macro sparky_computer_sensor}
SparkyTurboChargeSensor() { SparkyComputerSensor() {
renderBody = false; renderBody = false;
} }
@override @override
Body createBody() { Body createBody() {
final shape = CircleShape()..radius = 0.1; final shape = CircleShape()..radius = 0.1;
final fixtureDef = FixtureDef( final fixtureDef = FixtureDef(shape, isSensor: true);
shape,
isSensor: true,
);
final bodyDef = BodyDef( final bodyDef = BodyDef(
position: initialPosition, position: initialPosition,
userData: this, userData: this,
); );
return world.createBody(bodyDef)..createFixture(fixtureDef); return world.createBody(bodyDef)..createFixture(fixtureDef);
} }
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); 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 @visibleForTesting
class SparkyTurboChargeSensorBallContactCallback
extends ContactCallback<SparkyTurboChargeSensor, ControlledBall> {
/// {@macro sparky_turbo_charge_sensor_ball_contact_callback}
SparkyTurboChargeSensorBallContactCallback();
@override
void begin(
SparkyTurboChargeSensor sparkyTurboChargeSensor,
ControlledBall ball,
_,
) {
// TODO(alestiago): Revisit once this has been merged: // TODO(alestiago): Revisit once this has been merged:
// https://github.com/flame-engine/flame/pull/1547 // https://github.com/flame-engine/flame/pull/1547
ball.controller.turboCharge(); // ignore: public_member_api_docs
ball.gameRef.firstChild<SparkyAnimatronic>()?.playing = true; class SparkyComputerSensorBallContactCallback
extends ContactCallback<SparkyComputerSensor, ControlledBall> {
@override
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} /// {@template sparky_computer}
/// A computer owned by Sparky. /// A computer owned by Sparky.
///
/// Register a [ContactCallback] for [SparkyComputerSensor] to listen when
/// something enters the [SparkyComputer].
/// {@endtemplate} /// {@endtemplate}
class SparkyComputer extends Blueprint { class SparkyComputer extends Blueprint {
/// {@macro sparky_computer} /// {@macro sparky_computer}
@ -18,7 +15,6 @@ class SparkyComputer extends Blueprint {
components: [ components: [
_ComputerBase(), _ComputerBase(),
_ComputerTopSpriteComponent(), _ComputerTopSpriteComponent(),
SparkyComputerSensor(),
], ],
); );
} }
@ -104,24 +100,3 @@ class _ComputerTopSpriteComponent extends SpriteComponent with HasGameRef {
size = sprite.originalSize / 10; 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