sourcegames/tower-defense/play.xtl

1⍝!/usr/bin/env xetal 2⍝│ Tower defense: stop the creeps on the road before they get out. Pick 3⍝│ a tower on the toolbar (or 1, 2, 3; left and right step through 4⍝│ them), click a grass tile to build it there, click a tower to upgrade 5⍝│ it; Next wave (or the space bar) sends the next wave; the Grimoire (or 6⍝│ g) shows every tower and creep, turning, with what it does. Six waves 7⍝│ to beat; a creep that gets out costs lives. The game moves with the 8⍝│ clock: on the page it is in a dialog, and the clock's ticks (twenty a 9⍝│ second), your clicks and your keys come to the program as events; at 10⍝│ the command line, lines such as "tick 0.05", "click 384 192" and 11⍝│ "key space" on standard input. When the waves are beaten or the lives 12⍝│ are gone the program ends (on the page, Play or any key then starts a 13⍝│ new game). The rules are Defense.xtl's, built on lib/Ecs. 14"td:" u̲se< "Defense" 15 16⍝│ Show the game, then wait for the next event. 17ᵘs̲how ← { s → 18 _ ← ⎕S̲HOW ᵗᵈd̲raw s 19 0 < ᵗᵈs̲tatus s ? ᵘe̲nd s 20 ᵘl̲oop s 21} 22⍝│ The next event: a tick moves the game on and shows it; a click or a 23⍝│ key acts. 24ᵘl̲oop ← { s → 25 e ← ⎕E̲VENT @ 26 "end" m̲atch ⎕E̲KIND e ? ᵘq̲uit s 27 "tick" m̲atch ⎕E̲KIND e ? ᵘs̲how s ᵗᵈt̲ick f̲irst ⎕E̲AT e 28 "click" m̲atch ⎕E̲KIND e ? ᵘl̲oop s ᵗᵈc̲lick ⎕E̲AT e 29 n̲ot "key" m̲atch ⎕E̲KIND e ? ᵘl̲oop s 30 ᵘl̲oop s ᵗᵈk̲ey f̲ormat ⎕E̲KEY e 31} 32⍝│ The end of the events: how it stands. 33ᵘq̲uit ← { s → 34 _ ← 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}."
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 ".")
35 "THE END" 36} 37⍝│ How the game ended (the program ends with it). 38ᵘe̲nd ← { s → 39 1 = ᵗᵈs̲tatus s ? @ f̲ormat< "Every wave beaten, with {2 s_elect td:s_core s} lives left!"
f̲ormat< expands to
("Every wave beaten, with " c̲at (f̲ormat (2 s̲elect ᵗᵈs̲core s)) c̲at " lives left!")
40 "The creeps got through." 41} 42ᵘs̲how ᵗᵈn̲ew 0