const {ccclass, property} = cc._decorator; @ccclass export default class FruitGroove extends cc.Component { @property({ type:cc.Prefab }) cutPrefab:cc.Prefab = null; @property({ type:cc.Prefab }) cutFruitPrefab:cc.Prefab = null; @property({ type:cc.SpriteFrame }) fruitFrames:cc.SpriteFrame[] = []; fruitNames:string[] = ['椰子','橘子','橙子','梨子','西瓜','菠萝','香蕉','苹果']; fruitScale:number[] = [0.64,0.64,0.64,0.56,0.64,0.56,0.56,0.64]; grooveNodes:cc.Node[] = []; nameLabels:cc.Label[] = []; questionNodes:cc.Node[] = []; fruitNodes:cc.Node[] = []; fruitCount:number = 0; touchGrooveIndexes:number[]; validGrooveIndexes:number[]; touchStartPoint:cc.Vec2; touchEndPoint:cc.Vec2; canCut:boolean = true; onLoad(){ window.fruitGroove = this; this.node.zIndex = 1; this.node.children.forEach((grove,index) => { this.grooveNodes[index] = grove; this.nameLabels[index] = grove.getChildByName('Name').getComponent(cc.Label); this.questionNodes[index] = grove.getChildByName('Question'); }); this.hide(); } hide(){ this.node.children.forEach((child)=>{ child.active = false; }); } show(){ this.node.children.forEach((child)=>{ child.active = true; }); } touchStart(event:cc.Event.EventTouch) { this.touchStartPoint = event.getLocation(); this.touchGrooveIndexes = []; this.checkAndSetTouchFlag(event.getLocation()); } touchMove(event:cc.Event.EventTouch) { this.checkAndSetTouchFlag(event.getLocation()); } touchEnd(event:cc.Event.EventTouch) { this.touchEndPoint = event.getLocation(); this.checkAndSetTouchFlag(event.getLocation()); this.validGrooveIndexes = this.getValidIndexes(); this.release(); } deviceTouch() { this.touchGrooveIndexes = []; this.touchStartPoint = this.grooveNodes[2].position .add(cc.v2(cc.winSize.width/2, cc.winSize.height/2)); this.checkAndSetTouchFlag(this.touchStartPoint); let touchCenterPoint = this.grooveNodes[1].position .add(cc.v2(cc.winSize.width/2, cc.winSize.height/2)); this.checkAndSetTouchFlag(touchCenterPoint); this.touchEndPoint = this.grooveNodes[0].position .add(cc.v2(cc.winSize.width/2, cc.winSize.height/2)); this.checkAndSetTouchFlag(this.touchEndPoint); this.validGrooveIndexes = this.getValidIndexes(); this.release(); } checkAndSetTouchFlag(touchLocation:cc.Vec2){ let touchPosition = cc.v2(touchLocation.x-cc.winSize.width/2,touchLocation.y-cc.winSize.height/2); for(let i=0;i < this.grooveNodes.length; i++){ let vec = touchPosition.sub(this.grooveNodes[i].position); if(Math.abs(vec.x)<=this.grooveNodes[i].width/2 &&Math.abs(vec.y)<=this.grooveNodes[i].height/2){ if(this.touchGrooveIndexes.indexOf(i)==-1){ this.touchGrooveIndexes.push(i); } } } } getValidIndexes():number[]{ let validIndexes = []; for(let i=0;i{ this.fruitNodes[grooveIndex] = node; this.questionNodes[grooveIndex].active = false; this.nameLabels[grooveIndex].string = this.fruitNames[fruitIndex]; this.closeToLeft(); }) )); } release(){ if(this.validGrooveIndexes.length==0||!this.canCut){ return; } this.canCut = false; let fruitIndexes:number[] = []; let fruitPositions:cc.Vec2[] = []; for(let i=0;i{ cutFruitNode.destroy(); }); this.node.addChild(cutFruitNode); } this.cutEffect(fruitPositions); this.scheduleOnce(()=>{ //put out of groove fruitIndexes.forEach((fruitIndex:number) => { this.fruitNodes[fruitIndex] = undefined; this.questionNodes[fruitIndex].active = true; this.nameLabels[fruitIndex].string = ""; this.fruitCount--; }); window.gm.birds[window.myPlayerInfo.index].quicken(fruitIndexes.length); window.player.call('quicken',[fruitIndexes.length]); this.closeToLeft(); this.canCut = true; },0.5); } cutEffect(fruitPositions:cc.Vec2[]) { //calculate position let firstFruitPosition = fruitPositions[0]; let endFruitPosition = fruitPositions[fruitPositions.length-1]; let centerFruitPositon = cc.v2( (firstFruitPosition.x + endFruitPosition.x) / 2, (firstFruitPosition.y + endFruitPosition.y) / 2 ); //calculate angle let vector = this.touchEndPoint.sub(this.touchStartPoint); let angle = Math.asin(vector.y / vector.mag()) / Math.PI * 180; if (vector.x < 0) { angle = 180 - angle; } angle -= 45; //create node let cutNode = cc.instantiate(this.cutPrefab); cutNode.setPosition(centerFruitPositon); cutNode.angle = angle; cutNode.getComponent(dragonBones.ArmatureDisplay).addEventListener(dragonBones.EventObject.COMPLETE ,()=>{ cutNode.destroy(); } ); this.node.addChild(cutNode); } closeToLeft(){ let rankIndex = 0; for(let i = 0; i < this.fruitNodes.length; i++){ if(this.fruitNodes[i]){ if(i != rankIndex){ let fruitNode = this.fruitNodes[i]; let targetPosition = this.grooveNodes[rankIndex].position.add(cc.v2(0,12)); fruitNode.stopAllActions(); fruitNode.runAction(cc.moveTo(0.1,targetPosition)); this.fruitNodes[rankIndex] = fruitNode; this.questionNodes[rankIndex].active = false; this.nameLabels[rankIndex].string = this.nameLabels[i].string; this.fruitNodes[i] = undefined; this.questionNodes[i].active = true; this.nameLabels[i].string = ""; } rankIndex++; } } } }