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

57 lines
1.4 KiB
TypeScript

import { _decorator, Component, Node, Vec3 } from 'cc'
const { ccclass, property } = _decorator
/**
* Lightweight NPC / dust motion — keeps scene alive without AI stack.
*/
@ccclass('NpcWander')
export class NpcWander extends Component {
@property(Node)
lookAt: Node | null = null
@property
mode: 'wander' | 'dust' = 'wander'
@property
radius = 1.8
@property
speed = 0.55
private _origin = new Vec3()
private _t = Math.random() * 10
private _baseY = 0
start() {
this._origin.set(this.node.position)
this._baseY = this.node.position.y
this._t = Math.random() * 10
}
update(dt: number) {
this._t += dt
if (this.mode === 'dust') {
const p = this.node.position
const y = this._baseY + Math.abs(Math.sin(this._t * this.speed)) * 1.2
const x = p.x + Math.sin(this._t * 0.25 + p.z) * dt * 0.15
this.node.setPosition(x, y, p.z)
return
}
const ox = this._origin.x + Math.cos(this._t * this.speed) * this.radius
const oz = this._origin.z + Math.sin(this._t * this.speed * 0.85) * this.radius
this.node.setPosition(ox, this._origin.y, oz)
if (this.lookAt) {
const t = this.lookAt.worldPosition
const dx = t.x - ox
const dz = t.z - oz
if (dx * dx + dz * dz > 0.01) {
const yaw = (Math.atan2(dx, dz) * 180) / Math.PI
this.node.setRotationFromEuler(0, yaw, 0)
}
}
}
}