The ships, the missions and the story are what players see of Cosmic Starship. Underneath is a smaller thing we kept re-inventing: a fixed-step clock, a way to read keyboards and gamepads, a way to make sounds, a way to put a 3D scene on a canvas. We wanted to move that into a separate engine that another game could reuse. We also had a rule for ourselves: nothing is allowed to look, sound or play differently afterwards. Here is how we did it in three stages, and what each stage found.
The first two stages: the part with no pictures
We began with the simulation core, the code that moves ships, fires shots and decides who wins. It has no pictures and no sound, which makes it the easiest to test. The game’s Battle class now extends a general SimCore, and the capital powers (the ultimates of each flagship) moved from one giant chain of if statements to a registry, a table you add entries to. Adding a power for a new ship no longer means editing the core.
The proof was the battle hashes described in the testing note: all 93 identical before and after. We also ran the same rules file on the engine alone, with no game around it, and it worked, which is the real test of a split.
Stage three: the things you can see and hear
The third stage moved the browser side: the fixed-step clock, input, the sound core and the 3D stage. This was the frightening one, because the hash cannot see pictures or sound. So each part got its own net, run on the old version and the new version side by side:
- The clock. The game steps its world eight times per drawn frame, and the simulator sixteen. We fed the old inline loop and the new engine clock 200,000 frames of random frame times, including stalls and slow motion, and compared how many steps each owed and what remainder it carried. They matched on every frame.
- Input. 100,000 frames of random keys, stick positions, screen rotations and gamepad buttons, comparing the old reading code with the new. Then a real browser with a pretend gamepad: stick right moved the ship, X cast the first ability, A boosted.
- Sound. A fake audio context records every call the game makes to the browser’s sound system. We played each of the 59 named sounds twice and compared the logs call by call. Sixty of sixty logs identical.
- The picture. One seeded battle drawn in the chase, tactical and cinematic cameras. We recorded draw calls and triangles per frame (about 240 and 397,000), the objects in the scene, the camera matrices, where three points on the screen land in the world, the actual pixels and the GPU memory. All identical.
What the split found
A refactor that finds bugs is a sign you had them. During the split we found that without WebGL the game did not start at all: a settings check ran before the thing it needed had been created. Nobody had noticed, because nobody tests with 3D switched off. The game now starts in a plain 2D mode instead, and we test that case on every release.
We also found that order matters in places we did not expect. In three.js the order objects are added to the scene decides the draw order for things at equal depth. Our renderer base creates its scene objects in the same order as before, for that reason, and a comment says so.
What we left alone
Not everything moved, on purpose. The chase camera follows this game’s own aim and phone layout, so it stays in the game. Which key does what, what each sound is, the HUD and the hangar are the game’s, too. An engine that contains your whole game is not an engine. The rule we ended on is a short one: the engine may not import anything from the game. If a file breaks that rule, it belongs in the game.