// isokit/util: helpers shared by the prefabs. /// Floor index of a level id: "gf" -> 0, "1f" -> 1, "2f" -> 2; anything else /// (roof, site, "") -> -1, which means "closed buildings". fn floor_of(lv) { if lv == "gf" { return 0; } if lv.len() >= 2 && lv.ends_with("f") { let k = lv.sub_string(0, lv.len() - 1); let digits = true; for c in k.chars() { if !"0123456789".contains(c) { digits = false; } } if digits { return parse_int(k); } } -1 } /// Walls of a cut-open room over the rectangle [x0, y0, x1, y1] from height z: /// walls facing the viewer are cut low (lo), the others stay high (hi), so /// the room can be seen into from every rotation. Each wall is its own sprite /// for the depth sort. skip: list of walls to leave out (0 S, 1 N, 2 W, 3 E). fn cut_walls(x0, y0, x1, y1, z, lo, hi, t, mat, skip) { let W = [ [x0, y0, x1, y0 + t, 0, -1], [x0, y1 - t, x1, y1, 0, 1], [x0, y0 + t, x0 + t, y1 - t, -1, 0], [x1 - t, y0 + t, x1, y1 - t, 1, 0], ]; for i in 0..4 { if skip.contains(i) { continue; } let w = W[i]; let h = if vis(w[4], w[5]) { lo } else { hi }; sprite().box(w[0], w[1], w[2], w[3], z, z + h, mat, #{ top: "cut" }); } } /// Windows spread along the face a -> b (length L) for one storey: /// returns [[u0, u1, z0, z1], ...]; leaves a gap around u = gap (or -1). fn win_list(L, z0, sill, height, width, spacing, gap) { let out = []; let n = ((L - 1.0) / spacing).floor().to_int(); if n < 1 { return out; } let start = (L - (n - 1) * spacing) / 2.0; for i in 0..n { let u = start + i * spacing; if gap >= 0.0 && (u - gap).abs() < width + 0.6 { continue; } out.push([u - width / 2.0, u + width / 2.0, z0 + sill, z0 + sill + height]); } out } /// Animate the next drawing of sprite s along (dx, dy) metres of its frame: /// call before drawing, then s.end(). Only animates when the motion runs /// along the axis the depth sort compares second; returns whether it does. fn drive(dx, dy, dur, delay) { let ok = if dx != 0.0 { along_x() } else { !along_x() }; if !ok { this.begin(""); return false; } let v = dv(dx, dy, 0.0); this.begin("a drive", `--dx:${v[0]}px;--dy:${v[1]}px;animation-duration:${dur}s;animation-delay:-${delay}s`); true }