Ver Fonte

Add more game mechanics

Toby Chui há 1 semana atrás
pai
commit
7bdb802c63

+ 24 - 4
src/web/Blocky/README.md

@@ -407,9 +407,22 @@ A cart touching a cactus (its box within 0.02 of the cactus's inset box,
 running into a cactus at the end of a track, never one passing a cactus
 beside the track (0.07 apart). `breakOnCactus` drops the item, and a chest
 cart's contents, in the middle of the block the cart was in, clear of the
-cactus, which destroys any dropped item touching it (its top included)
-like lava and fire do. Touching a cactus hurts the player half a heart a
-second.
+cactus, which destroys any dropped item touching it (its top included).
+Touching a cactus hurts the player half a heart a second.
+
+A cart whose middle reaches lava (`inLava`, also a cart lava flows over) is
+wrecked by `breakInLava`, in creative too, and a rider is put out first.
+Its item and a chest cart's contents are thrown up from the middle of the
+block, in an arc that lands in that same block and out of the player's
+reach until then (`LAVA_NO_PICKUP`), so they fall back in and burn up.
+
+A dropped item in lava or fire burns up over `BURN_TIME` (1.4 s,
+`Entities.startBurn` / `burnStep`): it stops, sinks slowly, shrinks and chars
+under two crossed sheets of the fire tile (`Renderer.makeFlameMesh`) with
+embers and smoke, while the `incinerate` sound plays (hiss and crackle in
+audio.js, one per `BURN_SOUND_GAP` so a chest load is not deafening). A
+burning item can no longer be picked up or collected by a feeder. It is not
+saved as burning: on load it simply starts again.
 
 ## Feeders and dispensers
 
@@ -433,7 +446,14 @@ in that block.
 Dispenser (`B.DISPENSER` 103; CCC / CBC / CDC, cobblestone, a bow, spark
 dust): faces any way (placed like a piston, front looking back at the
 player), meta bit 8 while powered. Each time power arrives (`powerTick`
-calls `world.onDispense`) it takes a random filled slot and
+calls `world.onDispense`) it acts on what is in front. Storage there (a
+chest, a chest cart in that block, another dispenser or a feeder) gets one
+item, of any kind, from a random slot that fits (`passOn`); when it is full
+the item stays and the dispenser just clicks. A furnace does not count (only
+feeders fill one) and neither do crafting tables and the like, so a dispenser
+in front of those throws as usual. A column of dispensers facing up with a
+torch tower beside it, toggled on and off, lifts items one block at a time.
+With no storage in front it takes a random filled slot and
 - shoots an arrow (22 blocks/s; dispensed arrows hurt the player too,
   `hitsPlayer` in mobs.js)
 - sets a cart on a rail in front (or one block lower), turned along the

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

@@ -281,11 +281,45 @@ export class Sound {
             case "fizz":
                 this.noiseBurst("highpass", 4000, 0.5, 0.3, 0.2);
                 break;
+            case "incinerate":
+                this.incinerate(opts.dur || 1.4, opts.distance || 0);
+                break;
             default:
                 break;
         }
     }
 
+    //Something burning up: a soft whoomph, a hiss that swells and dies with
+    //the flame and fat crackling on top. Fades out by 28 blocks.
+    incinerate(dur, distance) {
+        const near = Math.max(0, 1 - distance / 28);
+        if (near <= 0) {
+            return;
+        }
+        const ctx = this.ctx, t = ctx.currentTime;
+        this.noiseBurst("lowpass", 320, 0.7, 0.45, 0.5 * near, 0, this.brown);
+        const src = ctx.createBufferSource();
+        src.buffer = this.noise;
+        src.loop = true;
+        const f = ctx.createBiquadFilter();
+        f.type = "highpass";
+        f.Q.value = 0.6;
+        f.frequency.setValueAtTime(2200, t);
+        f.frequency.exponentialRampToValueAtTime(5200, t + dur);
+        const g = ctx.createGain();
+        g.gain.setValueAtTime(0.0001, t);
+        g.gain.exponentialRampToValueAtTime(0.2 * near, t + 0.12);
+        g.gain.setValueAtTime(0.2 * near, t + dur * 0.45);
+        g.gain.exponentialRampToValueAtTime(0.0001, t + dur);
+        src.connect(f).connect(g).connect(this.master);
+        src.start(t, Math.random() * 1.5);
+        src.stop(t + dur + 0.05);
+        for (let i = 0; i < 12; i++) {
+            const when = Math.random() * dur * 0.9;
+            this.noiseBurst("bandpass", 1800 + Math.random() * 3200, 3, 0.02 + Math.random() * 0.03, (0.12 + Math.random() * 0.2) * near, when);
+        }
+    }
+
     startRain() {
         const ctx = this.ctx;
         const src = ctx.createBufferSource();

+ 43 - 8
src/web/Blocky/js/carts.js

@@ -10,7 +10,8 @@
     Kinds: "cart" (right click to ride, W pushes, Shift to get out), "chest"
     (right click for 27 slots of storage) and "furnace" (right click with
     fuel to drive it away from you while the fuel lasts). Carts are saved
-    with the world.
+    with the world. Lava wrecks a cart: its item and load are thrown up out
+    of the lava, fall back in and burn up.
 */
 
 import * as THREE from "three";
@@ -57,6 +58,9 @@ const SOME_STACKS = 5;
 //Pace of a cart a player pushes along an unpowered powered rail
 const BRAKE_PUSH = 1.5;
 const HALF = 0.49;
+//How long the items of a cart wrecked in lava stay out of the player's
+//reach: longer than they fly, so they always go back in
+const LAVA_NO_PICKUP = 2;
 //Off the rails on the ground: speed falls by GROUND_DRAG per second
 //(exponential) plus a steady GROUND_STOP, so it comes to a real stop
 const GROUND_DRAG = 5;
@@ -242,8 +246,8 @@ export class Carts {
         }
     }
 
-    //at: where the items drop and how they fly ({ x, y, z, vel }), else
-    //from the cart, scattered
+    //at: where the items drop, how each flies and how long it cannot be
+    //picked up ({ x, y, z, vel(), delay }), else from the cart, scattered
     breakCart(c, drop, at = null) {
         const g = this.game;
         if (c.rider) {
@@ -251,11 +255,12 @@ export class Carts {
         }
         if (drop) {
             const [x, y, z] = at ? [at.x, at.y, at.z] : [c.x, c.y + 0.5, c.z];
-            const vel = () => (at ? at.vel.slice() : null);
-            g.entities.spawnItem(x, y, z, CART_ITEM[c.kind], 1, 0, vel());
+            const vel = () => (at ? at.vel() : null);
+            const delay = at ? at.delay : undefined;
+            g.entities.spawnItem(x, y, z, CART_ITEM[c.kind], 1, 0, vel(), delay);
             for (const s of c.slots || []) {
                 if (s) {
-                    g.entities.spawnItem(x, y, z, s.id, s.count, s.dmg || 0, vel());
+                    g.entities.spawnItem(x, y, z, s.id, s.count, s.dmg || 0, vel(), delay);
                 }
             }
         }
@@ -268,7 +273,34 @@ export class Carts {
     //rail beside the cactus), clear of the cactus so it does not destroy
     //them as well, and where a feeder under that rail takes them in
     breakOnCactus(c) {
-        this.breakCart(c, true, { x: Math.floor(c.x) + 0.5, y: c.y + 0.3, z: Math.floor(c.z) + 0.5, vel: [0, 1, 0] });
+        this.breakCart(c, true, { x: Math.floor(c.x) + 0.5, y: c.y + 0.3, z: Math.floor(c.z) + 0.5, vel: () => [0, 1, 0] });
+    }
+
+    inLava(c) {
+        return this.game.world.getBlock(Math.floor(c.x), Math.floor(c.y + 0.1), Math.floor(c.z)) === B.LAVA;
+    }
+
+    //The items are thrown straight up from the middle of the block, in a
+    //small enough arc to come down in the same block of lava. Under a roof,
+    //or deep in a lava fall, they burn where the cart was.
+    breakInLava(c) {
+        const g = this.game, w = g.world;
+        const x = Math.floor(c.x), z = Math.floor(c.z);
+        const cell = Math.floor(c.y + 0.1);
+        let top = cell;
+        while (top < cell + 2 && w.getBlock(x, top + 1, z) === B.LAVA) {
+            top++;
+        }
+        const above = w.getBlock(x, top + 1, z);
+        const open = above !== B.LAVA && !BLOCKS[above].solid;
+        const y = open ? top + 1.15 : c.y + 0.5;
+        g.renderer.spawnParticles(x + 0.5, top + 1, z + 0.5, [255, 120, 30], 24, 0.4, 4);
+        g.renderer.spawnParticles(x + 0.5, top + 1, z + 0.5, [255, 190, 70], 12, 0.35, 1.5, 1, -6);
+        g.sound.play("fizz");
+        this.breakCart(c, true, {
+            x: x + 0.5, y, z: z + 0.5, delay: LAVA_NO_PICKUP,
+            vel: () => [(Math.random() - 0.5) * 1.2, 5.5 + Math.random() * 2, (Math.random() - 0.5) * 1.2]
+        });
     }
 
     //Right click: ride, open the chest, or fuel the furnace
@@ -345,10 +377,13 @@ export class Carts {
                 c.hitCd = Math.max(0, c.hitCd - h);
                 this.step(c, h, input);
             }
-            //A cart running into a cactus breaks into its item
+            //A cart running into a cactus breaks into its item, one in lava
+            //is wrecked
             for (const c of this.list.slice()) {
                 if (touchingBlock(g.world, this.box(c), B.CACTUS, 0.02)) {
                     this.breakOnCactus(c);
+                } else if (this.inLava(c)) {
+                    this.breakInLava(c);
                 }
             }
             this.collide();

+ 90 - 8
src/web/Blocky/js/entities.js

@@ -3,26 +3,41 @@
 
     Dropped items (small spinning blocks or flat sprites that the player
     picks up) and falling blocks (sand and gravel that lost their support).
-    Lava, fire and cactus destroy dropped items (a cactus on touch, top
-    included).
+    A cactus destroys dropped items on touch (top included). Lava and fire
+    burn them up: the item stops, sinks and chars under a flame for BURN_TIME
+    seconds, out of reach of the player and of feeders, and is gone.
 */
 
 import { B, BLOCKS } from "./blocks.js";
 import { moveAxis, touchingBlock } from "./physics.js";
+import { waterHeight } from "./shapes.js";
 
 export const ITEM_HALF = 0.125;
 const DESPAWN = 300;
+//How long an item takes to burn up, how fast it sinks meanwhile, and the
+//least time between two burn sounds (a chest cart's load lands all at once)
+const BURN_TIME = 1.4;
+const BURN_SINK = 0.2;
+const BURN_SOUND_GAP = 0.15;
 
 export class Entities {
     constructor(renderer) {
         this.renderer = renderer;
         this.items = [];
         this.falling = [];
+        this.burnSoundCd = 0;
+    }
+
+    disposeItem(e) {
+        this.renderer.disposeItemMesh(e.mesh);
+        if (e.flame) {
+            this.renderer.disposeItemMesh(e.flame);
+        }
     }
 
     clear() {
         for (const e of this.items) {
-            this.renderer.disposeItemMesh(e.mesh);
+            this.disposeItem(e);
         }
         for (const e of this.falling) {
             this.renderer.disposeItemMesh(e.mesh);
@@ -33,8 +48,7 @@ export class Entities {
 
     spawnItem(x, y, z, id, count, dmg = 0, vel = null, pickupDelay = 0.5) {
         if (this.items.length > 300) {
-            const old = this.items.shift();
-            this.renderer.disposeItemMesh(old.mesh);
+            this.disposeItem(this.items.shift());
         }
         const mesh = this.renderer.makeItemMesh(id);
         mesh.scale.setScalar(mesh.userData.cube ? 0.25 : 0.42);
@@ -53,17 +67,85 @@ export class Entities {
         this.falling.push({ x: x + 0.5, y: y, z: z + 0.5, vy: 0, id, meta, mesh, age: 0 });
     }
 
-    update(dt, world, player, inventory, onPickup) {
+    //An item has just come to lava or fire
+    startBurn(e, here, world, onBurn) {
         const r = this.renderer;
+        const x = Math.floor(e.x), y = Math.floor(e.y), z = Math.floor(e.z);
+        e.burn = 0;
+        e.vx = e.vy = e.vz = 0;
+        e.spark = 0;
+        e.size = e.mesh.scale.x;
+        //The flame stands on the lava's surface, or where the item is in a fire
+        e.burnY = here === B.LAVA ? y + waterHeight(world.getMeta(x, y, z), world.getBlock(x, y + 1, z) === B.LAVA) : e.y - ITEM_HALF;
+        e.flame = r.makeFlameMesh();
+        e.flame.rotation.y = Math.random() * Math.PI;
+        r.scene.add(e.flame);
+        r.spawnParticles(e.x, e.burnY + 0.05, e.z, [255, 130, 40], 8, 0.1, 2.5);
+        if (this.burnSoundCd <= 0 && onBurn) {
+            this.burnSoundCd = BURN_SOUND_GAP;
+            onBurn(e, BURN_TIME);
+        }
+    }
+
+    //One frame of burning. Returns true once the item has burnt up.
+    burnStep(e, dt, world) {
+        const r = this.renderer;
+        e.burn += dt;
+        const k = e.burn / BURN_TIME;
+        if (k >= 1) {
+            r.spawnParticles(e.x, e.burnY + 0.15, e.z, [70, 70, 74], 7, 0.12, 0.9, 1, -3);
+            r.spawnParticles(e.x, e.burnY + 0.1, e.z, [255, 170, 60], 5, 0.1, 2);
+            return true;
+        }
+        const sunk = e.y - BURN_SINK * dt;
+        if (!BLOCKS[world.getBlock(Math.floor(e.x), Math.floor(sunk - ITEM_HALF), Math.floor(e.z))].solid) {
+            e.y = sunk;
+        }
+        e.spin += dt * 0.8;
+        e.mesh.position.set(e.x, e.y + 0.04, e.z);
+        e.mesh.rotation.y = e.spin;
+        e.mesh.scale.setScalar(e.size * (1 - 0.55 * k));
+        r.setMeshBrightness(e.mesh, 1 - 0.88 * k);
+        //Flares up, then dies down with the last of the item
+        const f = Math.max(0.01, Math.min(1, k * 5, (1 - k) * 4));
+        e.flame.scale.set(0.62 * f, 0.8 * f, 0.62 * f);
+        e.flame.position.set(e.x, e.burnY + 0.37 * f, e.z);
+        e.spark -= dt;
+        if (e.spark <= 0) {
+            e.spark = 0.07;
+            const hot = Math.random() < 0.5 ? [255, 190, 70] : [255, 100, 25];
+            r.spawnParticles(e.x, e.burnY + 0.12, e.z, hot, 2, 0.13, 1.1, 1, -6);
+            if (k > 0.4) {
+                r.spawnParticles(e.x, e.burnY + 0.35, e.z, [60, 60, 64], 1, 0.1, 0.8, 1, -3);
+            }
+        }
+        return false;
+    }
+
+    //onBurn(item, seconds): an item has started to burn (not called for
+    //every item of a batch landing together)
+    update(dt, world, player, inventory, onPickup, onBurn) {
+        const r = this.renderer;
+        this.burnSoundCd = Math.max(0, this.burnSoundCd - dt);
         for (let i = this.items.length - 1; i >= 0; i--) {
             const e = this.items[i];
             e.age += dt;
             const here = world.getBlock(Math.floor(e.x), Math.floor(e.y), Math.floor(e.z));
-            if (e.age > DESPAWN || here === B.LAVA || here === B.FIRE) {
-                r.disposeItemMesh(e.mesh);
+            if (e.age > DESPAWN) {
+                this.disposeItem(e);
                 this.items.splice(i, 1);
                 continue;
             }
+            if (e.burn === undefined && (here === B.LAVA || here === B.FIRE)) {
+                this.startBurn(e, here, world, onBurn);
+            }
+            if (e.burn !== undefined) {
+                if (this.burnStep(e, dt, world)) {
+                    this.disposeItem(e);
+                    this.items.splice(i, 1);
+                }
+                continue;
+            }
             //Attraction to the player
             const px = player.x, py = player.y + 0.8, pz = player.z;
             const dx = px - e.x, dy = py - e.y, dz = pz - e.z;

+ 2 - 1
src/web/Blocky/js/game.js

@@ -289,7 +289,8 @@ export class Game {
             }
             this.handlePlayerEvents();
             this.updateInteraction(dt);
-            this.entities.update(dt, this.world, p, this.inventory, (id, count) => this.onPickup(id, count));
+            this.entities.update(dt, this.world, p, this.inventory, (id, count) => this.onPickup(id, count),
+                (e, dur) => this.sound.play("incinerate", { dur, distance: Math.hypot(e.x - p.x, e.y - p.y, e.z - p.z) }));
             this.mobs.update(dt);
             this.carts.update(dt, this.riding ? this.movementInput() : null);
             if (this.riding) {

+ 30 - 4
src/web/Blocky/js/machines.js

@@ -15,7 +15,10 @@
       the block above it (a rail laid on the feeder), moving or not
 
     A dispenser (9 slots) fires once each time power arrives (power.js
-    calls world.onDispense): it picks a random filled slot and
+    calls world.onDispense). With storage right in front (a chest, a chest
+    cart, another dispenser or a feeder, not a furnace) it moves one item
+    into it, any kind of item, and keeps it when the storage is full.
+    Otherwise it picks a random filled slot and
     - shoots an arrow
     - sets a cart on a rail in front of it (or one block lower), turned
       along the rail away from it
@@ -165,7 +168,7 @@ export class Machines {
         const mine = range(te.slots.length);
         for (let i = items.length - 1; i >= 0; i--) {
             const e = items[i];
-            if (e.x < x || e.x >= x + 1 || e.z < z || e.z >= z + 1 || e.y < y + 10 / 16 || e.y >= y + 2) {
+            if (e.burn !== undefined || e.x < x || e.x >= x + 1 || e.z < z || e.z >= z + 1 || e.y < y + 10 / 16 || e.y >= y + 2) {
                 continue;
             }
             let n = e.count;
@@ -183,6 +186,26 @@ export class Machines {
 
     /* ---------------- dispensers ---------------- */
 
+    //One item into the storage in front, from a random slot that fits.
+    //True when there is storage there, whether or not anything fitted.
+    passOn(te, full, x, y, z) {
+        const to = this.containerAt(x, y, z);
+        if (!to || to.kind === "furnace") {
+            return false;
+        }
+        const all = range(to.slots.length);
+        const start = Math.floor(Math.random() * full.length);
+        for (let k = 0; k < full.length; k++) {
+            const i = full[(start + k) % full.length];
+            if (insertOne(to.slots, all, te.slots[i])) {
+                takeOne(te.slots, i);
+                break;
+            }
+        }
+        this.game.sound.play("click");
+        return true;
+    }
+
     dispense(x, y, z, face) {
         const g = this.game, w = g.world;
         const te = w.getTileEntity(x, y, z) || w.createTileEntity(x, y, z, "dispenser");
@@ -191,10 +214,13 @@ export class Machines {
             g.sound.play("click");
             return;
         }
-        const i = full[Math.floor(Math.random() * full.length)];
-        const s = te.slots[i];
         const d = FACE_DIR[face];
         const fx = x + d[0], fy = y + d[1], fz = z + d[2];
+        if (this.passOn(te, full, fx, fy, fz)) {
+            return;
+        }
+        const i = full[Math.floor(Math.random() * full.length)];
+        const s = te.slots[i];
         const front = w.getBlock(fx, fy, fz);
         //Just outside the mouth
         const ox = x + 0.5 + d[0] * 0.7, oy = y + 0.5 + d[1] * 0.7 - (d[1] === 0 ? 0.15 : 0), oz = z + 0.5 + d[2] * 0.7;

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

@@ -31,6 +31,8 @@ export const DIFFICULTY = [
 ];
 
 const HALF_W = 0.3, HEIGHT = 1.8, EYE = 1.62, STEP = 0.6;
+//Lava kills from full health on the 7th hit, just under 4 seconds in
+const LAVA_DAMAGE = 3, LAVA_INTERVAL = 0.6;
 
 export class Player {
     constructor(world) {
@@ -456,8 +458,8 @@ export class Player {
         if (this.inLava) {
             this.burnTime = 8;
             if (this.fireTimer === 0) {
-                this.fireTimer = 0.5;
-                this.hurt(4, "lava");
+                this.fireTimer = LAVA_INTERVAL;
+                this.hurt(LAVA_DAMAGE, "lava");
             }
         } else if (this.touching(B.FIRE)) {
             this.burnTime = Math.max(this.burnTime, 4);

+ 26 - 2
src/web/Blocky/js/render.js

@@ -768,6 +768,7 @@ export class Renderer {
         this.pCol = new Float32Array(this.pMax * 3);
         this.pVel = new Float32Array(this.pMax * 3);
         this.pLife = new Float32Array(this.pMax);
+        this.pGrav = new Float32Array(this.pMax);
         this.pCount = 0;
         const g = new THREE.BufferGeometry();
         g.setAttribute("position", new THREE.BufferAttribute(this.pPos, 3));
@@ -777,7 +778,8 @@ export class Renderer {
         this.scene.add(this.particles);
     }
 
-    spawnParticles(x, y, z, color, count, spread = 0.45, speed = 3, brightness = 1) {
+    //gravity: blocks / s^2 downward; below zero the particles rise (flames, smoke)
+    spawnParticles(x, y, z, color, count, spread = 0.45, speed = 3, brightness = 1, gravity = 16) {
         for (let i = 0; i < count; i++) {
             if (this.pCount >= this.pMax) {
                 return;
@@ -794,6 +796,7 @@ export class Renderer {
             this.pCol[k * 3 + 1] = color[1] * v;
             this.pCol[k * 3 + 2] = color[2] * v;
             this.pLife[k] = 0.5 + Math.random() * 0.6;
+            this.pGrav[k] = gravity;
         }
     }
 
@@ -804,6 +807,7 @@ export class Renderer {
             if (this.pLife[i] <= 0) {
                 n--;
                 this.pLife[i] = this.pLife[n];
+                this.pGrav[i] = this.pGrav[n];
                 for (let a = 0; a < 3; a++) {
                     this.pPos[i * 3 + a] = this.pPos[n * 3 + a];
                     this.pVel[i * 3 + a] = this.pVel[n * 3 + a];
@@ -812,7 +816,7 @@ export class Renderer {
                 i--;
                 continue;
             }
-            this.pVel[i * 3 + 1] -= 16 * dt;
+            this.pVel[i * 3 + 1] -= this.pGrav[i] * dt;
             const nx = this.pPos[i * 3] + this.pVel[i * 3] * dt;
             const ny = this.pPos[i * 3 + 1] + this.pVel[i * 3 + 1] * dt;
             const nz = this.pPos[i * 3 + 2] + this.pVel[i * 3 + 2] * dt;
@@ -868,6 +872,26 @@ export class Renderer {
         return mesh;
     }
 
+    //Two crossed sheets of the fire tile around a burning item, always bright.
+    //Goes away with disposeItemMesh.
+    makeFlameMesh() {
+        const entry = this.cardGeometry("fire");
+        const mat = this.terrainMaterial({ alphaTest: 0.3, override: true });
+        const mesh = new THREE.Mesh(entry.geometry, mat);
+        const cross = new THREE.Mesh(entry.geometry, mat);
+        cross.rotation.y = Math.PI / 2;
+        mesh.add(cross);
+        mesh.layers.enable(1);
+        cross.layers.enable(1);
+        this.setMeshBrightness(mesh, 1);
+        return mesh;
+    }
+
+    //A fixed brightness instead of the light around it
+    setMeshBrightness(mesh, v) {
+        mesh.material.uniforms.uLightOverride.value.set(2, v, 0);
+    }
+
     setMeshLight(mesh, sky, blk) {
         const v = mesh.material.uniforms.uLightOverride.value;
         if (this.real) {