@@ -27,14 +27,17 @@ const kFPS = 60;
27
27
const framesPerSecond = 1000 / kFPS ;
28
28
const kAutoMinAdvance = 1 ;
29
29
const kAutoMaxAdvance = 10 ;
30
- const kXNoiseMax = 2 ;
30
+ const kXNoiseMax = 50 ;
31
31
32
32
const kCoopMul = 1 ;
33
33
const kCoopBoost = kCoopMul * kAutoMaxAdvance ;
34
34
const kCoopDecay = kCoopMul * 0.05 ;
35
35
const kMaxCoopBost = 3 ;
36
36
const kCoopX = 2 ;
37
37
38
+ const kPlayerCollisionPushX = 35 ;
39
+ const kReachedPx = 0.5 ;
40
+
38
41
const kGoalZOffset = 100 ;
39
42
const kRockDensity = 20 ;
40
43
const kBushDensity = 30 ;
@@ -168,6 +171,37 @@ function checkForRockCollision(p, tresholds) {
168
171
return collided ;
169
172
}
170
173
174
+ function checkForPlayerCollision ( ) {
175
+ // Detect collision between game.$.players
176
+ const kImgSizeBody = kImgSize / 2.5 ;
177
+ const players = Object . values ( game . $ . players ) ;
178
+ for ( const p of players ) {
179
+ for ( const p2 of players ) {
180
+ if ( p === p2 ) continue ;
181
+ if (
182
+ p . x + kImgSizeBody >= p2 . x - kImgSizeBody &&
183
+ p . x - kImgSizeBody <= p2 . x + kImgSizeBody &&
184
+ p . y + kImgSizeBody >= p2 . y - kImgSizeBody &&
185
+ p . y - kImgSizeBody <= p2 . y + kImgSizeBody &&
186
+ p . z + kImgSizeBody >= p2 . z - kImgSizeBody &&
187
+ p . z - kImgSizeBody <= p2 . z + kImgSizeBody
188
+ ) {
189
+ // Push the players away from each other in the X axis
190
+ const direction = p . x < p2 . x ? - 1 : 1 ;
191
+ p . playerPushX = {
192
+ t : now ,
193
+ x : p . x + direction * kPlayerCollisionPushX ,
194
+ } ;
195
+ p2 . playerPushX = {
196
+ t : now ,
197
+ x : p2 . x + - 1 * direction * kPlayerCollisionPushX ,
198
+ } ;
199
+ return ;
200
+ }
201
+ }
202
+ }
203
+ }
204
+
171
205
async function prepareScene ( ) {
172
206
prepareSceneAddBushes ( ) ;
173
207
prepareSceneAddRocks ( ) ;
@@ -196,12 +230,12 @@ async function prepareScene() {
196
230
}
197
231
198
232
function clampPlayer ( p ) {
199
- if ( game . winner !== p ) {
200
- p . x = Math . min (
201
- kTrackWidth / 2 - kImgSize ,
202
- Math . max ( - ( kTrackWidth / 2 ) + kImgSize , p . x )
203
- ) ;
204
- }
233
+ // if (game.winner !== p) {
234
+ // p.x = Math.min(
235
+ // kTrackWidth / 2 - kImgSize,
236
+ // Math.max(-(kTrackWidth / 2) + kImgSize, p.x)
237
+ // );
238
+ // }
205
239
p . y = Math . max ( 0 , p . y ) ;
206
240
p . z = Math . max ( 0 , p . z ) ;
207
241
}
@@ -237,6 +271,12 @@ function renderPlayer(p) {
237
271
applySrc ( p , `./${ p . key } /lost.gif` ) ;
238
272
} else {
239
273
applySrc ( p , `./${ p . key } /${ p . frame + 1 } .png` ) ;
274
+
275
+ if ( p . playerPushX ) {
276
+ // Squeeze the player
277
+ p . $ . style . transform = `${ p . $ . style . transform } scaleX(0.85)` ;
278
+ }
279
+
240
280
game . $ . progress [ p . key ] . firstChild . textContent = `${ Math . floor (
241
281
ratio * game . trackLength
242
282
) } m`;
@@ -296,8 +336,8 @@ function jumpEquation(t) {
296
336
297
337
function movePlayer ( p ) {
298
338
if ( game . winner === p ) {
299
- const timeSinceNow = now - p . wonT ;
300
- const { x, y } = flyEquation ( timeSinceNow / 1000 ) ;
339
+ const delta = now - p . wonT ;
340
+ const { x, y } = flyEquation ( delta / 1000 ) ;
301
341
p . x += x ;
302
342
p . y = y ;
303
343
return ;
@@ -307,10 +347,33 @@ function movePlayer(p) {
307
347
return ;
308
348
}
309
349
350
+ checkForPlayerCollision ( ) ;
351
+
352
+ if ( p . playerPushX ) {
353
+ // Exponential movement towards playerPushX based on time
354
+ const diff = p . playerPushX . x - p . x ;
355
+ const step = diff / 10 ;
356
+ const delta = now - p . playerPushX . t ;
357
+ p . x = p . x + step * Math . pow ( 2 , delta / 1000 ) ;
358
+ if ( Math . abs ( diff ) <= kReachedPx ) {
359
+ p . playerPushX = null ;
360
+ p . nextX = null ;
361
+ }
362
+ } else if ( p . nextX ) {
363
+ // Linear movement towards nextX based on time
364
+ const diff = p . nextX . x - p . x ;
365
+ const step = diff / 10 ;
366
+ const delta = now - p . nextX . t ;
367
+ p . x = p . x + ( step * delta ) / 1000 ;
368
+ if ( Math . abs ( diff ) <= kReachedPx ) {
369
+ p . nextX = null ;
370
+ }
371
+ }
372
+
310
373
// Check if player is jumping
311
374
if ( p . jumpingT ) {
312
- const timeSinceJump = now - p . jumpingT ;
313
- const { z, y } = jumpEquation ( timeSinceJump / 1000 ) ;
375
+ const delta = now - p . jumpingT ;
376
+ const { z, y } = jumpEquation ( delta / 1000 ) ;
314
377
p . y = y ;
315
378
p . z -= z ;
316
379
@@ -336,8 +399,17 @@ function movePlayer(p) {
336
399
}
337
400
338
401
// Add noise
339
- const xNoise = - kXNoiseMax + Math . random ( ) * kXNoiseMax * 2 ;
340
- p . x += xNoise ;
402
+ if ( ! p . nextX ) {
403
+ const xNoise = - kXNoiseMax + Math . random ( ) * kXNoiseMax * 2 ;
404
+ const nextX = p . x + xNoise ;
405
+ p . nextX = {
406
+ t : now ,
407
+ x : Math . min (
408
+ kTrackWidth / 2 - kImgSize ,
409
+ Math . max ( - kTrackWidth / 2 + kImgSize , nextX )
410
+ ) ,
411
+ } ;
412
+ }
341
413
342
414
// Advance Z
343
415
const advanceBy = getAdvanceBy ( p . key ) ;
0 commit comments