sourceextensions/scene/demos/Play.xtl
1⍝│ Play.xtl -- playing in the endless world (Endless.xtl), shared by the
2⍝│ voxel demos that dig, build and pour water (voxels-dig, voxels-water
3⍝│ and those after): a crosshair and a hotbar drawn over the view, the
4⍝│ block under the crosshair dug out or one placed against it, and one
5⍝│ frame of the game -- an event, the world's work, the player.
6⍝│ Import: "pl:" u_se< "Play"
7
8"en:" u̲se< "Endless"
9"vx:" u̲se< "Voxels"
10"sc:" u̲se< "Scene"
11
12⍝━━ The hotbar and the crosshair
13
14⍝│ the blocks the hotbar places, keys 1 to 7: stone dirt grass sand
15⍝│ wood leaves water (water placed flows)
16ˡkinds ← 1 2 3 4 6 7 5
17
18⍝│ in a lit world (en:l_ighting) the hotbar has an eighth: a lamp
19ˡkindsLit ← 1 2 3 4 6 7 5 8
20
21⍝ the hotbar's blocks in the world of s
22ʰk̲inds ← { s →
23 0 < ᵉⁿl̲it s ? ˡkindsLit
24 ˡkinds
25}
26
27⍝│ the keys, said once at the start of a demo
28ˡhelp ← "keys: click or X dig, right-click or E place, 1-7 choose the block (7 water), W A S D walk, Space jump, F fly, drag or arrows look, H home, q quit"
29
30⍝│ the keys in a lit world
31ˡhelpLit ← "keys: click or X dig, right-click or E place, 1-8 choose the block (7 water, 8 a lamp), N night or day, W A S D walk, Space jump, F fly, drag or arrows look, H home, q quit"
32
33⍝│ w pl:h_ud sel: the overlay of window w (640 by 400) -- the hotbar at
34⍝│ the bottom (a dark bar, the chosen block framed in white, a square of
35⍝│ each block's color) and a crosshair at the center
36ˡh̲ud ← { w sel → (w, ˡkinds) ʰh̲udOf sel }
37
38⍝│ w pl:h_udLit sel: the same with a lit world's hotbar (with the lamp)
39ˡh̲udLit ← { w sel → (w, ˡkindsLit) ʰh̲udOf sel }
40
41⍝ (w, kinds) h:h_udOf sel: the hotbar of those blocks, centered
42ʰh̲udOf ← { wk sel →
43 (w, ks) ← wk
44 n ← t̲ally ks
45 left ← 320.0 − (42.0 × f̲loat n) ÷ 2.0
46 lefts ← (left + 3.0) + 42.0 × f̲loat (r̲ange n) − 1
47 bar ← 1 7 r̲eshape (left − 5.0) c̲at 344.0 c̲at (10.0 + 42.0 × f̲loat n) c̲at 52.0 0.1 0.1 0.12
48 chosen ← 1 7 r̲eshape ((sel s̲elect lefts) − 4.0) c̲at 348.0 44.0 44.0 1.0 1.0 1.0
49 squares ← 2 1 t̲ranspose (7 c̲at n) r̲eshape lefts c̲at (n r̲eshape 352.0) c̲at ((2 × n) r̲eshape 36.0) c̲at r̲avel 2 1 t̲ranspose (1 + ks) s̲elect ᵛˣcolors
50 cross ← 2 7 r̲eshape 310.0 199.0 20.0 2.0 1.0 1.0 1.0 319.0 190.0 2.0 20.0 1.0 1.0 1.0
51 w ˢᶜo̲verlay! bar c̲at chosen c̲at squares c̲at cross
52}
53
54⍝━━ Digging and building
55
56⍝│ The state of a game is a tuple: (the endless world and its player,
57⍝│ the chosen block 1 to 7).
58
59⍝│ pl:d_ig s: the block looked at dug out (air; water when it is beside
60⍝│ water, which then flows), if there is one; said
61ˡd̲ig ← { s →
62 h ← (ᵉⁿw̲orld s) ᵉⁿp̲ick ᵉⁿe̲ye s
63 0 = f̲irst h ? s
64 q ← 2 3 4 s̲elect h
65 _ ← p̲rint! "dug " c̲at f̲ormat q
66 (q, 0) ᵉⁿs̲et s
67}
68
69⍝│ pl:p_lace (s, sel): a block of the chosen kind placed in the empty
70⍝│ cell before the one looked at -- not where the player stands, nor
71⍝│ above the world's top; said
72ˡp̲lace ← { (s, sel) →
73 h ← (ᵉⁿw̲orld s) ᵉⁿp̲ick ᵉⁿe̲ye s
74 0 = 5 s̲elect h ? (s, sel)
75 q ← 6 7 8 s̲elect h
76 (ᵉⁿp̲osition s) ᵉⁿo̲verlaps q ? (s, sel)
77 (2 s̲elect q) > 31 ? (s, sel)
78 t ← sel s̲elect ʰk̲inds s
79 _ ← p̲rint! "placed " c̲at (d̲isclose (1 + t) s̲elect (n̲ot ᵛˣnames = f̲irst " ") p̲artition ᵛˣnames) c̲at " at " c̲at f̲ormat q
80 ((q, t) ᵉⁿs̲et s, sel)
81}
82
83⍝ (v, s, sel) h:c_hoose 0: a digit key chooses the hotbar's block
84ʰc̲hoose ← { (v, s, sel) z →
85 ks ← ʰk̲inds s
86 i ← "12345678" i̲ndexOf f̲irst -1 t̲ake v
87 ((5 = t̲ally v) × i ≤ t̲ally ks) = 0 ? (s, sel)
88 hud ← (ᵉⁿw̲indow s, ks) ʰh̲udOf i
89 (s, i)
90}
91ʰn̲ightIf ← { yes s →
92 yes ? ᵉⁿn̲ight s
93 s
94}
95
96ʰi̲f ← { yes s →
97 yes ? ᵉⁿh̲omeAgain s
98 s
99}
100ʰf̲ly ← { yes s →
101 yes ? ᵉⁿt̲oggleFly s
102 s
103}
104ʰw̲hen ← { yes s →
105 yes ? ˡd̲ig s
106 s
107}
108ʰp̲laceIf ← { yes st →
109 yes ? ˡp̲lace st
110 st
111}
112
113⍝│ pl:f_rame (s, sel): one frame of the game: an event (dig, place,
114⍝│ choose, home, fly, quit), then the world's work (a third of a column,
115⍝│ or a tick of flowing water), then the player
116ˡf̲rame ← { st →
117 (s, sel, _) ← ˡf̲rameV st
118 (s, sel)
119}
120
121⍝│ pl:f_rameV (s, sel): pl:f_rame, and the frame's event too (for a game
122⍝│ with keys of its own)
123ˡf̲rameV ← { (s, sel) →
124 ᵉⁿs̲topped s ? (s, sel, "")
125 w ← ᵉⁿw̲indow s
126 v ← ˢᶜn̲ext! w
127 ((v m̲atch "close") + (v m̲atch "key q") + v m̲atch "key Escape") > 0 ? (ᵉⁿs̲top s, sel, v)
128 flown ← (v m̲atch "key f") ʰf̲ly (v m̲atch "key h") ʰi̲f s
129 dimmed ← ((v m̲atch "key n") × 0 < ᵉⁿl̲it flown) ʰn̲ightIf flown
130 dig ← ((10 t̲ake v) m̲atch "click left") + v m̲atch "key x"
131 dug ← dig ʰw̲hen dimmed
132 put ← ((11 t̲ake v) m̲atch "click right") + v m̲atch "key e"
133 (placed, held) ← put ʰp̲laceIf (dug, sel)
134 (chosen, picked) ← (v, placed, held) ʰc̲hoose 0
135 streamed ← ᵉⁿs̲tream chosen
136 (streamed ᵉⁿm̲ove ˢᶜc̲ontrols w, picked, v)
137}