1
0

UserInfo.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. }
  29. },
  30. grossIncome: {
  31. get: function () {
  32. return this._grossIncome;
  33. },
  34. set: function (value) {
  35. this._grossIncome = value;
  36. this.grossIncomeLabel.string = DWTool.coinParse(this._grossIncome);
  37. }
  38. },
  39. // rate: {
  40. // get: function() {
  41. // return this._rate;
  42. // },
  43. // set: function(value) {
  44. // this._rate = value;
  45. // this.rateLabel.string = DWTool.coinParse(this._rate) + " / 秒"
  46. // }
  47. // },
  48. diamond: {
  49. get: function() {
  50. return this._diamond;
  51. },
  52. set: function(value) {
  53. this._diamond = value;
  54. this.diamondLabel.string = this._diamond;
  55. }
  56. },
  57. },
  58. onLoad() {
  59. GameModule.userInfo = this;
  60. this._stars = 0;
  61. this._grossIncome = 0;
  62. this._rate = 0;
  63. this._isPlayAnimation = false;
  64. this.seq = 1;
  65. this.headSprite.node.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
  66. Global.GameEvent.fire(NotiKey.ShowUserInfomation, this.userInfo.id);
  67. }, 1000, true), this);
  68. //监听用户收取金币事件
  69. Global.GameEvent.on(NotiKey.UserCollectCoin, this, (flag) => {
  70. console.log(flag);
  71. let animKey = flag ? 'jinbi_huoqu2' : 'jinbi_huoqu'
  72. this.grossCoin.setAnimation(0, animKey, false)
  73. })
  74. this.reportFunc = () => {
  75. if (this._reportFailDuration > 0) {
  76. this._reportFailDuration -= 5;
  77. return;
  78. }
  79. DWTool.reportInfo(this.seq, this.grossIncome, this.stars, this.recordModify, this.recordUnlockModify)
  80. .then((result) => {
  81. this._reportFailDuration = 0;
  82. this.recordModify = [];
  83. this.recordUnlockModify = [];
  84. this.seq += 1;
  85. }).catch((err) => {
  86. if (err.code === -4) {
  87. this._reportFailDuration = 60;
  88. }
  89. cc.error(err.msg);
  90. });
  91. }
  92. this.schedule(this.reportFunc, 5.0);
  93. },
  94. setUserInfo(userInfo) {
  95. if (arguments.length < 1) {
  96. throw new Error("setUserInfo Missing parameter...");
  97. }
  98. this.userInfo = userInfo;
  99. Api.createImageFromUrl(userInfo.head, (spriteFrame) => {
  100. this.headSprite.spriteFrame = spriteFrame;
  101. }, null);
  102. },
  103. setGrossIncomeAndStars(grossIncome, stars) {
  104. if (arguments.length < 2) {
  105. throw new Error("setGrossIncomeAndStars Missing parameter...");
  106. }
  107. this.grossIncome = grossIncome;
  108. this.stars = stars;
  109. },
  110. updateStarAnimation() {
  111. if (this._isPlayAnimation) { return; }
  112. this._isPlayAnimation = true;
  113. let jumpHeight = 30;
  114. let duration = 0.2;
  115. let animationArray = [];
  116. while(jumpHeight > 0.1) {
  117. jumpHeight = jumpHeight - (jumpHeight / 3);
  118. duration = duration - (duration / 3);
  119. let upAction = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
  120. let downAction = cc.moveBy(duration, 0, -jumpHeight).easing(cc.easeCubicActionIn());
  121. animationArray.push(upAction);
  122. animationArray.push(downAction);
  123. }
  124. let callback = cc.callFunc(() => {
  125. this._isPlayAnimation = false;
  126. });
  127. animationArray.push(callback);
  128. this.starsLabel.node.runAction(cc.sequence(animationArray));
  129. },
  130. updateRecordModify(buildingInfo) {
  131. for (let i = 0; i < this.recordModify.length; i++) {
  132. let temp = this.recordModify[i];
  133. if (buildingInfo.buildingId == temp.buildingId) {
  134. this.recordModify.splice(i, buildingInfo)
  135. return;
  136. }
  137. }
  138. this.recordModify.push(buildingInfo);
  139. }
  140. });