Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
refactor: remove BlocBuilder from GamePage (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago authored Feb 15, 2024
1 parent e1fad2d commit 4ff6152
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 70 deletions.
97 changes: 27 additions & 70 deletions packages/trashy_road/lib/src/game/view/game_page.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import 'dart:math';

import 'package:flame/game.dart' hide Route;
import 'package:flame_tiled/flame_tiled.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:trashy_road/src/game/game.dart';
import 'package:trashy_road/src/loading/loading.dart';
import 'package:trashy_road/src/pause/pause.dart';
import 'package:trashy_road/src/score/view/view.dart';

final _random = Random(0);

class GamePage extends StatelessWidget {
const GamePage({
required TiledMap map,
Expand Down Expand Up @@ -40,82 +34,45 @@ class GamePage extends StatelessWidget {
}
}

class _GameView extends StatefulWidget {
class _GameView extends StatelessWidget {
const _GameView();

@override
State<_GameView> createState() => _GameViewState();
}

class _GameViewState extends State<_GameView> {
TrashyRoadGame? _game;

@override
Widget build(BuildContext context) {
final loadingBloc = context.read<PreloadCubit>();
final gameBloc = context.read<GameBloc>();

TrashyRoadGame gameBuilder() {
return kDebugMode
? DebugTrashyRoadGame(
gameBloc: gameBloc,
images: loadingBloc.images,
random: _random,
)
: TrashyRoadGame(
gameBloc: gameBloc,
images: loadingBloc.images,
random: _random,
);
}

_game ??= gameBuilder();

return BlocListener<GameBloc, GameState>(
listenWhen: (previous, current) => current.status == GameStatus.completed,
listener: (context, state) {
Navigator.pushReplacement(context, ScorePage.route());
},
child: BlocBuilder<GameBloc, GameState>(
buildWhen: (previous, current) {
return previous.status == GameStatus.playing &&
current.status == GameStatus.resetting;
},
builder: (context, state) {
if (state.status == GameStatus.resetting) {
_game = gameBuilder();
gameBloc.add(const GameReadyEvent());
}

return Stack(
children: [
GameWidget(game: _game!),
const Align(
alignment: Alignment.topLeft,
child: Padding(
padding: EdgeInsets.all(8),
child: InventoryHud(),
),
),
Align(
alignment: Alignment.topRight,
child: Padding(
padding: const EdgeInsets.all(8),
child: PauseButton(
onPause: () {
gameBloc.add(const GamePausedEvent());
return true;
},
onResume: () {
gameBloc.add(const GameResumedEvent());
return true;
},
),
),
child: Stack(
children: [
const TrashyRoadGameWidget(),
const Align(
alignment: Alignment.topLeft,
child: Padding(
padding: EdgeInsets.all(8),
child: InventoryHud(),
),
),
Align(
alignment: Alignment.topRight,
child: Padding(
padding: const EdgeInsets.all(8),
child: PauseButton(
onPause: () {
gameBloc.add(const GamePausedEvent());
return true;
},
onResume: () {
gameBloc.add(const GameResumedEvent());
return true;
},
),
],
);
},
),
),
],
),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'dart:math';

import 'package:flame/game.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:trashy_road/src/game/game.dart';
import 'package:trashy_road/src/loading/loading.dart';

final _random = Random(0);

class TrashyRoadGameWidget extends StatefulWidget {
const TrashyRoadGameWidget({super.key});

@override
State<TrashyRoadGameWidget> createState() => _TrashyRoadGameWidgetState();
}

class _TrashyRoadGameWidgetState extends State<TrashyRoadGameWidget> {
TrashyRoadGame? _game;

@override
Widget build(BuildContext context) {
final gameBloc = context.read<GameBloc>();
final loadingBloc = context.read<PreloadCubit>();

TrashyRoadGame gameBuilder() {
return kDebugMode
? DebugTrashyRoadGame(
gameBloc: gameBloc,
images: loadingBloc.images,
random: _random,
)
: TrashyRoadGame(
gameBloc: gameBloc,
images: loadingBloc.images,
random: _random,
);
}

_game ??= gameBuilder();

return BlocListener<GameBloc, GameState>(
listenWhen: (previous, current) {
final hasLost = previous.status == GameStatus.playing &&
current.status == GameStatus.resetting;
return hasLost;
},
listener: (context, state) {
if (!mounted) return;
setState(() => _game = gameBuilder());
gameBloc.add(const GameReadyEvent());
},
child: GameWidget(game: _game!),
);
}
}
1 change: 1 addition & 0 deletions packages/trashy_road/lib/src/game/widgets/widgets.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'inventory_hud.dart';
export 'trashy_road_game_widget.dart';

0 comments on commit 4ff6152

Please sign in to comment.