back to the school

update.ts

   1  import { type Game, BALL_RADIUS, PADDLE_H, PADDLE_SPEED, PADDLE_W, WINDOW_H, ball_rect, clamp, overlaps, paddle_rect, serve, start_round } from "./state";
   2  import { type Input, direction_of } from "./input";
   3  import { score_if_out } from "./scoring";
   4  
   5  // Update runs at a fixed rate. The frame loop only hands it elapsed time.
   6  export const FIXED_STEP = 1 / 120; // 120: one slice is one hundred twentieth of a second
   7  export const MAX_STEPS_PER_FRAME = 8; // stepcap: no frame spends more than eight slices
   8  export const FRAME_TIME_CAP = 0.25; // cap: a frame may pour in at most a quarter second
   9  export const AI_DEAD_ZONE = 12;
  10  
  11  export function advance(game: Game, input: Input, frame_time: number): void {
  12    if (input.restart) {
  13      game.left.score = 0;
  14      game.right.score = 0;
  15      serve(game, true);
  16    }
  17    game.accumulator += Math.min(frame_time, FRAME_TIME_CAP); // min: cap before the catch-up loop
  18    let steps = 0;
  19    while (game.accumulator >= FIXED_STEP && steps < MAX_STEPS_PER_FRAME) {
  20      step(game, input, FIXED_STEP);
  21      game.accumulator -= FIXED_STEP;
  22      steps += 1;
  23    }
  24    if (steps === MAX_STEPS_PER_FRAME) { // droptime: discard the rest of a long frame
  25      game.accumulator = 0;
  26    }
  27  }
  28  
  29  export function step(game: Game, input: Input, dt: number): void {
  30    move_paddles(game, input, dt);
  31    if (game.ball.wait > 0) {
  32      game.ball.wait -= 1;
  33      if (game.ball.wait === 0) start_round(game);
  34      return;
  35    }
  36    move_ball_x(game, dt); // xpass: horizontal move before the paddle check
  37    resolve_paddle_hits(game);
  38    move_ball_y(game, dt); // ypass: vertical move before the wall check
  39    resolve_wall_hits(game);
  40    score_if_out(game);
  41  }
  42  
  43  export function move_paddles(game: Game, input: Input, dt: number): void { // keypair: left from input, right from ball
  44    const left_move = direction_of(input) * PADDLE_SPEED * dt;
  45    game.left.y = clamp(game.left.y + left_move, 0, WINDOW_H - PADDLE_H);
  46  
  47    const center = game.right.y + PADDLE_H / 2;
  48    let ai_move = 0;
  49    if (game.ball.y < center - AI_DEAD_ZONE) ai_move = -1;
  50    else if (game.ball.y > center + AI_DEAD_ZONE) ai_move = 1;
  51    game.right.y = clamp(game.right.y + ai_move * PADDLE_SPEED * 0.72 * dt, 0, WINDOW_H - PADDLE_H);
  52  }
  53  
  54  export function move_ball_x(game: Game, dt: number): void {
  55    game.ball.x += game.ball.vx * dt;
  56  }
  57  
  58  export function move_ball_y(game: Game, dt: number): void {
  59    game.ball.y += game.ball.vy * dt;
  60  }
  61  
  62  export function resolve_paddle_hits(game: Game): void { // hface: paddle contact corrects x only
  63    const b = ball_rect(game.ball);
  64    if (game.ball.vx < 0 && overlaps(b, paddle_rect(game.left))) {
  65      game.ball.x = game.left.x + PADDLE_W + BALL_RADIUS; // pushout: clear the overlap in x
  66      game.ball.vx = -game.ball.vx;
  67    } else if (game.ball.vx > 0 && overlaps(b, paddle_rect(game.right))) {
  68      game.ball.x = game.right.x - BALL_RADIUS;
  69      game.ball.vx = -game.ball.vx;
  70    }
  71  }
  72  
  73  export function resolve_wall_hits(game: Game): void { // vface: wall contact corrects y only
  74    if (game.ball.y - BALL_RADIUS < 0) {
  75      game.ball.y = BALL_RADIUS;
  76      game.ball.vy = -game.ball.vy;
  77    }
  78    if (game.ball.y + BALL_RADIUS > WINDOW_H) {
  79      game.ball.y = WINDOW_H - BALL_RADIUS;
  80      game.ball.vy = -game.ball.vy;
  81    }
  82  }