diff --git a/fastlane/metadata/android/en-US/changelogs/21.txt b/fastlane/metadata/android/en-US/changelogs/21.txt new file mode 100644 index 0000000000000000000000000000000000000000..8d16f79887ddae8a9bc35f91a9561a50e5882559 --- /dev/null +++ b/fastlane/metadata/android/en-US/changelogs/21.txt @@ -0,0 +1 @@ +Improve snake length management. diff --git a/fastlane/metadata/android/fr-FR/changelogs/21.txt b/fastlane/metadata/android/fr-FR/changelogs/21.txt new file mode 100644 index 0000000000000000000000000000000000000000..969352bf18ff3b6b8df53924aa7c8ebdf2d93d37 --- /dev/null +++ b/fastlane/metadata/android/fr-FR/changelogs/21.txt @@ -0,0 +1 @@ +Amélioration de la gestion de la longueur du serpent. diff --git a/lib/cubit/activity/activity_cubit.dart b/lib/cubit/activity/activity_cubit.dart index 3868df3c372361ac2d45773e6bb5339ee54e6120..b9e3cd447b3b3237ef3b8feedec373c84908912e 100644 --- a/lib/cubit/activity/activity_cubit.dart +++ b/lib/cubit/activity/activity_cubit.dart @@ -107,8 +107,13 @@ class ActivityCubit extends HydratedCubit<ActivityState> { refresh(); } - void moveSnake([bool enlarge = false]) { - state.currentActivity.moveSnake(enlarge); + void moveSnake() { + state.currentActivity.moveSnake(); + refresh(); + } + + void updateSnakeSize(int delta) { + state.currentActivity.updateSnakeSize(delta); refresh(); } diff --git a/lib/models/activity/activity.dart b/lib/models/activity/activity.dart index 31b9cce3edbb2603faf8518d31c8c9c5bcd10d09..51260bda26d26d153a7a0030799798709fe112ee 100644 --- a/lib/models/activity/activity.dart +++ b/lib/models/activity/activity.dart @@ -168,7 +168,14 @@ class Activity { return true; } - void moveSnake([bool enlarge = false]) { + void updateSnakeSize(int delta) { + snake.size += delta; + if (snake.size < 3) { + snake.size = 3; + } + } + + void moveSnake() { if (!canMove()) { printlog('boom'); isFinished = true; @@ -182,7 +189,7 @@ class Activity { snake.cells.add(head); // Drop tail - if (!enlarge) { + while (snake.cells.length > snake.size) { snake.cells.removeAt(0); } diff --git a/lib/models/activity/snake.dart b/lib/models/activity/snake.dart index 169be630a66623c94581cf520ea64cf9dbc10146..b901d065eeaa010e755e9287beb7caacbd53d5a1 100644 --- a/lib/models/activity/snake.dart +++ b/lib/models/activity/snake.dart @@ -8,15 +8,18 @@ enum SnakeDirection { top, left, bottom, right } class Snake { // Snake's cells: last in list is head SnakeCells cells; + int size; SnakeDirection direction; Snake({ required this.cells, + required this.size, required this.direction, }); factory Snake.create(ActivitySettings activitySettings) { - // Default init snake size: 3 + // Default init snake size + const int initialLength = 3; // ~ center cell: final int middleColumn = activitySettings.boardSize ~/ 2; @@ -24,12 +27,11 @@ class Snake { SnakeCells cells = [ CellLocation.go(middleRow, middleColumn), - CellLocation.go(middleRow, middleColumn), - CellLocation.go(middleRow, middleColumn), ]; return Snake( cells: cells, + size: initialLength, direction: SnakeDirection.right, ); } @@ -44,6 +46,7 @@ class Snake { Map<String, dynamic>? toJson() { return <String, dynamic>{ 'cells': cells, + 'size': size, 'direction': direction.toString(), }; } diff --git a/lib/ui/game/game_top.dart b/lib/ui/game/game_top.dart index abd99b8af704d53b9147d74745e760edff0f9226..19fa80eeb1edac9a40da643fe41a174195c8c08e 100644 --- a/lib/ui/game/game_top.dart +++ b/lib/ui/game/game_top.dart @@ -1,27 +1,37 @@ import 'package:flutter/material.dart'; +import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; + +import 'package:snake/cubit/activity/activity_cubit.dart'; +import 'package:snake/models/activity/activity.dart'; class GameTopWidget extends StatelessWidget { const GameTopWidget({super.key}); @override Widget build(BuildContext context) { - return Table( - children: const [ - TableRow( + return BlocBuilder<ActivityCubit, ActivityState>( + builder: (BuildContext context, ActivityState activityState) { + final Activity currentActivity = activityState.currentActivity; + + return Table( children: [ - Column( - children: [ - Text('xxx'), - ], - ), - Column( + TableRow( children: [ - Text('xxx'), + Column( + children: [ + Text('size: ${currentActivity.snake.size}'), + ], + ), + Column( + children: [ + Text('xxx'), + ], + ), ], ), ], - ), - ], + ); + }, ); } } diff --git a/lib/ui/widgets/game/controller_bar.dart b/lib/ui/widgets/game/controller_bar.dart index 640d61a1766336d5158b9f93611c3f7c68a1df8f..d07a7c4e33641858755f65526835e051e544b08d 100644 --- a/lib/ui/widgets/game/controller_bar.dart +++ b/lib/ui/widgets/game/controller_bar.dart @@ -14,26 +14,33 @@ class ControllerBar extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: [ - StyledButton( + StyledButton.text( + caption: '<', color: Colors.orange, onPressed: () { BlocProvider.of<ActivityCubit>(context).turnLeft(); }, - child: Text('<'), ), - StyledButton( + StyledButton.text( + caption: '-', + color: Colors.green, + onPressed: () { + BlocProvider.of<ActivityCubit>(context).updateSnakeSize(-1); + }, + ), + StyledButton.text( + caption: '+', color: Colors.red, onPressed: () { - BlocProvider.of<ActivityCubit>(context).moveSnake(true); + BlocProvider.of<ActivityCubit>(context).updateSnakeSize(1); }, - child: Text('+'), ), - StyledButton( + StyledButton.text( + caption: '>', color: Colors.orange, onPressed: () { BlocProvider.of<ActivityCubit>(context).turnRight(); }, - child: Text('>'), ), ], ); diff --git a/pubspec.yaml b/pubspec.yaml index 47427eb3a54f875e8179eab5befb99bf9a1dc148..06d5e35e80680aa2a46cf8048377be7e2de2ee0e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: snake game publish_to: "none" -version: 0.5.0+20 +version: 0.5.1+21 environment: sdk: "^3.0.0"