BasicObject.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import CreateID from "../Tool/Tool/CreateID";
  2. import EventManager from "../Event/EventManager";
  3. import Tool from "../Tool/Tool";
  4. /**
  5. * 基础节点 - 逻辑层
  6. */
  7. export default abstract class BasicObject extends cc.Component {
  8. /**唯一ID */
  9. protected _id: string;
  10. /**组件列表 */
  11. protected _components: Array<BasicObject>;
  12. /**父节点 */
  13. protected _parent: BasicObject;
  14. /**自定义数据 */
  15. protected _customData: any;
  16. /**
  17. * 是否启用
  18. * @see 若为false,将不会接收消息
  19. */
  20. protected _isEnabled: boolean;
  21. /**是否被管理器调用_update方法 */
  22. protected _isUpdate: boolean;
  23. /**是否销毁 */
  24. protected _isDestroy: boolean;
  25. constructor () {
  26. super();
  27. this._id = CreateID.createID()+"";
  28. this._components = new Array<BasicObject>();
  29. this._customData = {};
  30. this._isEnabled = true;
  31. this._isUpdate = false;
  32. this._isDestroy = false;
  33. }
  34. /**设置唯一ID */
  35. public set id (id: string) {
  36. this._id = id;
  37. }
  38. /**获得唯一ID */
  39. public get id (): string {
  40. return this._id;
  41. }
  42. /**设置自定义数据 */
  43. public set customData (data: any) {
  44. this._customData = Tool.dataAllCover(this._customData, data);
  45. }
  46. /**获得自定义数据 */
  47. public get customData (): any {
  48. return this._customData;
  49. }
  50. /**获得父节点 */
  51. public get parent (): BasicObject {
  52. return this._parent;
  53. }
  54. /**是否启用 */
  55. public set isEnabled (isEnabled: boolean) {
  56. this._isEnabled = isEnabled;
  57. }
  58. /**是否启用 */
  59. public get isEnabled (): boolean {
  60. return this._isEnabled;
  61. }
  62. /**是否被管理器调用_update方法 */
  63. public set isUpdate (isUpdate: boolean) {
  64. this._isUpdate = isUpdate;
  65. }
  66. /**是否被管理器调用_update方法 */
  67. public get isUpdate (): boolean {
  68. return this._isUpdate;
  69. }
  70. /**是否销毁 */
  71. public get isDestroy (): boolean {
  72. return this._isDestroy;
  73. }
  74. /**添加组件实例 */
  75. public addComponentIntance (component: BasicObject): any {
  76. if (component instanceof BasicObject) {
  77. this._components.push(component);
  78. component._parent = this;
  79. component.setNode(this.node);
  80. component.init();
  81. }
  82. return component;
  83. }
  84. /**删除组件实例 */
  85. public removeComponentIntance (component: BasicObject, isDestroy: boolean = true): void {
  86. for (let index in this._components) {
  87. if (this._components[index].id == component.id) {
  88. component._parent = null;
  89. component.setNode(null);
  90. if (isDestroy == true) {
  91. component._destroy();
  92. }
  93. this._components.splice(parseInt(index), 1);
  94. break;
  95. }
  96. }
  97. }
  98. /**获得组件 */
  99. public _getComponent (componentType: any): any {
  100. for (let index in this._components) {
  101. if (this._components[index] instanceof componentType) {
  102. return this._components[index];
  103. }
  104. }
  105. return null;
  106. }
  107. /**删除组件 */
  108. public _removeComponent (componentType: any): any {
  109. for (let index in this._components) {
  110. let component = this._components[index];
  111. if (component instanceof componentType) {
  112. this._components.splice(parseInt(index), 1);
  113. return component;
  114. }
  115. }
  116. return null;
  117. }
  118. /**获得组件列表 */
  119. public _getComponents (componentType: any): Array<any> {
  120. let _components: Array<any> = [];
  121. for (let index in this._components) {
  122. if (this._components[index] instanceof componentType) {
  123. _components.push(this._components[index]);
  124. }
  125. }
  126. return _components;
  127. }
  128. /**设置渲染节点 */
  129. private setNode (node: cc.Node): void {
  130. this.node = node;
  131. for (let index in this._components) {
  132. if (this._components[index].node == null) {
  133. this._components[index].node = node;
  134. }
  135. }
  136. }
  137. /**初始化 */
  138. protected init (): void {
  139. }
  140. /**帧循环 */
  141. public _update (...data: any): void {
  142. for (let index in this._components) {
  143. if (this._components[index].isUpdate == true) {
  144. this._components[index]._update(...data);
  145. }
  146. }
  147. }
  148. /**对外提供自定义销毁方法,执行 onDestroy,destroy */
  149. _destroy (): void {
  150. this.onDestroy();
  151. if (this.node != null) {
  152. this.destroy();
  153. }
  154. }
  155. /**销毁节点 */
  156. onDestroy () {
  157. if (this._isDestroy == true) {
  158. return;
  159. }
  160. for (let index in this._components) {
  161. this._components[index]._destroy();
  162. }
  163. this._components = [];
  164. this._isEnabled = false;
  165. this._isDestroy = true;
  166. this.unscheduleAllCallbacks();
  167. EventManager.offEventByTarget(this);
  168. }
  169. }