yantar/assets/Scripts/ModelSelector.ts

83 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { _decorator, Component, Node, CameraComponent, PhysicsSystem, geometry, Vec3, EventMouse, input, Input } from 'cc';
import { PulsatingHighlighter } from './PulsatingHighlighter';
const { ccclass, property } = _decorator;
@ccclass('ModelSelector')
export class ModelSelector extends Component {
@property({ type: CameraComponent })
public camera: CameraComponent | null = null;
private _ray = new geometry.Ray();
private _lastHighlighted: PulsatingHighlighter | null = null;
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.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);
} else {
console.log('❌ Raycast ничего не нашел.');
}
} else {
console.log('⚠️ Raycast не сработал.');
}
}
// В классе ModelSelector
private _highlightModel(model: Node) {
const switcher = model.getComponent( PulsatingHighlighter);
if (switcher) {
switcher.switchToRed();
}
}
}