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

112 lines
2.9 KiB
TypeScript

import {
_decorator,
Component,
input,
Input,
EventKeyboard,
KeyCode,
Vec3,
} from 'cc'
import { getBus } from '../core/EventBus'
const { ccclass, property } = _decorator
/**
* P0 third-person motor (XZ). Move input from EventBus or WASD.
* Uses Cocos Creator 3.8 Input API (cocos-engine).
*/
@ccclass('PlayerMotor')
export class PlayerMotor extends Component {
@property
speed = 5.4
@property
boundRadius = 28
private _ix = 0
private _iz = 0
private _vel = new Vec3()
private _tmp = new Vec3()
private _keys = new Set<KeyCode>()
private _yaw = 0
/** Camera yaw (degrees) so stick forward matches view. */
setCameraYaw(yawDeg: number) {
this._yaw = yawDeg
}
onEnable() {
input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this)
input.on(Input.EventType.KEY_UP, this.onKeyUp, this)
getBus().on('player:move-input', this.onMove)
}
onDisable() {
input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this)
input.off(Input.EventType.KEY_UP, this.onKeyUp, this)
getBus().off('player:move-input', this.onMove)
}
private onMove = (e: { x: number; z: number }) => {
this._ix = e.x
this._iz = e.z
}
private onKeyDown(e: EventKeyboard) {
this._keys.add(e.keyCode)
}
private onKeyUp(e: EventKeyboard) {
this._keys.delete(e.keyCode)
}
update(dt: number) {
let ix = this._ix
let iz = this._iz
if (this._keys.has(KeyCode.KEY_W) || this._keys.has(KeyCode.ARROW_UP)) iz -= 1
if (this._keys.has(KeyCode.KEY_S) || this._keys.has(KeyCode.ARROW_DOWN)) iz += 1
if (this._keys.has(KeyCode.KEY_A) || this._keys.has(KeyCode.ARROW_LEFT)) ix -= 1
if (this._keys.has(KeyCode.KEY_D) || this._keys.has(KeyCode.ARROW_RIGHT)) ix += 1
const len = Math.hypot(ix, iz)
if (len > 1) {
ix /= len
iz /= len
}
const yaw = (this._yaw * Math.PI) / 180
const fx = Math.sin(yaw)
const fz = Math.cos(yaw)
const rx = Math.cos(yaw)
const rz = -Math.sin(yaw)
const wx = fx * -iz + rx * ix
const wz = fz * -iz + rz * ix
const damp = 1 - Math.exp(-12 * dt)
this._vel.x += (wx * this.speed - this._vel.x) * damp
this._vel.z += (wz * this.speed - this._vel.z) * damp
const p = this.node.position
this._tmp.set(p.x + this._vel.x * dt, p.y, p.z + this._vel.z * dt)
const r = Math.hypot(this._tmp.x, this._tmp.z)
if (r > this.boundRadius) {
this._tmp.x *= this.boundRadius / r
this._tmp.z *= this.boundRadius / r
}
this.node.setPosition(this._tmp)
const spd = Math.hypot(this._vel.x, this._vel.z)
if (spd > 0.08) {
const ang = (Math.atan2(this._vel.x, this._vel.z) * 180) / Math.PI
const euler = this.node.eulerAngles
let diff = ((ang - euler.y + 540) % 360) - 180
this.node.setRotationFromEuler(0, euler.y + diff * damp * 1.4, 0)
this.node.setPosition(
this._tmp.x,
Math.abs(Math.sin(Date.now() * 0.012)) * 0.05,
this._tmp.z,
)
}
}
}