libraryextensions/scene/demos/Play.xtl
Play.xtl -- playing in the endless world (Endless.xtl), shared by the voxel demos that dig, build and pour water (voxels-dig, voxels-water and those after): a crosshair and a hotbar drawn over the view, the block under the crosshair dug out or one placed against it, and one frame of the game -- an event, the world's work, the player. Import: "pl:" u_se< "Play"
The hotbar and the crosshair
ˡkinds : Int
the blocks the hotbar places, keys 1 to 7: stone dirt grass sand wood leaves water (water placed flows)
ˡkinds ← 1 2 3 4 6 7 5
ˡkindsLit : Int
in a lit world (en:l_ighting) the hotbar has an eighth: a lamp
ˡkindsLit ← 1 2 3 4 6 7 5 8
ʰk̲inds : (Any a, Any b, Any c, Any d, Any e, Any f, Any g, Num h) => (a, b, c, (d, e, f, g, h)) -> Int
ʰk̲inds ← { s → 0 < ᵉⁿl̲it s ? ˡkindsLit ˡkinds }
ˡhelp : Char
the keys, said once at the start of a demo
ˡ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"
ˡhelpLit : Char
the keys in a lit world
ˡ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"
ˡh̲ud : Num a => a -> Int -> Int
w pl:h_ud sel: the overlay of window w (640 by 400) -- the hotbar at the bottom (a dark bar, the chosen block framed in white, a square of each block's color) and a crosshair at the center
ˡh̲ud ← { w sel → (w, ˡkinds) ʰh̲udOf sel }
ˡh̲udLit : Num a => a -> Int -> Int
w pl:h_udLit sel: the same with a lit world's hotbar (with the lamp)
ˡh̲udLit ← { w sel → (w, ˡkindsLit) ʰh̲udOf sel }
ʰh̲udOf : Num a => (a, Int) -> Int -> Int
ʰh̲udOf ← { wk sel → (w, ks) ← wk n ← t̲ally ks left ← 320.0 − (42.0 × f̲loat n) ÷ 2.0 lefts ← (left + 3.0) + 42.0 × f̲loat (r̲ange n) − 1 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 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 rgb ← r̲avel 2 1 t̲ranspose (1 + ks) s̲elect ᵛˣcolors 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 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 w ˢᶜo̲verlay! bar c̲at chosen c̲at squares c̲at cross }
Digging and building
ˡd̲ig : (Any a, Any b, Any c, Num d, Any e, Any f) => ((Float, a, Float, Float, b, c), (Int, Int, Box Int, Box Int, Int, Int, Int, d), e, f) -> ((Float, a, Float, Float, b, c), (Int, Int, Box Int, Box Int, Int, Int, Int, d), e, f)
pl:d_ig s: the block looked at dug out (air; water when it is beside water, which then flows), if there is one; said
ˡd̲ig ← { s → h ← (ᵉⁿw̲orld s) ᵉⁿp̲ick ᵉⁿe̲ye s 0 = f̲irst h ? s q ← 2 3 4 s̲elect h said ← p̲rint! "dug " c̲at f̲ormat q (q, 0) ᵉⁿs̲et s }
ˡp̲lace : (Any a, Any b, Any c, Num d, Any e, Any f, Any g, Any h, Any i, Num j) => (((Float, a, Float, Float, b, c), (Int, Int, Box Int, Box Int, Int, Int, Int, d), e, (f, g, h, i, j)), Int) -> (((Float, a, Float, Float, b, c), (Int, Int, Box Int, Box Int, Int, Int, Int, d), e, (f, g, h, i, j)), Int)
pl:p_lace (s, sel): a block of the chosen kind placed in the empty cell before the one looked at -- not where the player stands, nor above the world's top; said
ˡp̲lace ← { (s, sel) → h ← (ᵉⁿw̲orld s) ᵉⁿp̲ick ᵉⁿe̲ye s 0 = 5 s̲elect h ? (s, sel) q ← 6 7 8 s̲elect h (ᵉⁿp̲osition s) ᵉⁿo̲verlaps q ? (s, sel) (2 s̲elect q) > 31 ? (s, sel) t ← sel s̲elect ʰk̲inds s 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 ((q, t) ᵉⁿs̲et s, sel) }
ʰc̲hoose : (Any a, Any b, Any c, Num d, Any e, Any f, Any g, Num h, Any i) => (Char, (a, b, c, (d, e, f, g, h)), Int) -> i -> ((a, b, c, (d, e, f, g, h)), Int)
ʰc̲hoose ← { (v, s, sel) z → ks ← ʰk̲inds s i ← "12345678" i̲ndexOf f̲irst -1 t̲ake v ((5 = t̲ally v) × i ≤ t̲ally ks) = 0 ? (s, sel) hud ← (ᵉⁿw̲indow s, ks) ʰh̲udOf i (s, i) }
ʰn̲ightIf : (Truthy a, Any b, Any c, Any d, Num e, Any f, Any g, Any h, Num i) => a -> (b, c, d, (e, f, g, h, i)) -> (b, c, d, (e, f, g, h, i))
ʰn̲ightIf ← { yes s → yes ? ᵉⁿn̲ight s s }
ʰi̲f : (Truthy a, Any b, Any c, Any d, Any e, Any f, Any g, Any h) => a -> ((b, Float, Float, Float, Float, Float), c, d, (e, f, b, g, h)) -> ((b, Float, Float, Float, Float, Float), c, d, (e, f, b, g, h))
ʰi̲f ← { yes s → yes ? ᵉⁿh̲omeAgain s s }
ʰf̲ly : (Truthy a, Any b, Any c, Any d, Any e, Any f, Any g) => a -> ((b, Float, c, d, Float, Float), e, f, g) -> ((b, Float, c, d, Float, Float), e, f, g)
ʰf̲ly ← { yes s → yes ? ᵉⁿt̲oggleFly s s }
ʰw̲hen : (Truthy a, Any b, Any c, Any d, Num e, Any f, Any g) => a -> ((Float, b, Float, Float, c, d), (Int, Int, Box Int, Box Int, Int, Int, Int, e), f, g) -> ((Float, b, Float, Float, c, d), (Int, Int, Box Int, Box Int, Int, Int, Int, e), f, g)
ʰw̲hen ← { yes s → yes ? ˡd̲ig s s }
ʰp̲laceIf : (Truthy a, Any b, Any c, Any d, Num e, Any f, Any g, Any h, Any i, Any j, Num k) => a -> (((Float, b, Float, Float, c, d), (Int, Int, Box Int, Box Int, Int, Int, Int, e), f, (g, h, i, j, k)), Int) -> (((Float, b, Float, Float, c, d), (Int, Int, Box Int, Box Int, Int, Int, Int, e), f, (g, h, i, j, k)), Int)
ʰp̲laceIf ← { yes st → yes ? ˡp̲lace st st }
ˡf̲rame : (Num a, Num b, Num c, Num d) => (((Float, Float, Float, Float, Float, Float), (Int, Int, Box Int, Box Int, Int, Int, Int, a), (b, Int, Int, Int), (Int, Int, Float, c, d)), Int) -> (((Float, Float, Float, Float, Float, Float), (Int, Int, Box Int, Box Int, Int, Int, Int, a), (b, Int, Int, Int), (Int, Int, Float, c, d)), Int)
pl:f_rame (s, sel): one frame of the game: an event (dig, place, choose, home, fly, quit), then the world's work (a third of a column, or a tick of flowing water), then the player
ˡf̲rame ← { st → (s, sel, _) ← ˡf̲rameV st (s, sel) }
ˡf̲rameV : (Num a, Num b, Num c, Num d) => (((Float, Float, Float, Float, Float, Float), (Int, Int, Box Int, Box Int, Int, Int, Int, a), (b, Int, Int, Int), (Int, Int, Float, c, d)), Int) -> (((Float, Float, Float, Float, Float, Float), (Int, Int, Box Int, Box Int, Int, Int, Int, a), (b, Int, Int, Int), (Int, Int, Float, c, d)), Int, Char)
pl:f_rameV (s, sel): pl:f_rame, and the frame's event too (for a game with keys of its own)
ˡf̲rameV ← { (s, sel) → ᵉⁿs̲topped s ? (s, sel, "") w ← ᵉⁿw̲indow s v ← ˢᶜn̲ext! w ((v m̲atch "close") + (v m̲atch "key q") + v m̲atch "key Escape") > 0 ? (ᵉⁿs̲top s, sel, v) s ← (v m̲atch "key h") ʰi̲f s s ← (v m̲atch "key f") ʰf̲ly s s ← ((v m̲atch "key n") × 0 < ᵉⁿl̲it s) ʰn̲ightIf s dig ← ((10 t̲ake v) m̲atch "click left") + v m̲atch "key x" s ← dig ʰw̲hen s put ← ((11 t̲ake v) m̲atch "click right") + v m̲atch "key e" (s, sel) ← put ʰp̲laceIf (s, sel) (s, sel) ← (v, s, sel) ʰc̲hoose 0 s ← ᵉⁿs̲tream s (s ᵉⁿm̲ove ˢᶜc̲ontrols w, sel, v) }