GridView.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if(cellPos){
  61. var changeModels = this.selectCell(cellPos);
  62. this.isCanMove = changeModels.length < 3;
  63. }
  64. else{
  65. this.isCanMove = false;
  66. }
  67. return true;
  68. }, this);
  69. // 滑动操作逻辑
  70. this.node.on(cc.Node.EventType.TOUCH_MOVE, function(eventTouch){
  71. if(this.isCanMove){
  72. var startTouchPos = eventTouch.getStartLocation ();
  73. var startCellPos = this.convertTouchPosToCell(startTouchPos);
  74. var touchPos = eventTouch.getLocation();
  75. var cellPos = this.convertTouchPosToCell(touchPos);
  76. if(startCellPos.x != cellPos.x || startCellPos.y != cellPos.y){
  77. this.isCanMove = false;
  78. var changeModels = this.selectCell(cellPos);
  79. }
  80. }
  81. }, this);
  82. this.node.on(cc.Node.EventType.TOUCH_END, function(eventTouch){
  83. // console.log("1111");
  84. }, this);
  85. this.node.on(cc.Node.EventType.TOUCH_CANCEL, function(eventTouch){
  86. // console.log("1111");
  87. }, this);
  88. },
  89. // 根据点击的像素位置,转换成网格中的位置
  90. convertTouchPosToCell: function(pos){
  91. pos = this.node.convertToNodeSpace(pos);
  92. if(pos.x < 0 || pos.x >= GRID_PIXEL_WIDTH || pos.y < 0 || pos.y >= GRID_PIXEL_HEIGHT){
  93. return false;
  94. }
  95. var x = Math.floor(pos.x / CELL_WIDTH) + 1;
  96. var y = Math.floor(pos.y / CELL_HEIGHT) + 1;
  97. return cc.v2(x, y);
  98. },
  99. // 移动格子
  100. updateView: function(changeModels){
  101. let newCellViewInfo = [];
  102. for(var i in changeModels){
  103. var model = changeModels[i];
  104. var viewInfo = this.findViewByModel(model);
  105. var view = null;
  106. // 如果原来的cell不存在,则新建
  107. if(!viewInfo){
  108. var type = model.type;
  109. var aniView = cc.instantiate(this.aniPre[type]);
  110. aniView.parent = this.node;
  111. var cellViewScript = aniView.getComponent("CellView");
  112. cellViewScript.initWithModel(model);
  113. view = aniView;
  114. }
  115. // 如果已经存在
  116. else{
  117. view = viewInfo.view;
  118. this.cellViews[viewInfo.y][viewInfo.x] = null;
  119. }
  120. var cellScript = view.getComponent("CellView");
  121. cellScript.updateView();// 执行移动动作
  122. if (!model.isDeath) {
  123. newCellViewInfo.push({
  124. model: model,
  125. view: view
  126. });
  127. }
  128. }
  129. // 重新标记this.cellviews的信息
  130. newCellViewInfo.forEach(function(ele){
  131. let model = ele.model;
  132. this.cellViews[model.y][model.x] = ele.view;
  133. },this);
  134. },
  135. // 显示选中的格子背景
  136. updateSelect: function(pos){
  137. for(var i = 1;i <=9 ;i++){
  138. for(var j = 1 ;j <=9 ;j ++){
  139. if(this.cellViews[i][j]){
  140. var cellScript = this.cellViews[i][j].getComponent("CellView");
  141. if(pos.x == j && pos.y ==i){
  142. cellScript.setSelect(true);
  143. }
  144. else{
  145. cellScript.setSelect(false);
  146. }
  147. }
  148. }
  149. }
  150. },
  151. /**
  152. * 根据cell的model返回对应的view
  153. */
  154. findViewByModel: function(model){
  155. for(var i = 1;i <=9 ;i++){
  156. for(var j = 1 ;j <=9 ;j ++){
  157. if(this.cellViews[i][j] && this.cellViews[i][j].getComponent("CellView").model == model){
  158. return {view:this.cellViews[i][j],x:j, y:i};
  159. }
  160. }
  161. }
  162. return null;
  163. },
  164. getPlayAniTime: function(changeModels){
  165. if(!changeModels){
  166. return 0;
  167. }
  168. var maxTime = 0;
  169. changeModels.forEach(function(ele){
  170. ele.cmd.forEach(function(cmd){
  171. if(maxTime < cmd.playTime + cmd.keepTime){
  172. maxTime = cmd.playTime + cmd.keepTime;
  173. }
  174. },this)
  175. },this);
  176. return maxTime;
  177. },
  178. // 获得爆炸次数, 同一个时间算一个
  179. getStep: function(effectsQueue){
  180. if(!effectsQueue){
  181. return 0;
  182. }
  183. return effectsQueue.reduce(function(maxValue, efffectCmd){
  184. return Math.max(maxValue, efffectCmd.step || 0);
  185. }, 0);
  186. },
  187. //一段时间内禁止操作
  188. disableTouch: function(time, step){
  189. if(time <= 0){
  190. return ;
  191. }
  192. this.isInPlayAni = true;
  193. this.node.runAction(cc.sequence(cc.delayTime(time),cc.callFunc(function(){
  194. this.isInPlayAni = false;
  195. this.audioUtils.playContinuousMatch(step);
  196. }, this)));
  197. },
  198. // 正常击中格子后的操作
  199. selectCell: function(cellPos){
  200. var result = this.controller.selectCell(cellPos); // 直接先丢给model处理数据逻辑
  201. var changeModels = result[0]; // 有改变的cell,包含新生成的cell和生成马上摧毁的格子
  202. var effectsQueue = result[1]; //各种特效
  203. this.playEffect(effectsQueue);
  204. this.disableTouch(this.getPlayAniTime(changeModels), this.getStep(effectsQueue));
  205. this.updateView(changeModels);
  206. this.controller.cleanCmd();
  207. if(changeModels.length >= 2){
  208. this.updateSelect(cc.v2(-1,-1));
  209. this.audioUtils.playSwap();
  210. }
  211. else{
  212. this.updateSelect(cellPos);
  213. this.audioUtils.playClick();
  214. }
  215. return changeModels;
  216. },
  217. playEffect: function(effectsQueue){
  218. this.effectLayer.getComponent("EffectLayer").playEffects(effectsQueue);
  219. }
  220. // called every frame, uncomment this function to activate update callback
  221. // update: function (dt) {
  222. // },
  223. });