flix

0.81.0

Bunkers

The four bunkers the player shelters behind.

A bunker is a grid of small blocks. Anything that hits one -- a bullet on its way up, a bomb on its way down, or an invader that has marched far enough -- destroys the blocks near the impact and is itself stopped. Blocks never come back, so the bunkers erode over a game exactly as they do in the original.

All of this is pure. Damage is a function from a bunker and an impact point to a new bunker.

Definitions

def absorb(bunkers: Vector[Bunker], pos: Vec2, size: Vec2, crater: Float32): (Vector[Bunker], Bool)§ Source

Offers one shot to the bunkers.

Returns the bunkers after any damage, and whether the shot was absorbed. An absorbed shot is removed by the caller, which is what makes a bunker actually protect the player rather than merely look like it does.

findMap stops at the first bunker the shot meets, which is what keeps one shot from damaging two shelters standing in a line.

def absorbAll(bunkers: Vector[Bunker], shots: List[Vec2], size: Vec2, crater: Float32): (Vector[Bunker], List[Vec2])§ Source

Offers a whole list of shots to the bunkers.

Returns the damaged bunkers and the shots that got through, in the order they arrived. One shot damages at most one bunker: absorb stops at the first it makes contact with, so a bullet cannot bore through two shelters standing in a line.

def absorbBombs(bunkers: Vector[Bunker], bombs: List[Bomb]): (Vector[Bunker], List[Bomb])§ Source

Offers a whole list of bombs to the bunkers, which carry their own crater size.

Separate from absorbAll rather than generalising it: the crater is a property of the individual bomb here, not of the batch, and threading a per-item function through the batch version would make the common case -- bullets, all identical -- read worse to buy nothing.

def blocks(bunkers: Vector[Bunker], x: Float32): Bool§ Source

Whether any standing block sits directly above the single point x.

A point is not what gets fired, so this is rarely the question a caller wants -- see obstructs, which asks it of a whole bullet.

def cellCentre(bunker: Bunker, index: Int32): Vec2§ Source

The centre of one block, in screen coordinates.

def cellSize(): Vec2§ Source

The size of a single bunker cell block as a 2D vector.

def damage(bunker: Bunker, at: Vec2, radius: Float32): Bunker§ Source

Removes every block within radius of at.

def erode(bunkers: Vector[Bunker], invaders: Vector[Invader]): Vector[Bunker]§ Source

Invaders grind away any bunker they march into.

Guarded by the formation's lowest point, so for most of a game this costs one comparison rather than a scan of every block against every invader.

def initial(): Vector[Bunker]§ Source

Four undamaged bunkers, evenly spaced.

def obstructs(bunkers: Vector[Bunker], x: Float32, width: Float32): Bool§ Source

Whether a shot of width width, fired from x, would meet a bunker on its way up.

The rules do not care -- carving a slit through your own bunker is legal, and in the arcade original it is a tactic -- but a player who is not doing it deliberately is destroying their own cover and wasting the shot. This is the question to ask before pulling the trigger.

This asks the question of the same extent contact uses, and so gives the same answer by construction. Two narrower versions were wrong in opposite directions, and both cost real play:

  • Sampling only the centre (blocks) misses the block beside the shot, because a bullet is four pixels wide and so is a block. The bot cleared itself a slit, declared the way open, and demolished its own cover from below -- one shot in seven struck a bunker the guard had called clear.
  • Sampling the two edges as well over-corrects. Collide.overlaps is strict, so a bullet centred on a block column does not touch the next column -- but the right edge lands exactly on that column's boundary and floors into it. A perfectly drilled channel then reads as blocked, which makes camping impossible to aim for.

Sampling points cannot express a strict overlap at all, so this does not sample.

def remaining(bunker: Bunker): Int32§ Source

The number of intact, surviving blocks in a single bunker.

def runs(bunker: Bunker): List[(Int32, Int32, Int32)]§ Source

The surviving blocks, collapsed into horizontal runs as (row, startColumn, length).

The same trick the sprites use, for the same reason: an intact bunker is 96 blocks but only about a dozen runs, and drawing it a block at a time was costing real frame rate once there were four of them on screen.

def totalRemaining(bunkers: Vector[Bunker]): Int32§ Source

The total number of intact, surviving blocks across all bunkers in the vector.