123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- import CreateID from "../Tool/Tool/CreateID";
- import EventManager from "../Event/EventManager";
- import Tool from "../Tool/Tool";
- /**
- * 基础节点 - 逻辑层
- */
- export default abstract class BasicObject extends cc.Component {
- /**唯一ID */
- protected _id: string;
- /**组件列表 */
- protected _components: Array<BasicObject>;
- /**父节点 */
- protected _parent: BasicObject;
- /**自定义数据 */
- protected _customData: any;
- /**
- * 是否启用
- * @see 若为false,将不会接收消息
- */
- protected _isEnabled: boolean;
- /**是否被管理器调用_update方法 */
- protected _isUpdate: boolean;
- /**是否销毁 */
- protected _isDestroy: boolean;
- constructor () {
- super();
- this._id = CreateID.createID()+"";
- this._components = new Array<BasicObject>();
- this._customData = {};
- this._isEnabled = true;
- this._isUpdate = false;
- this._isDestroy = false;
- }
- /**设置唯一ID */
- public set id (id: string) {
- this._id = id;
- }
- /**获得唯一ID */
- public get id (): string {
- return this._id;
- }
- /**设置自定义数据 */
- public set customData (data: any) {
- this._customData = Tool.dataAllCover(this._customData, data);
- }
- /**获得自定义数据 */
- public get customData (): any {
- return this._customData;
- }
- /**获得父节点 */
- public get parent (): BasicObject {
- return this._parent;
- }
- /**是否启用 */
- public set isEnabled (isEnabled: boolean) {
- this._isEnabled = isEnabled;
- }
- /**是否启用 */
- public get isEnabled (): boolean {
- return this._isEnabled;
- }
- /**是否被管理器调用_update方法 */
- public set isUpdate (isUpdate: boolean) {
- this._isUpdate = isUpdate;
- }
- /**是否被管理器调用_update方法 */
- public get isUpdate (): boolean {
- return this._isUpdate;
- }
- /**是否销毁 */
- public get isDestroy (): boolean {
- return this._isDestroy;
- }
- /**添加组件实例 */
- public addComponentIntance (component: BasicObject): any {
- if (component instanceof BasicObject) {
- this._components.push(component);
- component._parent = this;
- component.setNode(this.node);
- component.init();
- }
- return component;
- }
- /**删除组件实例 */
- public removeComponentIntance (component: BasicObject, isDestroy: boolean = true): void {
- for (let index in this._components) {
- if (this._components[index].id == component.id) {
- component._parent = null;
- component.setNode(null);
- if (isDestroy == true) {
- component._destroy();
- }
- this._components.splice(parseInt(index), 1);
- break;
- }
- }
- }
- /**获得组件 */
- public _getComponent (componentType: any): any {
- for (let index in this._components) {
- if (this._components[index] instanceof componentType) {
- return this._components[index];
- }
- }
- return null;
- }
- /**删除组件 */
- public _removeComponent (componentType: any): any {
- for (let index in this._components) {
- let component = this._components[index];
- if (component instanceof componentType) {
- this._components.splice(parseInt(index), 1);
- return component;
- }
- }
- return null;
- }
- /**获得组件列表 */
- public _getComponents (componentType: any): Array<any> {
- let _components: Array<any> = [];
- for (let index in this._components) {
- if (this._components[index] instanceof componentType) {
- _components.push(this._components[index]);
- }
- }
- return _components;
- }
- /**设置渲染节点 */
- private setNode (node: cc.Node): void {
- this.node = node;
- for (let index in this._components) {
- if (this._components[index].node == null) {
- this._components[index].node = node;
- }
- }
- }
- /**初始化 */
- protected init (): void {
- }
- /**帧循环 */
- public _update (...data: any): void {
- for (let index in this._components) {
- if (this._components[index].isUpdate == true) {
- this._components[index]._update(...data);
- }
- }
- }
- /**对外提供自定义销毁方法,执行 onDestroy,destroy */
- _destroy (): void {
- this.onDestroy();
- if (this.node != null) {
- this.destroy();
- }
- }
- /**销毁节点 */
- onDestroy () {
- if (this._isDestroy == true) {
- return;
- }
-
- for (let index in this._components) {
- this._components[index]._destroy();
- }
- this._components = [];
- this._isEnabled = false;
- this._isDestroy = true;
- this.unscheduleAllCallbacks();
- EventManager.offEventByTarget(this);
- }
- }
|