Skip to content

Commit 57bb52a

Browse files
committed
Render changes to pending
1 parent 0d940ad commit 57bb52a

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

lib/game.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ const pendingBalls = (leftLevel, rightLevel) => {
2929
var rotation = 0
3030
var position = 5
3131

32-
const leftBall = ball(0, 0, leftLevel)
33-
const rightBall = ball(0, 0, rightLevel)
32+
const left = ball(0, 0, leftLevel)
33+
const right = ball(0, 0, rightLevel)
3434

35-
const isVertical = () => position === 1 || position === 3
36-
const maxLeft = () => (isVertical() ? 0 : 1)
35+
const isVertical = () => rotation === 1 || rotation === 3
3736

3837
function moveLeft () {
39-
position = Math.max(maxLeft(), position - 1)
38+
position = Math.max(0, position - 1)
4039
}
4140

4241
function moveRight () {
@@ -45,30 +44,31 @@ const pendingBalls = (leftLevel, rightLevel) => {
4544

4645
function rotate () {
4746
rotation = (rotation + 1) % 4
48-
position = Math.max(maxLeft(), position)
4947
}
5048

5149
function toArray () {
50+
const x = Math.max(isVertical() ? 0 : 1, position)
51+
const y = -1
5252
switch (rotation) {
5353
case 0:
5454
return [
55-
{ ...leftBall, x: position - 1, y: -1 },
56-
{ ...rightBall, x: position, y: -1 }
55+
{ ...left, x: x - 1, y },
56+
{ ...right, x, y }
5757
]
5858
case 2:
5959
return [
60-
{ ...leftBall, x: position, y: -1 },
61-
{ ...rightBall, x: position - 1, y: -1 }
60+
{ ...left, x, y },
61+
{ ...right, x: x - 1, y }
6262
]
6363
case 1:
6464
return [
65-
{ ...leftBall, x: position, y: -2 },
66-
{ ...rightBall, x: position, y: -1 }
65+
{ ...left, x, y: y - 1 },
66+
{ ...right, x, y }
6767
]
6868
case 3:
6969
return [
70-
{ ...leftBall, x: position, y: -1 },
71-
{ ...rightBall, x: position, y: -2 }
70+
{ ...left, x, y },
71+
{ ...right, x, y: y - 1 }
7272
]
7373
}
7474
}
@@ -105,25 +105,32 @@ module.exports = (getBallLevel, sleepFunction) => {
105105
level: 2
106106
}
107107

108-
function setState (newState) {
109-
state = Object.assign({}, state, newState)
108+
function queueUpdate () {
110109
if (queuedUpdate) return
111110
queuedUpdate = setTimeout(() => {
112111
updateCallback(state)
113112
queuedUpdate = null
114113
})
115114
}
116115

116+
function setState (newState) {
117+
state = Object.assign({}, state, newState)
118+
queueUpdate()
119+
}
120+
117121
function moveDotsLeft () {
118122
if (pending) pending.moveLeft()
123+
queueUpdate()
119124
}
120125

121126
function moveDotsRight () {
122127
if (pending) pending.moveRight()
128+
queueUpdate()
123129
}
124130

125131
function rotateDots () {
126132
if (pending) pending.rotate()
133+
queueUpdate()
127134
}
128135

129136
function pushDots () {

tests/game.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ test('it executes commands correctly', t => {
141141
`
142142
)
143143

144+
executeCommands(
145+
'lllllllll',
146+
`
147+
.......
148+
10.....
149+
.......
150+
.......
151+
.......
152+
.......
153+
.......
154+
.......
155+
.......
156+
`
157+
)
158+
144159
executeCommands(
145160
'llllllll',
146161
`

0 commit comments

Comments
 (0)