import { _decorator, Component, EventTouch, instantiate, Layout, Node, Size, Sprite, UITransform, Vec2, Vec3 } from 'cc'; import { ui_base } from './ui_base'; import { interact_puzzle_data } from '../../../data/data'; import { tools } from '../../tools'; import { gameManager } from '../gameManager'; const { ccclass, property } = _decorator; @ccclass('ui_interact_puzzle') export class ui_interact_puzzle extends ui_base { @property(Node) puzzle1:Node = null; @property(Node) puzzle2:Node = null; @property(Node) puzzle3:Node = null; @property(Node) puzzle4:Node = null; @property(Node) puzzle5:Node = null; @property(Node) puzzle6:Node = null; @property(Node) content:Node = null; @property(Node) actionNode:Node = null; private answer_list:string[] = [] private input_answer_list:string[] = [] private mPuzzleList:Node[] = []; private mInterctPuzzle:interact_puzzle_data = null; private mCurSelectDragPuzzle:Node = null; private mOriginPos:Vec3 = Vec3.ZERO; protected init(): void { this.mInterctPuzzle = this.mTopData._interact_puzzle_data; if(this.mInterctPuzzle===null){ return tools.showToast("设置拼图错误!") } this.answer_list= this.mInterctPuzzle.answer.split(",") this.initPuzzleView() } initPuzzleView(){ this.loadBg(this.mInterctPuzzle.bg) this.content.getComponent(UITransform).setContentSize(new Size(this.mInterctPuzzle.content.width,this.mInterctPuzzle.content.height)) this.content.position = new Vec3(this.mInterctPuzzle.content.x,this.mInterctPuzzle.content.y) this.puzzle1.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle1.res) this.puzzle2.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle2.res) this.puzzle3.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle3.res) this.puzzle4.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle4.res) this.puzzle5.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle5.res) this.puzzle6.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.mInterctPuzzle.puzzle6.res) this.actionNode.getComponent(UITransform).contentSize = this.content.getComponent(UITransform).contentSize; this.mPuzzleList.push(this.puzzle1) this.mPuzzleList.push(this.puzzle2) this.mPuzzleList.push(this.puzzle3) this.mPuzzleList.push(this.puzzle4) this.mPuzzleList.push(this.puzzle5) this.mPuzzleList.push(this.puzzle6) for (let index = 0; index < this.mPuzzleList.length; index++) { const puzzle = this.mPuzzleList[index]; puzzle.getComponent(UITransform).setContentSize(this.content.getComponent(UITransform).contentSize) } this.content.on(Node.EventType.TOUCH_START,(et:EventTouch)=>{ if(this.actionNode.children.length>1){ if(this.mCurSelectDragPuzzle!=null){ this.mCurSelectDragPuzzle.position = this.mOriginPos; this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = true; } this.mCurSelectDragPuzzle = null; this.actionNode.removeAllChildren() return } this.mCurSelectDragPuzzle = this.checkTouStartWho(et.getUILocation()) if(this.mCurSelectDragPuzzle!=null){ let item = instantiate(this.mCurSelectDragPuzzle) item.parent = this.actionNode; this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = false; this.mOriginPos = new Vec3(this.mCurSelectDragPuzzle.position.x, this.mCurSelectDragPuzzle.position.y) } }) this.content.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{ if(this.mCurSelectDragPuzzle!=null){ if(this.actionNode.children.length>0){ let n_p = this.actionNode.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(et.getUILocation().x,et.getUILocation().y)) this.actionNode.children[0].position = new Vec3(this.actionNode.children[0].position.x,n_p.y) } } }) this.content.on(Node.EventType.TOUCH_END,(et:EventTouch)=>{ if(this.mCurSelectDragPuzzle!=null){ let drag_item = this.checkTouStartWho(et.getUILocation()) if(drag_item!=null){ let drag_pos = new Vec3(drag_item.position.x,drag_item.position.y) drag_item.position = this.mOriginPos; this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = true; this.mCurSelectDragPuzzle.position = drag_pos; // else{ // this.onFialEvent() // } }else{ this.mCurSelectDragPuzzle.position = this.mOriginPos; } this.mCurSelectDragPuzzle = null; this.actionNode.removeAllChildren() let isFinish = this.sortPuzzle() if(isFinish){ this.onFinishEvent() return } } }) this.content.on(Node.EventType.TOUCH_CANCEL,()=>{ if(this.mCurSelectDragPuzzle!=null){ this.mCurSelectDragPuzzle.position = this.mOriginPos; this.mCurSelectDragPuzzle.getComponent(Sprite).enabled = true; } this.mCurSelectDragPuzzle = null; this.actionNode.removeAllChildren() }) } checkTouStartWho(point:Vec2){ for (let index = 0; index < this.mPuzzleList.length; index++) { const puzzle = this.mPuzzleList[index]; if(puzzle.getComponent(UITransform).getBoundingBoxToWorld().contains(point)){ return puzzle } } return null; } //最终按照y坐标排序 计算出是否正确 sortPuzzle(){ let sort_list = this.mPuzzleList.sort((a,b)=>{ return b.position.y- a.position.y }) this.input_answer_list = [] for (let index = 0; index < sort_list.length; index++) { const puzzle =sort_list[index]; this.input_answer_list.push(puzzle.name.substring(6,puzzle.name.length)) } console.log("this.input_answer_list",this.answer_list,this.input_answer_list) return JSON.stringify(this.input_answer_list)===JSON.stringify(this.answer_list); } }