1
1
import React , { Component } from 'react' ;
2
2
import cs from 'classnames' ;
3
3
4
+ import { some } from 'lodash' ;
5
+
4
6
import './App.css' ;
5
7
6
8
var GRID_ARRAY = [ ] ;
@@ -30,12 +32,46 @@ const DIRECTION_TICKS = {
30
32
LEFT : ( x , y ) => ( { x : x - 1 , y } ) ,
31
33
} ;
32
34
33
- // TODO compose instead
34
- const applySnakePosition = ( prevState ) => ( {
35
- snake : {
36
- position : {
37
- ...DIRECTION_TICKS [ prevState . controls . direction ] ( prevState . snake . position . x , prevState . snake . position . y ) ,
35
+ const isPosition = ( x , y , diffX , diffY ) =>
36
+ x === diffX && y === diffY ;
37
+
38
+ const get = ( array , property ) =>
39
+ array . map ( v => v [ property ] ) ;
40
+
41
+ // TODO make own some, use compose
42
+ const isSnake = ( x , y , snakeCoordinates ) => {
43
+ for ( var i = 0 ; i < snakeCoordinates . length ; i ++ ) {
44
+ if ( isPosition ( snakeCoordinates [ i ] . x , snakeCoordinates [ i ] . y , x , y ) ) {
45
+ return true ;
46
+ }
47
+ }
48
+ return false ;
49
+ }
50
+
51
+ // TODO compose instead: direction ticks
52
+ // TODO make last a previous compose step
53
+ const applySnakePosition = ( prevState ) => {
54
+ // TODO babel stage
55
+ // const [...snakeCoordinatesWithoutLast, lastCoordinate] = prevState.snake.coordinates;
56
+
57
+ // const snakeCoordinatesWithoutLast = prevState.snake.coordinates.slice()
58
+
59
+ return {
60
+ 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
+ ] ,
38
68
} ,
69
+ } ;
70
+ } ;
71
+
72
+ const applySnakeAte = ( prevState ) => ( {
73
+ snake : {
74
+
39
75
} ,
40
76
} ) ;
41
77
@@ -45,6 +81,9 @@ const doChangeDirection = (direction) => () => ({
45
81
} ,
46
82
} ) ;
47
83
84
+ const isSnakeEating = ( { snake, snack } ) =>
85
+ isPosition ( snake . coordinates [ 0 ] . x , snack . coordinate . x ) && isPosition ( snake . coordinates [ 0 ] . y , snack . coordinate . y ) ;
86
+
48
87
class App extends Component {
49
88
constructor ( props ) {
50
89
super ( props ) ;
@@ -54,13 +93,13 @@ class App extends Component {
54
93
direction : CONTROLS . RIGHT ,
55
94
} ,
56
95
snake : {
57
- position : {
96
+ coordinates : [ {
58
97
x : 10 ,
59
98
y : 25 ,
60
- } ,
99
+ } ] ,
61
100
} ,
62
101
snack : {
63
- position : {
102
+ coordinate : {
64
103
x : 40 ,
65
104
y : 25 ,
66
105
} ,
@@ -82,6 +121,10 @@ class App extends Component {
82
121
}
83
122
84
123
onTick = ( ) => {
124
+ // if (isSnakeEating(this.state)) {
125
+ // this.setState(applySnakeAte);
126
+ // }
127
+
85
128
this . setState ( applySnakePosition ) ;
86
129
}
87
130
@@ -93,7 +136,6 @@ class App extends Component {
93
136
94
137
render ( ) {
95
138
const { snake, snack } = this . state
96
- console . log ( this . state . controls . direction ) ; ;
97
139
return (
98
140
< div >
99
141
< Grid
@@ -126,9 +168,6 @@ const Row = ({ snake, snack, y }) =>
126
168
/> ) }
127
169
</ div >
128
170
129
- const isPosition = ( x , y , diffX , diffY ) =>
130
- x === diffX && y === diffY ;
131
-
132
171
const Cell = ( {
133
172
snake,
134
173
snack,
@@ -138,8 +177,8 @@ const Cell = ({
138
177
const cellCs = cs (
139
178
"grid-cell" ,
140
179
{
141
- "grid-cell-snake" : isPosition ( x , y , snake . position . x , snake . position . y ) ,
142
- "grid-cell-snack" : isPosition ( x , y , snack . position . x , snack . position . y ) ,
180
+ "grid-cell-snake" : isSnake ( x , y , snake . coordinates ) ,
181
+ "grid-cell-snack" : isPosition ( x , y , snack . coordinate . x , snack . coordinate . y ) ,
143
182
}
144
183
) ;
145
184
0 commit comments