nomadro/apps/minigame-cocos/assets/scripts/bootstrap/GameRoot.ts
Eric 58a845db32 Add WeChat minigame triple-engine scaffold with Godot Chiang Mai plaza SoT.
Unity and Cocos stay as parallel schemes; apps/minigame ships the Godot 4.7 WeChat pack for DevTools.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-06 20:17:14 +08:00

90 lines
2.5 KiB
TypeScript

import { _decorator, Component, Node } from 'cc'
import { getBus, resetBus } from '../core/EventBus'
import { CITY, ECONOMY } from '../data/CatalogSeed'
import { SimWorld } from '../sim/SimWorld'
import { PlayerMotor } from '../player/PlayerMotor'
import { CameraFollow } from '../player/CameraFollow'
import { WorldBootstrap } from '../scene/WorldBootstrap'
import { HudView } from '../ui/HudView'
import { VirtualJoystick } from '../ui/VirtualJoystick'
const { ccclass, property } = _decorator
/**
* Thin scene glue — wires views to SimWorld. No economy rules here.
* Attach to scene root in Cocos Creator 3.8.6.
*/
@ccclass('GameRoot')
export class GameRoot extends Component {
@property(PlayerMotor)
player: PlayerMotor | null = null
@property(CameraFollow)
cameraFollow: CameraFollow | null = null
@property(WorldBootstrap)
world: WorldBootstrap | null = null
@property(HudView)
hud: HudView | null = null
@property(VirtualJoystick)
joystick: VirtualJoystick | null = null
@property(Node)
playerNode: Node | null = null
private _sim: SimWorld | null = null
/** start() so AutoSceneSetup.onLoad can wire refs first */
start() {
if (this._sim) return
resetBus()
this._sim = new SimWorld(CITY, ECONOMY)
const pn = this.playerNode || this.player?.node
if (this.world) this.world.playerForNpcLook = pn
this.world?.build(CITY)
const spawn = CITY.spawn
pn?.setPosition(spawn.x, spawn.y, spawn.z)
if (this.player) this.player.boundRadius = CITY.boundRadius
this._sim.bootFromSave()
this.world?.applySun(this._sim.stats.minutes)
getBus().on('interact:request', this.onInteract, this)
getBus().on('stats:changed', this.onStats, this)
}
onDestroy() {
getBus().off('interact:request', this.onInteract, this)
getBus().off('stats:changed', this.onStats, this)
}
update() {
if (!this._sim || !this.player) return
const p = this.player.node.worldPosition
this._sim.updateProximity(p.x, p.z)
}
private onInteract = () => {
if (!this._sim || !this.player) return
const p = this.player.node.worldPosition
this._sim.tryInteract(p.x, p.z)
}
private onStats = () => {
if (!this._sim) return
this.world?.applySun(this._sim.stats.minutes)
}
/** Optional UI restart button */
onRestart() {
if (!this._sim) return
this._sim.stats = this._sim.defaultStats()
this._sim.flags = {}
this._sim.persist()
this._sim.emitAll()
const spawn = CITY.spawn
this.player?.node.setPosition(spawn.x, spawn.y, spawn.z)
}
}