nomadro/apps/minigame-cocos/assets/scripts/sim/SimWorld.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

142 lines
3.4 KiB
TypeScript

/**
* Game simulation root — NO Cocos imports.
* Views subscribe via EventBus; this class never touches nodes.
*/
import { getBus } from '../core/EventBus'
import type {
InteractableDef,
PlayerStats,
SaveBlob,
StructureDef,
} from '../core/types'
import type { ActionEconomy } from './EconomySystem'
import { applyAction } from './WorkSystem'
import { formatClock } from './TimeSystem'
import { loadBlob, saveBlob } from './SaveSystem'
export type CityCatalog = {
id: string
name: string
boundRadius: number
spawn: { x: number; y: number; z: number }
structures: StructureDef[]
interactables: InteractableDef[]
}
export type EconomyCatalog = {
startCash: number
startEnergy: number
maxEnergy: number
actions: Record<string, ActionEconomy>
}
export class SimWorld {
readonly city: CityCatalog
readonly economy: EconomyCatalog
stats: PlayerStats
flags: Record<string, boolean> = {}
placed: SaveBlob['structures'] = []
constructor(city: CityCatalog, economy: EconomyCatalog) {
this.city = city
this.economy = economy
this.stats = this.defaultStats()
}
defaultStats(): PlayerStats {
return {
cash: this.economy.startCash,
energy: this.economy.startEnergy,
maxEnergy: this.economy.maxEnergy,
minutes: 7 * 60 + 10,
day: 1,
locationId: 'street',
}
}
bootFromSave(): void {
const blob = loadBlob()
if (blob) {
this.stats = { ...this.defaultStats(), ...blob.stats }
this.flags = { ...blob.flags }
this.placed = [...(blob.structures || [])]
}
this.emitAll()
}
persist(): boolean {
const blob: SaveBlob = {
version: 1,
stats: this.stats,
flags: this.flags,
structures: this.placed,
}
const ok = saveBlob(blob)
getBus().emit('save:done', { ok })
return ok
}
nearestInteractable(
x: number,
z: number,
): InteractableDef | null {
let best: InteractableDef | null = null
let bestD = Infinity
for (const it of this.city.interactables) {
const d = Math.hypot(it.x - x, it.z - z)
if (d <= it.radius && d < bestD) {
best = it
bestD = d
}
}
return best
}
updateProximity(x: number, z: number): void {
const near = this.nearestInteractable(x, z)
getBus().emit('hud:hint', {
text: near ? near.label : '探索城市 · 靠近高亮物互动',
interactable: !!near,
})
}
tryInteract(x: number, z: number): void {
const near = this.nearestInteractable(x, z)
if (!near) {
getBus().emit('interact:done', {
id: '',
action: '',
ok: false,
message: '附近没有可互动对象',
})
return
}
const action = this.economy.actions[near.action]
const result = applyAction(this.stats, action)
this.stats = result.stats
if (result.ok) {
this.flags[`did_${near.action}`] = true
this.stats.locationId = near.structureId
getBus().emit('scene:enter', { structureId: near.structureId })
this.persist()
this.emitAll()
}
getBus().emit('interact:done', {
id: near.id,
action: near.action,
ok: result.ok,
message: result.message,
})
}
emitAll(): void {
getBus().emit('stats:changed', this.stats)
getBus().emit('time:tick', {
minutes: this.stats.minutes,
day: this.stats.day,
label: `D${this.stats.day} ${formatClock(this.stats.minutes)}`,
})
}
}