| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- 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.touchGrooveIndexes.length;i++){
- if(validIndexes.length==0){
- if(this.fruitNodes[this.touchGrooveIndexes[i]]){
- validIndexes.push(this.touchGrooveIndexes[i])
- }
- }else{
- let thisIndex = this.touchGrooveIndexes[i];
- let lastIndex = this.touchGrooveIndexes[i-1];
- if(this.nameLabels[thisIndex].string==this.nameLabels[lastIndex].string){
- validIndexes.push(this.touchGrooveIndexes[i]);
- }else{
- break;
- }
- }
- }
- return validIndexes;
- }
- addFruit(type:number,x:number,y:number){
- this.fruitCount++;
- let fruitCount = this.fruitCount;
- let grooveIndex = fruitCount-1;
- let fruitIndex = type-1;
-
- let node = new cc.Node(type.toString());
- node.addComponent(cc.Sprite).spriteFrame = this.fruitFrames[fruitIndex];
- node.setScale(this.fruitScale[fruitIndex]);
- node.setPosition(this.node.convertToNodeSpaceAR(
- window.gc.fruitGroup.convertToWorldSpaceAR(cc.v2(x,y))
- ));
- this.node.addChild(node);
- let targetPosition = this.grooveNodes[grooveIndex].position.add(cc.v2(0,12));
- let centerPositon = cc.v2(
- (node.x+targetPosition.x)/2 + Math.abs((node.x-targetPosition.x))
- ,(node.y+targetPosition.y/2)
- );
- node.runAction(cc.sequence(
- cc.bezierTo(0.5,[node.position,centerPositon,targetPosition]),
- cc.callFunc(()=>{
- 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<this.validGrooveIndexes.length;i++){
- let index = this.validGrooveIndexes[i];
- let fruitNode = this.fruitNodes[index];
- fruitIndexes.push(index);
- fruitPositions.push(fruitNode.position);
- fruitNode.destroy();
- //new an cut furit Effect
- let cutFruitNode = cc.instantiate(this.cutFruitPrefab);
- cutFruitNode.setScale(fruitNode.scale);
- cutFruitNode.setPosition(fruitNode.position);
- cutFruitNode.getComponent(dragonBones.ArmatureDisplay).addEventListener(dragonBones.EventObject.COMPLETE, ()=>{
- 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++;
- }
- }
- }
- }
|