////// Pixel art, as data.////// A sprite is written out as rows of text -- `#` for a lit pixel, anything else for empty --/// so the artwork is legible in the source and can be edited without a drawing tool. There/// are no image files anywhere in this project.////// Each row is collapsed into horizontal runs of lit pixels, and drawing emits one rectangle/// per run rather than one per pixel -- roughly a quarter of the draw calls. The runs are/// computed once, by `Sprite.of`, and stored: parsing the text on every frame for every/// invader was enough to halve the frame rate.///pubenumSprite({ width = Int32, height = Int32, rows = Vector[String], runs = List[(Int32, Int32, Int32)]})mod Sprite {/// Marks a lit pixel.deflit(): Char = '#'////// Builds a sprite from rows of text. The width is the longest row.///pubdefof(rows: Vector[String]): Sprite = {letfindMaxW = (widest, row) -> Int32.max(widest, String.length(row));letmaxW = Vector.foldLeft(findMaxW, 0, rows);leth = Vector.length(rows);letparsedRuns = parse(rows); Sprite.Sprite({width = maxW, height = h, rows = rows, runs = parsedRuns}) }/// The sprite's width in cells -- the longest row.pubdefwidth(sprite: Sprite): Int32 = matchsprite {case Sprite.Sprite(r) => r#width }/// The sprite's height in cells.pubdefheight(sprite: Sprite): Int32 = matchsprite {case Sprite.Sprite(r) => r#height }/// The rows of text the sprite was built from.pubdefrows(sprite: Sprite): Vector[String] = matchsprite {case Sprite.Sprite(r) => r#rows }/// Closes off a run that was in progress, if any.defclose(y: Int32, x: Int32, start: Option[Int32], acc: List[(Int32, Int32, Int32)]):List[(Int32, Int32, Int32)] =matchstart {case None => acccase Some(startX) =>letlen = x-startX; (y, startX, len) :: acc }////// The horizontal runs of lit pixels in one row, as `(row, startColumn, length)`.///defrowRuns(y: Int32, cells: List[Char]): List[(Int32, Int32, Int32)] = {letinitScan = {column = 0, start = (None: Option[Int32]), runs = Nil};letstepScan = (scan, cell) ->if (cell==lit())letnextCol = scan#column +1;letstartCol = Option.getWithDefault(scan#column, scan#start); {column = nextCol, start = Some(startCol) | scan}else {letnextCol = scan#column +1;letclosedRuns = close(y, scan#column, scan#start, scan#runs); {column = nextCol, start = None, runs = closedRuns | scan} };letscanned = List.foldLeft(stepScan, initScan, cells);letfinalRuns = close(y, scanned#column, scanned#start, scanned#runs);List.reverse(finalRuns) }/// Collapses every row into runs. Called once per sprite, by `of`.defparse(rows: Vector[String]): List[(Int32, Int32, Int32)] =Vector.toList(rows) |> List.mapWithIndex((y, row) -> rowRuns(y, String.toList(row))) |> List.flatten////// Every lit run in the sprite, as `(row, startColumn, length)`, top row first.///pubdefruns(sprite: Sprite): List[(Int32, Int32, Int32)] = matchsprite {case Sprite.Sprite(r) => r#runs }////// The number of lit pixels. Handy for tests; not used when drawing.///pubdeflitPixels(sprite: Sprite): Int32 =List.foldLeft( (total, segment) -> {let (_, _, length) = segment;total+length },0,runs(sprite) )////// Draws a sprite centred on `centre`, stretched independently in each axis.////// Separate axes so a shape can be made wide without also becoming tall -- which is what/// a flying saucer wants and a square pixel does not give you.////// The two scales are named: they are both `Float32`, and swapping them silently/// transposes the sprite.pubdefdrawScaled(sprite: Sprite,centre: Vec2,scaleX: {scaleX = Float32},scaleY: {scaleY = Float32} ): Unit \ Canvas = {letsx = scaleX#scaleX;letsy = scaleY#scaleY;letleft = centre#x -Int32.toFloat32(width(sprite)) *sx/2.0f32;lettop = centre#y -Int32.toFloat32(height(sprite)) *sy/2.0f32;List.forEach(segment -> {let (y, x, length) = segment;Canvas.rect(left+Int32.toFloat32(x) *sx,top+Int32.toFloat32(y) *sy,Int32.toFloat32(length) *sx,sy ) },runs(sprite) ) }////// Draws a sprite centred on `centre`, with each sprite pixel `scale` screen pixels wide.///pubdefdraw(sprite: Sprite, centre: Vec2, scale: Float32): Unit \ Canvas =drawScaled(sprite, centre, scaleX = scale, scaleY = scale)}////// The artwork.////// Each invader has two frames. Alternating between them as the formation marches is what/// makes them look like they are walking -- the original does exactly this, and it is the/// whole animation budget of the game.///mod Sprites {/// Top row invader (squid), animation frame A.pubdefsquidA(): Sprite =Sprite.of(Vector#{"...##...","..####..",".######.","##.##.##","########","..#..#..",".#.##.#.","#.#..#.#" })/// Top row invader (squid), animation frame B.pubdefsquidB(): Sprite =Sprite.of(Vector#{"...##...","..####..",".######.","##.##.##","########",".#.##.#.","#......#",".#....#." })/// Middle row invader (crab), animation frame A.pubdefcrabA(): Sprite =Sprite.of(Vector#{"..#.....#..","...#...#...","..#######..",".##.###.##.","###########","#.#######.#","#.#.....#.#","...##.##..." })/// Middle row invader (crab), animation frame B.pubdefcrabB(): Sprite =Sprite.of(Vector#{"..#.....#..","#..#...#..#","#.#######.#","###.###.###","###########",".#########.","..#.....#..",".#.......#." })/// Bottom row invader (octopus), animation frame A.pubdefoctopusA(): Sprite =Sprite.of(Vector#{"....####....",".##########.","############","###..##..###","############","...##..##...","..##.##.##..",".##......##." })/// Bottom row invader (octopus), animation frame B.pubdefoctopusB(): Sprite =Sprite.of(Vector#{"....####....",".##########.","############","###..##..###","############","..##.##.##..",".##......##.","..##....##.." })/// Player defense cannon sprite.pubdefcannon(): Sprite =Sprite.of(Vector#{"......#......",".....###.....",".....###.....",".###########.","#############","#############","#############","#############" })/// Single laser bullet fired by player cannon.pubdefbullet(): Sprite = Sprite.of(Vector#{"#", "#", "#", "#", "#", "#"})/// The invaders' bomb, which tumbles as it falls. The common, slow one.pubdefbomb(): Sprite = Sprite.of(Vector#{".#.", "..#", ".#.", "#..", ".#.", "..#", ".#."})////// The fast bomb: a dagger, drawn straight so its speed reads as purpose.////// The three bombs have to be told apart at a glance while the screen is busy, and the/// only channels available are shape and motion -- they are all one colour. A vertical/// bar against the tumbler's zigzag is the largest difference two three-pixel-wide/// sprites can carry.///pubdefbombFast(): Sprite = Sprite.of(Vector#{".#.", ".#.", ".#.", ".#.", ".#.", "###", ".#."})/// The wiggler: a wide zigzag, the one that tears the biggest hole in a shelter.pubdefbombWiggly(): Sprite =Sprite.of(Vector#{"#..", ".#.", "..#", ".#.", "#..", ".#.", "..#"})/// The sprite for a bomb of the given kind.pubdefbombFor(kind: BombKind): Sprite = matchkind {case BombKind.Slow => bomb()case BombKind.Fast => bombFast()case BombKind.Wiggly => bombWiggly() }////// The mystery ship that crosses the top of the screen.///pubdefmystery(): Sprite =Sprite.of(Vector#{".....######.....","...##########...","..############..",".##.##.##.##.##.","################","..###..##..###..","...##......##..." })////// The starburst an invader leaves behind.///pubdefinvaderBlast(): Sprite =Sprite.of(Vector#{"....#...#....","#...#...#...#",".#.#.....#.#.","..#..###..#..","###..###..###","..#..###..#..",".#.#.....#.#.","#...#...#...#" })////// The cannon coming apart, in two frames so the debris flickers.///pubdefplayerBlastA(): Sprite =Sprite.of(Vector#{"..#.#...#.#..","#...#.#.#...#",".#.#..#..#.#.","#..####.##..#",".###########.","#############",".#.#######.#.","#.#.#####.#.#" })/// Explosion artwork for player destruction, frame B.pubdefplayerBlastB(): Sprite =Sprite.of(Vector#{"#.#...#...#.#","..#.#...#.#..","#..#..#..#..#",".#.##.#.##.#.","#.#########.#",".###########.","#.#.#####.#.#",".#.#.###.#.#." })////// The frame for a blast, given how many ticks of it are left.///pubdefforBlast(kind: BlastKind, ticksLeft: Int32): Sprite = matchkind {case BlastKind.InvaderBlast => invaderBlast()case BlastKind.PlayerBlast =>if (Int32.remainder(ticksLeft/6, 2) ==0) playerBlastA() elseplayerBlastB() }////// The frame for an invader of `kind`, alternating on `frame`.///pubdefforKind(kind: InvaderKind, frame: Int32): Sprite =match (kind, Int32.remainder(frame, 2)) {case (InvaderKind.Squid, 0) => squidA()case (InvaderKind.Squid, _) => squidB()case (InvaderKind.Crab, 0) => crabA()case (InvaderKind.Crab, _) => crabB()case (InvaderKind.Octopus, 0) => octopusA()case (InvaderKind.Octopus, _) => octopusB() }}