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ᵉⁿ⁼u̲se< "Endless"
9ᵛˣ⁼u̲se< "Voxels"
10ˢᶜ⁼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 rgb ← r̲avel 2 1 t̲ranspose (1 + ks) s̲elect ᵛˣcolors
50 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 rgb
51 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
52 w ˢᶜo̲verlay! bar c̲at chosen c̲at squares c̲at cross
53}
54
55⍝## Digging and building
56
57⍝# The state of a game is a tuple: (the endless world and its player,
58⍝# the chosen block 1 to 7).
59
60⍝# pl:d_ig s: the block looked at dug out (air; water when it is beside
61⍝# water, which then flows), if there is one; said
62ˡd̲ig ← { s →
63 h ← (ᵉⁿw̲orld s) ᵉⁿp̲ick ᵉⁿe̲ye s
64 0 = f̲irst h ? s
65 q ← 2 3 4 s̲elect h
66 said ← p̲rint! "dug " c̲at f̲ormat q
67 (q, 0) ᵉⁿs̲et s
68}
69
70⍝# pl:p_lace (s, sel): a block of the chosen kind placed in the empty
71⍝# cell before the one looked at -- not where the player stands, nor
72⍝# above the world's top; said
73ˡp̲lace ← { (s, sel) →
74 h ← (ᵉⁿw̲orld s) ᵉⁿp̲ick ᵉⁿe̲ye s
75 0 = 5 s̲elect h ? (s, sel)
76 q ← 6 7 8 s̲elect h
77 (ᵉⁿp̲osition s) ᵉⁿo̲verlaps q ? (s, sel)
78 (2 s̲elect q) > 31 ? (s, sel)
79 t ← sel s̲elect ʰk̲inds s
80 said ← 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
81 ((q, t) ᵉⁿs̲et s, sel)
82}
83
84⍝ (v, s, sel) h:c_hoose 0: a digit key chooses the hotbar's block
85ʰc̲hoose ← { (v, s, sel) z →
86 ks ← ʰk̲inds s
87 i ← "12345678" i̲ndexOf f̲irst -1 t̲ake v
88 ((5 = t̲ally v) × i ≤ t̲ally ks) = 0 ? (s, sel)
89 hud ← (ᵉⁿw̲indow s, ks) ʰh̲udOf i
90 (s, i)
91}
92ʰn̲ightIf ← { yes s →
93 yes ? ᵉⁿn̲ight s
94 s
95}
96
97ʰi̲f ← { yes s →
98 yes ? ᵉⁿh̲omeAgain s
99 s
100}
101ʰf̲ly ← { yes s →
102 yes ? ᵉⁿt̲oggleFly s
103 s
104}
105ʰw̲hen ← { yes s →
106 yes ? ˡd̲ig s
107 s
108}
109ʰp̲laceIf ← { yes st →
110 yes ? ˡp̲lace st
111 st
112}
113
114⍝# pl:f_rame (s, sel): one frame of the game: an event (dig, place,
115⍝# choose, home, fly, quit), then the world's work (a third of a column,
116⍝# or a tick of flowing water), then the player
117ˡf̲rame ← { st →
118 (s, sel, _) ← ˡf̲rameV st
119 (s, sel)
120}
121
122⍝# pl:f_rameV (s, sel): pl:f_rame, and the frame's event too (for a game
123⍝# with keys of its own)
124ˡf̲rameV ← { (s, sel) →
125 ᵉⁿs̲topped s ? (s, sel, "")
126 w ← ᵉⁿw̲indow s
127 v ← ˢᶜn̲ext! w
128 ((v m̲atch "close") + (v m̲atch "key q") + v m̲atch "key Escape") > 0 ? (ᵉⁿs̲top s, sel, v)
129 s ← (v m̲atch "key h") ʰi̲f s
130 s ← (v m̲atch "key f") ʰf̲ly s
131 s ← ((v m̲atch "key n") × 0 < ᵉⁿl̲it s) ʰn̲ightIf s
132 dig ← ((10 t̲ake v) m̲atch "click left") + v m̲atch "key x"
133 s ← dig ʰw̲hen s
134 put ← ((11 t̲ake v) m̲atch "click right") + v m̲atch "key e"
135 (s, sel) ← put ʰp̲laceIf (s, sel)
136 (s, sel) ← (v, s, sel) ʰc̲hoose 0
137 s ← ᵉⁿs̲tream s
138 (s ᵉⁿm̲ove ˢᶜc̲ontrols w, sel, v)
139}