MessageList.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  2. // const GameModule = require("../utils/GameModule");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. messageItem: cc.Prefab,
  7. /// 最大同时8条信息流
  8. maxCount: 8,
  9. },
  10. // LIFE-CYCLE CALLBACKS:
  11. onLoad () {
  12. this._items = [];
  13. this._itemsData = [];
  14. this.updateInitData(-1);
  15. GameEvent.on(GameNotificationKey.GameUpdateMessageList, this, (updateCount, isTime = false) => {
  16. // this._updateCount =
  17. this.updateInitData(updateCount, isTime);
  18. });
  19. this.schedule(this.timeAction, 1);
  20. },
  21. updateInitData(updateCount, isTime) {
  22. let timeInformations = Global._timeInformations;
  23. let itemDatas = timeInformations.concat(Global._fixInformations);
  24. let originLength = this._itemsData.length;
  25. let upLength = updateCount == -1 ? itemDatas.length : updateCount;
  26. let max = upLength + originLength;
  27. let sub = max - 8;
  28. /// 如果当前的长度已经大于8了
  29. if (sub > 0) {
  30. //// 如果是实时的信息流 那么减去所加的
  31. let timeLength = isTime ? timeInformations.length - updateCount : timeInformations.length;
  32. let arrIndex = timeInformations.length;
  33. for (let i = 0; i < sub; i++) {
  34. let index = timeLength + i;
  35. let item = this._items[index];
  36. this._items.splice(index, 1);
  37. item.active = false;
  38. item.destroy();
  39. item = null;
  40. this._itemsData.splice(index, 1);
  41. itemDatas.splice(arrIndex + i, 1);
  42. Global._fixInformations.splice(i, 1);
  43. }
  44. max -= sub;
  45. originLength -= sub;
  46. }
  47. for (let i = originLength; i < max; ++i) {
  48. let item = cc.instantiate(this.messageItem);
  49. let itemData = itemDatas[i];
  50. item.getComponent('MessageItem').init(itemData);
  51. this.node.addChild(item);
  52. this._items.push(item);
  53. }
  54. this._itemsData = itemDatas;
  55. if (isTime === true) {
  56. this.reloadAllItemData();
  57. }
  58. },
  59. reloadAllItemData() {
  60. for(let i = 0; i < this._items.length; ++ i) {
  61. let item = this._items[i];
  62. let itemData = this._itemsData[i];
  63. item.getComponent('MessageItem').init(itemData);
  64. }
  65. },
  66. timeAction() {
  67. let hiddenIndexArr = [];
  68. for (let i = 0; i < this._items.length; ++ i) {
  69. let itemNode = this._items[i];
  70. let itemScript = itemNode.getComponent('MessageItem');
  71. itemScript.updateTime();
  72. let messageData = itemScript._messageData;
  73. /// 如果是技能并且可以使用的时候 那么就什么都不修改
  74. if (messageData.type === 2 && messageData.skillStatus === 0) {
  75. continue;
  76. }
  77. if (messageData.cdTime === 0) {
  78. hiddenIndexArr.push(i);
  79. }
  80. }
  81. if (hiddenIndexArr.length > 0) {
  82. for (let i = 0; i < hiddenIndexArr.length; ++ i) {
  83. this.hiddenItem(hiddenIndexArr[i]);
  84. }
  85. }
  86. },
  87. hiddenItem(index) {
  88. if (index < this._items.length) {
  89. let item = this._items[index];
  90. let fadeAction = cc.fadeTo(0.5, 0); //0.5秒透明度从255降到0
  91. let action = cc.sequence(fadeAction, cc.callFunc(() => {
  92. this.deleteItem(index);
  93. }));
  94. item.runAction(action);
  95. }
  96. },
  97. deleteItem(index) {
  98. if (index < this._items.length) {
  99. let item = this._items[index];
  100. item.active = false;
  101. this._items.splice(index, 1);
  102. item.destroy();
  103. item = null;
  104. this._itemsData.splice(index, 1);
  105. if (index < Global._timeInformations.length) {
  106. Global._timeInformations.splice(index, 1);
  107. } else {
  108. Global._fixInformations.splice(index - Global._timeInformations.length - 1, 1);
  109. }
  110. }
  111. },
  112. start () {
  113. },
  114. // update (dt) {},
  115. });