— так компоненты не упадут, даже если кнопка или её node к моменту onDestroy уже не существует сделал ноды менеджеров списка оборудовани, деталей, и аварий Persistent Node, чтобы при смене сцены не сбрасывалось их состояние.
92 lines
2.7 KiB
TypeScript
92 lines
2.7 KiB
TypeScript
// scripts/ui/equipmentListSwitch.ts
|
||
import { _decorator, Component, Node, tween, Vec3, Button } from 'cc';
|
||
const { ccclass, property } = _decorator;
|
||
|
||
@ccclass('equipmentListSwitch')
|
||
export class equipmentListSwitch extends Component {
|
||
/** Кнопка, которая переключает список */
|
||
@property(Button)
|
||
public toggleButton: Button | null = null;
|
||
|
||
/** Узел со списком оборудования */
|
||
@property(Node)
|
||
public equipmentList: Node = null!;
|
||
|
||
/** Смещение по X для закрытого состояния */
|
||
@property
|
||
public offsetX = 250;
|
||
|
||
/** Длительность анимации */
|
||
@property
|
||
public animationDuration = 0.3;
|
||
|
||
/** Узел стрелки, который будет поворачиваться */
|
||
@property(Node)
|
||
public arrow: Node | null = null;
|
||
|
||
private openPos = new Vec3();
|
||
private closedPos = new Vec3();
|
||
private isOnToggle = false;
|
||
|
||
onLoad() {
|
||
// Подписываем кнопку на метод переключения
|
||
this.toggleButton?.node?.on('click', this.equipmentSwitch, this);
|
||
}
|
||
|
||
start() {
|
||
// Сохраняем открытое и закрытое положения
|
||
this.openPos = this.equipmentList.getPosition().clone();
|
||
this.closedPos = new Vec3(
|
||
this.openPos.x - this.offsetX,
|
||
this.openPos.y,
|
||
this.openPos.z
|
||
);
|
||
|
||
// Изначально скрываем список (перемещаем в closedPos)
|
||
this.equipmentList.setPosition(this.closedPos);
|
||
// Изначально стрелка в 0°
|
||
if (this.arrow) this.arrow.angle = 0;
|
||
}
|
||
|
||
private stopTween() {
|
||
tween(this.equipmentList).stop();
|
||
if (this.arrow) tween(this.arrow).stop();
|
||
}
|
||
|
||
/** Вызывается по нажатию toggleButton */
|
||
public equipmentSwitch() {
|
||
this.stopTween();
|
||
|
||
if (this.isOnToggle) {
|
||
// закрываем
|
||
this.isOnToggle = false;
|
||
tween(this.equipmentList)
|
||
.to(this.animationDuration, { position: this.closedPos })
|
||
.start();
|
||
if (this.arrow) {
|
||
tween(this.arrow)
|
||
.to(this.animationDuration, { angle: 0 })
|
||
.start();
|
||
}
|
||
} else {
|
||
// открываем
|
||
this.isOnToggle = true;
|
||
tween(this.equipmentList)
|
||
.to(this.animationDuration, { position: this.openPos })
|
||
.start();
|
||
if (this.arrow) {
|
||
tween(this.arrow)
|
||
.to(this.animationDuration, { angle: 180 })
|
||
.start();
|
||
}
|
||
}
|
||
}
|
||
|
||
onDestroy() {
|
||
// Корректно отписываемся от клика
|
||
this.toggleButton?.node?.off('click', this.equipmentSwitch, this);
|
||
// Останавливаем любые выполняющиеся твины
|
||
this.stopTween();
|
||
}
|
||
}
|