JetCtrl.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. var actTime = 0.2;
  2. cc.Class({
  3. extends: cc.BaseClass,
  4. properties: {
  5. m_JetPre:cc.Prefab,
  6. },
  7. ctor:function(){
  8. this.m_JetValue = new Array( 100, 50, 20, 10, 5, 2, 1); //筹码数值
  9. this.m_JetPool = new cc.NodePool('JetPool'); //回收池
  10. this.m_WaitArr = new Array(); //动作队列
  11. this.m_WaitDelArr = new Array(); //等待删除队列
  12. this.m_reSetCnt = 40; //绘制上限
  13. this.m_ActJetCount = 0; //移动中数量
  14. this.m_TableJet = new Array(); //桌面筹码
  15. for(var i in this.m_JetValue){
  16. this.m_TableJet[this.m_JetValue[i]] = new Array();
  17. }
  18. this.m_lTableScore = 0;
  19. },
  20. ////////////////////////////////////////// view 调用接口
  21. OnUserAdd:function (wChair, Score){
  22. this.Jet2Table(wChair, Score);
  23. //无动画立刻刷新
  24. if(wChair == INVALID_CHAIR) this.UpdateTableScore();
  25. },
  26. OnUserGet:function (wChair, Score){
  27. if( this.m_ActJetCount > 0){
  28. this.m_WaitArr.push(['JetOutTable', wChair, Score]);
  29. }else{
  30. this.JetOutTable(wChair, Score);
  31. }
  32. //无动画立刻刷新
  33. if(wChair == INVALID_CHAIR) this.UpdateTableScore();
  34. },
  35. OnGameSetScore:function (Score){
  36. if( this.m_ActJetCount > 0){
  37. this.m_WaitArr.push(['SetTableJet', Score, null]);
  38. }else{
  39. this.SetTableJet(Score);
  40. }
  41. },
  42. //////////////////////////////////////////
  43. //获取待用筹码
  44. GetJet:function(value){
  45. var TempJet;
  46. if(this.m_JetPool.size()){
  47. TempJet = this.m_JetPool.get();
  48. }else{
  49. TempJet = cc.instantiate(this.m_JetPre);
  50. }
  51. this.node.addChild(TempJet);
  52. var js = TempJet.getComponent('JetPre');
  53. js.SetJet(value);
  54. this.m_TableJet[value].push(js);
  55. return js;
  56. },
  57. //回收筹码
  58. DelJet:function(JetNode){
  59. JetNode.parent = null;
  60. this.m_JetPool.put(JetNode);
  61. },
  62. //桌面筹码基准点和范围
  63. SetBenchmarkPos:function (centerPos, Width, Height){
  64. this.m_CenterPos = centerPos;
  65. this.RandomW = Width;
  66. this.RandomH = Height;
  67. },
  68. //筹码飞出坐标
  69. SetUserPos:function (PosArr){
  70. this.m_UserPos = PosArr;
  71. },
  72. //桌面筹码随机坐标
  73. GetRandomPos:function (){
  74. var Pos = cc.v2(0,0);
  75. Pos.x = this.m_CenterPos.x + parseInt(Math.random() * 123321) % (this.RandomW*2) - this.RandomW;
  76. Pos.y = this.m_CenterPos.y + parseInt(Math.random() * 123321) % (this.RandomH*2) - this.RandomH;
  77. return Pos;
  78. },
  79. //筹码移动动作
  80. SetNodeAct:function (node, ePos){
  81. this.m_ActJetCount++;
  82. var act = cc.sequence(cc.moveTo(actTime, ePos), cc.callFunc(this.EndCallFunc, this, this));
  83. node.runAction(act);
  84. },
  85. //向桌子添加筹码
  86. Jet2Table:function(wChair, Score){
  87. var TempScore = Score;
  88. for(var i in this.m_JetValue){
  89. while(TempScore >= this.m_JetValue[i]){
  90. TempScore -= this.m_JetValue[i];
  91. var JetJs = this.GetJet(this.m_JetValue[i]);
  92. if(wChair == INVALID_CHAIR){
  93. JetJs.node.setPosition(this.GetRandomPos());
  94. }else{
  95. JetJs.node.setPosition(this.m_UserPos[wChair]);
  96. this.SetNodeAct(JetJs.node,this.GetRandomPos());
  97. }
  98. }
  99. }
  100. },
  101. //筹码移出桌子
  102. JetOutTable:function(wChair, Score){
  103. var TempScore = Score;
  104. for(var i in this.m_JetValue){
  105. while(TempScore >= this.m_JetValue[i] && this.m_TableJet[this.m_JetValue[i]].length){
  106. TempScore -= this.m_JetValue[i];
  107. var JetJs = this.m_TableJet[this.m_JetValue[i]].shift();
  108. if(wChair == INVALID_CHAIR){
  109. this.DelJet(JetJs.node);
  110. }else{
  111. this.SetNodeAct(JetJs.node,this.m_UserPos[wChair]);
  112. this.m_WaitDelArr.push(JetJs);
  113. }
  114. }
  115. }
  116. },
  117. //移动完成回调
  118. EndCallFunc:function (node, para){
  119. cc.gSoundRes.PlaySound('Jet');
  120. para.m_ActJetCount--;
  121. if( para.m_ActJetCount == 0){
  122. //删除筹码
  123. for(var i in para.m_WaitDelArr){
  124. para.DelJet(para.m_WaitDelArr[i].node);
  125. }
  126. para.m_WaitDelArr = new Array();
  127. para.UpdateTableScore();
  128. if(para.m_Hook.SetTableScore)para.m_Hook.SetTableScore(para.m_lTableScore);
  129. //动作队列
  130. if( para.m_WaitArr.length > 0){
  131. var obj = para.m_WaitArr.shift();
  132. para[obj[0]](obj[1], obj[2]);
  133. }else {
  134. para.CheckReSet();
  135. }
  136. }
  137. },
  138. UpdateTableScore:function(){
  139. this.m_lTableScore = this.GetTableScore();
  140. if(this.m_Hook.SetTableScore)this.m_Hook.SetTableScore(this.m_lTableScore);
  141. },
  142. //当前筹码面值
  143. GetTableScore:function(){
  144. var Score = 0;
  145. for(var i in this.m_TableJet){
  146. Score += i * this.m_TableJet[i].length;
  147. }
  148. return Score;
  149. },
  150. //检查重绘
  151. CheckReSet:function (){
  152. //无需重绘
  153. if( this.m_reSetCnt == 0) return;
  154. var Cnt = 0;
  155. for(var i in this.m_TableJet){
  156. Cnt += this.m_TableJet[i].length;
  157. }
  158. if(Cnt < this.m_reSetCnt) return;
  159. var Score=this.GetTableScore();
  160. this.JetOutTable(INVALID_CHAIR, Score);
  161. this.Jet2Table(INVALID_CHAIR, Score);
  162. },
  163. //设置桌面筹码
  164. SetTableJet:function (Score){
  165. if(Score == this.GetTableScore()) return
  166. if(Score < this.GetTableScore()){
  167. this.JetOutTable(INVALID_CHAIR,this.GetTableScore() - Score);
  168. }else{
  169. this.Jet2Table(INVALID_CHAIR, Score - this.GetTableScore());
  170. }
  171. //无动画立刻刷新
  172. this.UpdateTableScore();
  173. },
  174. // update (dt) {},
  175. });