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

Improve manage snake size

parent 8745e541
No related branches found
No related tags found
1 merge request!22Resolve "Use snake length"
Pipeline #6750 passed
This commit is part of merge request !22. Comments created here will be created in the context of that merge request.
Improve snake length management.
Amélioration de la gestion de la longueur du serpent.
...@@ -107,8 +107,13 @@ class ActivityCubit extends HydratedCubit<ActivityState> { ...@@ -107,8 +107,13 @@ class ActivityCubit extends HydratedCubit<ActivityState> {
refresh(); refresh();
} }
void moveSnake([bool enlarge = false]) { void moveSnake() {
state.currentActivity.moveSnake(enlarge); state.currentActivity.moveSnake();
refresh();
}
void updateSnakeSize(int delta) {
state.currentActivity.updateSnakeSize(delta);
refresh(); refresh();
} }
......
...@@ -168,7 +168,14 @@ class Activity { ...@@ -168,7 +168,14 @@ class Activity {
return true; 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()) { if (!canMove()) {
printlog('boom'); printlog('boom');
isFinished = true; isFinished = true;
...@@ -182,7 +189,7 @@ class Activity { ...@@ -182,7 +189,7 @@ class Activity {
snake.cells.add(head); snake.cells.add(head);
// Drop tail // Drop tail
if (!enlarge) { while (snake.cells.length > snake.size) {
snake.cells.removeAt(0); snake.cells.removeAt(0);
} }
......
...@@ -8,15 +8,18 @@ enum SnakeDirection { top, left, bottom, right } ...@@ -8,15 +8,18 @@ enum SnakeDirection { top, left, bottom, right }
class Snake { class Snake {
// Snake's cells: last in list is head // Snake's cells: last in list is head
SnakeCells cells; SnakeCells cells;
int size;
SnakeDirection direction; SnakeDirection direction;
Snake({ Snake({
required this.cells, required this.cells,
required this.size,
required this.direction, required this.direction,
}); });
factory Snake.create(ActivitySettings activitySettings) { factory Snake.create(ActivitySettings activitySettings) {
// Default init snake size: 3 // Default init snake size
const int initialLength = 3;
// ~ center cell: // ~ center cell:
final int middleColumn = activitySettings.boardSize ~/ 2; final int middleColumn = activitySettings.boardSize ~/ 2;
...@@ -24,12 +27,11 @@ class Snake { ...@@ -24,12 +27,11 @@ class Snake {
SnakeCells cells = [ SnakeCells cells = [
CellLocation.go(middleRow, middleColumn), CellLocation.go(middleRow, middleColumn),
CellLocation.go(middleRow, middleColumn),
CellLocation.go(middleRow, middleColumn),
]; ];
return Snake( return Snake(
cells: cells, cells: cells,
size: initialLength,
direction: SnakeDirection.right, direction: SnakeDirection.right,
); );
} }
...@@ -44,6 +46,7 @@ class Snake { ...@@ -44,6 +46,7 @@ class Snake {
Map<String, dynamic>? toJson() { Map<String, dynamic>? toJson() {
return <String, dynamic>{ return <String, dynamic>{
'cells': cells, 'cells': cells,
'size': size,
'direction': direction.toString(), 'direction': direction.toString(),
}; };
} }
......
import 'package:flutter/material.dart'; 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 { class GameTopWidget extends StatelessWidget {
const GameTopWidget({super.key}); const GameTopWidget({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, ActivityState activityState) {
final Activity currentActivity = activityState.currentActivity;
return Table( return Table(
children: const [ children: [
TableRow( TableRow(
children: [ children: [
Column( Column(
children: [ children: [
Text('xxx'), Text('size: ${currentActivity.snake.size}'),
], ],
), ),
Column( Column(
...@@ -23,5 +31,7 @@ class GameTopWidget extends StatelessWidget { ...@@ -23,5 +31,7 @@ class GameTopWidget extends StatelessWidget {
), ),
], ],
); );
},
);
} }
} }
...@@ -14,26 +14,33 @@ class ControllerBar extends StatelessWidget { ...@@ -14,26 +14,33 @@ class ControllerBar extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: [ children: [
StyledButton( StyledButton.text(
caption: '<',
color: Colors.orange, color: Colors.orange,
onPressed: () { onPressed: () {
BlocProvider.of<ActivityCubit>(context).turnLeft(); 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, color: Colors.red,
onPressed: () { onPressed: () {
BlocProvider.of<ActivityCubit>(context).moveSnake(true); BlocProvider.of<ActivityCubit>(context).updateSnakeSize(1);
}, },
child: Text('+'),
), ),
StyledButton( StyledButton.text(
caption: '>',
color: Colors.orange, color: Colors.orange,
onPressed: () { onPressed: () {
BlocProvider.of<ActivityCubit>(context).turnRight(); BlocProvider.of<ActivityCubit>(context).turnRight();
}, },
child: Text('>'),
), ),
], ],
); );
......
...@@ -3,7 +3,7 @@ description: snake game ...@@ -3,7 +3,7 @@ description: snake game
publish_to: "none" publish_to: "none"
version: 0.5.0+20 version: 0.5.1+21
environment: environment:
sdk: "^3.0.0" sdk: "^3.0.0"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment