import { _decorator, Component, Node, MeshRenderer, primitives, utils, Material, Color, Vec3, DirectionalLight, } from 'cc' import type { CityCatalog } from '../sim/SimWorld' import type { StructureDef } from '../core/types' import { sunElevFromMinutes } from '../sim/TimeSystem' import { NpcWander } from './NpcWander' const { ccclass, property } = _decorator function hexColor(hex: string): Color { const h = hex.replace('#', '') const n = parseInt( h.length === 3 ? h.split('').map((c) => c + c).join('') : h, 16, ) return new Color((n >> 16) & 255, (n >> 8) & 255, n & 255, 255) } type Prim = 'box' | 'sphere' | 'cylinder' | 'capsule' /** * Stylized cozy city — aligned with Unity WorldBootstrap pastel look. * Procedural primitives until Kenney CC0 GLB swap. */ @ccclass('WorldBootstrap') export class WorldBootstrap extends Component { @property(Node) worldRoot: Node | null = null @property(DirectionalLight) sun: DirectionalLight | null = null @property(Node) playerForNpcLook: Node | null = null private _matCache = new Map() private _meshCache = new Map>() build(city: CityCatalog) { const root = this.worldRoot || this.node root.removeAllChildren() // grass disc (cylinder flattened) this.spawn( root, 'ground', 'cylinder', new Vec3(0, -0.02, 0), new Vec3(36, 0.04, 36), '#7BC67E', ) this.spawn( root, 'sand', 'cylinder', new Vec3(0, 0.03, 12), new Vec3(18, 0.03, 10), '#C4A574', ) // cross roads this.spawn(root, 'road_ns', 'box', new Vec3(0, 0.05, 0), new Vec3(5.2, 0.03, 28), '#E8E0D4') this.spawn(root, 'road_ew', 'box', new Vec3(0, 0.055, 0), new Vec3(28, 0.03, 5.2), '#E8E0D4') for (const s of city.structures) { if (s.kind === 'street') continue this.placeBuilding(root, s) this.placeInterior(root, s) } // cafe terrace this.spawn(root, 'CafeDeck', 'box', new Vec3(5.4, 0.12, -2.2), new Vec3(2.8, 0.12, 2.2), '#D4A574') this.spawn(root, 'UmbrellaPole', 'cylinder', new Vec3(5.2, 0.7, -2.2), new Vec3(0.08, 0.7, 0.08), '#F5F5F5') this.spawn(root, 'Umbrella', 'cylinder', new Vec3(5.2, 1.55, -2.2), new Vec3(2.4, 0.06, 2.4), '#FFD56A') // river + pier this.spawn(root, 'River', 'box', new Vec3(0, 0.02, 15.5), new Vec3(38, 0.08, 6.5), '#4DB7D6') this.spawn(root, 'Pier', 'box', new Vec3(0, 0.2, 13.2), new Vec3(3.2, 0.15, 2.4), '#D9A066') for (const it of city.interactables) { this.spawn( root, `marker_${it.id}`, 'cylinder', new Vec3(it.x, 0.55, it.z), new Vec3(0.28, 0.55, 0.28), '#FFD27A', ) } for (let i = 0; i < 14; i++) { const a = (i / 14) * Math.PI * 2 const r = 12 + (i % 3) * 1.5 const x = Math.cos(a) * r const z = Math.sin(a) * r * 0.88 if (Math.hypot(x, z) < 5) continue this.placeTree(root, new Vec3(x, 0, z), 0.85 + (i % 3) * 0.15) } for (let i = 0; i < 10; i++) { const ang = i * 0.62 this.spawn( root, `Bush_${i}`, 'sphere', new Vec3(Math.cos(ang) * 7.5, 0.35, Math.sin(ang) * 7.5), new Vec3(0.55 + (i % 3) * 0.12, 0.55 + (i % 3) * 0.12, 0.55 + (i % 3) * 0.12), i % 2 === 0 ? '#5FBF6A' : '#F28BA0', ) } this.spawnNpc(root, 'Mira', new Vec3(5.2, 0, -3), '#5ED4C8') this.spawnNpc(root, 'Alex', new Vec3(3.2, 0, 7), '#9BB0C4') this.spawnNpc(root, 'Friend', new Vec3(-5, 0, 6.2), '#B39DDB') for (let i = 0; i < 8; i++) { const d = this.spawn( root, `Dust_${i}`, 'sphere', new Vec3((i - 4) * 2.5, 1.2 + (i % 3) * 0.4, (i % 5) - 2), new Vec3(0.07, 0.07, 0.07), '#FFE9A8', ) d.addComponent(NpcWander).mode = 'dust' } } applySun(minutes: number) { if (!this.sun) return const elev = sunElevFromMinutes(minutes) const az = 0.85 this.sun.node.setWorldPosition( Math.cos(az) * 18, 3 + elev * 20, Math.sin(az) * 18, ) this.sun.illuminance = 18000 + elev * 45000 this.sun.color = Color.lerp( new Color(), new Color(255, 170, 120), new Color(255, 245, 220), elev, ) } private placeBuilding(root: Node, s: StructureDef) { const group = new Node(s.id) root.addChild(group) group.setPosition(s.x, 0, s.z) this.spawn( group, 'Body', 'box', new Vec3(0, s.h * 0.5, 0), new Vec3(s.w, s.h, s.d), s.color, ) const roofHex = s.kind === 'apartment' ? '#E86860' : s.kind === 'cafe' ? '#3CB8AE' : s.kind === 'cowork' ? '#6B7C90' : s.kind === 'market' ? '#E0A820' : '#9B7ED9' this.spawn( group, 'Roof', 'box', new Vec3(0, s.h + 0.18, 0), new Vec3(s.w * 1.12, 0.28, s.d * 1.12), roofHex, ) const rows = Math.max(1, Math.floor(s.h / 1.4)) for (let r = 0; r < rows; r++) { for (let c = 0; c < 2; c++) { this.spawn( group, `Win_${r}_${c}`, 'box', new Vec3((c - 0.5) * s.w * 0.35, 0.9 + r * 1.35, s.d * 0.51), new Vec3(0.55, 0.65, 0.08), '#FFF6D8', ) } } this.spawn( group, 'Door', 'box', new Vec3(0, 0.7, s.d * 0.51), new Vec3(0.7, 1.35, 0.1), '#6B4F3A', ) } /** Simple furniture props for apartment / cafe / studio */ private placeInterior(root: Node, s: StructureDef) { if (s.kind === 'apartment') { this.spawn(root, 'Bed', 'box', new Vec3(s.x - 1.4, 0.35, s.z - 1.4), new Vec3(1.8, 0.4, 1.1), '#F8E1E7') this.spawn(root, 'Desk', 'box', new Vec3(s.x + 1.5, 0.4, s.z + 0.2), new Vec3(1.2, 0.08, 0.6), '#C4A574') this.spawn(root, 'PC', 'box', new Vec3(s.x + 1.5, 0.7, s.z + 0.2), new Vec3(0.55, 0.4, 0.08), '#333333') this.spawn(root, 'Sofa', 'box', new Vec3(s.x, 0.35, s.z + 1.6), new Vec3(2.2, 0.55, 0.9), '#E85D4C') this.spawn(root, 'Rug', 'box', new Vec3(s.x, 0.06, s.z), new Vec3(2.4, 0.04, 2.0), '#F2D6A2') } else if (s.kind === 'cafe') { this.spawn(root, 'Counter', 'box', new Vec3(s.x, 0.55, s.z), new Vec3(2.4, 1.0, 0.7), '#8B5A3C') this.spawn(root, 'StoolA', 'cylinder', new Vec3(s.x - 0.9, 0.35, s.z + 1.1), new Vec3(0.4, 0.35, 0.4), '#E8D5B7') this.spawn(root, 'StoolB', 'cylinder', new Vec3(s.x + 0.9, 0.35, s.z + 1.1), new Vec3(0.4, 0.35, 0.4), '#E8D5B7') } else if (s.kind === 'studio') { this.spawn(root, 'CamStand', 'cylinder', new Vec3(s.x, 0.7, s.z + 1.2), new Vec3(0.08, 0.7, 0.08), '#444444') this.spawn(root, 'Cam', 'box', new Vec3(s.x, 1.35, s.z + 1.2), new Vec3(0.35, 0.22, 0.45), '#222222') this.spawn(root, 'RingLight', 'cylinder', new Vec3(s.x, 1.5, s.z + 0.4), new Vec3(1.1, 0.05, 1.1), '#FFE082') } else if (s.kind === 'market') { this.spawn(root, 'Shelf', 'box', new Vec3(s.x, 0.9, s.z), new Vec3(2.8, 1.6, 0.5), '#F5C542') } else if (s.kind === 'cowork') { this.spawn(root, 'DeskRow', 'box', new Vec3(s.x, 0.4, s.z), new Vec3(3.2, 0.08, 1.2), '#B0BEC5') this.spawn(root, 'Chair', 'box', new Vec3(s.x, 0.35, s.z + 1.0), new Vec3(0.55, 0.55, 0.55), '#78909C') } } private placeTree(root: Node, pos: Vec3, scale: number) { const t = new Node('Tree') root.addChild(t) t.setPosition(pos) t.setScale(scale, scale, scale) this.spawn(t, 'Trunk', 'cylinder', new Vec3(0, 0.45, 0), new Vec3(0.22, 0.45, 0.22), '#8B5A3C') this.spawn(t, 'Leaf1', 'sphere', new Vec3(0, 1.25, 0), new Vec3(1.35, 1.35, 1.35), '#4CAF50') this.spawn(t, 'Leaf2', 'sphere', new Vec3(0.35, 1.55, 0.1), new Vec3(0.9, 0.9, 0.9), '#66BB6A') } private spawnNpc(root: Node, name: string, pos: Vec3, accent: string) { const g = new Node(name) root.addChild(g) g.setPosition(pos) this.spawn(g, 'Body', 'capsule', new Vec3(0, 0.85, 0), new Vec3(0.58, 0.5, 0.58), accent) this.spawn(g, 'Head', 'sphere', new Vec3(0, 1.5, 0), new Vec3(0.42, 0.42, 0.42), '#FFE0BD') this.spawn(g, 'Bag', 'box', new Vec3(0.28, 0.9, -0.05), new Vec3(0.22, 0.28, 0.12), '#5D4037') const npc = g.addComponent(NpcWander) npc.lookAt = this.playerForNpcLook npc.mode = 'wander' } private spawn( parent: Node, name: string, prim: Prim, pos: Vec3, scale: Vec3, colorHex: string, ): Node { const n = new Node(name) parent.addChild(n) n.setPosition(pos) n.setScale(scale) const mr = n.addComponent(MeshRenderer) mr.mesh = this.mesh(prim) mr.setMaterial(this.mat(colorHex), 0) return n } private mesh(prim: Prim) { let m = this._meshCache.get(prim) if (m) return m const geo = prim === 'sphere' ? primitives.sphere() : prim === 'cylinder' ? primitives.cylinder() : prim === 'capsule' ? primitives.capsule() : primitives.box() m = utils.MeshUtils.createMesh(geo) this._meshCache.set(prim, m) return m } private mat(hex: string): Material { let m = this._matCache.get(hex) if (m) return m m = new Material() m.initialize({ effectName: 'builtin-unlit', technique: 0 }) m.setProperty('mainColor', hexColor(hex)) this._matCache.set(hex, m) return m } }