Skip to content

Commit 9add9ed

Browse files
committed
Bit more refactoring
1 parent d41d07f commit 9add9ed

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

lib/game.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const game = (getBallLevel, sleepFunction, gameBoard) => {
143143
const newDots = pending.mergeWithPending(state.dots)
144144
setState({ dots: newDots })
145145
pending = null
146-
sleepFunction(() => cleanupBoard([], newDots))
146+
sleepFunction(() => cleanupBoard(newDots))
147147
}
148148

149149
function board (dots) {
@@ -201,8 +201,7 @@ const game = (getBallLevel, sleepFunction, gameBoard) => {
201201
map[d.x][d.y <= 0 ? 0 : d.y - 1]
202202
]
203203
.filter(n => n && n.type === d.type)
204-
.reduce((l, d) => l.concat(checkTile(d)), [])
205-
.concat([d])
204+
.reduce((l, d) => l.concat(checkTile(d)), [d])
206205
}
207206

208207
let freshDots = []
@@ -243,20 +242,19 @@ const game = (getBallLevel, sleepFunction, gameBoard) => {
243242
window.localStorage['score'] = highScore
244243
}
245244

246-
function cleanupBoard (oldRun, newRun) {
247-
oldRun = newRun
248-
newRun = cleanupDots(oldRun)
249-
const level = levelUpAt.findIndex(l => l > blocksKilled)
245+
function outsidePlayingArea (ball) {
246+
return ball.y < 0
247+
}
248+
249+
function cleanupBoard (oldRun) {
250+
const newRun = cleanupDots(oldRun)
250251
setState({
251-
level: level < 0 ? colors.length - 1 : level,
252+
level: levelUpAt.findIndex(l => l > blocksKilled),
252253
dots: newRun
253254
})
254255
if (oldRun.length === newRun.length) {
255-
if (newRun.some(d => d.y < 0)) {
256-
return setState({
257-
gameover: true,
258-
dots: []
259-
})
256+
if (newRun.some(outsidePlayingArea)) {
257+
return setState({ gameover: true })
260258
}
261259
pending = pendingBalls(
262260
getBallLevel(state.level),
@@ -266,11 +264,11 @@ const game = (getBallLevel, sleepFunction, gameBoard) => {
266264
}
267265

268266
sleepFunction(() => {
269-
newRun = settleDots(newRun)
270-
setState({
271-
dots: newRun
267+
const ballsInPlace = settleDots(newRun)
268+
setState({ dots: ballsInPlace })
269+
sleepFunction(() => {
270+
cleanupBoard(ballsInPlace)
272271
})
273-
sleepFunction(() => cleanupBoard(oldRun, newRun))
274272
})
275273
}
276274

0 commit comments

Comments
 (0)