refactor: moved Sensor to ControlledSparkyComputer

pull/212/head
alestiago 3 years ago
parent 1ef699db79
commit 7dcde691d1

@ -1,5 +1,6 @@
// ignore_for_file: avoid_renaming_method_parameters // ignore_for_file: avoid_renaming_method_parameters
import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
@ -10,16 +11,17 @@ import 'package:pinball_flame/pinball_flame.dart';
/// [SparkyComputer] with a [SparkyComputerController] attached. /// [SparkyComputer] with a [SparkyComputerController] attached.
/// {@endtemplate} /// {@endtemplate}
class ControlledSparkyComputer extends SparkyComputer class ControlledSparkyComputer extends SparkyComputer
with Controls<SparkyComputerController> { with Controls<SparkyComputerController>, HasGameRef<Forge2DGame> {
/// {@macro controlled_sparky_computer} /// {@macro controlled_sparky_computer}
ControlledSparkyComputer() ControlledSparkyComputer() : super() {
: super(
components: [
SparkyTurboChargeSensor()..initialPosition = Vector2(-13, -49.8)
],
) {
controller = SparkyComputerController(this); controller = SparkyComputerController(this);
} }
@override
Future<void> onLoad() async {
await super.onLoad();
gameRef.addContactCallback(SparkyTurboChargeSensorBallContactCallback());
}
} }
/// {@template sparky_computer_controller} /// {@template sparky_computer_controller}
@ -34,49 +36,18 @@ class SparkyComputerController
: super(controlledComputer); : super(controlledComputer);
} }
/// {@template sparky_turbo_charge_sensor}
/// Small sensor body used to detect when a ball has entered the
/// [SparkyComputer] with the [SparkyTurboChargeSensorBallContactCallback].
/// {@endtemplate}
@visibleForTesting
class SparkyTurboChargeSensor extends BodyComponent with InitialPosition {
/// {@macro sparky_turbo_charge_sensor}
SparkyTurboChargeSensor() {
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);
}
@override
Future<void> onLoad() async {
await super.onLoad();
gameRef.addContactCallback(SparkyTurboChargeSensorBallContactCallback());
}
}
/// {@template sparky_turbo_charge_sensor_ball_contact_callback} /// {@template sparky_turbo_charge_sensor_ball_contact_callback}
/// Turbo charges the [Ball] on contact with [SparkyTurboChargeSensor]. /// Turbo charges the [Ball] when it enters the [SparkyComputer].
/// {@endtemplate} /// {@endtemplate}
@visibleForTesting @visibleForTesting
class SparkyTurboChargeSensorBallContactCallback class SparkyTurboChargeSensorBallContactCallback
extends ContactCallback<SparkyTurboChargeSensor, ControlledBall> { extends ContactCallback<SparkyComputerSensor, ControlledBall> {
/// {@macro sparky_turbo_charge_sensor_ball_contact_callback} /// {@macro sparky_turbo_charge_sensor_ball_contact_callback}
SparkyTurboChargeSensorBallContactCallback(); SparkyTurboChargeSensorBallContactCallback();
@override @override
void begin( void begin(
SparkyTurboChargeSensor sparkyTurboChargeSensor, SparkyComputerSensor sparkyTurboChargeSensor,
ControlledBall ball, ControlledBall ball,
_, _,
) { ) {

@ -6,75 +6,64 @@ import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart'; import 'package:pinball_flame/pinball_flame.dart';
/// {@template sparky_computer} /// {@template sparky_computer}
/// A [Blueprint] which creates the [_ComputerBase] and /// A computer owned by Sparky.
/// [_ComputerTopSpriteComponent]. ///
/// 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}
SparkyComputer({ SparkyComputer()
Iterable<Component>? components, : super(
}) : super(
components: [ components: [
_ComputerBase(), _ComputerBase(),
_ComputerTopSpriteComponent(), _ComputerTopSpriteComponent(),
if (components != null) ...components, SparkyComputerSensor(),
], ],
); );
} }
class _ComputerBase extends BodyComponent with InitialPosition { class _ComputerBase extends BodyComponent with InitialPosition {
_ComputerBase() : super(priority: RenderPriority.computerBase); _ComputerBase()
: super(
priority: RenderPriority.computerBase,
children: [_ComputerBaseSpriteComponent()],
) {
renderBody = false;
}
List<FixtureDef> _createFixtureDefs() { List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[];
final leftEdge = EdgeShape() final leftEdge = EdgeShape()
..set( ..set(
Vector2(-14.9, -46), Vector2(-14.9, -46),
Vector2(-15.3, -49.6), Vector2(-15.3, -49.6),
); );
final leftEdgeFixtureDef = FixtureDef(leftEdge);
fixturesDef.add(leftEdgeFixtureDef);
final topEdge = EdgeShape() final topEdge = EdgeShape()
..set( ..set(
Vector2(-15.3, -49.6), Vector2(-15.3, -49.6),
Vector2(-10.7, -50.6), Vector2(-10.7, -50.6),
); );
final topEdgeFixtureDef = FixtureDef(topEdge);
fixturesDef.add(topEdgeFixtureDef);
final rightEdge = EdgeShape() final rightEdge = EdgeShape()
..set( ..set(
Vector2(-10.7, -50.6), Vector2(-10.7, -50.6),
Vector2(-9, -47.2), Vector2(-9, -47.2),
); );
final rightEdgeFixtureDef = FixtureDef(rightEdge);
fixturesDef.add(rightEdgeFixtureDef);
return fixturesDef; return [
FixtureDef(leftEdge),
FixtureDef(topEdge),
FixtureDef(rightEdge),
];
} }
@override @override
Body createBody() { Body createBody() {
final bodyDef = BodyDef( final bodyDef = BodyDef(position: initialPosition);
position: initialPosition,
userData: this,
);
final body = world.createBody(bodyDef); final body = world.createBody(bodyDef);
_createFixtureDefs().forEach(body.createFixture); _createFixtureDefs().forEach(body.createFixture);
return body; return body;
} }
@override
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await add(_ComputerBaseSpriteComponent());
}
} }
class _ComputerBaseSpriteComponent extends SpriteComponent with HasGameRef { class _ComputerBaseSpriteComponent extends SpriteComponent with HasGameRef {
@ -115,3 +104,24 @@ class _ComputerTopSpriteComponent extends SpriteComponent with HasGameRef {
size = sprite.originalSize / 10; size = sprite.originalSize / 10;
} }
} }
/// {@template sparky_turbo_charge_sensor}
/// Small sensor body used to detect when a ball has entered the
/// [SparkyComputer].
/// {@endtemplate}
class SparkyComputerSensor extends BodyComponent with InitialPosition {
/// {@macro sparky_turbo_charge_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);
}
}

@ -16,6 +16,8 @@ void main() {
'renders correctly', 'renders correctly',
setUp: (game, tester) async { setUp: (game, tester) async {
await game.addFromBlueprint(SparkyComputer()); await game.addFromBlueprint(SparkyComputer());
await game.ready();
game.camera.followVector2(Vector2(-15, -50)); game.camera.followVector2(Vector2(-15, -50));
}, },
verify: (game, tester) async { verify: (game, tester) async {

@ -69,8 +69,8 @@ class MockDashNestBumper extends Mock implements DashNestBumper {}
class MockPinballAudio extends Mock implements PinballAudio {} class MockPinballAudio extends Mock implements PinballAudio {}
class MockSparkyTurboChargeSensor extends Mock class MockSparkyTurboChargeSensor extends Mock implements SparkyComputerSensor {
implements SparkyTurboChargeSensor {} }
class MockAssetsManagerCubit extends Mock implements AssetsManagerCubit {} class MockAssetsManagerCubit extends Mock implements AssetsManagerCubit {}

Loading…
Cancel
Save