-
Notifications
You must be signed in to change notification settings - Fork 67
/
index.js
141 lines (136 loc) · 3.8 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
/** @jsx React.DOM */
var BoardView = React.createClass({
getInitialState: function () {
return {board: new Board};
},
handleKeyDown: function (event) {
if (this.state.board.hasWon()) {
return;
}
if (event.keyCode >= 37 && event.keyCode <= 40) {
event.preventDefault();
var direction = event.keyCode - 37;
this.setState({board: this.state.board.move(direction)});
}
},
handleTouchStart: function (event) {
if (this.state.board.hasWon()) {
return;
}
if (event.touches.length != 1) {
return;
}
this.startX = event.touches[0].screenX;
this.startY = event.touches[0].screenY;
console.log(this);
event.preventDefault();
},
handleTouchEnd: function (event) {
if (this.state.board.hasWon()) {
return;
}
if (event.changedTouches.length != 1) {
return;
}
var deltaX = event.changedTouches[0].screenX - this.startX;
var deltaY = event.changedTouches[0].screenY - this.startY;
console.log(deltaX, deltaY);
var direction = -1;
if (Math.abs(deltaX) > 3 * Math.abs(deltaY) && Math.abs(deltaX) > 30) {
direction = deltaX > 0 ? 2 : 0;
} else if (Math.abs(deltaY) > 3 * Math.abs(deltaX) && Math.abs(deltaY) > 30) {
direction = deltaY > 0 ? 3 : 1;
}
if (direction != -1) {
this.setState({board: this.state.board.move(direction)});
}
},
componentDidMount: function () {
window.addEventListener('keydown', this.handleKeyDown);
},
componentWillUnmount: function () {
window.removeEventListener('keydown', this.handleKeyDown);
},
render: function () {
var cells = this.state.board.cells.map(function (row) {
return <div>{row.map(function () {return <Cell />; })}</div>;
});
var tiles = this.state.board.tiles.filter(function (tile) {
return tile.value != 0;
}).map(function (tile) {
return <TileView tile={tile} />;
});
return (
<div className='board' onTouchStart={this.handleTouchStart} onTouchEnd={this.handleTouchEnd} tabIndex="1">
{cells}
{tiles}
<GameEndOverlay board={this.state.board} />
</div>
);
}
});
var Cell = React.createClass({
shouldComponentUpdate: function () {
return false;
},
render: function () {
return (
<span className='cell'>{''}</span>
);
}
});
var TileView = React.createClass({
shouldComponentUpdate: function (nextProps) {
if (this.props.tile != nextProps.tile) {
return true;
}
if (!nextProps.tile.hasMoved() && !nextProps.tile.isNew()) {
return false;
}
return true;
},
render: function () {
var tile = this.props.tile;
var classArray = ['tile'];
classArray.push('tile' + this.props.tile.value);
if (!tile.mergedInto) {
classArray.push('position_' + tile.row + '_' + tile.column);
}
if (tile.mergedInto) {
classArray.push('merged');
}
if (tile.isNew()) {
classArray.push('new');
}
if (tile.hasMoved()) {
classArray.push('row_from_' + tile.fromRow() + '_to_' + tile.toRow());
classArray.push('column_from_' + tile.fromColumn() + '_to_' + tile.toColumn());
classArray.push('isMoving');
}
var classes = React.addons.classSet.apply(null, classArray);
return (
<span className={classes} key={tile.id}>{tile.value}</span>
);
}
});
var GameEndOverlay = React.createClass({
render: function () {
var board = this.props.board;
var contents = '';
if (board.hasWon()) {
contents = 'Good Job!';
} else if (board.hasLost()) {
contents = 'Game Over';
}
if (!contents) {
return null;
}
return (
<div className='overlay'>
{contents}
</div>
)
}
});
React.initializeTouchEvents(true);
React.renderComponent(<BoardView />, document.getElementById('boardDiv'));