-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
174 lines (167 loc) · 4.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React from 'react';
import immutable, { List } from 'immutable';
import classnames from 'classnames';
import propTypes from 'prop-types';
import style from './index.less';
import { isClear } from '../../unit/';
import { fillLine, blankLine } from '../../unit/const';
import states from '../../control/states';
const t = setTimeout;
export default class Matrix extends React.Component {
constructor() {
super();
this.state = {
clearLines: false,
animateColor: 2,
isOver: false,
overState: null,
};
}
componentWillReceiveProps(nextProps = {}) {
const clears = isClear(nextProps.matrix);
const overs = nextProps.reset;
this.setState({
clearLines: clears,
isOver: overs,
});
if (clears && !this.state.clearLines) {
this.clearAnimate(clears);
}
if (!clears && overs && !this.state.isOver) {
this.over(nextProps);
}
}
shouldComponentUpdate(nextProps = {}) { // 使用Immutable 比较两个List 是否相等
const props = this.props;
return !(
immutable.is(nextProps.matrix, props.matrix) &&
immutable.is(
(nextProps.cur && nextProps.cur.shape),
(props.cur && props.cur.shape)
) &&
immutable.is(
(nextProps.cur && nextProps.cur.xy),
(props.cur && props.cur.xy)
)
) || this.state.clearLines
|| this.state.isOver;
}
getResult(props = this.props) {
const cur = props.cur;
const shape = cur && cur.shape;
const xy = cur && cur.xy;
let matrix = props.matrix;
const clearLines = this.state.clearLines;
if (clearLines) {
const animateColor = this.state.animateColor;
clearLines.forEach((index) => {
matrix = matrix.set(index, List([
animateColor,
animateColor,
animateColor,
animateColor,
animateColor,
animateColor,
animateColor,
animateColor,
animateColor,
animateColor,
]));
});
} else if (shape) {
shape.forEach((m, k1) => (
m.forEach((n, k2) => {
if (n && xy.get(0) + k1 >= 0) { // 竖坐标可以为负
let line = matrix.get(xy.get(0) + k1);
let color;
if (line.get(xy.get(1) + k2) === 1 && !clearLines) { // 矩阵与方块重合
color = 2;
} else {
color = 1;
}
line = line.set(xy.get(1) + k2, color);
matrix = matrix.set(xy.get(0) + k1, line);
}
})
));
}
return matrix;
}
clearAnimate() {
const anima = (callback) => {
t(() => {
this.setState({
animateColor: 0,
});
t(() => {
this.setState({
animateColor: 2,
});
if (typeof callback === 'function') {
callback();
}
}, 100);
}, 100);
};
anima(() => {
anima(() => {
anima(() => {
t(() => {
states.clearLines(this.props.matrix, this.state.clearLines);
}, 100);
});
});
});
}
over(nextProps) {
let overState = this.getResult(nextProps);
this.setState({
overState,
});
const exLine = (index) => {
if (index <= 19) {
overState = overState.set(19 - index, List(fillLine));
} else if (index >= 20 && index <= 39) {
overState = overState.set(index - 20, List(blankLine));
} else {
states.overEnd();
return;
}
this.setState({
overState,
});
};
for (let i = 0; i <= 40; i++) {
t(exLine.bind(null, i), 40 * (i + 1));
}
}
render() {
let matrix;
if (this.state.isOver) {
matrix = this.state.overState;
} else {
matrix = this.getResult();
}
return (
<div className={style.matrix}>{
matrix.map((p, k1) => (<p key={k1}>
{
p.map((e, k2) => <b
className={classnames({
c: e === 1,
d: e === 2,
})}
key={k2}
/>)
}
</p>))
}
</div>
);
}
}
Matrix.propTypes = {
matrix: propTypes.object.isRequired,
cur: propTypes.object,
reset: propTypes.bool.isRequired,
};