Skip to content
Snippets Groups Projects
Commit c23f7fb7 authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Normalize game architecture

parent c3ed1087
Branches
Tags
1 merge request!43Resolve "Normalize game architecture"
Pipeline #5686 passed
Showing
with 157 additions and 91 deletions
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
app.versionName=1.2.33
app.versionCode=39
app.versionName=1.3.0
app.versionCode=40
{
"app_name": "Categories",
"bottom_nav_home": "Game",
"bottom_nav_settings": "Settings",
"bottom_nav_about": "About",
"settings_title": "Settings",
"settings_label_theme": "Theme mode",
......
{
"app_name": "Petit Bac",
"bottom_nav_home": "Jeu",
"bottom_nav_settings": "Réglages",
"bottom_nav_about": "Infos",
"settings_title": "Réglages",
"settings_label_theme": "Thème de couleurs",
......
File moved
assets/ui/button_delete_saved_game.png

5.68 KiB

assets/ui/button_resume_game.png

3.57 KiB

File moved
assets/ui/game_end.png

7.75 KiB

File moved
Improve/normalize game architecture.
Amélioration/normalisation de l'architecture du jeu.
......@@ -10,42 +10,70 @@ class DefaultGameSettings {
];
// items count: available values
static const int itemsCountValueNoLimit = 0;
static const int itemsCountValueShort = 5;
static const int itemsCountValueMedium = 10;
static const int itemsCountValueLong = 20;
static const List<int> allowedItemsCountValues = [
static const String itemsCountValueNoLimit = 'nolimit';
static const String itemsCountValueShort = 'short';
static const String itemsCountValueMedium = 'medium';
static const String itemsCountValueLong = 'long';
static const List<String> allowedItemsCountValues = [
itemsCountValueNoLimit,
itemsCountValueShort,
itemsCountValueMedium,
itemsCountValueLong,
];
// items count: default value
static const int defaultItemsCountValue = itemsCountValueMedium;
static const String defaultItemsCountValue = itemsCountValueMedium;
// timer value: available values
static const int timerValueNoTimer = 0;
static const int timerValueLow = 10;
static const int timerValueMedium = 30;
static const int timerValueHigh = 60;
static const List<int> allowedTimerValues = [
static const String timerValueNoTimer = 'nolimit';
static const String timerValueLow = 'low';
static const String timerValueMedium = 'medium';
static const String timerValueHigh = 'high';
static const List<String> allowedTimerValues = [
timerValueNoTimer,
timerValueLow,
timerValueMedium,
timerValueHigh,
];
// timer value: default value
static const int defaultTimerValue = timerValueMedium;
static const String defaultTimerValue = timerValueMedium;
static List<int> getAvailableValues(String parameterCode) {
// available values from parameter code
static List<String> getAvailableValues(String parameterCode) {
switch (parameterCode) {
case 'itemsCount':
case parameterCodeItemsCount:
return DefaultGameSettings.allowedItemsCountValues;
case 'timerValue':
case parameterCodeTimerValue:
return DefaultGameSettings.allowedTimerValues;
}
printlog('Did not find any available value for game parameter "$parameterCode".');
return [];
}
static int getItemsCountValueFromCode(String itemsCountParameterValue) {
const Map<String, int> values = {
DefaultGameSettings.itemsCountValueNoLimit: 0,
DefaultGameSettings.itemsCountValueShort: 5,
DefaultGameSettings.itemsCountValueMedium: 10,
DefaultGameSettings.itemsCountValueLong: 20,
};
return values[itemsCountParameterValue] ??
getItemsCountValueFromCode(DefaultGameSettings.defaultItemsCountValue);
}
static int getTimerValueFromCode(String timerValueParameterValue) {
const Map<String, int> values = {
timerValueNoTimer: 0,
timerValueLow: 5,
timerValueMedium: 30,
timerValueHigh: 90,
};
return values[timerValueParameterValue] ??
getTimerValueFromCode(DefaultGameSettings.defaultTimerValue);
}
// parameters displayed with assets (instead of painter)
static List<String> displayedWithAssets = [
//
];
}
import 'package:petitbac/utils/tools.dart';
class DefaultGlobalSettings {
static const List<String> availableParameters = [];
// available global parameters codes
static const String parameterCodeSkin = 'skin';
static const List<String> availableParameters = [
parameterCodeSkin,
];
// skin: available values
static const String skinValueDefault = 'default';
static const List<String> allowedSkinValues = [
skinValueDefault,
];
// skin: default value
static const String defaultSkinValue = skinValueDefault;
// available values from parameter code
static List<String> getAvailableValues(String parameterCode) {
switch (parameterCode) {
case parameterCodeSkin:
return DefaultGlobalSettings.allowedSkinValues;
}
static List<int> getAvailableValues(String parameterCode) {
printlog('Did not find any available value for global parameter "$parameterCode".');
return [];
}
// parameters displayed with assets (instead of painter)
static List<String> displayedWithAssets = [
//
];
}
......@@ -6,12 +6,10 @@ import 'package:petitbac/ui/screens/page_game.dart';
import 'package:petitbac/ui/screens/page_settings.dart';
class MenuItem {
final String code;
final Icon icon;
final Widget page;
const MenuItem({
required this.code,
required this.icon,
required this.page,
});
......@@ -20,21 +18,18 @@ class MenuItem {
class Menu {
static const indexGame = 0;
static const menuItemGame = MenuItem(
code: 'bottom_nav_game',
icon: Icon(UniconsLine.home),
page: PageGame(),
);
static const indexSettings = 1;
static const menuItemSettings = MenuItem(
code: 'bottom_nav_settings',
icon: Icon(UniconsLine.setting),
page: PageSettings(),
);
static const indexAbout = 2;
static const menuItemAbout = MenuItem(
code: 'bottom_nav_about',
icon: Icon(UniconsLine.info_circle),
page: PageAbout(),
);
......
......@@ -39,11 +39,9 @@ final ColorScheme lightColorScheme = ColorScheme.light(
secondary: primarySwatch.shade500,
onSecondary: Colors.white,
error: errorColor,
background: textSwatch.shade200,
onBackground: textSwatch.shade500,
onSurface: textSwatch.shade500,
surface: textSwatch.shade50,
surfaceVariant: Colors.white,
surfaceContainerHighest: Colors.white,
shadow: textSwatch.shade900.withOpacity(.1),
);
......@@ -52,11 +50,9 @@ final ColorScheme darkColorScheme = ColorScheme.dark(
secondary: primarySwatch.shade500,
onSecondary: Colors.white,
error: errorColor,
background: const Color(0xFF171724),
onBackground: textSwatch.shade400,
onSurface: textSwatch.shade300,
surface: const Color(0xFF262630),
surfaceVariant: const Color(0xFF282832),
surfaceContainerHighest: const Color(0xFF282832),
shadow: textSwatch.shade900.withOpacity(.2),
);
......@@ -192,5 +188,3 @@ final ThemeData darkTheme = lightTheme.copyWith(
),
),
);
final ThemeData appTheme = darkTheme;
......@@ -6,17 +6,17 @@ import 'package:flutter/material.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:petitbac/data/fetch_data_helper.dart';
import 'package:petitbac/models/data/game_item.dart';
import 'package:petitbac/models/game.dart';
import 'package:petitbac/models/settings_game.dart';
import 'package:petitbac/models/settings_global.dart';
import 'package:petitbac/models/game/game.dart';
import 'package:petitbac/models/game/game_item.dart';
import 'package:petitbac/models/settings/settings_game.dart';
import 'package:petitbac/models/settings/settings_global.dart';
part 'game_state.dart';
class GameCubit extends HydratedCubit<GameState> {
GameCubit()
: super(GameState(
currentGame: Game.createNull(),
currentGame: Game.createEmpty(),
));
void updateState(Game game) {
......@@ -27,13 +27,19 @@ class GameCubit extends HydratedCubit<GameState> {
void refresh() {
final Game game = Game(
// Settings
gameSettings: state.currentGame.gameSettings,
globalSettings: state.currentGame.globalSettings,
items: state.currentGame.items,
// State
isRunning: state.currentGame.isRunning,
isStarted: state.currentGame.isStarted,
isFinished: state.currentGame.isFinished,
countdown: state.currentGame.countdown,
animationInProgress: state.currentGame.animationInProgress,
// Base data
items: state.currentGame.items,
// Game data
position: state.currentGame.position,
countdown: state.currentGame.countdown,
);
// game.dump();
......@@ -44,7 +50,8 @@ class GameCubit extends HydratedCubit<GameState> {
required GameSettings gameSettings,
required GlobalSettings globalSettings,
}) {
Game newGame = Game.createNew(
final Game newGame = Game.createNew(
// Settings
gameSettings: gameSettings,
globalSettings: globalSettings,
);
......@@ -60,8 +67,19 @@ class GameCubit extends HydratedCubit<GameState> {
refresh();
}
void resumeSavedGame() {
state.currentGame.isRunning = true;
refresh();
}
void deleteSavedGame() {
state.currentGame.isRunning = false;
state.currentGame.isFinished = true;
refresh();
}
void next() {
if (state.currentGame.position < (state.currentGame.gameSettings.itemsCount - 1)) {
if (state.currentGame.position < (state.currentGame.gameSettings.itemsCountValue - 1)) {
state.currentGame.position++;
startTimer();
} else {
......@@ -71,7 +89,7 @@ class GameCubit extends HydratedCubit<GameState> {
}
void startTimer() {
int timerValue = state.currentGame.gameSettings.timerValue;
int timerValue = state.currentGame.gameSettings.timerCountValue;
if (timerValue != 0) {
state.currentGame.countdown = timerValue;
......@@ -88,7 +106,7 @@ class GameCubit extends HydratedCubit<GameState> {
if (state.currentGame.countdown == 0) {
timer.cancel();
if (state.currentGame.position ==
(state.currentGame.gameSettings.itemsCount - 1)) {
(state.currentGame.gameSettings.itemsCountValue - 1)) {
state.currentGame.isFinished = true;
}
}
......@@ -97,7 +115,7 @@ class GameCubit extends HydratedCubit<GameState> {
},
);
} else {
if (state.currentGame.position == (state.currentGame.gameSettings.itemsCount - 1)) {
if (state.currentGame.position == (state.currentGame.gameSettings.itemsCountValue - 1)) {
state.currentGame.isFinished = true;
}
}
......@@ -132,7 +150,7 @@ class GameCubit extends HydratedCubit<GameState> {
@override
GameState? fromJson(Map<String, dynamic> json) {
Game currentGame = json['currentGame'] as Game;
final Game currentGame = json['currentGame'] as Game;
return GameState(
currentGame: currentGame,
......
......@@ -9,7 +9,7 @@ class GameState extends Equatable {
final Game currentGame;
@override
List<Object?> get props => <Object?>[
List<dynamic> get props => <dynamic>[
currentGame,
];
}
......@@ -2,8 +2,8 @@ import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:petitbac/models/settings_game.dart';
import 'package:petitbac/utils/tools.dart';
import 'package:petitbac/config/default_game_settings.dart';
import 'package:petitbac/models/settings/settings_game.dart';
part 'settings_game_state.dart';
......@@ -11,8 +11,8 @@ class GameSettingsCubit extends HydratedCubit<GameSettingsState> {
GameSettingsCubit() : super(GameSettingsState(settings: GameSettings.createDefault()));
void setValues({
int? itemsCount,
int? timerValue,
String? itemsCount,
String? timerValue,
}) {
emit(
GameSettingsState(
......@@ -24,22 +24,24 @@ class GameSettingsCubit extends HydratedCubit<GameSettingsState> {
);
}
int getParameterValue(String code) {
String getParameterValue(String code) {
switch (code) {
case 'itemsCount':
case DefaultGameSettings.parameterCodeItemsCount:
return GameSettings.getItemsCountValueFromUnsafe(state.settings.itemsCount);
case 'timerValue':
case DefaultGameSettings.parameterCodeTimerValue:
return GameSettings.getTimerValueFromUnsafe(state.settings.timerValue);
}
return 0;
}
void setParameterValue(String code, int value) {
printlog('GameSettingsCubit.setParameterValue');
printlog('code: $code / value: $value');
return '';
}
int itemsCount = code == 'itemsCount' ? value : getParameterValue('itemsCount');
int timerValue = code == 'timerValue' ? value : getParameterValue('timerValue');
void setParameterValue(String code, String value) {
final String itemsCount = code == DefaultGameSettings.parameterCodeItemsCount
? value
: getParameterValue(DefaultGameSettings.parameterCodeItemsCount);
final String timerValue = code == DefaultGameSettings.parameterCodeTimerValue
? value
: getParameterValue(DefaultGameSettings.parameterCodeTimerValue);
setValues(
itemsCount: itemsCount,
......@@ -49,8 +51,8 @@ class GameSettingsCubit extends HydratedCubit<GameSettingsState> {
@override
GameSettingsState? fromJson(Map<String, dynamic> json) {
int itemsCount = json['itemsCount'] as int;
int timerValue = json['timerValue'] as int;
final String itemsCount = json[DefaultGameSettings.parameterCodeItemsCount] as String;
final String timerValue = json[DefaultGameSettings.parameterCodeTimerValue] as String;
return GameSettingsState(
settings: GameSettings(
......@@ -63,8 +65,8 @@ class GameSettingsCubit extends HydratedCubit<GameSettingsState> {
@override
Map<String, dynamic>? toJson(GameSettingsState state) {
return <String, dynamic>{
'itemsCount': state.settings.itemsCount,
'timerValue': state.settings.timerValue,
DefaultGameSettings.parameterCodeItemsCount: state.settings.itemsCount,
DefaultGameSettings.parameterCodeTimerValue: state.settings.timerValue,
};
}
}
......@@ -12,8 +12,4 @@ class GameSettingsState extends Equatable {
List<dynamic> get props => <dynamic>[
settings,
];
Map<String, dynamic> get values => <String, dynamic>{
'settings': settings,
};
}
......@@ -2,43 +2,59 @@ import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:petitbac/models/settings_global.dart';
import 'package:petitbac/utils/tools.dart';
import 'package:petitbac/config/default_global_settings.dart';
import 'package:petitbac/models/settings/settings_global.dart';
part 'settings_global_state.dart';
class GlobalSettingsCubit extends HydratedCubit<GlobalSettingsState> {
GlobalSettingsCubit() : super(GlobalSettingsState(settings: GlobalSettings.createDefault()));
void setValues() {
void setValues({
String? skin,
}) {
emit(
GlobalSettingsState(
settings: GlobalSettings(),
settings: GlobalSettings(
skin: skin ?? state.settings.skin,
),
),
);
}
int getParameterValue(String code) {
switch (code) {}
return 0;
String getParameterValue(String code) {
switch (code) {
case DefaultGlobalSettings.parameterCodeSkin:
return GlobalSettings.getSkinValueFromUnsafe(state.settings.skin);
}
return '';
}
void setParameterValue(String code, int value) {
printlog('GlobalSettingsCubit.setParameterValue');
printlog('code: $code / value: $value');
void setParameterValue(String code, String value) {
final String skin = (code == DefaultGlobalSettings.parameterCodeSkin)
? value
: getParameterValue(DefaultGlobalSettings.parameterCodeSkin);
setValues();
setValues(
skin: skin,
);
}
@override
GlobalSettingsState? fromJson(Map<String, dynamic> json) {
final String skin = json[DefaultGlobalSettings.parameterCodeSkin] as String;
return GlobalSettingsState(
settings: GlobalSettings(),
settings: GlobalSettings(
skin: skin,
),
);
}
@override
Map<String, dynamic>? toJson(GlobalSettingsState state) {
return <String, dynamic>{};
return <String, dynamic>{
DefaultGlobalSettings.parameterCodeSkin: state.settings.skin,
};
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment