Toby Chui 2 недель назад
Родитель
Сommit
ef7e189ef2

+ 22 - 0
src/web/Blocky/README.md

@@ -23,6 +23,7 @@ its folder.
 | `js/realistic.js` | Realistic graphics pipeline: shadow map, sea reflection, terrain lighting chunk, HDR post passes. |
 | `js/power.js` | Spark circuits: power sources, wire levels, torches, lamps, pistons, rail shapes. |
 | `js/carts.js` | Minecarts (plain, chest, furnace) following rails. |
+| `js/boats.js` | Boats: floating, rowing, steering, riding. |
 | `js/dynlight.js` | Light from a glowing item held in hand. |
 | `js/signs.js` | The text on signs: canvas planes in front of the boards. |
 | `js/blocks.js` | Block / item registry, mining rules, shaped recipes and the grid matcher, smelting. |
@@ -247,6 +248,27 @@ with the slope it is on (`pose`, from the rail direction kept by `step`).
 Two hits break a cart (one in creative); a cart takes the click over the
 rail under it, whose box can stick out in front of a cart sitting in a dip.
 
+## Boats
+
+Five planks in a U make a boat. Right click water (or the ground) to put
+one down facing the way you look; right click it to sit in it. W / S row
+forward (up to 7 blocks/s) and back (2.5), A / D turn the boat and your
+view with it, Shift gets you out onto the nearest dry spot within two
+blocks. On land a boat only shuffles along at 1.2 blocks/s. Two hits break
+it (one in creative) and drop the boat; lava burns it up.
+
+`js/boats.js`: a boat looks for the top of the water column it is in
+(`surface`) and springs toward 0.14 below it (`DRAFT`), so one placed under
+water rises; off water it falls. Its collision box is square and 0.98 wide
+(the hull turns, the box does not), so it fits a one block canal; sideways
+speed is damped hard, like a keel. Walking into an empty boat nudges it,
+boats bump into each other, and they are saved with the world (`boats` in
+the save). The model is textured boxes cut from the planks tile at block
+pixel size, with oars that sweep while rowing (a soft `paddle` sound); a
+depth-only plane just under the rim keeps the water surface from showing
+inside the hull. While seated the game skips player physics, puts the
+player at `Boats.seat` and refreshes `Player.sense` for breath and fire.
+
 ## Realistic graphics
 
 Settings > Realistic Graphics. `js/realistic.js` with `render.js`:

+ 5 - 0
src/web/Blocky/js/audio.js

@@ -244,6 +244,11 @@ export class Sound {
                 f.frequency.exponentialRampToValueAtTime(300, this.ctx.currentTime + 0.4);
                 break;
             }
+            case "paddle": {
+                const f = this.noiseBurst("lowpass", 1300, 0.7, 0.3, 0.16);
+                f.frequency.exponentialRampToValueAtTime(260, this.ctx.currentTime + 0.28);
+                break;
+            }
             case "toolBreak":
                 this.tone(900, 0.2, 0.2, "square", 0, 300);
                 this.noiseBurst("highpass", 3000, 0.5, 0.2, 0.3);

+ 4 - 2
src/web/Blocky/js/blocks.js

@@ -47,7 +47,7 @@ export const I = {
     COOKED_MUTTON: 279, BOW: 305, ARROW: 306, RAW_CHICKEN: 307, COOKED_CHICKEN: 308, FEATHER: 309,
     STRING: 310, BONE: 311, BONE_MEAL: 312,
     DYE: 313, //313-328: dyes, in WOOL_COLORS order
-    SPARK_DUST: 329, MINECART: 330, CHEST_MINECART: 331, FURNACE_MINECART: 332
+    SPARK_DUST: 329, MINECART: 330, CHEST_MINECART: 331, FURNACE_MINECART: 332, BOAT: 333
 };
 
 export const dyeId = (key) => I.DYE + WOOL_COLORS.findIndex((c) => c[0] === key);
@@ -430,6 +430,7 @@ item(I.SPARK_DUST, "Spark Dust", { icon: { flat: "spark_dust" }, block: B.SPARK_
 item(I.MINECART, "Minecart", { icon: { flat: "minecart" }, stack: 1, cart: "cart" });
 item(I.CHEST_MINECART, "Chest Minecart", { icon: { flat: "chest_minecart" }, stack: 1, cart: "chest" });
 item(I.FURNACE_MINECART, "Furnace Minecart", { icon: { flat: "furnace_minecart" }, stack: 1, cart: "furnace" });
+item(I.BOAT, "Boat", { icon: { flat: "boat_item" }, stack: 1, boat: true, fuel: 400 });
 
 //Fuel for blocks (ticks at 20 ticks per second; one smelt takes 200 ticks)
 ITEMS[B.LOG].fuel = 300;
@@ -654,6 +655,7 @@ shapeless(I.FURNACE_MINECART, 1, [I.MINECART, B.FURNACE], "Spark");
 shapeless(I.FLINT_AND_STEEL, 1, [I.IRON_INGOT, I.FLINT], "Tools");
 
 shaped(I.BOWL, 4, ["P.P", ".P."], { P: B.PLANKS }, "Food");
+shaped(I.BOAT, 1, ["P.P", "PPP"], { P: B.PLANKS }, "Tools");
 shaped(I.BREAD, 1, ["WWW"], { W: I.WHEAT }, "Food");
 shapeless(I.STEW, 1, [B.MUSHROOM_BROWN, B.MUSHROOM_RED, I.BOWL], "Food");
 
@@ -797,7 +799,7 @@ export function creativeGroups() {
     for (let mi = 0; mi < 5; mi++) {
         tools.push(swordId(mi));
     }
-    tools.push(I.BOW, I.ARROW, I.BUCKET, I.WATER_BUCKET, I.LAVA_BUCKET, I.FLINT_AND_STEEL);
+    tools.push(I.BOW, I.ARROW, I.BUCKET, I.WATER_BUCKET, I.LAVA_BUCKET, I.FLINT_AND_STEEL, I.BOAT);
     const spark = [B.SPARK_ORE, I.SPARK_DUST, B.SPARK_BLOCK, B.SPARK_TORCH, B.LEVER, B.BUTTON, B.PLATE_WOOD, B.PLATE_STONE, B.RETIMER, B.SPARK_LAMP,
         B.PISTON, B.STICKY_PISTON, B.RAIL, B.POWERED_RAIL, I.MINECART, I.CHEST_MINECART, I.FURNACE_MINECART];
     const items = [I.STICK, I.COAL, I.IRON_INGOT, I.GOLD_INGOT, I.CRYSTAL, I.FLINT, I.CLAY_BALL, I.BRICK,

+ 453 - 0
src/web/Blocky/js/boats.js

@@ -0,0 +1,453 @@
+/*
+    Blocky - boats.js
+
+    Wooden boats. A boat floats on water (it rises to the surface of the
+    water it is in and bobs there), slides slowly on land and falls through
+    the air. Right click to sit in it; W / S row forward and back, A / D
+    turn the boat (and your view with it), Shift gets out onto the nearest
+    dry spot. Two hits break a boat (one in creative) and drop it as an item.
+    Walking into an empty boat nudges it along. Boats bump into each other,
+    burn up in lava and are saved with the world.
+*/
+
+import * as THREE from "three";
+import { B, I, BLOCKS } from "./blocks.js";
+import { moveAxis } from "./physics.js";
+import { waterHeight } from "./mesher.js";
+
+//Collision box: square (the hull turns, the box does not) and just under
+//one block wide, so a boat fits a one block wide canal
+const HALF = 0.49, HEIGHT = 0.55;
+//How deep the hull sits below the water surface
+const DRAFT = 0.14;
+//Rowing on water: forward / backward thrust and top speed (blocks / s)
+const THRUST = 7, BACK_THRUST = 3, MAX_FWD = 7, MAX_BACK = 2.5;
+//On land a boat only shuffles along
+const LAND_THRUST = 1.5, LAND_MAX = 1.2;
+//Turning speed (radians / s)
+const TURN = 2.2;
+//Hull size (model space, bow toward -z like the player's view)
+const LEN = 1.375, WID = 0.875;
+
+export class Boats {
+    constructor(game) {
+        this.game = game;
+        this.renderer = game.renderer;
+        this.list = [];
+        this.texture = null;
+    }
+
+    clear() {
+        for (const b of this.list) {
+            this.disposeBoat(b);
+        }
+        this.list = [];
+    }
+
+    /* ---------------- model ---------------- */
+
+    woodTexture() {
+        if (!this.texture) {
+            const canvas = this.game.app.tex.canvases.get("planks");
+            const t = new THREE.CanvasTexture(canvas);
+            t.magFilter = THREE.NearestFilter;
+            t.minFilter = THREE.NearestFilter;
+            t.wrapS = t.wrapT = THREE.RepeatWrapping;
+            this.texture = t;
+        }
+        return this.texture;
+    }
+
+    buildModel() {
+        const group = new THREE.Group();
+        const hull = new THREE.Group();
+        group.add(hull);
+        const wood = new THREE.MeshBasicMaterial({ map: this.woodTexture() });
+        const dark = new THREE.MeshBasicMaterial({ map: this.woodTexture() });
+        wood.userData.base = new THREE.Color(1, 1, 1);
+        dark.userData.base = new THREE.Color(0.62, 0.56, 0.5);
+        //A box whose texture keeps the block's pixel size on every face
+        const box = (parent, w, h, d, m, x, y, z) => {
+            const g = new THREE.BoxGeometry(w, h, d);
+            const uv = g.attributes.uv;
+            const dims = [[d, h], [d, h], [w, d], [w, d], [w, h], [w, h]];
+            for (let f = 0; f < 6; f++) {
+                for (let k = 0; k < 4; k++) {
+                    const i = f * 4 + k;
+                    uv.setXY(i, uv.getX(i) * dims[f][0], uv.getY(i) * dims[f][1]);
+                }
+            }
+            const mesh = new THREE.Mesh(g, m);
+            mesh.position.set(x, y, z);
+            parent.add(mesh);
+            return mesh;
+        };
+        const t = 0.1, rim = 0.55, hw = WID / 2, hl = LEN / 2;
+        box(hull, WID, t, LEN, dark, 0, t / 2, 0);
+        box(hull, t, rim - t, LEN, wood, -hw + t / 2, (rim + t) / 2, 0);
+        box(hull, t, rim - t, LEN, wood, hw - t / 2, (rim + t) / 2, 0);
+        box(hull, WID - 2 * t, rim - t, t, wood, 0, (rim + t) / 2, -hl + t / 2);
+        box(hull, WID - 2 * t, rim - t, t, wood, 0, (rim + t) / 2, hl - t / 2);
+        //Seat plank across the middle
+        box(hull, WID - 2 * t, 0.06, 0.28, wood, 0, 0.3, 0.1);
+        //Oars: a pivot at each oarlock turns a shaft with a blade
+        const oars = [];
+        for (const side of [-1, 1]) {
+            const pivot = new THREE.Group();
+            pivot.position.set(side * (hw + 0.02), rim - 0.04, 0);
+            box(pivot, 0.05, 0.05, 1.1, wood, side * 0.06, 0, 0);
+            box(pivot, 0.03, 0.2, 0.26, dark, side * 0.06, 0, 0.5);
+            hull.add(pivot);
+            oars.push({ pivot, side });
+        }
+        //Keeps the water surface from showing inside the hull: drawn after
+        //the boat, it writes depth only, so the water behind it is hidden
+        const mask = new THREE.Mesh(new THREE.PlaneGeometry(WID - 2 * t, LEN - 2 * t),
+            new THREE.MeshBasicMaterial({ colorWrite: false }));
+        mask.rotation.x = -Math.PI / 2;
+        mask.position.y = rim - 0.06;
+        mask.renderOrder = 1;
+        hull.add(mask);
+        this.renderer.scene.add(group);
+        hull.children.forEach((o) => {
+            if (o !== mask) {
+                this.renderer.castShadows(o);
+            }
+        });
+        return { group, hull, oars, mask, mats: [wood, dark] };
+    }
+
+    spawn(x, y, z, data = {}) {
+        const b = {
+            x, y, z, vx: data.vx || 0, vy: 0, vz: data.vz || 0, yaw: data.yaw || 0,
+            hits: 0, hitCd: 0, rider: false, onGround: false, inWater: false,
+            row: 0, rowPhase: 0, splash: 0, bob: Math.random() * 6
+        };
+        b.model = this.buildModel();
+        this.list.push(b);
+        return b;
+    }
+
+    disposeBoat(b) {
+        this.renderer.scene.remove(b.model.group);
+        b.model.group.traverse((o) => {
+            if (o.geometry) {
+                o.geometry.dispose();
+            }
+        });
+        for (const m of b.model.mats) {
+            m.dispose();
+        }
+        b.model.mask.material.dispose();
+    }
+
+    box(b) {
+        return [b.x - HALF, b.y, b.z - HALF, b.x + HALF, b.y + HEIGHT, b.z + HALF];
+    }
+
+    raycast(ox, oy, oz, dx, dy, dz, reach) {
+        let best = null;
+        for (const b of this.list) {
+            if (b.rider) {
+                continue;
+            }
+            const bx = this.box(b);
+            let tmin = 0, tmax = Infinity, miss = false;
+            const o = [ox, oy, oz], d = [dx, dy, dz];
+            for (let a = 0; a < 3 && !miss; a++) {
+                if (Math.abs(d[a]) < 1e-9) {
+                    miss = o[a] < bx[a] || o[a] > bx[a + 3];
+                    continue;
+                }
+                let t1 = (bx[a] - o[a]) / d[a], t2 = (bx[a + 3] - o[a]) / d[a];
+                if (t1 > t2) {
+                    [t1, t2] = [t2, t1];
+                }
+                tmin = Math.max(tmin, t1);
+                tmax = Math.min(tmax, t2);
+                miss = tmin > tmax;
+            }
+            if (!miss && tmin <= reach && (!best || tmin < best.t)) {
+                best = { boat: b, t: tmin };
+            }
+        }
+        return best;
+    }
+
+    /* ---------------- player actions ---------------- */
+
+    //Left click: two hits break a boat (one in creative)
+    hit(b) {
+        const g = this.game;
+        if (b.hitCd > 0) {
+            return;
+        }
+        b.hitCd = 0.25;
+        b.hits++;
+        g.sound.block("dig", "wood");
+        if (b.hits >= 2 || g.creative) {
+            this.breakBoat(b, !g.creative);
+        }
+    }
+
+    breakBoat(b, drop) {
+        const g = this.game;
+        if (b.rider) {
+            this.dismount();
+        }
+        if (drop) {
+            g.entities.spawnItem(b.x, b.y + 0.5, b.z, I.BOAT, 1);
+        }
+        g.sound.block("break", "wood");
+        this.disposeBoat(b);
+        this.list.splice(this.list.indexOf(b), 1);
+    }
+
+    //Right click: sit in it
+    use(b) {
+        const g = this.game;
+        if (b.rider || g.boat || g.riding) {
+            return;
+        }
+        b.rider = true;
+        g.boat = b;
+        g.player.eyeSmooth = 0;
+        g.ui.toast("W / S to row, A / D to turn, Shift to get out");
+    }
+
+    dismount() {
+        const g = this.game, b = g.boat;
+        if (!b) {
+            return;
+        }
+        b.rider = false;
+        g.boat = null;
+        const w = g.world, p = g.player;
+        //Step onto the nearest dry spot beside the boat, else on top of it
+        const fx = Math.floor(b.x), fz = Math.floor(b.z);
+        let best = null;
+        for (let dx = -2; dx <= 2; dx++) {
+            for (let dz = -2; dz <= 2; dz++) {
+                for (let y = Math.floor(b.y) + 1; y >= Math.floor(b.y) - 1; y--) {
+                    const x = fx + dx, z = fz + dz;
+                    const below = w.getBlock(x, y - 1, z);
+                    if (!BLOCKS[below].solid || BLOCKS[w.getBlock(x, y, z)].solid || BLOCKS[w.getBlock(x, y + 1, z)].solid ||
+                        w.getBlock(x, y, z) === B.WATER || w.getBlock(x, y, z) === B.LAVA) {
+                        continue;
+                    }
+                    const d = Math.hypot(x + 0.5 - b.x, z + 0.5 - b.z) + Math.abs(y - b.y) * 0.5;
+                    if (!best || d < best.d) {
+                        best = { x, y, z, d };
+                    }
+                    break;
+                }
+            }
+        }
+        if (best) {
+            p.setPos(best.x + 0.5, best.y, best.z + 0.5);
+        } else {
+            p.setPos(b.x, b.y + HEIGHT + 0.05, b.z);
+        }
+    }
+
+    /* ---------------- per frame ---------------- */
+
+    //Top of the water the boat is in (null when it is not on water) and
+    //whether that is lava
+    surface(b) {
+        const w = this.game.world;
+        const bx = Math.floor(b.x), bz = Math.floor(b.z);
+        for (const by of [Math.floor(b.y + DRAFT + 0.05), Math.floor(b.y + DRAFT + 0.05) - 1]) {
+            const id = w.getBlock(bx, by, bz);
+            if (id !== B.WATER && id !== B.LAVA) {
+                continue;
+            }
+            //Under water: rise to the top of the column
+            let top = by;
+            while (top - by < 8 && w.getBlock(bx, top + 1, bz) === id) {
+                top++;
+            }
+            return { y: top + waterHeight(w.getMeta(bx, top, bz), false), lava: id === B.LAVA };
+        }
+        return null;
+    }
+
+    //input: the rider's movement input (forward, strafe, sneak)
+    update(dt, input) {
+        const g = this.game;
+        if (g.boat && input && input.sneak) {
+            this.dismount();
+        }
+        let rem = Math.min(dt, 0.1);
+        while (rem > 1e-4) {
+            const h = Math.min(rem, 1 / 60);
+            rem -= h;
+            for (let i = this.list.length - 1; i >= 0; i--) {
+                const b = this.list[i];
+                b.hitCd = Math.max(0, b.hitCd - h);
+                this.step(b, h, b.rider ? input : null);
+            }
+            this.collide();
+        }
+        for (const b of this.list) {
+            this.pose(b, dt);
+        }
+    }
+
+    step(b, h, input) {
+        const g = this.game, w = g.world;
+        if (!w.isLoaded(Math.floor(b.x), Math.floor(b.z))) {
+            return;
+        }
+        const s = this.surface(b);
+        if (s && s.lava) {
+            //Burns up with whatever it carried
+            g.sound.play("fizz");
+            this.breakBoat(b, false);
+            return;
+        }
+        b.inWater = !!s;
+        //Float: spring toward the surface, damped; otherwise fall
+        if (s) {
+            b.vy += ((s.y - DRAFT) - b.y) * 40 * h;
+            b.vy *= Math.exp(-6 * h);
+        } else {
+            b.vy -= 28 * h;
+            b.vy = Math.max(b.vy, -40);
+        }
+        //Rowing and turning
+        const fx = -Math.sin(b.yaw), fz = -Math.cos(b.yaw);
+        let along = b.vx * fx + b.vz * fz;
+        let side = b.vx * fz - b.vz * fx;
+        const fwd = input ? input.forward : 0, turn = input ? input.strafe : 0;
+        if (turn) {
+            const d = turn * TURN * h * (b.inWater || !b.onGround ? 1 : 0.5);
+            b.yaw -= d;
+            g.player.yaw -= d;
+        }
+        if (b.inWater) {
+            along += (fwd > 0 ? THRUST : BACK_THRUST) * fwd * h;
+            along = Math.max(-MAX_BACK, Math.min(MAX_FWD, along));
+            along *= Math.exp(-(fwd ? 0.25 : 0.6) * h);
+            side *= Math.exp(-4 * h);
+        } else if (b.onGround) {
+            along += LAND_THRUST * fwd * h;
+            along = Math.max(-LAND_MAX, Math.min(LAND_MAX, along));
+            along *= Math.exp(-5 * h);
+            side *= Math.exp(-8 * h);
+        }
+        b.row = fwd;
+        const nfx = -Math.sin(b.yaw), nfz = -Math.cos(b.yaw);
+        b.vx = nfx * along + nfz * side;
+        b.vz = nfz * along - nfx * side;
+        //Move against the blocks
+        const box = this.box(b);
+        const dy = b.vy * h;
+        const my = moveAxis(w, box, 1, dy);
+        b.onGround = dy < 0 && my > dy + 1e-6;
+        if (my !== dy) {
+            b.vy = 0;
+        }
+        if (Math.abs(moveAxis(w, box, 0, b.vx * h) - b.vx * h) > 1e-6) {
+            b.vx = 0;
+        }
+        if (Math.abs(moveAxis(w, box, 2, b.vz * h) - b.vz * h) > 1e-6) {
+            b.vz = 0;
+        }
+        b.x = (box[0] + box[3]) / 2;
+        b.y = box[1];
+        b.z = (box[2] + box[5]) / 2;
+        //A player walking into an empty boat nudges it
+        const p = g.player;
+        if (!b.rider && g.boat !== b) {
+            const pb = p.box();
+            if (pb[0] < box[3] && pb[3] > box[0] && pb[2] < box[5] && pb[5] > box[2] && pb[1] < box[4] && pb[4] > box[1]) {
+                const dx = b.x - p.x, dz = b.z - p.z, l = Math.hypot(dx, dz) || 1;
+                b.vx += dx / l * 6 * h;
+                b.vz += dz / l * 6 * h;
+            }
+        }
+    }
+
+    //Boats bump into each other
+    collide() {
+        const L = this.list;
+        for (let i = 0; i < L.length; i++) {
+            for (let j = i + 1; j < L.length; j++) {
+                const a = L[i], b = L[j];
+                const dx = b.x - a.x, dz = b.z - a.z, d = Math.hypot(dx, dz);
+                if (d >= 1.1 || d < 1e-4 || Math.abs(a.y - b.y) > 0.6) {
+                    continue;
+                }
+                const nx = dx / d, nz = dz / d;
+                const va = a.vx * nx + a.vz * nz, vb = b.vx * nx + b.vz * nz;
+                if (va - vb > 0) {
+                    a.vx += (vb - va) * nx;
+                    a.vz += (vb - va) * nz;
+                    b.vx += (va - vb) * nx;
+                    b.vz += (va - vb) * nz;
+                }
+                const push = (1.1 - d) / 2;
+                a.x -= nx * push;
+                a.z -= nz * push;
+                b.x += nx * push;
+                b.z += nz * push;
+            }
+        }
+    }
+
+    pose(b, dt) {
+        const g = this.game, m = b.model;
+        b.bob += dt;
+        const bob = b.inWater ? Math.sin(b.bob * 1.8) * 0.025 : 0;
+        const roll = b.inWater ? Math.sin(b.bob * 1.3) * 0.03 : 0;
+        m.group.position.set(b.x, b.y + bob, b.z);
+        m.group.rotation.set(0, b.yaw, 0);
+        m.hull.rotation.set(0, 0, roll);
+        //Oars sweep while rowing, rest along the sides otherwise
+        if (b.row) {
+            b.rowPhase += dt * 6 * Math.sign(b.row);
+            b.splash -= dt;
+            if (b.inWater && b.splash <= 0) {
+                b.splash = Math.PI * 2 / 6;
+                if (b.rider) {
+                    g.sound.play("paddle");
+                }
+            }
+        }
+        for (const o of m.oars) {
+            const sweep = b.row ? Math.sin(b.rowPhase) * 0.7 : 0;
+            const dip = b.row ? 0.35 + Math.cos(b.rowPhase) * 0.2 : 0.55;
+            o.pivot.rotation.set(0, sweep * -o.side + (b.row ? 0 : 0.2 * o.side), dip * o.side, "ZYX");
+        }
+        const w = g.world;
+        const fx = Math.floor(b.x), fy = Math.floor(b.y + 0.6), fz = Math.floor(b.z);
+        const sky = w.getSky(fx, fy, fz) / 15, blk = Math.max(w.getBlockLight(fx, fy, fz), this.renderer.dyn.at(fx, fy, fz)) / 15;
+        const bright = this.renderer.objectLight(sky, blk);
+        for (const mat of m.mats) {
+            mat.color.copy(mat.userData.base).multiplyScalar(bright);
+        }
+    }
+
+    //Where the rider sits
+    seat(b) {
+        return [b.x, b.y - 0.12, b.z];
+    }
+
+    /* ---------------- save data ---------------- */
+
+    serialize() {
+        return this.list.map((b) => ({ x: b.x, y: b.y, z: b.z, vx: b.vx, vz: b.vz, yaw: b.yaw }));
+    }
+
+    load(list) {
+        if (!Array.isArray(list)) {
+            return;
+        }
+        for (const d of list) {
+            if (Number.isFinite(d.x) && Number.isFinite(d.y) && Number.isFinite(d.z)) {
+                this.spawn(d.x, d.y, d.z, d);
+            }
+        }
+    }
+}

+ 70 - 7
src/web/Blocky/js/game.js

@@ -15,6 +15,7 @@ import {
 } from "./blocks.js";
 import { Mobs } from "./mobs.js";
 import { Carts } from "./carts.js";
+import { Boats } from "./boats.js";
 import { Signs } from "./signs.js";
 import { World, WORLD_H } from "./world.js";
 import { BIOME, BIOME_NAMES } from "./worldgen.js";
@@ -117,8 +118,10 @@ export class Game {
         this.entities = new Entities(this.renderer);
         this.mobs = new Mobs(this);
         this.carts = new Carts(this);
+        this.boats = new Boats(this);
         this.signs = new Signs(this);
         this.riding = null;
+        this.boat = null;
         this.player = new Player(this.world);
         this.inventory = new Inventory();
         this.player.creative = meta.mode === "creative";
@@ -188,6 +191,7 @@ export class Game {
         }
         this.entities.load(state.entities);
         this.carts.load(state.carts);
+        this.boats.load(state.boats);
         this.ui.hudKey = "";
     }
 
@@ -210,6 +214,7 @@ export class Game {
         this.entities.clear();
         this.mobs.clear();
         this.carts.clear();
+        this.boats.clear();
         this.signs.clear();
         this.renderer.detachWorld();
         this.renderer.setHeldItem(0);
@@ -263,8 +268,8 @@ export class Game {
             }
             if (this.sleeping) {
                 this.updateSleep(dt);
-            } else if (this.riding) {
-                //The cart carries the player (carts.update below)
+            } else if (this.riding || this.boat) {
+                //The cart or boat carries the player (updates below)
             } else if (ready && !this.placeOnGround) {
                 const mv = this.movementInput();
                 let rem = Math.min(dt, 0.1);
@@ -287,6 +292,14 @@ export class Game {
                 p.vx = p.vy = p.vz = 0;
                 p.fallDistance = 0;
             }
+            this.boats.update(dt, this.boat ? this.movementInput() : null);
+            if (this.boat) {
+                [p.x, p.y, p.z] = this.boats.seat(this.boat);
+                p.vx = p.vy = p.vz = 0;
+                p.fallDistance = 0;
+                p.sense();
+                p.onGround = true;
+            }
             r.updateParticles(dt, this.world);
             r.updateLightning(dt);
             this.updateWeather(dt);
@@ -598,9 +611,12 @@ export class Game {
         //sticks out a little in front of the cart's lower half
         const onRail = cartHit && hit && SHAPE[hit.id] === "rail" && cartHit.t < hit.t + 1;
         const aimCart = (near(cartHit) || onRail) && (!mobHit || cartHit.t < mobHit.t) ? cartHit.cart : null;
-        const aimMob = !aimCart && near(mobHit) ? mobHit.mob : null;
+        //Boats sit in water, which the block ray passes through
+        const boatHit = aimCart ? null : this.boats.raycast(ox, oy, oz, dx, dy, dz, reach);
+        const aimBoat = near(boatHit) && (!mobHit || boatHit.t < mobHit.t) ? boatHit.boat : null;
+        const aimMob = !aimCart && !aimBoat && near(mobHit) ? mobHit.mob : null;
         this.aimMob = aimMob;
-        r.setSelection(this.hideHud || aimMob || aimCart ? null : hit);
+        r.setSelection(this.hideHud || aimMob || aimCart || aimBoat ? null : hit);
 
         const leftDown = input.buttons.has(0) || this.touchBreak;
         const rightDown = input.buttons.has(2);
@@ -624,16 +640,20 @@ export class Game {
             this.swing = 0;
             this.carts.hit(aimCart);
         }
+        if (aimBoat && input.clicked.has(0)) {
+            this.swing = 0;
+            this.boats.hit(aimBoat);
+        }
         //After hitting a cart or an animal, holding the button on mines
         //nothing until it is let go (the block behind would break next)
-        if ((aimCart || aimMob) && leftDown) {
+        if ((aimCart || aimBoat || aimMob) && leftDown) {
             this.attackHold = true;
         } else if (!leftDown) {
             this.attackHold = false;
         }
 
         //Mining
-        if (leftDown && !aimMob && !aimCart && !this.attackHold) {
+        if (leftDown && !aimMob && !aimCart && !aimBoat && !this.attackHold) {
             if (input.clicked.has(0) || this.swing >= 1) {
                 this.swing = 0;
             }
@@ -694,6 +714,12 @@ export class Game {
             this.useHold = true;
             return;
         }
+        if (aimBoat && rightClick) {
+            this.swing = 0;
+            this.boats.use(aimBoat);
+            this.useHold = true;
+            return;
+        }
         if (this.useHold) {
             return;
         }
@@ -949,6 +975,10 @@ export class Game {
             }
             return;
         }
+        if (it && it.boat) {
+            this.placeBoat();
+            return;
+        }
         if (!rawHit || !held) {
             return;
         }
@@ -1043,6 +1073,37 @@ export class Game {
         }
     }
 
+    //A boat goes onto the water (or ground) under the crosshair, facing the
+    //way the player looks
+    placeBoat() {
+        const w = this.world, p = this.player;
+        const [ox, oy, oz, dx, dy, dz] = this.eyeRay();
+        const h = w.raycast(ox, oy, oz, dx, dy, dz, 4.5, (bid, m, bx, by, bz) =>
+            (bid === B.WATER ? [0, 0, 0, 1, 0.875, 1] : selectionBox(bid, m, w, bx, by, bz)));
+        if (!h) {
+            return;
+        }
+        let x = h.point[0], y, z = h.point[2];
+        if (h.id === B.WATER || h.face === 2) {
+            y = h.point[1];
+        } else {
+            const [tx, ty, tz] = this.placeTarget(h);
+            x = tx + 0.5;
+            y = ty;
+            z = tz + 0.5;
+        }
+        if (y < 0 || y >= WORLD_H) {
+            return;
+        }
+        this.boats.spawn(x, y, z, { yaw: p.yaw });
+        this.sound.block("place", "wood");
+        this.swing = 0;
+        this.useHold = true;
+        if (!this.creative) {
+            this.inventory.consumeHeld(1);
+        }
+    }
+
     placeTarget(hit) {
         const def = BLOCKS[hit.id];
         if (def.replaceable && hit.id !== B.WATER && hit.id !== B.LAVA) {
@@ -1497,6 +1558,7 @@ export class Game {
             } else if (e.type === "death") {
                 this.wake(false);
                 this.carts.dismount();
+                this.boats.dismount();
                 this.onDeath(e.cause);
             } else if (e.type === "extinguish") {
                 this.sound.play("fizz");
@@ -1653,7 +1715,8 @@ export class Game {
             inventory: this.inventory.serialize(),
             world: this.world.serialize(),
             entities: this.entities.serialize(),
-            carts: this.carts.serialize()
+            carts: this.carts.serialize(),
+            boats: this.boats.serialize()
         };
     }
 

+ 23 - 1
src/web/Blocky/js/textures.js

@@ -1377,6 +1377,27 @@ function paintMinecart(t, cargo) {
     t.outline(0.7);
 }
 
+//Boat item: a wooden hull seen from the side with an oar across it
+function paintBoatItem(t) {
+    t.clear();
+    for (let x = 3; x < 13; x++) {
+        t.set(x, 2 + Math.abs(x - 7.5) * 0.25 | 0, [84, 60, 34]);
+    }
+    for (let i = 0; i < 9; i++) {
+        t.set(4 + i, 1 + i, [130, 96, 56]);
+    }
+    t.rect(10, 6, 3, 2, [96, 70, 40]);
+    for (let y = 8; y < 13; y++) {
+        const inset = Math.max(0, y - 10);
+        for (let x = 1 + inset; x < 15 - inset; x++) {
+            const seam = y === 10;
+            const rim = y === 8;
+            t.set(x, y, rim ? [214, 176, 116] : seam ? [118, 88, 50] : t.vary([176, 136, 80], 6));
+        }
+    }
+    t.outline(0.65);
+}
+
 function paintIngot(t, base) {
     t.clear();
     const light = [Math.min(255, base[0] + 45), Math.min(255, base[1] + 45), Math.min(255, base[2] + 45)];
@@ -1792,7 +1813,8 @@ Object.assign(PAINTERS, {
     retimer_pin_on: (t) => t.fill([255, 60, 44], 14),
     minecart: (t) => paintMinecart(t, null),
     chest_minecart: (t) => paintMinecart(t, "chest"),
-    furnace_minecart: (t) => paintMinecart(t, "furnace")
+    furnace_minecart: (t) => paintMinecart(t, "furnace"),
+    boat_item: paintBoatItem
 });
 
 //Coloured wool blocks and the dyes that make them (colours as in blocks.js)