(width, height, simTime, simSpeed, lag)
| 1509 | |
| 1510 | |
| 1511 | function tick(width, height, simTime, simSpeed, lag) { |
| 1512 | PERF_START('frame'); |
| 1513 | PERF_START('tick'); |
| 1514 | |
| 1515 | state.game.time += simTime; |
| 1516 | |
| 1517 | if (slowmoRemaining > 0) { |
| 1518 | slowmoRemaining -= simTime; |
| 1519 | if (slowmoRemaining < 0) { |
| 1520 | slowmoRemaining = 0; |
| 1521 | } |
| 1522 | targetSpeed = pointerIsDown ? 0.075 : 0.3; |
| 1523 | } else { |
| 1524 | const menuPointerDown = isMenuVisible() && pointerIsDown; |
| 1525 | targetSpeed = menuPointerDown ? 0.025 : 1; |
| 1526 | } |
| 1527 | |
| 1528 | renderSlowmoStatus(slowmoRemaining / slowmoDuration); |
| 1529 | |
| 1530 | gameSpeed += (targetSpeed - gameSpeed) / 22 * lag; |
| 1531 | gameSpeed = clamp(gameSpeed, 0, 1); |
| 1532 | |
| 1533 | const centerX = width / 2; |
| 1534 | const centerY = height / 2; |
| 1535 | |
| 1536 | const simAirDrag = 1 - (airDrag * simSpeed); |
| 1537 | const simAirDragSpark = 1 - (airDragSpark * simSpeed); |
| 1538 | |
| 1539 | // Pointer Tracking |
| 1540 | // ------------------- |
| 1541 | |
| 1542 | // Compute speed and x/y deltas. |
| 1543 | // There is also a "scaled" variant taking game speed into account. This serves two purposes: |
| 1544 | // - Lag won't create large spikes in speed/deltas |
| 1545 | // - In slow mo, speed is increased proportionately to match "reality". Without this boost, |
| 1546 | // it feels like your actions are dampened in slow mo. |
| 1547 | const forceMultiplier = 1 / (simSpeed * 0.75 + 0.25); |
| 1548 | pointerDelta.x = 0; |
| 1549 | pointerDelta.y = 0; |
| 1550 | pointerDeltaScaled.x = 0; |
| 1551 | pointerDeltaScaled.y = 0; |
| 1552 | const lastPointer = touchPoints[touchPoints.length - 1]; |
| 1553 | |
| 1554 | if (pointerIsDown && lastPointer && !lastPointer.touchBreak) { |
| 1555 | pointerDelta.x = (pointerScene.x - lastPointer.x); |
| 1556 | pointerDelta.y = (pointerScene.y - lastPointer.y); |
| 1557 | pointerDeltaScaled.x = pointerDelta.x * forceMultiplier; |
| 1558 | pointerDeltaScaled.y = pointerDelta.y * forceMultiplier; |
| 1559 | } |
| 1560 | const pointerSpeed = Math.hypot(pointerDelta.x, pointerDelta.y); |
| 1561 | const pointerSpeedScaled = pointerSpeed * forceMultiplier; |
| 1562 | |
| 1563 | // Track points for later calculations, including drawing trail. |
| 1564 | touchPoints.forEach(p => p.life -= simTime); |
| 1565 | |
| 1566 | if (pointerIsDown) { |
| 1567 | touchPoints.push({ |
| 1568 | x: pointerScene.x, |
no test coverage detected