Skip to content

Commit 2b40203

Browse files
committed
Move killed blocks out of state object
1 parent c15fda5 commit 2b40203

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

lib/game.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
module.exports = () => {
1+
2+
3+
module.exports = (getBallLevel) => {
4+
getBallLevel = getBallLevel || (level => Math.floor(Math.random() * level))
5+
26
const thresholds = [0, 0, 5, 30, 90, 150, 230, 280, 350, 450]
7+
var blocksKilled = 0
38
var updateCallback = () => {}
49
var dotsId = 0
510
var state = {
611
dots: [],
712
score: 0,
813
highScore: 0,
914
floatingDots: [newDot(4, 1), newDot(5, 0)],
10-
level: 2,
11-
blocksKilled: 0
15+
level: 2
1216
}
1317

1418
const colors = [
@@ -25,7 +29,7 @@ module.exports = () => {
2529
]
2630

2731
function setState (newState) {
28-
Object.assign(state, newState)
32+
state = Object.assign({}, state, newState)
2933
updateCallback(newState)
3034
}
3135

@@ -39,7 +43,7 @@ module.exports = () => {
3943
y,
4044
key: dotsId++,
4145
type:
42-
level !== undefined ? level : Math.floor(Math.random() * state.level)
46+
level !== undefined ? level : getBallLevel(state.level)
4347
}
4448
}
4549

@@ -133,7 +137,7 @@ module.exports = () => {
133137
keep[d.x][d.y] = false
134138
updateScore(state.score + (newColoredDot.type + 1) * 5)
135139
})
136-
state.blocksKilled++
140+
blocksKilled++
137141
}
138142
}
139143
}
@@ -154,7 +158,6 @@ module.exports = () => {
154158
return
155159
}
156160
setState({
157-
...state,
158161
floatingDots: state.floatingDots.map(d => {
159162
return {
160163
...d,
@@ -195,7 +198,7 @@ module.exports = () => {
195198
function cleanupBoard (oldRun, newRun) {
196199
oldRun = newRun
197200
newRun = cleanupDots(oldRun)
198-
const level = thresholds.findIndex(l => l > state.blocksKilled)
201+
const level = thresholds.findIndex(l => l > blocksKilled)
199202
setState({
200203
...state,
201204
level: level < 0 ? colors.length - 1 : level,

pages/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default class Page extends React.Component {
4848
}}
4949
>
5050
<GameBoard
51-
width={500}
51+
width={600}
5252
colors={this.game.colors()}
5353
score={this.game.score()}
5454
highScore={this.game.highScore()}

0 commit comments

Comments
 (0)