@@ -56,33 +56,31 @@ const applySnakePosition = (prevState) => {
56
56
57
57
// const snakeCoordinatesWithoutLast = prevState.snake.coordinates.slice()
58
58
59
+ const snakeHead = DIRECTION_TICKS [ prevState . controls . direction ] (
60
+ prevState . snake . coordinates [ 0 ] . x ,
61
+ prevState . snake . coordinates [ 0 ] . y ,
62
+ ) ;
63
+
64
+ const isSnakeEating = getIsSnakeEating ( prevState ) ;
65
+ const snakeTail = isSnakeEating
66
+ ? prevState . snake . coordinates
67
+ : prevState . snake . coordinates . slice ( 0 , prevState . snake . coordinates . length - 1 )
68
+
59
69
return {
60
70
snake : {
61
- coordinates : [
62
- DIRECTION_TICKS [ prevState . controls . direction ] (
63
- prevState . snake . coordinates [ 0 ] . x ,
64
- prevState . snake . coordinates [ 0 ] . y ,
65
- ) ,
66
- // ...prevState.snake.coordinates,
67
- ] ,
71
+ coordinates : [ snakeHead ] . concat ( snakeTail )
68
72
} ,
69
73
} ;
70
74
} ;
71
75
72
- const applySnakeAte = ( prevState ) => ( {
73
- snake : {
74
-
75
- } ,
76
- } ) ;
77
-
78
76
const doChangeDirection = ( direction ) => ( ) => ( {
79
77
controls : {
80
78
direction,
81
79
} ,
82
80
} ) ;
83
81
84
- const isSnakeEating = ( { snake, snack } ) =>
85
- isPosition ( snake . coordinates [ 0 ] . x , snack . coordinate . x ) && isPosition ( snake . coordinates [ 0 ] . y , snack . coordinate . y ) ;
82
+ const getIsSnakeEating = ( { snake, snack } ) =>
83
+ isPosition ( snake . coordinates [ 0 ] . x , snake . coordinates [ 0 ] . y , snack . coordinate . x , snack . coordinate . y )
86
84
87
85
class App extends Component {
88
86
constructor ( props ) {
@@ -121,10 +119,6 @@ class App extends Component {
121
119
}
122
120
123
121
onTick = ( ) => {
124
- // if (isSnakeEating(this.state)) {
125
- // this.setState(applySnakeAte);
126
- // }
127
-
128
122
this . setState ( applySnakePosition ) ;
129
123
}
130
124
0 commit comments