ModulerBase.ts 2.6 KB

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