Introduction

In the last post, we looked at the splitting laser. But it isn’t the only beam in the game. The ship can pick up support pods that fly alongside it, and one kind of pod carries a laser. That laser doesn’t care where the ship is pointing. Every frame, the pod turns toward the nearest enemy, in any direction, and the beam follows.

Described like that, it sounds a lot like the splitting laser with the splitting switched off. A beam that lasts while the trigger is held, is calculated again every frame, and aims at the nearest enemy. So why not just reuse the splitting laser?

Almost the Same Beam

At that point, there were two beams I could build on: the straight laser, which fires straight up, and the splitting laser.

What about the straight laser? Its collision box is an AABB sized for a vertical beam. A beam at an arbitrary angle doesn’t fit into that box. So the pod laser would have to replace the collision shape anyway, and there’d be nothing left worth inheriting.

And the splitting laser? It can aim in any direction, and with a split depth of zero, it wouldn’t split. But it brings a lot of machinery for a weapon that fans one shot out into up to 63 parts: the beam tree, the timing that lets each generation grow in, the damage that’s passed on when a split beam finds no target, and the cone in front of the ship. A pod laser with a single beam and no upgrade levels would carry all of that around without ever using it.

There’s another reason, too. Weapon variants in the game are data: each weapon has a small type object that describes it, while the code that runs it stays the same. Making the pod laser’s behavior a subclass of another weapon’s behavior would break that rule.

So instead, the pod laser became a sibling of the other two. A new class next to them, not below either. But before writing it, three pieces that really were the same moved out of the splitting laser into helpers of their own:

  • drawing a single beam segment from any origin in any direction,
  • starting, updating and stopping the sparks where a beam burns into an enemy,
  • creating a short-lived object that carries a beam’s damage into a hit.

The splitting laser now calls these helpers instead of its own copies. That leaves the pod laser itself pretty small. One ray per frame, one damage carrier, and one set of sparks. Though one difference is deliberate: the splitting laser only looks for its first target in a cone in front of the ship, because the player aims it by moving the ship. Nobody aims the pod laser, so it goes after the nearest enemy.

Of course, this comes at a price. Three beam classes with similar, but not identical, update and draw code. The shared helpers keep the duplicated code small, but when reading one of them, we have to check which of the three we’re looking at.

A Flash in the Corner

While wiring up the new beam, an old bug showed up again, and the straight laser had it, too: for exactly one frame after a beam was spawned, a glow flashed at the world coordinate (0, 0), the top-left corner of the level.

Where did that come from? It’s the order of things within a frame. First, every object in the scene is updated. Then, the objects spawned during that frame are added, and each one is told that it has been spawned. Finally, everything is drawn, including the new objects. But a beam calculates where its ray starts during the update. So on its first frame, a new beam is drawn without ever having been updated, and its origin is still the default: (0, 0).

And the splitting laser? It was only spared by luck: it draws the parts of its beam tree, and that tree is empty until the first update.

The first fix was a flag: “Has this beam been updated yet?” Until it had, the beam didn’t draw anything. That worked, but every new beam had to remember to copy the flag. And it didn’t make the first frame right, it just hid it.

What about moving that check into the engine, so that nothing is drawn before its first update? That would change the first frame of every object in the game, not just the beams. And it would still skip a frame instead of drawing the right one.

Instead, the fix that stuck uses the spawn callback. It runs after the update and before the draw, and by then the beam knows the object it’s attached to, the ship or the pod, so it can work out where its muzzle is. To do that in one place, the beams got a shared abstract base class. It owns everything all three had kept their own copies of: the color, the position relative to the ship or pod, how to calculate the muzzle position, and the spawn callback. The callback is sealed and calls a small method to initialize the render state, which each beam can override to set up what it draws. The straight laser and the pod laser use it to set their origin and bounds. The splitting laser doesn’t need to, since its tree is still empty at that point. And the flag? It’s gone.

This has a cost, too. Since the spawn callback is sealed, a beam that needs to do something on spawn has to do it in that init method. And unit tests that create a beam directly now have to call the spawn callback before they check what it draws. But that’s what happens in the game anyway, so the tests are closer to the real thing.

Putting It Together

So what does the pod laser do, and what changed around it?

  1. Every frame, the pod turns toward the nearest enemy, in any direction, and the laser casts one ray from the pod’s muzzle.
  2. The pod laser is a class of its own next to the straight laser and the splitting laser, not a subclass of either.
  3. The code that really was the same lives in shared helpers: drawing a segment, the sparks at the hit, and the object that carries the damage.
  4. A shared base class, sets up what a beam draws when it is spawned, so its first frame is drawn at the muzzle instead of at (0, 0).