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

Merge branch '23-use-snake-length' into 'master'

Resolve "Use snake length"

Closes #23

See merge request !22
parents 8745e541 25838572
No related branches found
No related tags found
1 merge request!22Resolve "Use snake length"
Pipeline #6797 passed
Improve snake length management.
Amélioration de la gestion de la longueur du serpent.
......@@ -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();
}
......
......@@ -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);
}
......
......@@ -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(),
};
}
......
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'),
],
),
],
),
],
),
],
);
},
);
}
}
......@@ -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('>'),
),
],
);
......
......@@ -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"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment