feat: implementing logic

pull/151/head
alestiago 4 years ago
parent 8c278d591a
commit 2fdd886a8e

@ -10,6 +10,7 @@ part 'game_state.dart';
class GameBloc extends Bloc<GameEvent, GameState> { class GameBloc extends Bloc<GameEvent, GameState> {
GameBloc() : super(const GameState.initial()) { GameBloc() : super(const GameState.initial()) {
on<BallLost>(_onBallLost); on<BallLost>(_onBallLost);
on<BonusBallLost>(_onBonusBallLost);
on<Scored>(_onScored); on<Scored>(_onScored);
on<BonusLetterActivated>(_onBonusLetterActivated); on<BonusLetterActivated>(_onBonusLetterActivated);
on<DashNestActivated>(_onDashNestActivated); on<DashNestActivated>(_onDashNestActivated);
@ -19,9 +20,11 @@ class GameBloc extends Bloc<GameEvent, GameState> {
static const bonusWordScore = 10000; static const bonusWordScore = 10000;
void _onBallLost(BallLost event, Emitter emit) { void _onBallLost(BallLost event, Emitter emit) {
if (state.balls > 0) {
emit(state.copyWith(balls: state.balls - 1)); emit(state.copyWith(balls: state.balls - 1));
} }
void _onBonusBallLost(BonusBallLost event, Emitter emit) {
emit(state.copyWith(bonusBalls: state.bonusBalls - 1));
} }
void _onScored(Scored event, Emitter emit) { void _onScored(Scored event, Emitter emit) {

@ -71,12 +71,12 @@ class BottomWall extends Wall {
} }
/// {@template bottom_wall_ball_contact_callback} /// {@template bottom_wall_ball_contact_callback}
/// Listens when a [Ball] falls into a [BottomWall]. /// Listens when a [ControlledBall] falls into a [BottomWall].
/// {@endtemplate} /// {@endtemplate}
class BottomWallBallContactCallback extends ContactCallback<Ball, BottomWall> { class BottomWallBallContactCallback
extends ContactCallback<ControlledBall, BottomWall> {
@override @override
void begin(Ball ball, BottomWall wall, Contact contact) { void begin(ControlledBall ball, BottomWall wall, Contact contact) {
// TODO(alestiago): replace with .firstChild when available. ball.controller.lost();
ball.children.whereType<BallController>().first.lost();
} }
} }

@ -30,21 +30,23 @@ class PinballGame extends Forge2DGame
final PinballAudio audio; final PinballAudio audio;
@override
void onAttach() {
super.onAttach();
controller.spawnBall();
}
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad();
_addContactCallbacks(); _addContactCallbacks();
// Fix camera on the center of the board.
camera
..followVector2(Vector2(0, -7.8))
..zoom = size.y / 16;
await _addGameBoundaries(); await _addGameBoundaries();
unawaited(addFromBlueprint(Boundaries())); unawaited(addFromBlueprint(Boundaries()));
unawaited(addFromBlueprint(LaunchRamp())); unawaited(addFromBlueprint(LaunchRamp()));
unawaited(_addPlunger());
final plunger = Plunger(compressionDistance: 29)
..initialPosition =
BoardDimensions.bounds.center.toVector2() + Vector2(41.5, -49);
await add(plunger);
unawaited(add(Board())); unawaited(add(Board()));
unawaited(addFromBlueprint(DinoWalls())); unawaited(addFromBlueprint(DinoWalls()));
unawaited(_addBonusWord()); unawaited(_addBonusWord());
@ -62,10 +64,8 @@ class PinballGame extends Forge2DGame
), ),
); );
// Fix camera on the center of the board. controller.attachTo(plunger);
camera await super.onLoad();
..followVector2(Vector2(0, -7.8))
..zoom = size.y / 16;
} }
void _addContactCallbacks() { void _addContactCallbacks() {
@ -79,13 +79,6 @@ class PinballGame extends Forge2DGame
createBoundaries(this).forEach(add); createBoundaries(this).forEach(add);
} }
Future<void> _addPlunger() async {
final plunger = Plunger(compressionDistance: 29)
..initialPosition =
BoardDimensions.bounds.center.toVector2() + Vector2(41.5, -49);
await add(plunger);
}
Future<void> _addBonusWord() async { Future<void> _addBonusWord() async {
await add( await add(
BonusWord( BonusWord(
@ -102,9 +95,10 @@ class _GameBallsController extends ComponentController<PinballGame>
with BlocComponent<GameBloc, GameState>, HasGameRef<PinballGame> { with BlocComponent<GameBloc, GameState>, HasGameRef<PinballGame> {
_GameBallsController(PinballGame game) : super(game); _GameBallsController(PinballGame game) : super(game);
Plunger? _plunger;
@override @override
bool listenWhen(GameState? previousState, GameState newState) { bool listenWhen(GameState? previousState, GameState newState) {
// TODO(alestiago): Fix how the logic works.
final previousBalls = final previousBalls =
(previousState?.balls ?? 0) + (previousState?.bonusBalls ?? 0); (previousState?.balls ?? 0) + (previousState?.bonusBalls ?? 0);
final currentBalls = newState.balls + newState.bonusBalls; final currentBalls = newState.balls + newState.bonusBalls;
@ -119,20 +113,29 @@ class _GameBallsController extends ComponentController<PinballGame>
spawnBall(); spawnBall();
} }
Future<void> spawnBall() async { @override
// TODO(alestiago): Remove once this logic is moved to controller. Future<void> onLoad() async {
var plunger = firstChild<Plunger>(); await super.onLoad();
if (plunger == null) { spawnBall();
await add(plunger = Plunger(compressionDistance: 1));
} }
void spawnBall() {
if (_plunger == null) return;
final ball = ControlledBall.launch( final ball = ControlledBall.launch(
theme: gameRef.theme, theme: gameRef.theme,
)..initialPosition = Vector2( )..initialPosition = Vector2(
plunger.body.position.x, _plunger!.body.position.x,
plunger.body.position.y + Ball.size.y, _plunger!.body.position.y + Ball.size.y,
); );
await 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;
} }
} }

Loading…
Cancel
Save