UserInfo.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. const GameModule = require("./utils/GameModule");
  2. const DWTool = require("./utils/DWTool");
  3. const Api = require('./net/Api');
  4. const NotiKey = require('./utils/GameEnum').GameNotificationKey;
  5. const BuildingModel = require('./utils/BuildingModel');
  6. const AlertManager = require('./utils/AlertManager');
  7. cc.Class({
  8. extends: cc.Component,
  9. properties: {
  10. _reportFailDuration: 0,
  11. grossIncomeLabel: cc.Label,
  12. grossCoin: sp.Skeleton,
  13. // rateLabel: cc.Label,
  14. headSprite: cc.Sprite,
  15. starsLabel: cc.Label,
  16. starNode: cc.Node,
  17. diamondLabel: cc.Label,
  18. recordModify: [],
  19. recordUnlockModify: [],
  20. stars: {
  21. get: function () {
  22. return this._stars;
  23. },
  24. set: function (value) {
  25. this._stars = value;
  26. this.starsLabel.string = this._stars;
  27. this.updateStarAnimation();
  28. DWTool.submitWechatStars(this._stars);
  29. }
  30. },
  31. grossIncome: {
  32. get: function () {
  33. return this._grossIncome;
  34. },
  35. set: function (value) {
  36. this._grossIncome = value;
  37. this.grossIncomeLabel.string = DWTool.coinParse(this._grossIncome);
  38. }
  39. },
  40. // rate: {
  41. // get: function() {
  42. // return this._rate;
  43. // },
  44. // set: function(value) {
  45. // this._rate = value;
  46. // this.rateLabel.string = DWTool.coinParse(this._rate) + " / 秒"
  47. // }
  48. // },
  49. diamond: {
  50. get: function () {
  51. return this._diamond;
  52. },
  53. set: function (value) {
  54. this._diamond = value;
  55. this.diamondLabel.string = this._diamond;
  56. }
  57. },
  58. levelHomeItemFullCount: 0
  59. },
  60. onLoad() {
  61. GameModule.userInfo = this;
  62. this._stars = 0;
  63. this._grossIncome = 0;
  64. this._rate = 0;
  65. this._isPlayAnimation = false;
  66. this.seq = 1;
  67. this.headSprite.node.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
  68. GameEvent.fire(NotiKey.ShowUserInfomation, this.userInfo.uid);
  69. }, 1000, true), this);
  70. //监听用户收取金币事件
  71. GameEvent.on(NotiKey.UserCollectCoin, this, (flag) => {
  72. console.log(flag);
  73. let animKey = flag ? 'jinbi_huoqu2' : 'jinbi_huoqu'
  74. this.grossCoin.setAnimation(0, animKey, false)
  75. });
  76. GameEvent.on(NotiKey.LevelHomeItemBuildingFull, this, () => {
  77. this.levelHomeItemFullCount += 1;
  78. if (this.levelHomeItemFullCount === 5) {
  79. // 满级后去别的城市就置零
  80. this.levelHomeItemFullCount = 0;
  81. GameEvent.fire(NotiKey.LevelHomeItemBuildingAllFull);
  82. GameEvent.fire(NotiKey.CurrentCompanyMax);
  83. }
  84. });
  85. this.reportFunc = () => {
  86. if (this._reportFailDuration > 0) {
  87. this._reportFailDuration -= 5;
  88. return;
  89. }
  90. DWTool.reportInfo(this.seq, this.grossIncome, this.stars, this.recordModify, this.recordUnlockModify)
  91. .then((result) => {
  92. this._reportFailDuration = 0;
  93. this.recordModify = [];
  94. this.recordUnlockModify = [];
  95. this.seq += 1;
  96. }).catch((err) => {
  97. if (err.code === -4) {
  98. this._reportFailDuration = 60;
  99. }
  100. console.log(err.msg);
  101. });
  102. }
  103. this.schedule(this.reportFunc, 3.0);
  104. },
  105. setUserInfo(userInfo) {
  106. if (arguments.length < 1) {
  107. throw new Error("setUserInfo Missing parameter...");
  108. }
  109. this.userInfo = userInfo;
  110. Api.createImageFromUrl(userInfo.head, (spriteFrame) => {
  111. this.headSprite.spriteFrame = spriteFrame;
  112. }, null);
  113. },
  114. setGrossIncomeAndStars(grossIncome, stars) {
  115. if (arguments.length < 2) {
  116. throw new Error("setGrossIncomeAndStars Missing parameter...");
  117. }
  118. this.grossIncome = grossIncome;
  119. this.stars = stars;
  120. },
  121. updateStarAnimation() {
  122. if (this._isPlayAnimation) { return; }
  123. this._isPlayAnimation = true;
  124. let jumpHeight = 30;
  125. let duration = 0.2;
  126. let animationArray = [];
  127. while (jumpHeight > 0.1) {
  128. jumpHeight = jumpHeight - (jumpHeight / 3);
  129. duration = duration - (duration / 3);
  130. let upAction = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
  131. let downAction = cc.moveBy(duration, 0, -jumpHeight).easing(cc.easeCubicActionIn());
  132. animationArray.push(upAction);
  133. animationArray.push(downAction);
  134. }
  135. let callback = cc.callFunc(() => {
  136. this._isPlayAnimation = false;
  137. });
  138. animationArray.push(callback);
  139. this.starsLabel.node.runAction(cc.sequence(animationArray));
  140. },
  141. updateRecordModify(buildingInfo) {
  142. for (let i = 0; i < this.recordModify.length; i++) {
  143. let temp = this.recordModify[i];
  144. if (buildingInfo.buildingId == temp.buildingId) {
  145. this.recordModify.splice(i, buildingInfo)
  146. return;
  147. }
  148. }
  149. this.recordModify.push(buildingInfo);
  150. }
  151. });