Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

How I Built an Undertale-Inspired Bad Time Simulator in Pure JavaScript (60 FPS on Browser)

Every indie developer has that one passion project inspired by a game they love. For me, that obsession was the notorious Sans genocide boss fight from Undertale. If you've ever played it, you know the feeling: hours o

How I Built an Undertale-Inspired Bad Time Simulator in Pure JavaScript (60 FPS on Browser)

Every indie developer has that one passion project inspired by a game they love. For me, that obsession was the notorious Sans genocide boss fight from Undertale.

If you've ever played it, you know the feeling: hours of muscle memory wiped out in seconds by a barrage of frame-perfect bones, spinning platforms, and laser-firing Gaster Blasters. The problem? If you want to practice specific attack patterns, you traditionally have to replay the entire game or mess around with local desktop save files.

I asked myself: Could I rebuild a pixel-perfect, deterministic bullet-hell engine right inside the web browser that runs at a locked 60 FPS without any downloads?

That idea led to the creation of Bad Time Simulator.

Here is the technical retrospective of how I engineered the engine, handled sub-pixel collision physics, optimized high-volume particle pooling, and brought retro bullet-hell mechanics to the modern web.

The Core Goals

Before writing a single line of code, I set three non-negotiable architectural requirements:

  1. Zero Input Latency: When dodging bullet hell, even a 16ms frame drop feels catastrophic.
  2. Sub-Pixel Deterministic Hitboxes: Replicating the distinct Red Soul (free directional flight) vs. Blue Soul (gravity-affected platformer physics) precision.
  3. Instant Accessibility: Lightweight, fully responsive across both desktop (keyboard) and mobile touchscreens.

The Tech Stack

I deliberately chose a lightweight, dependency-free architecture:

  • Rendering Layer: HTML5 Canvas 2D Context with offscreen double buffering (bypassing heavy third-party engine overhead).
  • Core Engine: Vanilla TypeScript/JavaScript using an explicit fixed-step requestAnimationFrame game loop.
  • Audio Engine: Web Audio API for zero-delay retro SFX playback and chiptune sound wave synchronization.
  • State & Attack Sequencing: Finite State Machine (FSM) defining procedural attack phases and bullet timing scripts.

Engineering Challenges & Solutions

1. Object Pooling for 200+ Active Projectiles

In the most frantic phases of the battle, the screen is flooded with moving bones, lasers, and particle debris. Allocating and destroying JavaScript objects inside the render loop causes frequent Garbage Collection (GC) pauses, resulting in micro-stutters.

To eliminate GC hitches, I implemented an Entity Object Pool:

class BonePool {
  constructor(initialSize = 300) {
    this.pool = Array.from({ length: initialSize }, () => new Bone());
    this.activeBones = [];
  }

  spawn(x, y, height, speedX, type = 'white') {
    const bone = this.pool.pop() || new Bone();
    bone.init(x, y, height, speedX, type);
    this.activeBones.push(bone);
    return bone;
  }

  recycle(bone) {
    const index = this.activeBones.indexOf(bone);
    if (index !== -1) {
      this.activeBones.splice(index, 1);
      bone.reset();
      this.pool.push(bone);
    }
  }
}

By reusing instantiated nodes, runtime heap allocation dropped to near-zero during combat loops, maintaining a solid 60 FPS on low-power mobile browsers.

2. The Dual-Soul Physics Engine

Recreating the two distinct movement states of the player soul required different physics models:

  • Red Soul (Omnidirectional Mode): Fixed velocity vector with instantaneous acceleration and deceleration.
  • Blue Soul (Gravity Mode): Standard jump-curve trajectory with gravity constants ($g$), terminal velocity caps, and AABB platform collision snapping.
updateSoulPhysics(deltaTime) {
  if (this.mode === 'RED') {
    this.x += this.inputVector.x * this.speed * deltaTime;
    this.y += this.inputVector.y * this.speed * deltaTime;
  } else if (this.mode === 'BLUE') {
    // Apply gravity
    this.velocityY = Math.min(this.velocityY + this.gravity * deltaTime, this.terminalVelocity);
    this.y += this.velocityY * deltaTime;
    this.x += this.inputVector.x * this.horizontalSpeed * deltaTime;

    // Platform snapping check
    this.resolvePlatformCollisions();
  }
  this.clampToBounds(this.battleBox);
}

3. Audio & Beam Synchronization (The Gaster Blaster)

One of the most satisfying visual cues is the Gaster Blaster: it spawns, rotates toward the player, charges for a fixed duration, and beams down a wide column of damage.

To sync the visual beam expansion with the Web Audio sound explosion:

  • Audio samples are pre-decoded into memory buffers via AudioContext.decodeAudioData.
  • Sound trigger timestamps are locked to the FSM attack timeline rather than asynchronous DOM callbacks, guaranteeing audio-visual alignment down to the millisecond.

Live Demo & Try It Out

You can play, test your dodging skills, and practice attack patterns directly in your browser without any installation:

Try the Bad Time Simulator Here

Whether you want to warm up your reflexes or analyze how classic 2D retro bosses were constructed, give it a spin!

Lessons Learned & What's Next

Building this project reinforced a few key web game development truths:

  1. Canvas 2D is still remarkably fast: You don't always need complex WebGL shaders for retro 2D games; clean object lifecycle management and efficient math often provide better performance dividends.
  2. Deterministic timing is everything: Avoid tying game physics directly to fluctuating refresh rates (like 120Hz vs 60Hz displays). Always use a fixed-delta timestep loop.

Future roadmap:

  • [ ] Attack Pattern Editor: allowing players to design custom bullet-hell choreography via JSON.
  • [ ] Global Leaderboards for endless survival mode.
  • [ ] Gamepad / Controller API integration.

Have you ever rebuilt a retro game mechanic on the modern web? Let me know your thoughts or feedback in the comments below!

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.