Skip to content
Snippets Groups Projects
Select Git revision
  • e6b786dfb23879a36b1c54346ad8892a3aecd9dc
  • master default protected
  • 49-upgrade-framework-and-dependencies
  • 32-improve-app-metadata
  • Release_0.9.0_43 protected
  • Release_0.8.2_42 protected
  • Release_0.8.1_41 protected
  • Release_0.8.0_40 protected
  • Release_0.7.0_39 protected
  • Release_0.6.0_38 protected
  • Release_0.5.0_37 protected
  • Release_0.4.2_36 protected
  • Release_0.4.1_35 protected
  • Release_0.4.0_34 protected
  • Release_0.3.1_33 protected
  • Release_0.3.0_32 protected
  • Release_0.2.1_31 protected
  • Release_0.2.0_30 protected
  • Release_0.1.2_29 protected
  • Release_0.1.1_28 protected
  • Release_0.1.0_27 protected
  • Release_0.0.26_26 protected
  • Release_0.0.25_25 protected
  • Release_0.0.24_24 protected
24 results

game_cubit.dart

Blame
  • game_cubit.dart 1.97 KiB
    import 'package:equatable/equatable.dart';
    import 'package:flutter/material.dart';
    import 'package:hydrated_bloc/hydrated_bloc.dart';
    
    import 'package:sortgame/models/game.dart';
    import 'package:sortgame/models/settings_game.dart';
    import 'package:sortgame/models/settings_global.dart';
    
    part 'game_state.dart';
    
    class GameCubit extends HydratedCubit<GameState> {
      GameCubit()
          : super(GameState(
              currentGame: Game.createNull(),
            ));
    
      void updateState(Game game) {
        emit(GameState(
          currentGame: game,
        ));
      }
    
      void refresh() {
        final Game game = Game(
          items: state.currentGame.items,
          gameSettings: state.currentGame.gameSettings,
          globalSettings: state.currentGame.globalSettings,
          isRunning: state.currentGame.isRunning,
          isFinished: state.currentGame.isFinished,
          position: state.currentGame.position,
          score: state.currentGame.score,
        );
    
        updateState(game);
      }
    
      void quitGame() {
        state.currentGame.updateGameIsRunning(false);
        refresh();
      }
    
      void increasePosition() {
        if (state.currentGame.position < state.currentGame.items.length) {
          state.currentGame.increasePosition();
        } else {
          state.currentGame.updateGameIsFinished(true);
        }
        refresh();
      }
    
      void increaseScore(int increment) {
        state.currentGame.increaseScore(increment);
        refresh();
      }
    
      void startNewGame({
        required GameSettings gameSettings,
        required GlobalSettings globalSettings,
      }) {
        Game newGame = Game.createNew(
          gameSettings: gameSettings,
          globalSettings: globalSettings,
        );
    
        newGame.dump();
    
        updateState(newGame);
        refresh();
      }
    
      @override
      GameState? fromJson(Map<String, dynamic> json) {
        Game currentGame = json['currentGame'] as Game;
    
        return GameState(
          currentGame: currentGame,
        );
      }
    
      @override
      Map<String, dynamic>? toJson(GameState state) {
        return <String, dynamic>{
          'currentGame': state.currentGame.toJson(),
        };
      }
    }