Skip to content

Commit c8e1886

Browse files
committed
Squeeze players
1 parent 5b84f02 commit c8e1886

File tree

1 file changed

+85
-13
lines changed

1 file changed

+85
-13
lines changed

script.js

+85-13
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ const kFPS = 60;
2727
const framesPerSecond = 1000 / kFPS;
2828
const kAutoMinAdvance = 1;
2929
const kAutoMaxAdvance = 10;
30-
const kXNoiseMax = 2;
30+
const kXNoiseMax = 50;
3131

3232
const kCoopMul = 1;
3333
const kCoopBoost = kCoopMul * kAutoMaxAdvance;
3434
const kCoopDecay = kCoopMul * 0.05;
3535
const kMaxCoopBost = 3;
3636
const kCoopX = 2;
3737

38+
const kPlayerCollisionPushX = 35;
39+
const kReachedPx = 0.5;
40+
3841
const kGoalZOffset = 100;
3942
const kRockDensity = 20;
4043
const kBushDensity = 30;
@@ -168,6 +171,37 @@ function checkForRockCollision(p, tresholds) {
168171
return collided;
169172
}
170173

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+
171205
async function prepareScene() {
172206
prepareSceneAddBushes();
173207
prepareSceneAddRocks();
@@ -196,12 +230,12 @@ async function prepareScene() {
196230
}
197231

198232
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+
// }
205239
p.y = Math.max(0, p.y);
206240
p.z = Math.max(0, p.z);
207241
}
@@ -237,6 +271,12 @@ function renderPlayer(p) {
237271
applySrc(p, `./${p.key}/lost.gif`);
238272
} else {
239273
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+
240280
game.$.progress[p.key].firstChild.textContent = `${Math.floor(
241281
ratio * game.trackLength
242282
)}m`;
@@ -296,8 +336,8 @@ function jumpEquation(t) {
296336

297337
function movePlayer(p) {
298338
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);
301341
p.x += x;
302342
p.y = y;
303343
return;
@@ -307,10 +347,33 @@ function movePlayer(p) {
307347
return;
308348
}
309349

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+
310373
// Check if player is jumping
311374
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);
314377
p.y = y;
315378
p.z -= z;
316379

@@ -336,8 +399,17 @@ function movePlayer(p) {
336399
}
337400

338401
// 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+
}
341413

342414
// Advance Z
343415
const advanceBy = getAdvanceBy(p.key);

0 commit comments

Comments
 (0)