redPoint.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { cocosz } from "../Framework/CocosZ";
  2. import Constant from "../Framework/Constant";
  3. import GameDate from "../Game/gameDate";
  4. const { ccclass, property } = cc._decorator;
  5. enum RedPointType {
  6. none = 0,
  7. sign,
  8. turntable,
  9. online
  10. }
  11. @ccclass
  12. export default class RedPoint extends cc.Component {
  13. @property({ type: cc.Enum(RedPointType), tooltip: "红点类型" })
  14. type: number = RedPointType.none;
  15. @property({ tooltip: "点击父节点刷新红点" })
  16. refreshOnClick: boolean = true;
  17. @property({ tooltip: "是否刷新" })
  18. isRefresh: boolean = false
  19. @property({ tooltip: "是否缓动角度" })
  20. isTweenAngle: boolean = false;
  21. @property({ tooltip: "是否缓动缩放" })
  22. isTweenScale: boolean = false;
  23. @property({ tooltip: "是否监听" })
  24. isListen: boolean = false;
  25. start() {
  26. this.node.opacity = 0;
  27. // 刷新
  28. this.setRedPoint();
  29. if (this.isRefresh) {
  30. cc.tween(this.node)
  31. .delay(1)
  32. .call(() => { this.setRedPoint(); })
  33. .union()
  34. .repeatForever()
  35. .start();
  36. }
  37. // 缓动角度
  38. if (this.isTweenAngle) {
  39. cc.tween(this.node).by(0.25, { angle: 15 }).by(0.25 * 2, { angle: -30 }).by(0.25, { angle: 15 }).delay(1).union().repeatForever().start();
  40. }
  41. // 缓动缩放
  42. if (this.isTweenScale) {
  43. cc.tween(this.node).by(0.25, { scale: 0.1 }).by(0.25 * 2, { scale: -0.2 }).by(0.25, { scale: 0.1 }).delay(1).union().repeatForever().start();
  44. }
  45. // 监听事件
  46. if (this.isListen) {
  47. cc.game.on(Constant.E_GAME_LOGIC, this._onGameMessageHandler, this);
  48. }
  49. }
  50. /** 销毁监听 */
  51. protected onDestroy(): void {
  52. cc.game.targetOff(this);
  53. }
  54. /** 监听事件 */
  55. private _onGameMessageHandler(event: any) {
  56. switch (event.type) {
  57. }
  58. }
  59. /** 监听消息 */
  60. onClick() {
  61. // 本地存储
  62. switch (this.type) { }
  63. // 点击父节点刷新红点
  64. if (this.refreshOnClick) {
  65. this.setRedPoint();
  66. }
  67. }
  68. /** 显示 */
  69. show() {
  70. this.node.opacity = 255;
  71. // 点击监听
  72. if (this.node.parent) {
  73. this.node.parent.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
  74. }
  75. }
  76. /** 隐藏 */
  77. hide() {
  78. this.node.opacity = 0;
  79. // 取消监听
  80. if (this.node.parent) {
  81. this.node.parent.off(cc.Node.EventType.TOUCH_END, this.onClick, this);
  82. }
  83. }
  84. /** 设置红点 */
  85. setRedPoint() {
  86. switch (this.type) {
  87. case RedPointType.sign: {
  88. let bool = (new Date().toDateString() != cocosz.dataMgr.LastDailyBonusTime);
  89. bool ? this.show() : this.hide();
  90. break;
  91. }
  92. case RedPointType.turntable: {
  93. let bool = (cocosz.useCJTimes < Constant.commonCJTimes);
  94. bool ? this.show() : this.hide();
  95. break;
  96. }
  97. case RedPointType.online: {
  98. let arr = cocosz.dataMgr.receiveToday;
  99. for (let i = 0; i < arr.length; i++) {
  100. if (!arr[i]) {
  101. if (cocosz.dataMgr.OnlineToday > GameDate.TimeReward[i].time) {
  102. this.show();
  103. return;
  104. }
  105. }
  106. }
  107. this.hide();
  108. break;
  109. }
  110. }
  111. }
  112. }