feat: implemented `BallType` (#105)

* feat: implemented BallType

* docs: improved doc comment

* refactor: renamed egg to extra
pull/108/head
Alejandro Santiago 2 years ago committed by GitHub
parent 6f32c4f2b8
commit c4012b1c65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,33 +1,64 @@
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/flame/blueprint.dart'; import 'package:pinball/flame/blueprint.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';
/// {@template ball_type}
/// Specifies the type of [Ball].
///
/// Different [BallType]s are affected by different game mechanics.
/// {@endtemplate}
enum BallType {
/// A [Ball] spawned from the [Plunger].
///
/// [normal] balls decrease the [GameState.balls] when they fall through the
/// the [BottomWall].
normal,
/// A [Ball] that does not alter [GameState.balls].
///
/// For example, a [Ball] spawned by Dash in the [FlutterForest].
extra,
}
/// {@template ball_blueprint} /// {@template ball_blueprint}
/// [Blueprint] which cretes a ball game object /// [Blueprint] which cretes a ball game object.
/// {@endtemplate} /// {@endtemplate}
class BallBlueprint extends Blueprint<PinballGame> { class BallBlueprint extends Blueprint<PinballGame> {
/// {@macro ball_blueprint} /// {@macro ball_blueprint}
BallBlueprint({required this.position}); BallBlueprint({
required this.position,
required this.type,
});
/// The initial position of the [Ball] /// The initial position of the [Ball].
final Vector2 position; final Vector2 position;
/// {@macro ball_type}
final BallType type;
@override @override
void build(PinballGame gameRef) { void build(PinballGame gameRef) {
final baseColor = gameRef.theme.characterTheme.ballColor; final baseColor = gameRef.theme.characterTheme.ballColor;
final ball = Ball(baseColor: baseColor)..add(BallController()); final ball = Ball(baseColor: baseColor)
..add(
BallController(type: type),
);
add(ball..initialPosition = position + Vector2(0, ball.size.y / 2)); add(ball..initialPosition = position + Vector2(0, ball.size.y / 2));
} }
} }
/// {@template ball} /// {@template ball_controller}
/// A solid, [BodyType.dynamic] sphere that rolls and bounces along the /// Controller attached to a [Ball] that handles its game related logic.
/// [PinballGame].
/// {@endtemplate} /// {@endtemplate}
class BallController extends Component with HasGameRef<PinballGame> { class BallController extends Component with HasGameRef<PinballGame> {
/// {@macro ball_controller}
BallController({required this.type});
/// {@macro ball_type}
final BallType type;
/// Removes the [Ball] from a [PinballGame]; spawning a new [Ball] if /// Removes the [Ball] from a [PinballGame]; spawning a new [Ball] if
/// any are left. /// any are left.
/// ///
@ -35,9 +66,11 @@ class BallController extends Component with HasGameRef<PinballGame> {
/// a [BottomWall]. /// a [BottomWall].
void lost() { void lost() {
parent?.shouldRemove = true; parent?.shouldRemove = true;
// TODO(alestiago): Consider adding test for this logic once we remove the
// BallX extension.
if (type != BallType.normal) return;
final bloc = gameRef.read<GameBloc>()..add(const BallLost()); final bloc = gameRef.read<GameBloc>()..add(const BallLost());
final shouldBallRespwan = !bloc.state.isLastBall && !bloc.state.isGameOver; final shouldBallRespwan = !bloc.state.isLastBall && !bloc.state.isGameOver;
if (shouldBallRespwan) { if (shouldBallRespwan) {
gameRef.spawnBall(); gameRef.spawnBall();

@ -35,6 +35,7 @@ class FlutterForest extends Component
gameRef.addFromBlueprint( gameRef.addFromBlueprint(
BallBlueprint( BallBlueprint(
position: Vector2(17.2, 52.7), position: Vector2(17.2, 52.7),
type: BallType.extra,
), ),
); );
} }

@ -88,7 +88,12 @@ class PinballGame extends Forge2DGame
} }
void spawnBall() { void spawnBall() {
addFromBlueprint(BallBlueprint(position: plunger.body.position)); addFromBlueprint(
BallBlueprint(
position: plunger.body.position,
type: BallType.normal,
),
);
} }
} }
@ -120,6 +125,11 @@ class DebugPinballGame extends PinballGame with TapDetector {
@override @override
void onTapUp(TapUpInfo info) { void onTapUp(TapUpInfo info) {
addFromBlueprint(BallBlueprint(position: info.eventPosition.game)); addFromBlueprint(
BallBlueprint(
position: info.eventPosition.game,
type: BallType.extra,
),
);
} }
} }

Loading…
Cancel
Save