JettonCtrl.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. var actTime = 0.2;
  2. cc.Class({
  3. extends: cc.BaseClass,
  4. properties: {
  5. m_Item: cc.Prefab,
  6. },
  7. ctor: function () {
  8. this.m_ValueArr = new Array(100, 50, 20, 10, 5, 2, 1); //筹码数值
  9. this.m_Pool = new cc.NodePool('JettonPool'); //回收池
  10. this.m_WaitArr = new Array(); //动作队列
  11. this.m_WaitDelArr = new Array(); //等待删除队列
  12. this.m_reSetCnt = 0; //绘制上限
  13. this.m_ActionItemCnt = 0; //移动中数量
  14. this.m_TableItem = new Array(); //桌面筹码
  15. for (var i in this.m_ValueArr) {
  16. this.m_TableItem[this.m_ValueArr[i]] = new Array();
  17. }
  18. this.m_lTableScore = 0;
  19. this.m_Res = {atlas: null, bundle: null, url: null};
  20. },
  21. ////////////////////////////////////////// 外部调用接口
  22. // 设置资源
  23. SetRes: function(valueArr, atlas, bundle, url) {
  24. if(!Array.isArray(valueArr) || valueArr.length == 0) return false;
  25. if(!atlas && (!bundle || !url)) return false;
  26. this.m_ValueArr = clone(valueArr);
  27. this.m_TableItem = new Array(); //桌面筹码
  28. for (var i in this.m_ValueArr) {
  29. this.m_TableItem[this.m_ValueArr[i]] = new Array();
  30. }
  31. this.m_Res.atlas = atlas ? atlas : null;
  32. this.m_Res.bundle = bundle ? bundle : null;
  33. this.m_Res.url = url ? url : null;
  34. },
  35. //桌面筹码基准点和范围
  36. SetBenchmarkPos: function (centerPos, Width, Height) {
  37. this.m_CenterPos = centerPos;
  38. this.RandomW = Width;
  39. this.RandomH = Height;
  40. },
  41. //筹码飞出坐标
  42. SetUserPos: function (PosArr) {
  43. this.m_UserPos = PosArr;
  44. },
  45. OnUserAdd: function (wChair, Score) {
  46. this._InTable(wChair, Score);
  47. //无动画立刻刷新
  48. if (wChair == INVALID_CHAIR) this._UpdateTableScore();
  49. },
  50. OnUserGet: function (wChair, Score) {
  51. if (this.m_ActionItemCnt > 0) {
  52. this.m_WaitArr.push(['_OutTable', wChair, Score]);
  53. } else {
  54. this._OutTable(wChair, Score);
  55. }
  56. //无动画立刻刷新
  57. if (wChair == INVALID_CHAIR) this._UpdateTableScore();
  58. },
  59. OnGameSetScore: function (Score) {
  60. if (this.m_ActionItemCnt > 0) {
  61. this.m_WaitArr.push(['_SetTableJet', Score, null]);
  62. } else {
  63. this._SetTableJet(Score);
  64. }
  65. },
  66. //////////////////////////////////////////
  67. //桌面筹码随机坐标
  68. _RandomPos: function () {
  69. var Pos = cc.v2(0, 0);
  70. Pos.x = this.m_CenterPos.x + parseInt(Math.random() * 123321) % (this.RandomW * 2) - this.RandomW;
  71. Pos.y = this.m_CenterPos.y + parseInt(Math.random() * 123321) % (this.RandomH * 2) - this.RandomH;
  72. return Pos;
  73. },
  74. //获取待用筹码
  75. _GetItem: function (value, score) {
  76. var TempJet;
  77. if (this.m_Pool.size()) {
  78. TempJet = this.m_Pool.get();
  79. } else {
  80. TempJet = cc.instantiate(this.m_Item);
  81. }
  82. this.node.addChild(TempJet);
  83. var js = TempJet.getComponent('Jetton');
  84. js.SetData({value: value, score: score, atlas: this.m_Res.atlas, bundle: this.m_Res.bundle, url: this.m_Res.url});
  85. this.m_TableItem[value].push(js);
  86. return js;
  87. },
  88. //回收筹码
  89. _DelItem: function (JetNode) {
  90. JetNode.parent = null;
  91. this.m_Pool.put(JetNode);
  92. },
  93. _Score2Value: function(Score) {
  94. return Number(Score / window.PLATFORM_RATIO);
  95. },
  96. _Value2Score: function(Value) {
  97. return Number(Value * window.PLATFORM_RATIO);
  98. },
  99. //向桌子添加筹码
  100. _InTable: function (wChair, Score) {
  101. var temp = this._Score2Value(Score);
  102. for (var i in this.m_ValueArr) {
  103. while (temp >= this.m_ValueArr[i]) {
  104. temp -= this.m_ValueArr[i];
  105. var Item = this._GetItem(this.m_ValueArr[i], this._Value2Score(this.m_ValueArr[i]));
  106. var bAction = (wChair != INVALID_CHAIR);
  107. if(bAction) {
  108. Item.node.setPosition(this.m_UserPos[wChair]);
  109. var ptTo = this._RandomPos();
  110. Item.Move(ptTo, actTime, 0, function(){this._OnMoveStartCallback();}.bind(this), this._OnMoveEndCallback, this);
  111. } else {
  112. Item.node.setPosition(this._RandomPos());
  113. }
  114. }
  115. }
  116. },
  117. //筹码移出桌子
  118. _OutTable: function (wChair, Score) {
  119. var temp = this._Score2Value(Score);
  120. for (var i in this.m_ValueArr) {
  121. while (temp >= this.m_ValueArr[i] && this.m_TableItem[this.m_ValueArr[i]].length) {
  122. temp -= this.m_ValueArr[i];
  123. var Item = this.m_TableItem[this.m_ValueArr[i]].shift();
  124. var bAction = (wChair != INVALID_CHAIR);
  125. if(bAction) {
  126. Item.Move(this.m_UserPos[wChair], actTime, 0, function(){this._OnMoveStartCallback();}.bind(this), this._OnMoveEndDelCallback, this);
  127. this.m_WaitDelArr.push(Item);
  128. } else {
  129. this._DelItem(Item.node);
  130. }
  131. }
  132. }
  133. },
  134. _OnMoveStartCallback: function() {
  135. this.m_ActionItemCnt++;
  136. },
  137. _OnMoveEndCallback: function(tag, param) {
  138. this.m_ActionItemCnt--;
  139. },
  140. _OnMoveEndDelCallback: function(tag, param) {
  141. this.m_ActionItemCnt--;
  142. if (this.m_ActionItemCnt == 0) {
  143. //删除筹码
  144. for (var i in this.m_WaitDelArr) {
  145. this._DelItem(this.m_WaitDelArr[i].node);
  146. }
  147. this.m_WaitDelArr = new Array();
  148. this._UpdateTableScore();
  149. //动作队列
  150. if (this.m_WaitArr.length > 0) {
  151. var obj = this.m_WaitArr.shift();
  152. this[obj[0]](obj[1], obj[2]);
  153. } else {
  154. this._CheckReSet();
  155. }
  156. }
  157. },
  158. _UpdateTableScore: function () {
  159. this.m_lTableScore = this._GetTableScore();
  160. if (this.m_Hook.SetTableScore) this.m_Hook.SetTableScore(this.m_lTableScore);
  161. },
  162. //当前筹码面值
  163. _GetTableScore: function () {
  164. var Score = 0;
  165. for (var i in this.m_TableItem) {
  166. if(!this.m_TableItem[i][0]) continue;
  167. Score += this.m_TableItem[i][0].GetData().score * this.m_TableItem[i].length;
  168. }
  169. return Score;
  170. },
  171. //检查重绘
  172. _CheckReSet: function () {
  173. //无需重绘
  174. if (this.m_reSetCnt == 0) return;
  175. var Cnt = 0;
  176. for (var i in this.m_TableItem) {
  177. Cnt += this.m_TableItem[i].length;
  178. }
  179. if (Cnt < this.m_reSetCnt) return;
  180. var Score = this._GetTableScore();
  181. this._OutTable(INVALID_CHAIR, Score);
  182. this._InTable(INVALID_CHAIR, Score);
  183. },
  184. //设置桌面筹码
  185. _SetTableJet: function (Score) {
  186. if (Score == this._GetTableScore()) return;
  187. if (Score < this._GetTableScore()) {
  188. this._OutTable(INVALID_CHAIR, this._GetTableScore() - Score);
  189. } else {
  190. this._InTable(INVALID_CHAIR, Score - this._GetTableScore());
  191. }
  192. //无动画立刻刷新
  193. this._UpdateTableScore();
  194. },
  195. });