92 lines
3.1 KiB
TypeScript
92 lines
3.1 KiB
TypeScript
import { _decorator, Component, Node, CameraComponent, PhysicsSystem, geometry, Vec3, EventMouse, input, Input } from 'cc';
|
||
import { PulsatingHighlighter } from './PulsatingHighlighter';
|
||
import { CameraMover } from './CameraMover';
|
||
|
||
const { ccclass, property } = _decorator;
|
||
|
||
@ccclass('ModelSelector')
|
||
export class ModelSelector extends Component {
|
||
@property({ type: CameraComponent })
|
||
public camera: CameraComponent | null = null;
|
||
|
||
@property({ type: CameraMover })
|
||
public cameraMover: CameraMover | null = null; // 🔥 Добавляем CameraMover
|
||
|
||
private _ray = new geometry.Ray();
|
||
private _clickCount = 0;
|
||
private _doubleClickThreshold = 0.3; // 300 мс между кликами
|
||
|
||
onLoad() {
|
||
input.on(Input.EventType.MOUSE_UP, this._onMouseUp, this);
|
||
}
|
||
|
||
onDestroy() {
|
||
input.off(Input.EventType.MOUSE_UP, this._onMouseUp, this);
|
||
}
|
||
|
||
private _onMouseUp(event: EventMouse) {
|
||
if (event.getButton() !== 2) return; // Используем правую кнопку мыши
|
||
|
||
console.log('🖱 Правый клик зафиксирован!');
|
||
|
||
this.unschedule(this._resetClickCount);
|
||
this._clickCount++;
|
||
|
||
if (this._clickCount === 2) {
|
||
console.log('✅ Двойной клик зафиксирован!');
|
||
this._performRaycast(event);
|
||
this._clickCount = 0;
|
||
} else {
|
||
this.scheduleOnce(this._resetClickCount, this._doubleClickThreshold);
|
||
}
|
||
}
|
||
|
||
private _resetClickCount() {
|
||
this._clickCount = 0;
|
||
console.log('⏳ Сброс двойного клика из-за таймаута.');
|
||
}
|
||
|
||
private _performRaycast(event: EventMouse) {
|
||
if (!this.camera) {
|
||
console.warn('🚨 Камера не найдена!');
|
||
return;
|
||
}
|
||
|
||
const screenPos = new Vec3(event.getLocationX(), event.getLocationY(), 0);
|
||
this.camera.screenPointToRay(screenPos.x, screenPos.y, this._ray);
|
||
|
||
console.log('🔍 Запускаем raycast...');
|
||
|
||
if (PhysicsSystem.instance.raycast(this._ray)) {
|
||
const results = PhysicsSystem.instance.raycastResults;
|
||
if (results.length > 0) {
|
||
const hitNode = results[0].collider.node;
|
||
console.log(`🎯 Попадание по объекту: ${hitNode.name}`);
|
||
|
||
this._highlightModel(hitNode , "red");
|
||
this._moveCamera(hitNode); // 🔥 Вызываем камеру!
|
||
} else {
|
||
console.log('❌ Raycast ничего не нашел.');
|
||
}
|
||
} else {
|
||
console.log('⚠️ Raycast не сработал.');
|
||
}
|
||
}
|
||
|
||
private _highlightModel(model: Node, color: string) {
|
||
const switcher = model.getComponent(PulsatingHighlighter);
|
||
if (switcher) {
|
||
switcher.switchToColor(color);
|
||
}
|
||
}
|
||
|
||
private _moveCamera(model: Node ) {
|
||
if (this.cameraMover) {
|
||
this.cameraMover.moveToTarget(model);
|
||
|
||
} else {
|
||
console.warn('🚨 CameraMover не подключен!');
|
||
}
|
||
}
|
||
}
|