83 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-11-15 18:38:00 +08:00
import { _decorator, Component, Node } from 'cc';
import { PlayerTacticalItem } from './PlayerTacticalItem';
2023-11-16 02:44:43 +08:00
import { app } from '../../../App';
import { PlayerTacticalEvent } from '../../../data/PlayerTacticalData';
2023-11-16 19:10:19 +08:00
import JNodeDrag from '../../../../../extensions/ngame/assets/ngame/util/components/JNodeDrag';
import { EventTouch } from 'cc';
import { UITransform } from 'cc';
2023-11-15 18:38:00 +08:00
const { ccclass, property } = _decorator;
/**
*
*/
@ccclass('PlayerTacticalView')
export class PlayerTacticalView extends Component {
//阵法子节点列表
items:PlayerTacticalItem[] = [];
2023-11-16 02:44:43 +08:00
onLoad(){
2023-11-15 18:38:00 +08:00
2023-11-16 02:44:43 +08:00
//阵法
this.items = this.node.getComponentsInChildren(PlayerTacticalItem);
this.items.forEach((item,index) => {
item.onInit(index); //初始化阵法下标
2023-11-16 19:10:19 +08:00
item.addMoveEvent(this.onMoveItem.bind(this));
2023-11-16 02:44:43 +08:00
});
2023-11-15 18:38:00 +08:00
2023-11-16 02:44:43 +08:00
this.onUpdateView();
2023-11-15 18:38:00 +08:00
2023-11-16 02:44:43 +08:00
this.onEvent();
2023-11-15 18:38:00 +08:00
2023-11-16 02:44:43 +08:00
}
2023-11-16 19:10:19 +08:00
//移动子节点
onMoveItem(root:PlayerTacticalItem,e:EventTouch){
//获取接触的节点(除了自己)
this.items.forEach(item => {
//排除自己
if(item == root) return;
//排除在移动的节点
if(!(item.node.position.equals(item.drag.original))) return;
//检测接触
if(item.getComponent(UITransform).isHit(e.getUILocation())){
//如果手指接触了 则 将接触的阵法移动到自己
item.onExchange(root);
}
})
}
2023-11-16 02:44:43 +08:00
protected onDestroy(): void {
this.offEvent();
}
//添加监听
onEvent(){
app.event.on(PlayerTacticalEvent.UPDATE_TACTICAL,this.onUpdateView,this);
}
//移除监听
offEvent(){
app.event.off(PlayerTacticalEvent.UPDATE_TACTICAL,this.onUpdateView,this);
}
//更新阵法显示
onUpdateView(){
this.items.forEach(item => {
item.onUpdateView();
})
}
2023-11-15 18:38:00 +08:00
}