ModulerBase.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { _decorator, Button, Component, Constructor, Label, Node, PageView, Sprite } from 'cc';
  2. import { GameMgr } from './GameMgr';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('ModulerBase')
  5. export class ModulerBase extends Component {
  6. protected _nodes: Map<string, Node> = new Map();
  7. protected _nodeName: string = "Node";
  8. get NodeName() { return this._nodeName };
  9. set NodeName(name: string) { this._nodeName = name }
  10. init() {
  11. this._visit(this.node);
  12. this.onStart();
  13. }
  14. protected onStart() {
  15. }
  16. //刷新
  17. refresh() {
  18. }
  19. //获取管理者中的其他模块
  20. getModuler<T extends ModulerBase>(type: Constructor<T>): T {
  21. return GameMgr.Instance.getModuler(type);
  22. }
  23. //销毁自己
  24. removeSelf() {
  25. this.node.destroy();
  26. this.clearSelf();
  27. }
  28. protected clearSelf() {
  29. }
  30. private _visit(node: Node) {
  31. if (node.name.startsWith("_")) {
  32. this._nodes.set(node.name, node);
  33. }
  34. for (const child of node.children) {
  35. this._visit(child);
  36. }
  37. }
  38. show() {
  39. this.node.active = true;
  40. this.use;
  41. }
  42. showing() {
  43. return this.node.active;
  44. }
  45. hide(clear: boolean = false) {
  46. this.unUse();
  47. if (clear) {
  48. this.node.destroy();
  49. return;
  50. }
  51. this.node.active = false;
  52. }
  53. onBtnClick(name: string, callback: Function, ...arg) {
  54. const node = this._nodes.get(name);
  55. if (!node) {
  56. Error("No find");
  57. return;
  58. }
  59. if (node) {
  60. node.on(Button.EventType.CLICK, () => {
  61. callback.apply(this, [...arg]);
  62. })
  63. }
  64. }
  65. onPageView(name: string, type, callback: Function) {
  66. this._nodes.get(name).on(type, callback);
  67. }
  68. getPageView(name: string): PageView {
  69. return this._nodes.get(name).getComponent(PageView);
  70. }
  71. getNode(name: string): Node {
  72. return this._nodes.get(name);
  73. }
  74. getLabel(name: string): Label {
  75. return this.getNode(name).getComponent(Label);
  76. }
  77. getSprite(name: string): Sprite {
  78. return this.getNode(name).getComponent(Sprite);
  79. }
  80. protected use() {
  81. }
  82. protected unUse() {
  83. }
  84. }