feat: made SparkyFireZone a Blueprint

pull/194/head
alestiago 3 years ago
parent e0c2708257
commit 23754818bc

@ -5,6 +5,7 @@ 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';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart';
/// {@template sparky_fire_zone} /// {@template sparky_fire_zone}
/// Area positioned at the top left of the [Board] where the [Ball] /// Area positioned at the top left of the [Board] where the [Ball]
@ -12,27 +13,21 @@ import 'package:pinball_components/pinball_components.dart';
/// ///
/// When a [Ball] hits [SparkyBumper]s, the bumper animates. /// When a [Ball] hits [SparkyBumper]s, the bumper animates.
/// {@endtemplate} /// {@endtemplate}
class SparkyFireZone extends Component with HasGameRef<Forge2DGame> { class SparkyFireZone extends Blueprint with HasGameRef<Forge2DGame> {
/// {@macro sparky_fire_zone} /// {@macro sparky_fire_zone}
SparkyFireZone() SparkyFireZone()
: super( : super(
children: [ components: [
_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),
SparkyComputer(),
SparkyTurboChargeSensor()..initialPosition = Vector2(-13, -49.8), SparkyTurboChargeSensor()..initialPosition = Vector2(-13, -49.8),
SparkyAnimatronic()..position = Vector2(-13.8, -58.2), SparkyAnimatronic()..position = Vector2(-13.8, -58.2),
], ],
blueprints: [
SparkyComputer(),
],
); );
@override
Future<void> onLoad() async {
await super.onLoad();
gameRef
..addContactCallback(SparkyBumperBallContactCallback())
..addContactCallback(SparkyTurboChargeSensorBallContactCallback());
}
} }
// TODO(alestiago): Revisit ScorePoints logic once the FlameForge2D // TODO(alestiago): Revisit ScorePoints logic once the FlameForge2D
@ -46,6 +41,16 @@ class _SparkyBumper extends SparkyBumper with ScorePoints {
@override @override
int get points => 20; int get points => 20;
@override
Future<void> onLoad() async {
await super.onLoad();
// TODO(alestiago): Revisit once this has been merged:
// https://github.com/flame-engine/flame/pull/1547
gameRef
..removeContactCallback(SparkyBumperBallContactCallback())
..addContactCallback(SparkyBumperBallContactCallback());
}
} }
/// Listens when a [Ball] bounces bounces against a [SparkyBumper]. /// Listens when a [Ball] bounces bounces against a [SparkyBumper].
@ -67,6 +72,8 @@ class SparkyBumperBallContactCallback
/// [SparkyComputer] with the [SparkyTurboChargeSensorBallContactCallback]. /// [SparkyComputer] with the [SparkyTurboChargeSensorBallContactCallback].
/// {@endtemplate} /// {@endtemplate}
@visibleForTesting @visibleForTesting
// TODO(alestiago): Revisit once this has been merged:
// https://github.com/flame-engine/flame/pull/1547
class SparkyTurboChargeSensor extends BodyComponent with InitialPosition { class SparkyTurboChargeSensor extends BodyComponent with InitialPosition {
/// {@macro sparky_turbo_charge_sensor} /// {@macro sparky_turbo_charge_sensor}
SparkyTurboChargeSensor() { SparkyTurboChargeSensor() {
@ -87,6 +94,12 @@ class SparkyTurboChargeSensor extends BodyComponent with InitialPosition {
return world.createBody(bodyDef)..createFixture(fixtureDef); 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}
@ -104,9 +117,9 @@ class SparkyTurboChargeSensorBallContactCallback
ControlledBall ball, ControlledBall ball,
_, _,
) { ) {
final parent = sparkyTurboChargeSensor.parent; // TODO(alestiago): Revisit once this has been merged:
if (parent is SparkyFireZone) { // https://github.com/flame-engine/flame/pull/1547
ball.controller.turboCharge(); ball.controller.turboCharge();
} ball.gameRef.firstChild<SparkyAnimatronic>()?.playing = true;
} }
} }

@ -54,7 +54,7 @@ class PinballGame extends Forge2DGame
unawaited(addFromBlueprint(launcher)); unawaited(addFromBlueprint(launcher));
unawaited(add(Board())); unawaited(add(Board()));
unawaited(add(AlienZone())); unawaited(add(AlienZone()));
unawaited(add(SparkyFireZone())); await addFromBlueprint(SparkyFireZone());
unawaited(addFromBlueprint(Slingshots())); unawaited(addFromBlueprint(Slingshots()));
unawaited(addFromBlueprint(DinoWalls())); unawaited(addFromBlueprint(DinoWalls()));
unawaited(_addBonusWord()); unawaited(_addBonusWord());

@ -39,7 +39,7 @@ void main() {
await game.ready(); await game.ready();
expect( expect(
sparkyFireZone.blueprints.whereType<SparkyComputer>().single, game.descendants().whereType<SparkyComputer>().single,
isNotNull, isNotNull,
); );
}, },

Loading…
Cancel
Save