FruitGroove.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. const {ccclass, property} = cc._decorator;
  2. @ccclass
  3. export default class FruitGroove extends cc.Component {
  4. @property({
  5. type:cc.Prefab
  6. })
  7. cutPrefab:cc.Prefab = null;
  8. @property({
  9. type:cc.Prefab
  10. })
  11. cutFruitPrefab:cc.Prefab = null;
  12. @property({
  13. type:cc.SpriteFrame
  14. })
  15. fruitFrames:cc.SpriteFrame[] = [];
  16. fruitNames:string[] = ['椰子','橘子','橙子','梨子','西瓜','菠萝','香蕉','苹果'];
  17. fruitScale:number[] = [0.64,0.64,0.64,0.56,0.64,0.56,0.56,0.64];
  18. grooveNodes:cc.Node[] = [];
  19. nameLabels:cc.Label[] = [];
  20. questionNodes:cc.Node[] = [];
  21. fruitNodes:cc.Node[] = [];
  22. fruitCount:number = 0;
  23. touchGrooveIndexes:number[];
  24. validGrooveIndexes:number[];
  25. touchStartPoint:cc.Vec2;
  26. touchEndPoint:cc.Vec2;
  27. canCut:boolean = true;
  28. onLoad(){
  29. window.fruitGroove = this;
  30. this.node.zIndex = 1;
  31. this.node.children.forEach((grove,index) => {
  32. this.grooveNodes[index] = grove;
  33. this.nameLabels[index] = grove.getChildByName('Name').getComponent(cc.Label);
  34. this.questionNodes[index] = grove.getChildByName('Question');
  35. });
  36. this.hide();
  37. }
  38. hide(){
  39. this.node.children.forEach((child)=>{
  40. child.active = false;
  41. });
  42. }
  43. show(){
  44. this.node.children.forEach((child)=>{
  45. child.active = true;
  46. });
  47. }
  48. touchStart(event:cc.Event.EventTouch) {
  49. this.touchStartPoint = event.getLocation();
  50. this.touchGrooveIndexes = [];
  51. this.checkAndSetTouchFlag(event.getLocation());
  52. }
  53. touchMove(event:cc.Event.EventTouch) {
  54. this.checkAndSetTouchFlag(event.getLocation());
  55. }
  56. touchEnd(event:cc.Event.EventTouch) {
  57. this.touchEndPoint = event.getLocation();
  58. this.checkAndSetTouchFlag(event.getLocation());
  59. this.validGrooveIndexes = this.getValidIndexes();
  60. this.release();
  61. }
  62. deviceTouch() {
  63. this.touchGrooveIndexes = [];
  64. this.touchStartPoint = this.grooveNodes[2].position
  65. .add(cc.v2(cc.winSize.width/2, cc.winSize.height/2));
  66. this.checkAndSetTouchFlag(this.touchStartPoint);
  67. let touchCenterPoint = this.grooveNodes[1].position
  68. .add(cc.v2(cc.winSize.width/2, cc.winSize.height/2));
  69. this.checkAndSetTouchFlag(touchCenterPoint);
  70. this.touchEndPoint = this.grooveNodes[0].position
  71. .add(cc.v2(cc.winSize.width/2, cc.winSize.height/2));
  72. this.checkAndSetTouchFlag(this.touchEndPoint);
  73. this.validGrooveIndexes = this.getValidIndexes();
  74. this.release();
  75. }
  76. checkAndSetTouchFlag(touchLocation:cc.Vec2){
  77. let touchPosition = cc.v2(touchLocation.x-cc.winSize.width/2,touchLocation.y-cc.winSize.height/2);
  78. for(let i=0;i < this.grooveNodes.length; i++){
  79. let vec = touchPosition.sub(this.grooveNodes[i].position);
  80. if(Math.abs(vec.x)<=this.grooveNodes[i].width/2
  81. &&Math.abs(vec.y)<=this.grooveNodes[i].height/2){
  82. if(this.touchGrooveIndexes.indexOf(i)==-1){
  83. this.touchGrooveIndexes.push(i);
  84. }
  85. }
  86. }
  87. }
  88. getValidIndexes():number[]{
  89. let validIndexes = [];
  90. for(let i=0;i<this.touchGrooveIndexes.length;i++){
  91. if(validIndexes.length==0){
  92. if(this.fruitNodes[this.touchGrooveIndexes[i]]){
  93. validIndexes.push(this.touchGrooveIndexes[i])
  94. }
  95. }else{
  96. let thisIndex = this.touchGrooveIndexes[i];
  97. let lastIndex = this.touchGrooveIndexes[i-1];
  98. if(this.nameLabels[thisIndex].string==this.nameLabels[lastIndex].string){
  99. validIndexes.push(this.touchGrooveIndexes[i]);
  100. }else{
  101. break;
  102. }
  103. }
  104. }
  105. return validIndexes;
  106. }
  107. addFruit(type:number,x:number,y:number){
  108. this.fruitCount++;
  109. let fruitCount = this.fruitCount;
  110. let grooveIndex = fruitCount-1;
  111. let fruitIndex = type-1;
  112. let node = new cc.Node(type.toString());
  113. node.addComponent(cc.Sprite).spriteFrame = this.fruitFrames[fruitIndex];
  114. node.setScale(this.fruitScale[fruitIndex]);
  115. node.setPosition(this.node.convertToNodeSpaceAR(
  116. window.gc.fruitGroup.convertToWorldSpaceAR(cc.v2(x,y))
  117. ));
  118. this.node.addChild(node);
  119. let targetPosition = this.grooveNodes[grooveIndex].position.add(cc.v2(0,12));
  120. let centerPositon = cc.v2(
  121. (node.x+targetPosition.x)/2 + Math.abs((node.x-targetPosition.x))
  122. ,(node.y+targetPosition.y/2)
  123. );
  124. node.runAction(cc.sequence(
  125. cc.bezierTo(0.5,[node.position,centerPositon,targetPosition]),
  126. cc.callFunc(()=>{
  127. this.fruitNodes[grooveIndex] = node;
  128. this.questionNodes[grooveIndex].active = false;
  129. this.nameLabels[grooveIndex].string = this.fruitNames[fruitIndex];
  130. this.closeToLeft();
  131. })
  132. ));
  133. }
  134. release(){
  135. if(this.validGrooveIndexes.length==0||!this.canCut){
  136. return;
  137. }
  138. this.canCut = false;
  139. let fruitIndexes:number[] = [];
  140. let fruitPositions:cc.Vec2[] = [];
  141. for(let i=0;i<this.validGrooveIndexes.length;i++){
  142. let index = this.validGrooveIndexes[i];
  143. let fruitNode = this.fruitNodes[index];
  144. fruitIndexes.push(index);
  145. fruitPositions.push(fruitNode.position);
  146. fruitNode.destroy();
  147. //new an cut furit Effect
  148. let cutFruitNode = cc.instantiate(this.cutFruitPrefab);
  149. cutFruitNode.setScale(fruitNode.scale);
  150. cutFruitNode.setPosition(fruitNode.position);
  151. cutFruitNode.getComponent(dragonBones.ArmatureDisplay).addEventListener(dragonBones.EventObject.COMPLETE, ()=>{
  152. cutFruitNode.destroy();
  153. });
  154. this.node.addChild(cutFruitNode);
  155. }
  156. this.cutEffect(fruitPositions);
  157. this.scheduleOnce(()=>{
  158. //put out of groove
  159. fruitIndexes.forEach((fruitIndex:number) => {
  160. this.fruitNodes[fruitIndex] = undefined;
  161. this.questionNodes[fruitIndex].active = true;
  162. this.nameLabels[fruitIndex].string = "";
  163. this.fruitCount--;
  164. });
  165. window.gm.birds[window.myPlayerInfo.index].quicken(fruitIndexes.length);
  166. window.player.call('quicken',[fruitIndexes.length]);
  167. this.closeToLeft();
  168. this.canCut = true;
  169. },0.5);
  170. }
  171. cutEffect(fruitPositions:cc.Vec2[]) {
  172. //calculate position
  173. let firstFruitPosition = fruitPositions[0];
  174. let endFruitPosition = fruitPositions[fruitPositions.length-1];
  175. let centerFruitPositon = cc.v2(
  176. (firstFruitPosition.x + endFruitPosition.x) / 2,
  177. (firstFruitPosition.y + endFruitPosition.y) / 2
  178. );
  179. //calculate angle
  180. let vector = this.touchEndPoint.sub(this.touchStartPoint);
  181. let angle = Math.asin(vector.y / vector.mag()) / Math.PI * 180;
  182. if (vector.x < 0) {
  183. angle = 180 - angle;
  184. }
  185. angle -= 45;
  186. //create node
  187. let cutNode = cc.instantiate(this.cutPrefab);
  188. cutNode.setPosition(centerFruitPositon);
  189. cutNode.angle = angle;
  190. cutNode.getComponent(dragonBones.ArmatureDisplay).addEventListener(dragonBones.EventObject.COMPLETE
  191. ,()=>{
  192. cutNode.destroy();
  193. }
  194. );
  195. this.node.addChild(cutNode);
  196. }
  197. closeToLeft(){
  198. let rankIndex = 0;
  199. for(let i = 0; i < this.fruitNodes.length; i++){
  200. if(this.fruitNodes[i]){
  201. if(i != rankIndex){
  202. let fruitNode = this.fruitNodes[i];
  203. let targetPosition = this.grooveNodes[rankIndex].position.add(cc.v2(0,12));
  204. fruitNode.stopAllActions();
  205. fruitNode.runAction(cc.moveTo(0.1,targetPosition));
  206. this.fruitNodes[rankIndex] = fruitNode;
  207. this.questionNodes[rankIndex].active = false;
  208. this.nameLabels[rankIndex].string = this.nameLabels[i].string;
  209. this.fruitNodes[i] = undefined;
  210. this.questionNodes[i].active = true;
  211. this.nameLabels[i].string = "";
  212. }
  213. rankIndex++;
  214. }
  215. }
  216. }
  217. }