RomanSun2Game.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. var GameProtocol = require("RomanGameProtocol");
  2. import ScenceManager from 'HNScenceManager'
  3. const arr_posX = [-430, -218, -3, 210, 420];
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. itemClone: cc.Prefab,
  8. itemSpines: [sp.SkeletonData],
  9. kuangClone: cc.Node,
  10. maskClone: cc.Node,
  11. leftAni: sp.Skeleton,
  12. rightAni: sp.Skeleton,
  13. leftLabel: cc.Label,
  14. rightLabel: cc.Label,
  15. sizec: 150,
  16. },
  17. // LIFE-CYCLE CALLBACKS:
  18. onLoad () {
  19. this.maxItemLen = 7;
  20. this.unCheck = true; //不update检测
  21. this.rollBG = [];
  22. this.elementNode = [];
  23. this.elementJS = [];
  24. this.kuangNode = [];
  25. this.maskNode = [];
  26. this.needRolls = []; //存储每个元素是否可以滚动.(金币元素不用滚动)
  27. this.initData();
  28. },
  29. start () {
  30. },
  31. resetData(){
  32. this.nowIndex = 0; //服务器data中m_desk_data的Index
  33. this.surTimes = 3; //界面显示剩余几次
  34. },
  35. initData(){
  36. this.resetData();
  37. for(let i=0; i < GameProtocol.MAX_ROW_NUM; i++){
  38. this.rollBG[i] = [];
  39. this.elementNode[i] = [];
  40. this.elementJS[i] = [];
  41. this.kuangNode[i] = [];
  42. this.maskNode[i] = [];
  43. this.needRolls[i] = [];
  44. let rollNode = this.node.getChildByName(`svNode${i}`);
  45. if(rollNode){
  46. for(let j=0; j < GameProtocol.MAX_COL_NUM; j++){
  47. this.rollBG[i][j] = rollNode.getChildByName("content"+(j+1));
  48. this.elementNode[i][j] = [];
  49. this.elementJS[i][j] = [];
  50. this.needRolls[i][j] = true;
  51. this.rollBG[i][j].y = 0;
  52. this.rollBG[i][j].removeAllChildren();
  53. for(let k=0; k < this.maxItemLen; k++){
  54. let item = cc.instantiate(this.itemClone);
  55. item.y = (this.maxItemLen - 2 - k) * this.sizec;
  56. item.parent = this.rollBG[i][j];
  57. item.active = false;
  58. this.elementNode[i][j][k] = item;
  59. this.elementJS[i][j][k] = item.getComponent("LBItem");
  60. }
  61. let kuang = cc.instantiate(this.kuangClone);
  62. kuang.active = false;
  63. kuang.x = this.rollBG[i][j].x;
  64. kuang.y = 0;
  65. kuang.parent = rollNode;
  66. this.kuangNode[i][j] = kuang;
  67. let mask = cc.instantiate(this.maskClone);
  68. mask.active = false;
  69. mask.x = this.rollBG[i][j].x;
  70. mask.y = 0;
  71. mask.parent = rollNode;
  72. this.maskNode[i][j] = mask;
  73. }
  74. }
  75. }
  76. },
  77. //更新剩余次数.
  78. upLeftTimes(val){
  79. this.leftLabel.string = val;
  80. this.rightLabel.string = val;
  81. if(val == 3){
  82. ScenceManager.Instance().getGameComponent().playEffect("sunadd");
  83. this.leftAni.node.active = true;
  84. this.leftAni.setAnimation(0, "a3", false);
  85. this.rightAni.node.active = true;
  86. this.rightAni.setAnimation(0, "a3", false);
  87. }
  88. },
  89. showSubgame(firstData, firstValueData,sunData){
  90. console.log('sun2Data: ', firstData, firstValueData, sunData);
  91. this.unCheck = false;
  92. this.firstData = firstData;
  93. this.firstValueData = firstValueData;
  94. this.sunData = sunData;
  95. this.upLeftTimes(this.surTimes);
  96. this.newGolds = []; //存储金币元素.
  97. for(let i=0; i < GameProtocol.MAX_ROW_NUM; i++){
  98. for(let j=0; j < GameProtocol.MAX_COL_NUM; j++){
  99. //随机生成.
  100. for(let k=0; k < this.maxItemLen; k++){
  101. let tempValue = this.getRandomValue();
  102. this.elementJS[i][j][k].setValue(tempValue, this.itemSpines[tempValue], 0);
  103. }
  104. //给界面展示的元素赋值.
  105. this.elementJS[i][j][5].setValue(this.firstData[i][j], this.itemSpines[this.firstData[i][j]], this.firstValueData[i][j]);
  106. if(this.isSunGold(this.firstData[i][j])){
  107. this.needRolls[i][j] = false;
  108. this.kuangNode[i][j].active = true;
  109. this.kuangNode[i][j].getComponent(sp.Skeleton).setAnimation(0, "a2", true);
  110. }else{
  111. this.kuangNode[i][j].active = false;
  112. this.needRolls[i][j] = true;
  113. this.maskNode[i][j].active = true;
  114. }
  115. }
  116. }
  117. },
  118. rolls(){
  119. this.surTimes -= 1;
  120. if(this.nowIndex >= this.sunData.length){ //停止滚动
  121. this.rollEnd();
  122. return;
  123. }
  124. this.newGolds = [];
  125. this.upLeftTimes(this.surTimes);
  126. let index =0;
  127. for(let i=0; i< GameProtocol.MAX_COL_NUM; i++){
  128. for(let j=0; j < GameProtocol.MAX_ROW_NUM; j++){
  129. if(this.needRolls[j][i]){ //非金币元素才会滚动
  130. this.rollItem(this.rollBG[j][i], j, i, this.sunData[this.nowIndex].m_desk_data[j][i], this.sunData[this.nowIndex].m_desk_value[j][i], index++);
  131. }
  132. }
  133. }
  134. cc.tween(this.node)
  135. .delay(index * 0.15+2) //延迟能确保上面的滚动能结束
  136. .call(()=>{
  137. for(let k=0; k < this.newGolds.length; k++){
  138. let i = this.newGolds[k].i;
  139. let j = this.newGolds[k].j;
  140. this.needRolls[i][j] = false;
  141. this.kuangNode[i][j].active = true;
  142. this.kuangNode[i][j].getComponent(sp.Skeleton).setAnimation(0, "a2", true);
  143. }
  144. if(this.newGolds.length >0){ //有新金币产出
  145. this.surTimes = 2;
  146. }
  147. })
  148. .delay(0.2)
  149. .call(()=>{
  150. this.nowIndex += 1;
  151. this.rolls();
  152. })
  153. .start();
  154. },
  155. rollItem(tNode, i, j, tarItem, tarValue, idx){
  156. // console.log('tNode: ', tNode, i, j, tarItem, tarValue, idx);
  157. this.elementJS[i][j][1].setValue(tarItem, this.itemSpines[tarItem], tarValue);
  158. // this.elementJS[i][j][5].setValue(tarItem, this.itemSpines[tarItem], tarValue);
  159. tNode.y = 0;
  160. tNode.stopAllActions();
  161. if(this.isSunGold(tarItem)){
  162. this.newGolds.push({'i': i, 'j': j});
  163. }
  164. let preTime = 0.5;
  165. let times = idx*0.1 + preTime*3;
  166. let repT = Math.floor(times/preTime);
  167. let desPos = idx%5;
  168. cc.tween(tNode)
  169. .sequence(
  170. cc.tween().call(()=>{ tNode.y = 0 }),
  171. cc.tween().to(preTime, {y: -4 * this.sizec})
  172. )
  173. .repeat(repT)
  174. .call(()=>{
  175. tNode.y = 0;
  176. this.elementJS[i][j][5-desPos].setValue(tarItem, this.itemSpines[tarItem], tarValue);
  177. })
  178. .to(times-repT*preTime, {y: -desPos*this.sizec})
  179. .call(()=>{
  180. if(this.isSunGold(tarItem)){
  181. this.elementJS[i][j][5-desPos].showGoldLight();
  182. this.maskNode[i][j].active = false;
  183. ScenceManager.Instance().getGameComponent().playEffect("suncoin");
  184. }
  185. })
  186. .start();
  187. },
  188. rollEnd(){
  189. this.unCheck = true;
  190. for(let i = 0; i < GameProtocol.MAX_ROW_NUM; ++i) {
  191. for(let j = 0; j < GameProtocol.MAX_COL_NUM; ++j) {
  192. this.kuangNode[i][j].active = false;
  193. this.maskNode[i][j].active = false;
  194. }
  195. }
  196. let gameJS = ScenceManager.Instance().getGameComponent();
  197. let index = 0;
  198. for(let i=0; i < GameProtocol.MAX_COL_NUM; i++){
  199. for(let j=0; j < GameProtocol.MAX_ROW_NUM; j++){
  200. if(!this.needRolls[j][i]){
  201. let dy = Math.floor(this.rollBG[j][i].y/this.sizec)+5;
  202. let nnode = this.elementNode[j][i][dy];
  203. let label = cc.instantiate(nnode.getChildByName("goldLabel"));
  204. label.parent = this.node;
  205. label.x = arr_posX[i];
  206. label.y = (1-j)*this.sizec+45;
  207. nnode.getChildByName("goldLabel").active = false;
  208. cc.tween(label)
  209. .delay(index*0.3+0.5)
  210. .call(()=>{
  211. this.elementJS[j][i][dy].playGetCoinAni();
  212. })
  213. .to(0.2, {scale: 1.5, y: label.y+50})
  214. .call(()=>{
  215. gameJS.doCoinAni();
  216. })
  217. .to(0.4, {x: 0, y:-290, scale: 1})
  218. .call(()=>{
  219. gameJS.addCoinGet(this.sunData[this.sunData.length-1].m_desk_value[j][i]);
  220. })
  221. .to(0.1, {opacity: 0})
  222. .start()
  223. index++;
  224. }
  225. }
  226. }
  227. cc.tween(this.node)
  228. .delay(index*0.3+1)
  229. .call(()=>{
  230. gameJS.sunGameEnd();
  231. this.initData();
  232. })
  233. .start()
  234. },
  235. getRandomValue() {
  236. return Math.floor(Math.random()*100)%10;
  237. },
  238. isSunGold(val) {
  239. return val == GameProtocol.SUN_ICON;
  240. },
  241. update (dt) {
  242. if(this.unCheck) return;
  243. for(let i=0; i < GameProtocol.MAX_ROW_NUM; i++){
  244. for(let j=0; j < GameProtocol.MAX_COL_NUM; j++){
  245. for(let k=0; k < this.maxItemLen; k++){
  246. let dy = this.elementNode[i][j][k].y + this.rollBG[i][j].y;
  247. this.elementNode[i][j][k].active = (dy > -2*this.sizec) && (dy < 2*this.sizec);
  248. }
  249. }
  250. }
  251. },
  252. });