GridView.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import {CELL_WIDTH, CELL_HEIGHT, GRID_PIXEL_WIDTH, GRID_PIXEL_HEIGHT, ANITIME} from '../Model/ConstValue';
  2. import AudioUtils from "../Utils/AudioUtils";
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. // foo: {
  7. // default: null, // The default value will be used only when the component attaching
  8. // to a node for the first time
  9. // url: cc.Texture2D, // optional, default is typeof default
  10. // serializable: true, // optional, default is true
  11. // visible: true, // optional, default is true
  12. // displayName: 'Foo', // optional
  13. // readonly: false, // optional, default is false
  14. // },
  15. // ...
  16. aniPre: {
  17. default: [],
  18. type: [cc.Prefab]
  19. },
  20. effectLayer: {
  21. default: null,
  22. type: cc.Node
  23. },
  24. audioUtils:{
  25. type: AudioUtils,
  26. default: null
  27. }
  28. },
  29. // use this for initialization
  30. onLoad: function () {
  31. this.setListener();
  32. this.lastTouchPos = cc.Vec2(-1, -1);
  33. this.isCanMove = true;
  34. this.isInPlayAni = false; // 是否在播放中
  35. },
  36. setController: function(controller){
  37. this.controller = controller;
  38. },
  39. initWithCellModels: function(cellsModels){
  40. this.cellViews = [];
  41. for(var i = 1;i<=9;i++){
  42. this.cellViews[i] = [];
  43. for(var j = 1;j<=9;j++){
  44. var type = cellsModels[i][j].type;
  45. var aniView = cc.instantiate(this.aniPre[type]);
  46. aniView.parent = this.node
  47. var cellViewScript = aniView.getComponent("CellView");
  48. cellViewScript.initWithModel(cellsModels[i][j]);
  49. this.cellViews[i][j] = aniView;
  50. }
  51. }
  52. },
  53. setListener: function(){
  54. this.node.on(cc.Node.EventType.TOUCH_START, function(eventTouch){
  55. if(this.isInPlayAni){//播放动画中,不允许点击
  56. return true;
  57. }
  58. var touchPos = eventTouch.getLocation();
  59. var cellPos = this.convertTouchPosToCell(touchPos);
  60. // 技能
  61. if(this.controller.isCrushOne){
  62. this.controller.isCrushOne = false
  63. this.controller.showSkillTip(false)
  64. const result = this.controller.processCrushOne(cellPos)
  65. var changeModels = result[0]; // 有改变的cell
  66. var effectsQueue = result[1]; //各种特效
  67. this.playEffect(effectsQueue);
  68. this.disableTouch(this.getPlayAniTime(changeModels), this.getStep(effectsQueue));
  69. this.updateView(changeModels);
  70. this.controller.cleanCmd();
  71. return false
  72. }
  73. if(cellPos){
  74. var changeModels = this.selectCell(cellPos);
  75. this.isCanMove = changeModels.length < 3;
  76. }
  77. else{
  78. this.isCanMove = false;
  79. }
  80. return true;
  81. }, this);
  82. // 滑动操作逻辑
  83. this.node.on(cc.Node.EventType.TOUCH_MOVE, function(eventTouch){
  84. if(this.isCanMove){
  85. var startTouchPos = eventTouch.getStartLocation ();
  86. var startCellPos = this.convertTouchPosToCell(startTouchPos);
  87. var touchPos = eventTouch.getLocation();
  88. var cellPos = this.convertTouchPosToCell(touchPos);
  89. if(startCellPos.x != cellPos.x || startCellPos.y != cellPos.y){
  90. this.isCanMove = false;
  91. var changeModels = this.selectCell(cellPos);
  92. }
  93. }
  94. }, this);
  95. this.node.on(cc.Node.EventType.TOUCH_END, function(eventTouch){
  96. // console.log("1111");
  97. }, this);
  98. this.node.on(cc.Node.EventType.TOUCH_CANCEL, function(eventTouch){
  99. // console.log("1111");
  100. }, this);
  101. },
  102. // 根据点击的像素位置,转换成网格中的位置
  103. convertTouchPosToCell: function(pos){
  104. pos = this.node.convertToNodeSpaceAR(pos);
  105. if(pos.x < 0 || pos.x >= GRID_PIXEL_WIDTH || pos.y < 0 || pos.y >= GRID_PIXEL_HEIGHT){
  106. return false;
  107. }
  108. var x = Math.floor(pos.x / CELL_WIDTH) + 1;
  109. var y = Math.floor(pos.y / CELL_HEIGHT) + 1;
  110. return cc.v2(x, y);
  111. },
  112. // 移动格子
  113. updateView: function(changeModels){
  114. let newCellViewInfo = [];
  115. for(var i in changeModels){
  116. var model = changeModels[i];
  117. var viewInfo = this.findViewByModel(model);
  118. var view = null;
  119. // 如果原来的cell不存在,则新建
  120. if(!viewInfo){
  121. var type = model.type;
  122. var aniView = cc.instantiate(this.aniPre[type]);
  123. aniView.parent = this.node;
  124. var cellViewScript = aniView.getComponent("CellView");
  125. cellViewScript.initWithModel(model);
  126. view = aniView;
  127. }
  128. // 如果已经存在
  129. else{
  130. view = viewInfo.view;
  131. this.cellViews[viewInfo.y][viewInfo.x] = null;
  132. }
  133. var cellScript = view.getComponent("CellView");
  134. cellScript.updateView();// 执行移动动作
  135. if (!model.isDeath) {
  136. newCellViewInfo.push({
  137. model: model,
  138. view: view
  139. });
  140. }
  141. }
  142. // 重新标记this.cellviews的信息
  143. newCellViewInfo.forEach(function(ele){
  144. let model = ele.model;
  145. this.cellViews[model.y][model.x] = ele.view;
  146. },this);
  147. },
  148. // 显示选中的格子背景
  149. updateSelect: function(pos){
  150. for(var i = 1;i <=9 ;i++){
  151. for(var j = 1 ;j <=9 ;j ++){
  152. if(this.cellViews[i][j]){
  153. var cellScript = this.cellViews[i][j].getComponent("CellView");
  154. if(pos.x == j && pos.y ==i){
  155. cellScript.setSelect(true);
  156. }
  157. else{
  158. cellScript.setSelect(false);
  159. }
  160. }
  161. }
  162. }
  163. },
  164. /**
  165. * 根据cell的model返回对应的view
  166. */
  167. findViewByModel: function(model){
  168. for(var i = 1;i <=9 ;i++){
  169. for(var j = 1 ;j <=9 ;j ++){
  170. if(this.cellViews[i][j] && this.cellViews[i][j].getComponent("CellView").model == model){
  171. return {view:this.cellViews[i][j],x:j, y:i};
  172. }
  173. }
  174. }
  175. return null;
  176. },
  177. getPlayAniTime: function(changeModels){
  178. if(!changeModels){
  179. return 0;
  180. }
  181. var maxTime = 0;
  182. changeModels.forEach(function(ele){
  183. ele.cmd.forEach(function(cmd){
  184. if(maxTime < cmd.playTime + cmd.keepTime){
  185. maxTime = cmd.playTime + cmd.keepTime;
  186. }
  187. },this)
  188. },this);
  189. return maxTime;
  190. },
  191. // 获得爆炸次数, 同一个时间算一个
  192. getStep: function(effectsQueue){
  193. if(!effectsQueue){
  194. return 0;
  195. }
  196. return effectsQueue.reduce(function(maxValue, efffectCmd){
  197. return Math.max(maxValue, efffectCmd.step || 0);
  198. }, 0);
  199. },
  200. //一段时间内禁止操作
  201. disableTouch: function(time, step){
  202. if(time <= 0){
  203. return ;
  204. }
  205. this.isInPlayAni = true;
  206. this.node.runAction(cc.sequence(cc.delayTime(time),cc.callFunc(function(){
  207. this.isInPlayAni = false;
  208. this.audioUtils.playContinuousMatch(step);
  209. }, this)));
  210. },
  211. // 正常击中格子后的操作
  212. selectCell: function(cellPos){
  213. var result = this.controller.selectCell(cellPos); // 直接先丢给model处理数据逻辑
  214. var changeModels = result[0]; // 有改变的cell,包含新生成的cell和生成马上摧毁的格子
  215. var effectsQueue = result[1]; //各种特效
  216. this.playEffect(effectsQueue);
  217. this.disableTouch(this.getPlayAniTime(changeModels), this.getStep(effectsQueue));
  218. this.updateView(changeModels);
  219. this.controller.cleanCmd();
  220. if(changeModels.length >= 2){
  221. this.updateSelect(cc.v2(-1,-1));
  222. this.audioUtils.playSwap();
  223. }
  224. else{
  225. this.updateSelect(cellPos);
  226. this.audioUtils.playClick();
  227. }
  228. return changeModels;
  229. },
  230. playEffect: function(effectsQueue){
  231. this.effectLayer.getComponent("EffectLayer").playEffects(effectsQueue, this.controller);
  232. }
  233. // called every frame, uncomment this function to activate update callback
  234. // update: function (dt) {
  235. // },
  236. });