83 lines
3.3 KiB
TypeScript
83 lines
3.3 KiB
TypeScript
import { _decorator, Component, Node, tween, Vec3 } from 'cc';
|
||
const { ccclass, property } = _decorator;
|
||
|
||
@ccclass('equipmentListSwitch')
|
||
export class equipmentListSwitch extends Component {
|
||
// Узел со списком оборудования
|
||
@property(Node)
|
||
public equipmentList: Node = null!;
|
||
|
||
// Смещение по оси X для закрытого состояния списка
|
||
@property
|
||
public offsetX: number = 250;
|
||
|
||
// Длительность анимации
|
||
@property
|
||
public animationDuration: number = 0.3;
|
||
|
||
// Узел стрелки, который будет поворачиваться
|
||
@property({ type: Node })
|
||
public arrow: Node | null = null;
|
||
|
||
// Позиции для анимации списка
|
||
private openPos: Vec3 = new Vec3();
|
||
private closedPos: Vec3 = new Vec3();
|
||
|
||
// Флаг, показывающий текущее состояние списка
|
||
private isOnToggle: boolean = false;
|
||
|
||
start() {
|
||
// Сохраняем позицию списка как открытое положение
|
||
this.openPos = this.equipmentList.getPosition().clone();
|
||
// Вычисляем закрытое положение (смещение по оси X)
|
||
this.closedPos = new Vec3(this.openPos.x - this.offsetX, this.openPos.y, this.openPos.z);
|
||
|
||
// Изначально скрываем список, устанавливая его в закрытое положение
|
||
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();
|
||
}
|
||
}
|
||
|
||
// Метод переключения состояния списка и одновременной анимации стрелки
|
||
public equipmentSwitch() {
|
||
// Останавливаем текущие анимации, чтобы избежать их наложения
|
||
this.stopTween();
|
||
|
||
if (this.isOnToggle) {
|
||
// Если список открыт — закрываем его и поворачиваем стрелку обратно в 0°
|
||
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 {
|
||
// Если список закрыт — открываем его и поворачиваем стрелку на 180°
|
||
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();
|
||
}
|
||
}
|
||
}
|
||
}
|