// Cupcake Village: the first island. Pure data generation + prop meshes. import * as THREE from 'three'; import { B, VoxelWorld } from './voxels'; import { mulberry32 } from '../core/util'; import { softMaterial, furMaterial, furSphere } from '../render/fur'; import type { Bouncer } from '../physics/body'; export interface Level { world: VoxelWorld; group: THREE.Group; bouncers: Bouncer[]; trampolineMeshes: { mesh: THREE.Object3D; b: Bouncer }[]; candySpots: THREE.Vector3[]; catSpots: THREE.Vector3[]; hideSpots: THREE.Vector3[]; // giant cupcakes a cat can hide in spawn: THREE.Vector3; princessSpot: THREE.Vector3; pedestal: THREE.Vector3; castle: THREE.Vector3; animated: ((t: number, dt: number) => void)[]; /** Candy trees: the game hides any tree that comes between the camera and the player. */ trees: { pos: THREE.Vector3; crown: THREE.Group; idx: number; hidden: boolean }[]; trunks: THREE.InstancedMesh; /** Giant cupcakes: hidden like trees when they block the view. */ cups: THREE.Object3D[]; } export function buildVillage(seed = 7): Level { const SX = 76, SY = 18, SZ = 76; const w = new VoxelWorld(SX, SY, SZ); const r = mulberry32(seed); const group = new THREE.Group(); const cx = SX / 2, cz = SZ / 2; const heights: number[][] = []; // rolling hills: sum of soft bumps, clipped by an island mask const bumps = Array.from({ length: 14 }, () => ({ x: (r() - 0.5) * 56, z: (r() - 0.5) * 56, h: 1 + r() * 3.2, s: 5 + r() * 7 })); for (let i = 0; i < SX; i++) { heights[i] = []; for (let k = 0; k < SZ; k++) { const x = i - cx + 0.5, z = k - cz + 0.5; const d = Math.hypot(x, z * 1.05) + Math.sin(Math.atan2(z, x) * 5) * 1.8; let h = 0; if (d < 33) { h = 3; for (const b of bumps) { const q = Math.hypot(x - b.x, z - b.z) / b.s; if (q < 1) h += b.h * (1 - q * q) * (1 - q * q); } // keep the centre plaza flat and low; terraces become 1-block steps const plaza = Math.hypot(x, z - 4); if (plaza < 9) h = 3; h = Math.round(h); if (d > 30) h = Math.min(h, 2); } heights[i][k] = h; } } // castle plateau to the north const castle = new THREE.Vector3(0, 0, -21); for (let i = 0; i < SX; i++) for (let k = 0; k < SZ; k++) { const x = i - cx + 0.5, z = k - cz + 0.5; if (Math.abs(x) < 9 && z > -28 && z < -15) heights[i][k] = Math.max(heights[i][k], 5); } for (let i = 0; i < SX; i++) for (let k = 0; k < SZ; k++) { const h = heights[i][k]; if (!h) continue; const x = i - cx + 0.5, z = k - cz + 0.5; let top = B.GRASS; const n = Math.sin(x * 0.35) + Math.cos(z * 0.3); if (h >= 6) top = B.PINK; else if (n > 1.3) top = B.MINT; if (Math.hypot(x, z - 4) < 8.5) top = (Math.floor(x) + Math.floor(z)) % 2 ? B.PATH : B.SUGAR; if (Math.abs(x) < 1.6 && z > -15 && z < -4) top = B.PATH; // path to castle if (Math.abs(x) < 9 && z > -28 && z < -15) top = B.SUGAR; w.fillColumn(i, k, h, top, h <= 2 ? B.COOKIE : B.COOKIE); } // ramp of steps from plaza up to the castle plateau for (let s = 0; s < 3; s++) for (let x = -2; x <= 1; x++) { const zi = Math.floor(-15 + s) + cz; w.fillColumn(x + cx, zi, 5 - s, B.PATH); } // castle: walls, gate, towers const ci = (x: number) => Math.floor(x + cx), ck = (z: number) => Math.floor(z + cz); for (let x = -7; x <= 7; x++) for (const z of [-26, -18]) for (let y = 5; y < 8; y++) if (!(z === -18 && Math.abs(x) < 2)) w.set(ci(x), y, ck(z), B.CASTLE); for (let z = -26; z <= -18; z++) for (const x of [-7, 7]) for (let y = 5; y < 8; y++) w.set(ci(x), y, ck(z), B.CASTLE); for (let x = -7; x <= 7; x += 2) for (const z of [-26, -18]) if (!(z === -18 && Math.abs(x) < 2)) w.set(ci(x), 8, ck(z), B.CASTLE); // keep inside w.box(ci(-3), 5, ck(-25), ci(3), 9, ck(-22), B.CASTLE); w.box(ci(-2), 10, ck(-24), ci(2), 10, ck(-23), B.BERRY); // a few block structures: cupcake houses const houses: [number, number, number][] = [[-14, 10, B.BERRY], [14, 12, B.LEMON], [-18, -4, B.MINT], [17, -6, B.BERRY], [6, 22, B.LEMON]]; for (const [hx, hz, roof] of houses) { const g = w.topAt(hx, hz); const base = Math.max(1, g); w.box(ci(hx - 2), base, ck(hz - 2), ci(hx + 2), base + 2, ck(hz + 2), B.SUGAR); w.box(ci(hx - 1), base, ck(hz + 2), ci(hx), base + 1, ck(hz + 2), 0); // door w.box(ci(hx - 2), base + 3, ck(hz - 2), ci(hx + 2), base + 3, ck(hz + 2), roof); w.box(ci(hx - 1), base + 4, ck(hz - 1), ci(hx + 1), base + 4, ck(hz + 1), roof); } // stepping-stone tower with candy on top (jump practice) const steps: [number, number, number][] = [[-6, 14, 4], [-4, 16, 5], [-2, 18, 6], [0, 20, 7]]; for (const [x, z, h] of steps) w.box(ci(x), 0, ck(z), ci(x), h - 1, ck(z), B.LEMON); const mesh = w.buildMesh(); group.add(mesh); // --- props --------------------------------------------------------------- const animated: ((t: number, dt: number) => void)[] = []; const hideSpots: THREE.Vector3[] = []; const bouncers: Bouncer[] = []; const trampolineMeshes: { mesh: THREE.Object3D; b: Bouncer }[] = []; const free = (x: number, z: number) => w.topAt(x, z) > 0 && Math.hypot(x, z - 4) > 12 && !(Math.abs(x) < 10 && z < -14 && z > -29) && !houses.some(([hx, hz]) => Math.abs(hx - x) < 4 && Math.abs(hz - z) < 4); // candy trees (lollipops + cotton-candy puffs), instanced trunks const treeSpots: THREE.Vector3[] = []; for (let n = 0; n < 400 && treeSpots.length < 18; n++) { const x = (r() - 0.5) * 58, z = (r() - 0.5) * 58; if (!free(x, z) || Math.hypot(x, z) > 29) continue; if (treeSpots.some(p => p.distanceTo(new THREE.Vector3(x, p.y, z)) < 6.5)) continue; treeSpots.push(new THREE.Vector3(x, w.topAt(x, z), z)); } const trunkGeo = new THREE.CylinderGeometry(0.16, 0.22, 2.4, 8); const trunks = new THREE.InstancedMesh(trunkGeo, softMaterial('#fff4f0'), treeSpots.length); const puffCols = ['#ffb3d9', '#b8e6ff', '#d7c2ff', '#fff1a6', '#b9f5d0']; const puffGeo = new THREE.IcosahedronGeometry(1, 2); const m4 = new THREE.Matrix4(); const trees: Level['trees'] = []; treeSpots.forEach((p, idx) => { m4.makeTranslation(p.x, p.y + 1.2, p.z); trunks.setMatrixAt(idx, m4); const col = puffCols[idx % puffCols.length]; const lolli = idx % 3 === 0; const crown = new THREE.Group(); crown.position.set(p.x, p.y + 2.6, p.z); if (lolli) { const disk = new THREE.Mesh(new THREE.CylinderGeometry(1.1, 1.1, 0.35, 24), softMaterial(col, 0.8)); disk.rotation.x = Math.PI / 2; crown.add(disk); const sw = new THREE.Mesh(new THREE.TorusGeometry(0.62, 0.12, 6, 24), softMaterial('#ffffff', 0.6)); sw.position.z = 0.19; crown.add(sw); const sw2 = sw.clone(); sw2.position.z = -0.19; crown.add(sw2); crown.position.y += 0.3; animated.push((t) => { crown.rotation.y = Math.sin(t * 0.5 + idx) * 0.6; }); } else { for (let q = 0; q < 5; q++) { const s = new THREE.Mesh(puffGeo, softMaterial(col, 0.2)); const a = q / 5 * Math.PI * 2; s.position.set(Math.cos(a) * 0.7, (q % 2) * 0.4, Math.sin(a) * 0.7); s.scale.setScalar(0.85 + (q % 3) * 0.15); crown.add(s); } const topP = new THREE.Mesh(puffGeo, softMaterial(col, 0.2)); topP.position.y = 0.8; crown.add(topP); animated.push((t) => { crown.scale.setScalar(1 + Math.sin(t * 1.5 + idx) * 0.03); }); } group.add(crown); trees.push({ pos: p, crown, idx, hidden: false }); }); group.add(trunks); // giant cupcakes: hiding spots const cups: THREE.Object3D[] = []; const cupSpots: [number, number][] = [[-22, 4], [21, 6], [-9, -10], [10, 18], [-12, 24]]; for (const [x, z] of cupSpots) { const y = w.topAt(x, z); const cup = makeCupcake(); cup.position.set(x, y, z); group.add(cup); cups.push(cup); hideSpots.push(new THREE.Vector3(x, y, z)); } // marshmallow trampolines const trampSpots: [number, number][] = [[6, 9], [-8, 2], [-20, 15], [22, -14], [0, -12.5], [-24, -12]]; for (const [x, z] of trampSpots) { const y = w.topAt(x, z); const g = new THREE.Group(); g.position.set(x, y, z); const base = new THREE.Mesh(new THREE.CylinderGeometry(1.1, 1.2, 0.5, 20), softMaterial('#fffaf5', 0.3)); base.position.y = 0.25; g.add(base); const topM = new THREE.Mesh(new THREE.CylinderGeometry(1.05, 1.05, 0.12, 20), softMaterial('#ff9ccf', 0.5)); topM.position.y = 0.52; g.add(topM); group.add(g); const b: Bouncer = { x, z, r: 1.15, top: y + 0.55, power: 15, squash: 0 }; bouncers.push(b); trampolineMeshes.push({ mesh: g, b }); animated.push((_t, dt) => { b.squash = Math.max(0, b.squash - dt * 3); const s = Math.sin(b.squash * Math.PI * 2) * 0.25 * b.squash; g.scale.set(1 + s, 1 - s * 1.5, 1 + s); }); } // castle towers for (const [x, z] of [[-7, -26], [7, -26], [-7, -18], [7, -18]] as [number, number][]) { const t = new THREE.Group(); t.position.set(x + 0.5, 5, z + 0.5); const body = new THREE.Mesh(new THREE.CylinderGeometry(1.4, 1.5, 5, 16), softMaterial('#f1e4ff', 0.2)); body.position.y = 2.5; t.add(body); const roof = new THREE.Mesh(new THREE.ConeGeometry(1.8, 2.8, 16), softMaterial('#ff8fc4', 0.5)); roof.position.y = 6.4; t.add(roof); const flag = new THREE.Mesh(new THREE.PlaneGeometry(0.9, 0.5), softMaterial('#ffd34d', 0, 0.2)); flag.position.set(0.45, 8.1, 0); (flag.material as THREE.ShaderMaterial).side = THREE.DoubleSide; t.add(flag); animated.push((tt) => { flag.rotation.y = Math.sin(tt * 3 + x) * 0.4; }); group.add(t); } // rainbow arch over the castle path const cols = ['#ff7a9c', '#ffb36b', '#ffe57a', '#8fe39b', '#7cc8ff', '#b79bff']; cols.forEach((c, i) => { const a = new THREE.Mesh(new THREE.TorusGeometry(6.5 - i * 0.35, 0.18, 8, 40, Math.PI), softMaterial(c, 0.3, 0.15)); a.position.set(0, 5, -15.5); group.add(a); }); // cotton-candy cloud sea + floating clouds const cloudMat = softMaterial('#fff4fb', 0.1, 0.05); const cloudGeo = new THREE.IcosahedronGeometry(1, 2); const clouds = new THREE.InstancedMesh(cloudGeo, cloudMat, 140); for (let q = 0; q < 140; q++) { const a = q / 140 * Math.PI * 2 + r() * 0.1, d = 36 + r() * 14; const s = 2.5 + r() * 3.5; m4.compose(new THREE.Vector3(Math.cos(a) * d, -1.5 - r() * 2, Math.sin(a) * d), new THREE.Quaternion(), new THREE.Vector3(s * 1.3, s * 0.6, s)); clouds.setMatrixAt(q, m4); } group.add(clouds); for (let q = 0; q < 9; q++) { const c = new THREE.Group(); for (let p = 0; p < 5; p++) { const s = new THREE.Mesh(cloudGeo, cloudMat); s.position.set((p - 2) * 1.6, Math.sin(p) * 0.5, (r() - 0.5)); s.scale.setScalar(1.3 + r()); c.add(s); } const a = r() * Math.PI * 2, d = 25 + r() * 30; c.position.set(Math.cos(a) * d, 16 + r() * 10, Math.sin(a) * d); group.add(c); animated.push((t) => { c.position.x += Math.sin(t * 0.1 + q) * 0.004; }); } // flowers: tiny instanced puffs on grass const fl = new THREE.InstancedMesh(new THREE.IcosahedronGeometry(0.16, 1), new THREE.MeshLambertMaterial({ color: '#ffffff', emissive: '#ff9fd0', emissiveIntensity: 0.25 }), 260); const fc = new THREE.Color(); let fi = 0; for (let n = 0; n < 2000 && fi < 260; n++) { const x = (r() - 0.5) * 60, z = (r() - 0.5) * 60; if (w.blockTop(x, z) !== B.GRASS && w.blockTop(x, z) !== B.MINT) continue; m4.makeTranslation(x, w.topAt(x, z) + 0.12, z); fl.setMatrixAt(fi, m4); fl.setColorAt(fi, fc.set(puffCols[fi % puffCols.length])); fi++; } fl.count = fi; group.add(fl); // candies: lines and arcs that guide exploration const candySpots: THREE.Vector3[] = []; const addC = (x: number, z: number, lift = 0.8) => { if (!w.inside(x, z, 2)) return; const y = w.topAt(x, z); if (y <= 0) return; candySpots.push(new THREE.Vector3(x, y + lift, z)); }; for (let i = 0; i < 6; i++) addC(0, 1 - i * 2.2); // plaza -> castle for (let i = 0; i < 8; i++) { const a = i / 8 * Math.PI * 2; addC(Math.cos(a) * 12, 4 + Math.sin(a) * 12); } // ring round plaza for (const [x, z, h] of steps) candySpots.push(new THREE.Vector3(x + 0.5, h + 0.8, z + 0.5)); for (const [x, z] of trampSpots) { candySpots.push(new THREE.Vector3(x, w.topAt(x, z) + 4.5, z)); candySpots.push(new THREE.Vector3(x, w.topAt(x, z) + 6, z)); } for (const [hx, hz] of houses) candySpots.push(new THREE.Vector3(hx + 0.5, w.topAt(hx, hz) + 0.8, hz + 0.5)); for (let i = 0; i < 5; i++) addC(-3 + i * 1.5, -22); // castle yard for (let i = 0; i < 14; i++) { const a = r() * Math.PI * 2, d = 14 + r() * 14; addC(Math.cos(a) * d, Math.sin(a) * d); } // cat starting spots spread around the island const catSpots: THREE.Vector3[] = []; const want: [number, number][] = [[3, 9], [-12, 5], [15, 2], [-6, -6], [18, 16], [-16, 18], [8, -10], [-20, -8], [0, -22], [22, -2], [-2, 26], [12, 24]]; for (const [x, z] of want) catSpots.push(new THREE.Vector3(x, w.topAt(x, z), z)); return { world: w, group, bouncers, trampolineMeshes, candySpots, catSpots, hideSpots, spawn: new THREE.Vector3(0, 3, 6), princessSpot: new THREE.Vector3(-3, 3, 1.5), pedestal: new THREE.Vector3(0, 3, 3), castle, animated, trees, trunks, cups, }; } export function makeCupcake(): THREE.Group { const g = new THREE.Group(); const wrap = new THREE.Mesh(new THREE.CylinderGeometry(1.5, 1.1, 1.6, 18), softMaterial('#ff9fc9', 0.3)); wrap.position.y = 0.8; g.add(wrap); for (let i = 0; i < 18; i++) { const rib = new THREE.Mesh(new THREE.BoxGeometry(0.08, 1.6, 0.08), softMaterial('#ffd1e6')); const a = i / 18 * Math.PI * 2; rib.position.set(Math.cos(a) * 1.32, 0.8, Math.sin(a) * 1.32); rib.rotation.z = Math.cos(a) * -0.24; rib.rotation.x = Math.sin(a) * 0.24; g.add(rib); } const frost = furMaterial({ top: '#fff7fb', bottom: '#ffe3f0', length: 0.12, density: 12 }); for (let i = 0; i < 4; i++) { const t = new THREE.Mesh(furSphere(), frost); t.scale.set(1.6 - i * 0.38, 0.55, 1.6 - i * 0.38); t.position.y = 1.7 + i * 0.45; g.add(t); } const cherry = new THREE.Mesh(new THREE.SphereGeometry(0.3, 14, 10), softMaterial('#ff3d6e', 1.2)); cherry.position.y = 3.3; g.add(cherry); for (let i = 0; i < 14; i++) { const s = new THREE.Mesh(new THREE.CapsuleGeometry(0.04, 0.12, 2, 4), softMaterial(['#7cc8ff', '#ffe57a', '#8fe39b', '#b79bff'][i % 4], 0, 0.2)); const a = i * 2.4; s.position.set(Math.cos(a) * (0.5 + (i % 3) * 0.3), 2.3 + (i % 4) * 0.25, Math.sin(a) * (0.5 + (i % 3) * 0.3)); s.rotation.set(a, i, 0); g.add(s); } return g; }