-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameGrid.js
74 lines (66 loc) · 1.56 KB
/
GameGrid.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
/**
* Grid for gameplay
*/
'use strict';
var React = require('react-native');
var Jewel = require('./Jewel');
var {
StyleSheet,
Text,
View,
} = React;
function gridStyle(viewport) {
if(viewport) {
var gridSize = viewport.width < viewport.height ? viewport.width : viewport.height;
return {
position: 'absolute',
width: gridSize,
height: gridSize,
top: 0,
left: 0,
};
} else {
return null;
}
}
var GameGrid = React.createClass({
getInitialState () {
return {
gridModel: null,
loaded: false,
viewport: null,
};
},
componentWillReceiveProps: function(nextProps) {
this.setState({
gridModel: nextProps.gridModel,
viewport: nextProps.viewport,
});
},
_renderJewels: function() {
if(!this.state.gridModel || !this.state.viewport) {
return null;
}
var jewelComponents =[];
var jewels = this.state.gridModel.jewels;
var gridSize = this.props.viewport.width < this.props.viewport.height ? this.props.viewport.width : this.props.viewport.height;
var jewelWidth = gridSize / this.state.gridModel.grid.length;
for (var j = 0; j < jewels.length; j++) {
jewelComponents.push(
<Jewel row={jewels[j].row} column={jewels[j].column} type={jewels[j].type} width={jewelWidth}></Jewel>
);
}
return (
{jewelComponents}
);
},
render: function () {
var jewels = this._renderJewels();
return (
<View style={gridStyle(this.state.viewport)}>
{jewels}
</View>
);
}
});
module.exports = GameGrid;