— так компоненты не упадут, даже если кнопка или её node к моменту onDestroy уже не существует сделал ноды менеджеров списка оборудовани, деталей, и аварий Persistent Node, чтобы при смене сцены не сбрасывалось их состояние.
126 lines
4.9 KiB
TypeScript
126 lines
4.9 KiB
TypeScript
import { _decorator, Component, Node, input, Input, EventKeyboard, EventMouse, KeyCode, Vec3, Quat, NodeSpace } from 'cc';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('FirstPersonController')
|
|
export class FirstPersonController extends Component {
|
|
@property({ type: Node })
|
|
public camera: Node | null = null;
|
|
|
|
@property
|
|
public moveSpeed: number = 5;
|
|
@property
|
|
public lookSpeed: number = 0.1;
|
|
|
|
@property
|
|
public speedStep: number = 1;
|
|
@property
|
|
public minSpeed: number = 1;
|
|
@property
|
|
public maxSpeed: number = 20;
|
|
|
|
// Movement and rotation state
|
|
private _moveDirection: Vec3 = new Vec3();
|
|
private _eulerAngles: Vec3 = new Vec3();
|
|
private _isLeftMousePressed: boolean = false;
|
|
private _lastEmittedPosition: Vec3 = new Vec3();
|
|
|
|
onLoad() {
|
|
input.on(Input.EventType.KEY_DOWN, this._onKeyDown, this);
|
|
input.on(Input.EventType.KEY_UP, this._onKeyUp, this);
|
|
input.on(Input.EventType.MOUSE_DOWN, this._onMouseDown, this);
|
|
input.on(Input.EventType.MOUSE_UP, this._onMouseUp, this);
|
|
input.on(Input.EventType.MOUSE_MOVE, this._onMouseMove, this);
|
|
input.on(Input.EventType.MOUSE_WHEEL, this._onMouseWheel, this);
|
|
|
|
// Initialize rotation angles
|
|
this._eulerAngles = this.node.eulerAngles.clone();
|
|
// Save start position for "moved" event
|
|
this._lastEmittedPosition = this.node.getPosition().clone();
|
|
}
|
|
|
|
onDestroy() {
|
|
// Safely unsubscribe from input events
|
|
input?.off(Input.EventType.KEY_DOWN, this._onKeyDown, this);
|
|
input?.off(Input.EventType.KEY_UP, this._onKeyUp, this);
|
|
input?.off(Input.EventType.MOUSE_DOWN, this._onMouseDown, this);
|
|
input?.off(Input.EventType.MOUSE_UP, this._onMouseUp, this);
|
|
input?.off(Input.EventType.MOUSE_MOVE, this._onMouseMove, this);
|
|
input?.off(Input.EventType.MOUSE_WHEEL, this._onMouseWheel, this);
|
|
}
|
|
|
|
private _onKeyDown(event: EventKeyboard) {
|
|
switch (event.keyCode) {
|
|
case KeyCode.KEY_W: this._moveDirection.z = 1; break;
|
|
case KeyCode.KEY_S: this._moveDirection.z = -1; break;
|
|
case KeyCode.KEY_A: this._moveDirection.x = -1; break;
|
|
case KeyCode.KEY_D: this._moveDirection.x = 1; break;
|
|
case KeyCode.KEY_Q: this._moveDirection.y = -1; break;
|
|
case KeyCode.KEY_E: this._moveDirection.y = 1; break;
|
|
}
|
|
}
|
|
|
|
private _onKeyUp(event: EventKeyboard) {
|
|
switch (event.keyCode) {
|
|
case KeyCode.KEY_W:
|
|
case KeyCode.KEY_S: this._moveDirection.z = 0; break;
|
|
case KeyCode.KEY_A:
|
|
case KeyCode.KEY_D: this._moveDirection.x = 0; break;
|
|
case KeyCode.KEY_Q:
|
|
case KeyCode.KEY_E: this._moveDirection.y = 0; break;
|
|
}
|
|
}
|
|
|
|
private _onMouseDown(event: EventMouse) {
|
|
if (event.getButton() === 0) {
|
|
this._isLeftMousePressed = true;
|
|
}
|
|
}
|
|
|
|
private _onMouseUp(event: EventMouse) {
|
|
if (event.getButton() === 0) {
|
|
this._isLeftMousePressed = false;
|
|
}
|
|
}
|
|
|
|
private _onMouseMove(event: EventMouse) {
|
|
if (!this._isLeftMousePressed) return;
|
|
// Update yaw and pitch
|
|
this._eulerAngles.y -= event.movementX * this.lookSpeed;
|
|
this._eulerAngles.x -= event.movementY * this.lookSpeed;
|
|
this._eulerAngles.x = Math.max(-89, Math.min(89, this._eulerAngles.x));
|
|
|
|
// Apply rotation
|
|
const newQuat = Quat.fromEuler(new Quat(), this._eulerAngles.x, this._eulerAngles.y, 0);
|
|
this.node.setRotation(newQuat);
|
|
// Reset camera local rotation if child
|
|
this.camera?.setRotation(Quat.IDENTITY);
|
|
}
|
|
|
|
private _onMouseWheel(event: EventMouse) {
|
|
if (event.getScrollY() > 0) {
|
|
this.moveSpeed = Math.min(this.moveSpeed + this.speedStep, this.maxSpeed);
|
|
} else if (event.getScrollY() < 0) {
|
|
this.moveSpeed = Math.max(this.moveSpeed - this.speedStep, this.minSpeed);
|
|
}
|
|
console.log(`Current move speed: ${this.moveSpeed}`);
|
|
}
|
|
|
|
update(deltaTime: number) {
|
|
// Movement in world space
|
|
const forward = Vec3.transformQuat(new Vec3(), Vec3.FORWARD, this.node.worldRotation);
|
|
const right = Vec3.transformQuat(new Vec3(), Vec3.RIGHT, this.node.worldRotation);
|
|
const move = new Vec3();
|
|
Vec3.scaleAndAdd(move, move, forward, this._moveDirection.z * this.moveSpeed * deltaTime);
|
|
Vec3.scaleAndAdd(move, move, right, this._moveDirection.x * this.moveSpeed * deltaTime);
|
|
Vec3.scaleAndAdd(move, move, Vec3.UP, this._moveDirection.y * this.moveSpeed * deltaTime);
|
|
this.node.translate(move, NodeSpace.WORLD);
|
|
|
|
// Emit "moved" if significant change
|
|
const currentPos = this.node.getPosition();
|
|
if (Vec3.distance(currentPos, this._lastEmittedPosition) > 0.1) {
|
|
this.node.emit('moved');
|
|
this._lastEmittedPosition = currentPos.clone();
|
|
}
|
|
}
|
|
}
|