@@ -79,6 +79,12 @@ const getIsSnakeClumy = (snake) =>
79
79
const getIsSnakeEating = ( { snake, snack } ) =>
80
80
isPosition ( getSnakeHead ( snake ) . x , getSnakeHead ( snake ) . y , snack . coordinate . x , snack . coordinate . y ) ;
81
81
82
+ const applyGameOver = ( prevState ) => ( {
83
+ playground : {
84
+ isGameOver : true
85
+ } ,
86
+ } )
87
+
82
88
// TODO compose instead: direction ticks
83
89
// TODO make last a previous compose step
84
90
const applySnakePosition = ( prevState ) => {
@@ -113,12 +119,13 @@ const doChangeDirection = (direction) => () => ({
113
119
} ,
114
120
} ) ;
115
121
116
- const getCellCs = ( snake , snack , x , y ) =>
122
+ const getCellCs = ( isGameOver , snake , snack , x , y ) =>
117
123
cs (
118
124
'grid-cell' ,
119
125
{
120
126
'grid-cell-snake' : isSnake ( x , y , snake . coordinates ) ,
121
127
'grid-cell-snack' : isPosition ( x , y , snack . coordinate . x , snack . coordinate . y ) ,
128
+ 'grid-cell-hit' : isGameOver && isPosition ( x , y , getSnakeHead ( snake ) . x , getSnakeHead ( snake ) . y )
122
129
}
123
130
) ;
124
131
@@ -153,10 +160,9 @@ class App extends Component {
153
160
}
154
161
155
162
onTick = ( ) => {
156
- if ( getIsSnakeOutside ( this . state . snake ) || getIsSnakeClumy ( this . state . snake ) ) {
157
- return ;
158
- }
159
- this . setState ( applySnakePosition ) ;
163
+ getIsSnakeOutside ( this . state . snake ) || getIsSnakeClumy ( this . state . snake )
164
+ ? this . setState ( applyGameOver )
165
+ : this . setState ( applySnakePosition ) ;
160
166
}
161
167
162
168
onChangeDirection = ( e ) => {
@@ -166,31 +172,42 @@ class App extends Component {
166
172
}
167
173
168
174
render ( ) {
169
- const { snake, snack } = this . state ;
175
+ const {
176
+ playground,
177
+ snake,
178
+ snack,
179
+ } = this . state ;
180
+
181
+ const {
182
+ isGameOver,
183
+ } = playground ;
184
+
170
185
return (
171
186
< div >
172
187
< Grid
173
188
snake = { snake }
174
189
snack = { snack }
190
+ isGameOver = { isGameOver }
175
191
/>
176
192
</ div >
177
193
) ;
178
194
}
179
195
}
180
196
181
- const Grid = ( { snake, snack } ) =>
197
+ const Grid = ( { isGameOver , snake, snack } ) =>
182
198
< div className = "grid" >
183
199
< Border grid = { GRID_ARRAY } />
184
200
{ GRID_ARRAY . map ( y => < Row
185
201
y = { y }
186
202
key = { y }
187
203
snake = { snake }
188
204
snack = { snack }
205
+ isGameOver = { isGameOver }
189
206
/> ) }
190
207
< Border grid = { GRID_ARRAY } />
191
208
</ div >
192
209
193
- const Row = ( { snake, snack, y } ) =>
210
+ const Row = ( { isGameOver , snake, snack, y } ) =>
194
211
< div className = "grid-row" >
195
212
< Block />
196
213
{ GRID_ARRAY . map ( x => < Cell
@@ -199,6 +216,7 @@ const Row = ({ snake, snack, y }) =>
199
216
key = { x }
200
217
snake = { snake }
201
218
snack = { snack }
219
+ isGameOver = { isGameOver }
202
220
/> ) }
203
221
< Block />
204
222
</ div >
@@ -213,7 +231,7 @@ const Border = ({ grid }) =>
213
231
const Block = ( ) =>
214
232
< div className = "grid-cell grid-cell-border" />
215
233
216
- const Cell = ( { snake, snack, x, y } ) =>
217
- < div className = { getCellCs ( snake , snack , x , y ) } /> ;
234
+ const Cell = ( { isGameOver , snake, snack, x, y } ) =>
235
+ < div className = { getCellCs ( isGameOver , snake , snack , x , y ) } /> ;
218
236
219
237
export default App ;
0 commit comments