programgames/tower-defense/play.xtl

Tower defense: stop the creeps on the road before they get out. Pick a tower on the toolbar (or 1, 2, 3; left and right step through them), click a grass tile to build it there, click a tower to upgrade it; Next wave (or the space bar) sends the next wave; the Grimoire (or g) shows every tower and creep, turning, with what it does. Six waves to beat; a creep that gets out costs lives. The game moves with the clock: on the page it is in a dialog, and the clock's ticks (twenty a second), your clicks and your keys come to the program as events; at the command line, lines such as "tick 0.05", "click 384 192" and "key space" on standard input. When the waves are beaten or the lives are gone the program ends (on the page, Play or any key then starts a new game). The rules are Defense.xtl's, built on lib/Ecs.

source · imports td: games/tower-defense/Defense.xtl

ᵘs̲how : (Num a, Num b, Num c) => (Float, Int, Int, Int, Int, Float, Int, a, Int, Float, b, c, Char) -> Char

function · line 17

Show the game, then wait for the next event.

ᵘs̲how ← { s →
  _ ← ⎕S̲HOW ᵗᵈd̲raw s
  0 < ᵗᵈs̲tatus s ? ᵘe̲nd s
  ᵘl̲oop s
}

ᵘl̲oop : (Num a, Num b, Num c) => (Float, Int, Int, Int, Int, Float, Int, a, Int, Float, b, c, Char) -> Char

function · line 24

The next event: a tick moves the game on and shows it; a click or a key acts.

ᵘl̲oop ← { s →
  e ← ⎕E̲VENT @
  "end" m̲atch ⎕E̲KIND e ? ᵘq̲uit s
  "tick" m̲atch ⎕E̲KIND e ? ᵘs̲how s ᵗᵈt̲ick f̲irst ⎕E̲AT e
  "click" m̲atch ⎕E̲KIND e ? ᵘl̲oop s ᵗᵈc̲lick ⎕E̲AT e
  n̲ot "key" m̲atch ⎕E̲KIND e ? ᵘl̲oop s
  ᵘl̲oop s ᵗᵈk̲ey f̲ormat ⎕E̲KEY e
}

ᵘq̲uit : (Num a, Num b, Num c) => (Float, Int, Int, Int, Int, Float, Int, a, Int, Float, b, c, Char) -> Char

function · line 33

The end of the events: how it stands.

ᵘq̲uit ← { s →
  _ ← p̲rint! @ f̲ormat< "Money {f_irst td:s_core s}, lives {2 s_elect td:s_core s}, waves begun {3 s_elect td:s_core s}."
  "THE END"
}
f̲ormat< expands to
("Money " c̲at (f̲ormat (f̲irst ᵗᵈs̲core s)) c̲at ", lives " c̲at (f̲ormat (2 s̲elect ᵗᵈs̲core s)) c̲at ", waves begun " c̲at (f̲ormat (3 s̲elect ᵗᵈs̲core s)) c̲at ".")
Used in: ᵘl̲oop

ᵘe̲nd : (Num a, Num b, Num c) => (Float, Int, Int, Int, Int, Float, Int, a, Int, Float, b, c, Char) -> Char

function · line 38

How the game ended (the program ends with it).

ᵘe̲nd ← { s →
  1 = ᵗᵈs̲tatus s ? @ f̲ormat< "Every wave beaten, with {2 s_elect td:s_core s} lives left!"
  "The creeps got through."
}
f̲ormat< expands to
("Every wave beaten, with " c̲at (f̲ormat (2 s̲elect ᵗᵈs̲core s)) c̲at " lives left!")
Used in: ᵘs̲how