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>
114 lines
3.1 KiB
TypeScript
114 lines
3.1 KiB
TypeScript
import { _decorator, Component, Node, Vec3, input, Input, EventTouch } from 'cc'
|
|
import { getBus } from '../core/EventBus'
|
|
import { PlayerMotor } from './PlayerMotor'
|
|
|
|
const { ccclass, property } = _decorator
|
|
|
|
/**
|
|
* P0 third-person follow + orbit (right-half drag).
|
|
* Pattern: spring follow + yaw/pitch — Cocos 3.8 Camera node.
|
|
*/
|
|
@ccclass('CameraFollow')
|
|
export class CameraFollow extends Component {
|
|
@property(Node)
|
|
target: Node | null = null
|
|
|
|
@property(PlayerMotor)
|
|
motor: PlayerMotor | null = null
|
|
|
|
@property
|
|
distance = 7.2
|
|
|
|
@property
|
|
height = 3.4
|
|
|
|
@property
|
|
minPitch = -10
|
|
|
|
@property
|
|
maxPitch = 55
|
|
|
|
@property
|
|
sens = 0.18
|
|
|
|
private _yaw = 25
|
|
private _pitch = 18
|
|
private _dragging = false
|
|
private _last = { x: 0, y: 0 }
|
|
private _pos = new Vec3()
|
|
private _look = new Vec3()
|
|
|
|
onEnable() {
|
|
input.on(Input.EventType.TOUCH_START, this.onTouchStart, this)
|
|
input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
|
|
input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this)
|
|
input.on(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
|
|
getBus().on('player:look-input', this.onLook)
|
|
}
|
|
|
|
onDisable() {
|
|
input.off(Input.EventType.TOUCH_START, this.onTouchStart, this)
|
|
input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
|
|
input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this)
|
|
input.off(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
|
|
getBus().off('player:look-input', this.onLook)
|
|
}
|
|
|
|
private onLook = (e: { yawDelta: number; pitchDelta: number }) => {
|
|
this._yaw += e.yawDelta
|
|
this._pitch = Math.min(
|
|
this.maxPitch,
|
|
Math.max(this.minPitch, this._pitch + e.pitchDelta),
|
|
)
|
|
}
|
|
|
|
private onTouchStart(e: EventTouch) {
|
|
const loc = e.getUILocation()
|
|
// right half = look (left reserved for joystick)
|
|
if (loc.x < 375) return
|
|
this._dragging = true
|
|
this._last.x = loc.x
|
|
this._last.y = loc.y
|
|
}
|
|
|
|
private onTouchMove(e: EventTouch) {
|
|
if (!this._dragging) return
|
|
const loc = e.getUILocation()
|
|
const dx = loc.x - this._last.x
|
|
const dy = loc.y - this._last.y
|
|
this._last.x = loc.x
|
|
this._last.y = loc.y
|
|
this._yaw -= dx * this.sens
|
|
this._pitch = Math.min(
|
|
this.maxPitch,
|
|
Math.max(this.minPitch, this._pitch + dy * this.sens),
|
|
)
|
|
}
|
|
|
|
private onTouchEnd() {
|
|
this._dragging = false
|
|
}
|
|
|
|
lateUpdate(dt: number) {
|
|
if (!this.target) return
|
|
const t = this.target.worldPosition
|
|
const yaw = (this._yaw * Math.PI) / 180
|
|
const pitch = (this._pitch * Math.PI) / 180
|
|
const cp = Math.cos(pitch)
|
|
const ox = Math.sin(yaw) * cp * this.distance
|
|
const oy = Math.sin(pitch) * this.distance + this.height * 0.15
|
|
const oz = Math.cos(yaw) * cp * this.distance
|
|
this._pos.set(t.x + ox, t.y + this.height + oy * 0.25, t.z + oz)
|
|
const damp = 1 - Math.exp(-10 * dt)
|
|
const cur = this.node.worldPosition
|
|
this.node.setWorldPosition(
|
|
cur.x + (this._pos.x - cur.x) * damp,
|
|
cur.y + (this._pos.y - cur.y) * damp,
|
|
cur.z + (this._pos.z - cur.z) * damp,
|
|
)
|
|
this._look.set(t.x, t.y + 1.2, t.z)
|
|
this.node.lookAt(this._look)
|
|
this.motor?.setCameraYaw(this._yaw)
|
|
}
|
|
}
|