GoodsItem.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. var Event = require("Event");
  2. var UserState = require("UserState");
  3. var GoodsItem = cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. //UI
  7. Atlas:cc.SpriteAtlas,
  8. IconSp:cc.Sprite,
  9. NameLbl:cc.Sprite,
  10. PriceLbl:cc.Label,
  11. State:1, //状态
  12. //Data
  13. Id:cc.Integer,
  14. price:cc.Integer,
  15. Data:null,
  16. },
  17. init:function (data) {
  18. this.Data = data;
  19. this.Id = data.Id;
  20. this.IconSp.spriteFrame = this.Atlas.getSpriteFrame(data.icon);
  21. this.NameLbl.spriteFrame = this.Atlas.getSpriteFrame(data.name);
  22. this.priceId = 0; //所在的价格区间
  23. this.refreshPrice();
  24. },
  25. refreshLowPrice:function(){
  26. var priceList = this.Data.priceList;
  27. var price = priceList[0].lowprice;
  28. this.priceId = 0;
  29. var d = Math.random();
  30. price = priceList[0].lowprice + d*(priceList[0].highprice - priceList[0].lowprice);
  31. this.price = Math.floor(price);
  32. this.Data.price = this.price;
  33. cc.Mgr.UserDataMgr.RefreshGoodsMarKetPrice(this.Id, this.price);
  34. this.PriceLbl.string = this.price.toString();
  35. var param = {};
  36. this.State = 0;
  37. param.State = 0; //-2表示暴跌 -1表示跌了 0 表示稳定 1有所增长 2表示暴涨
  38. param.Id = this.Id;
  39. param.icon = this.Data.icon;
  40. param.name = this.Data.name;
  41. param.price = this.price;
  42. return param;
  43. },
  44. refreshMidPrice:function(){
  45. var priceList = this.Data.priceList;
  46. var price = priceList[0].lowprice;
  47. this.priceId = 1;
  48. var d = Math.random();
  49. price = priceList[1].lowprice + d*(priceList[1].highprice - priceList[1].lowprice);
  50. this.price = Math.floor(price);
  51. this.Data.price = this.price;
  52. cc.Mgr.UserDataMgr.RefreshGoodsMarKetPrice(this.Id, this.price);
  53. this.PriceLbl.string = this.price.toString();
  54. var param = {};
  55. this.State = 1;
  56. param.State = 1; //-2表示暴跌 -1表示跌了 0 表示稳定 1有所增长 2表示暴涨
  57. param.Id = this.Id;
  58. param.icon = this.Data.icon;
  59. param.name = this.Data.name;
  60. param.price = this.price;
  61. return param;
  62. },
  63. refreshUpPrice:function(){
  64. var priceList = this.Data.priceList;
  65. var price = priceList[0].lowprice;
  66. /*
  67. var seed = 0.99999;
  68. if(seed < priceList[0].probability)
  69. {
  70. change = 0; //-this.priceId;
  71. this.priceId = 0;
  72. var d = Math.random();
  73. price = priceList[0].lowprice + d*(priceList[0].highprice - priceList[0].lowprice);
  74. }
  75. else if(seed <= priceList[0].probability + priceList[1].probability)
  76. {
  77. change = 1; //-this.priceId;
  78. this.priceId = 1;
  79. var d = Math.random();
  80. price = priceList[1].lowprice + d*(priceList[1].highprice - priceList[1].lowprice);
  81. }
  82. else
  83. {
  84. change = 2;//-this.priceId;
  85. this.priceId = 2;
  86. var d = Math.random();
  87. price = priceList[2].lowprice + (priceList[2].highprice - priceList[2].lowprice);
  88. }
  89. */
  90. this.priceId = 2;
  91. var d = Math.random();
  92. price = priceList[2].lowprice + d*(priceList[2].highprice - priceList[2].lowprice);
  93. this.State = 2;
  94. this.price = Math.floor(price);
  95. this.Data.price = this.price;
  96. cc.Mgr.UserDataMgr.RefreshGoodsMarKetPrice(this.Id, this.price);
  97. this.PriceLbl.string = this.price.toString();
  98. var param = {};
  99. param.State = 2; //-2表示暴跌 -1表示跌了 0 表示稳定 1有所增长 2表示暴涨
  100. param.Id = this.Id;
  101. param.icon = this.Data.icon;
  102. param.name = this.Data.name;
  103. param.price = this.price;
  104. return param;
  105. },
  106. //刷新价格信息
  107. refreshPrice:function(){
  108. var priceList = this.Data.priceList;
  109. var price = priceList[0].lowprice;
  110. var change = 0;
  111. var seed = Math.random();
  112. if(seed < priceList[0].probability)
  113. {
  114. change = 0; //-this.priceId;
  115. this.priceId = 0;
  116. var d = Math.random();
  117. price = priceList[0].lowprice + d*(priceList[0].highprice - priceList[0].lowprice);
  118. }
  119. else if(seed <= priceList[0].probability + priceList[1].probability)
  120. {
  121. change = 1; //-this.priceId;
  122. this.priceId = 1;
  123. var d = Math.random();
  124. price = priceList[1].lowprice + d*(priceList[1].highprice - priceList[1].lowprice);
  125. }
  126. else
  127. {
  128. change = 2;//-this.priceId;
  129. this.priceId = 2;
  130. var d = Math.random();
  131. price = priceList[2].lowprice + d*(priceList[2].highprice - priceList[2].lowprice);
  132. }
  133. this.State = change;
  134. this.price = Math.floor(price);
  135. this.Data.price = this.price;
  136. cc.Mgr.UserDataMgr.RefreshGoodsMarKetPrice(this.Id, this.price);
  137. this.PriceLbl.string = this.price.toString();
  138. var param = {};
  139. param.State = change; //-2表示暴跌 -1表示跌了 0 表示稳定 1有所增长 2表示暴涨
  140. param.Id = this.Id;
  141. param.icon = this.Data.icon;
  142. param.name = this.Data.name;
  143. param.price = this.price;
  144. return param;
  145. },
  146. OpenBuyTipPanel:function(){
  147. cc.Mgr.AudioMgr.playSFX("goods_" + this.Id);
  148. if(cc.Mgr.UserDataMgr.userState == UserState.NotOK)
  149. {
  150. var param = {};
  151. param.text = "您还处在恢复阶段,目前不能交易";
  152. cc.director.GlobalEvent.emit(cc.Mgr.Event.OpenCommonTip, param);
  153. return;
  154. }
  155. //cc.log("打开购买窗口 = " + this.Data.name);
  156. cc.director.GlobalEvent.emit(cc.Mgr.Event.OpenBuyTip, this.Data);
  157. },
  158. });
  159. module.exports = GoodsItem;