refactor: made LaunchRamp a Component

pull/282/head
alestiago 3 years ago
parent ecded856fc
commit bd106f4dbd

@ -19,7 +19,6 @@ class ControlledBall extends Ball with Controls<BallController> {
required CharacterTheme characterTheme, required CharacterTheme characterTheme,
}) : super(baseColor: characterTheme.ballColor) { }) : super(baseColor: characterTheme.ballColor) {
controller = BallController(this); controller = BallController(this);
priority = RenderPriority.ballOnLaunchRamp;
layer = Layer.launcher; layer = Layer.launcher;
} }

@ -1,4 +1,4 @@
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame/components.dart';
import 'package:pinball/game/components/components.dart'; import 'package:pinball/game/components/components.dart';
import 'package:pinball_components/pinball_components.dart' hide Assets; import 'package:pinball_components/pinball_components.dart' hide Assets;
import 'package:pinball_flame/pinball_flame.dart'; import 'package:pinball_flame/pinball_flame.dart';
@ -7,15 +7,15 @@ import 'package:pinball_flame/pinball_flame.dart';
/// A [Blueprint] which creates the [Plunger], [RocketSpriteComponent] and /// A [Blueprint] which creates the [Plunger], [RocketSpriteComponent] and
/// [LaunchRamp]. /// [LaunchRamp].
/// {@endtemplate} /// {@endtemplate}
class Launcher extends Blueprint { class Launcher extends Component {
/// {@macro launcher} /// {@macro launcher}
Launcher() Launcher()
: super( : super(
components: [ children: [
LaunchRamp(),
ControlledPlunger(compressionDistance: 10.5) ControlledPlunger(compressionDistance: 10.5)
..initialPosition = Vector2(41.1, 43), ..initialPosition = Vector2(41.1, 43),
RocketSpriteComponent()..position = Vector2(43, 62.3), RocketSpriteComponent()..position = Vector2(43, 62.3),
], ],
blueprints: [LaunchRamp()],
); );
} }

@ -51,7 +51,7 @@ class PinballGame extends Forge2DGame
await addFromBlueprint(Boundaries()); await addFromBlueprint(Boundaries());
final launcher = Launcher(); final launcher = Launcher();
await addFromBlueprint(launcher); await add(launcher);
await add(Multipliers()); await add(Multipliers());
await add(FlutterForest()); await add(FlutterForest());
await addFromBlueprint(SparkyScorch()); await addFromBlueprint(SparkyScorch());
@ -67,7 +67,6 @@ class PinballGame extends Forge2DGame
), ),
); );
controller.attachTo(launcher.components.whereType<Plunger>().single);
await super.onLoad(); await super.onLoad();
} }
@ -138,8 +137,6 @@ class _GameBallsController extends ComponentController<PinballGame>
with BlocComponent<GameBloc, GameState> { with BlocComponent<GameBloc, GameState> {
_GameBallsController(PinballGame game) : super(game); _GameBallsController(PinballGame game) : super(game);
late final Plunger _plunger;
@override @override
bool listenWhen(GameState? previousState, GameState newState) { bool listenWhen(GameState? previousState, GameState newState) {
final noBallsLeft = component.descendants().whereType<Ball>().isEmpty; final noBallsLeft = component.descendants().whereType<Ball>().isEmpty;
@ -164,18 +161,11 @@ class _GameBallsController extends ComponentController<PinballGame>
final ball = ControlledBall.launch( final ball = ControlledBall.launch(
characterTheme: component.characterTheme, characterTheme: component.characterTheme,
)..initialPosition = Vector2( )..initialPosition = Vector2(
_plunger.body.position.x, Vector2(41.1, 43).x,
_plunger.body.position.y - Ball.size.y, Vector2(41.1, 45).y - Ball.size.y,
); );
component.add(ball); component.add(ball);
} }
/// Attaches the controller to the plunger.
// TODO(alestiago): Remove this method and use onLoad instead.
// ignore: use_setters_to_change_properties
void attachTo(Plunger plunger) {
_plunger = plunger;
}
} }
class DebugPinballGame extends PinballGame with FPSCounter { class DebugPinballGame extends PinballGame with FPSCounter {

@ -11,11 +11,11 @@ import 'package:pinball_flame/pinball_flame.dart';
/// A [Blueprint] which creates the [_LaunchRampBase] and /// A [Blueprint] which creates the [_LaunchRampBase] and
/// [_LaunchRampForegroundRailing]. /// [_LaunchRampForegroundRailing].
/// {@endtemplate} /// {@endtemplate}
class LaunchRamp extends Blueprint { class LaunchRamp extends Component {
/// {@macro launch_ramp} /// {@macro launch_ramp}
LaunchRamp() LaunchRamp()
: super( : super(
components: [ children: [
_LaunchRampBase(), _LaunchRampBase(),
_LaunchRampForegroundRailing(), _LaunchRampForegroundRailing(),
_LaunchRampExit()..initialPosition = Vector2(0.6, -34), _LaunchRampExit()..initialPosition = Vector2(0.6, -34),
@ -31,7 +31,6 @@ class _LaunchRampBase extends BodyComponent with Layered {
/// {@macro launch_ramp_base} /// {@macro launch_ramp_base}
_LaunchRampBase() _LaunchRampBase()
: super( : super(
priority: RenderPriority.launchRamp,
renderBody: false, renderBody: false,
children: [ children: [
_LaunchRampBackgroundRailingSpriteComponent(), _LaunchRampBackgroundRailingSpriteComponent(),
@ -148,7 +147,6 @@ class _LaunchRampForegroundRailing extends BodyComponent {
/// {@macro launch_ramp_foreground_railing} /// {@macro launch_ramp_foreground_railing}
_LaunchRampForegroundRailing() _LaunchRampForegroundRailing()
: super( : super(
priority: RenderPriority.launchRampForegroundRailing,
children: [_LaunchRampForegroundRailingSpriteComponent()], children: [_LaunchRampForegroundRailingSpriteComponent()],
renderBody: false, renderBody: false,
); );

Loading…
Cancel
Save