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

Commit

Permalink
feat: next map navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Feb 17, 2024
1 parent 6536caf commit 2cb0d2e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/trashy_road/lib/src/game/view/game_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class _GameView extends StatelessWidget {
);
Navigator.pushReplacement(
context,
ScorePage.route(score: state.score),
ScorePage.route(identifier: state.identifier),
);
},
child: Stack(
Expand Down
8 changes: 2 additions & 6 deletions packages/trashy_road/lib/src/maps/bloc/game_maps_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ class GameMapsBloc extends Bloc<GameMapsEvent, GameMapsState> {

final firstTimeCompleted = currentMap.score == 0;
if (firstTimeCompleted) {
final mapsList = state.maps.keys.toList();
final currentIndex = mapsList.indexOf(event.identifier);
final nextIndex = currentIndex + 1;
final nextMap = state.next(currentMap.identifier);

final isLastMap = nextIndex == mapsList.length;
final isLastMap = nextMap == null;
if (!isLastMap) {
final nextMap = state.maps[mapsList[nextIndex]]!;

newMaps ??= Map.from(state.maps);
final updatedNextMap = nextMap.copyWith(locked: false);
newMaps[nextMap.identifier] = updatedNextMap;
Expand Down
14 changes: 14 additions & 0 deletions packages/trashy_road/lib/src/maps/bloc/game_maps_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ class GameMapsState extends Equatable {

final GameMapsCollection maps;

/// Returns the next map in the collection.
///
/// If there are no more maps, it will return `null`.
GameMap? next(String identifier) {
final mapsList = maps.keys.toList();
final currentIndex = mapsList.indexOf(identifier);
final nextIndex = currentIndex + 1;

final isLastMap = nextIndex == mapsList.length;
if (isLastMap) return null;

return maps[mapsList[nextIndex]];
}

GameMapsState copyWith({
Map<String, GameMap>? maps,
}) {
Expand Down
43 changes: 37 additions & 6 deletions packages/trashy_road/lib/src/score/view/score_page.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,61 @@
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/maps/maps.dart';

class ScorePage extends StatelessWidget {
const ScorePage({
required int score,
required String identifier,
super.key,
}) : _score = score;
}) : _identifier = identifier;

final int _score;
final String _identifier;

static Route<void> route({
required int score,
required String identifier,
}) {
return MaterialPageRoute<void>(
builder: (_) => ScorePage(score: score),
builder: (_) => ScorePage(identifier: identifier),
);
}

void _onNextMap(BuildContext context, {required GameMap nextMap}) {
if (nextMap.locked) return;

final preloadCubit = context.read<PreloadCubit>();
final tiledMap = preloadCubit.tiled.fromCache(nextMap.path);

Navigator.of(context).pushReplacement(
GamePage.route(
identifier: nextMap.identifier,
tiledMap: tiledMap,
),
);
}

@override
Widget build(BuildContext context) {
final gameMapsBloc = context.read<GameMapsBloc>();

final map = gameMapsBloc.state.maps[_identifier]!;

final score = map.score;
final nextMap = gameMapsBloc.state.next(_identifier);

return Scaffold(
appBar: AppBar(
title: const Text('Score'),
),
body: Center(
child: Text('Score: $_score'),
child: Text('Score: $score'),
),
floatingActionButton: nextMap != null
? FloatingActionButton(
onPressed: () => _onNextMap(context, nextMap: nextMap),
child: const Icon(Icons.play_arrow),
)
: null,
);
}
}

0 comments on commit 2cb0d2e

Please sign in to comment.