@@ -59,6 +59,22 @@ const isStuck = ({ x, y }) =>
59
59
const computePlayer = ( { x, y, name, coords, score, cardinal, direction } ) =>
60
60
( { x, y, name, coords, score, cardinal, direction } )
61
61
62
+ const calculateCoords = player => {
63
+ const { x, y } = player
64
+ const { cardinal, direction } = player . coords
65
+ . find ( coord => coord . x === x && coord . y === y )
66
+
67
+ player . coords = [
68
+ { x, y : y - 1 , cardinal : 0 , direction : ( 4 - cardinal ) % 4 } ,
69
+ { x : x + 1 , y, cardinal : 1 , direction : ( 5 - cardinal ) % 4 } ,
70
+ { x, y : y + 1 , cardinal : 2 , direction : ( 6 - cardinal ) % 4 } ,
71
+ { x : x - 1 , y, cardinal : 3 , direction : ( 7 - cardinal ) % 4 } ,
72
+ ]
73
+
74
+ player . cardinal = cardinal
75
+ player . direction = direction
76
+ }
77
+
62
78
const update = async forced => {
63
79
await update . lock
64
80
clearTimeout ( update . timeout )
@@ -129,22 +145,7 @@ const update = async forced => {
129
145
130
146
playersLeft
131
147
. filter ( isAlive )
132
- . forEach ( player => {
133
- console . log ( player . name , 'is not stuck at' , player . x , player . y )
134
- const { x, y } = player
135
- const { cardinal, direction } = player . coords
136
- . find ( coord => coord . x === x && coord . y === y )
137
-
138
- player . coords = [
139
- { x, y : y - 1 , cardinal : 0 , direction : ( 4 - cardinal ) % 4 } ,
140
- { x : x + 1 , y, cardinal : 1 , direction : ( 5 - cardinal ) % 4 } ,
141
- { x, y : y + 1 , cardinal : 2 , direction : ( 6 - cardinal ) % 4 } ,
142
- { x : x - 1 , y, cardinal : 3 , direction : ( 7 - cardinal ) % 4 } ,
143
- ]
144
-
145
- player . cardinal = cardinal
146
- player . direction = direction
147
- } )
148
+ . forEach ( calculateCoords )
148
149
149
150
graphic . update ( players )
150
151
const diff = performance . now ( ) - startTime
@@ -169,7 +170,7 @@ state.users.forEach(addPlayer)
169
170
Promise . all ( players . map ( p => p . load ) ) . then ( ( ) => {
170
171
Math . random = rseed . float
171
172
172
- shuffle ( players . sort ( ( a , b ) => a . name - b . name ) )
173
+ players . sort ( ( a , b ) => a . name - b . name )
173
174
174
175
state . map = Array ( SIZE * SIZE ) . fill ( emptyTile )
175
176
@@ -189,15 +190,19 @@ Promise.all(players.map(p => p.load)).then(() => {
189
190
const max1 = max ( 1 )
190
191
const max2PI = max ( Math . PI * 2 )
191
192
const angle = ( Math . PI * 2 ) / players . length
192
- const rate = ( 100 / players . length / 100 )
193
+ const rate = ( SIZE / players . length / SIZE )
193
194
const shift = angle * rseed . float ( )
194
195
const h = SIZE / 2
195
196
const m = h * 0.8
196
197
197
198
players . forEach ( ( player , i ) => {
198
- player . color = hslToRgb ( max1 ( i * rate + 0.25 ) , 1 , 0.4 )
199
199
player . cardinal = 0
200
200
player . direction = 0
201
+ player . color = hslToRgb ( max1 ( i * rate + 0.25 ) , 1 , 0.5 )
202
+ } )
203
+
204
+ // Shuffle players before calculating coords
205
+ shuffle ( players ) . forEach ( ( player , i ) => {
201
206
const x = player . x = Math . round ( max2PI ( Math . cos ( angle * i + shift ) ) * m + h )
202
207
const y = player . y = Math . round ( max2PI ( Math . sin ( angle * i + shift ) ) * m + h )
203
208
player . coords = [
0 commit comments