module els { /** * 游戏对象数据接口 */ export interface IGameObjectData{ name: string; UID: number; components: IComponentData[]; transform: any; gameobjects:IGameObjectData[] } export class GameObject extends BaseObject { private _components: Component[]; private _componentHash: any; private _alpha: number = 1; private _visible: boolean = true; private _snapPixelsEnabled: boolean = false; private _update: boolean = true; private _numChildren: number = 0; private _parent: GameObject; private _transfrom: Transform; private _isStatic: boolean; private _enabled: boolean; //一个游戏对象身上绑定的渲染组件只能有一个 private _rendererComponentCount: number = 0; protected _rendererComponent: Renderer; protected _children: GameObject[]; //渲染完毕 onUpdate: Function; constructor(name?: string) { super(); this.name = name; this._children = []; this._componentHash = {}; this._components = []; //添加transform组件 this._transfrom = new Transform(); } get dt() { return 1 / 60; } get numChildren() { return this._numChildren; } get children() { return this._children; } /** 获取游戏对象上的渲染组件 */ get rendererComponent() { return this._rendererComponent; } get renderer() { return this._rendererComponent && this._rendererComponent.renderer; } get transform() { return this._transfrom; } get enabled() { return this._enabled; } set enabled(value: boolean) { this._enabled = value; } get components() { return this._components; } get componentHash() { return this._componentHash; } get isStatic() { return this._isStatic; } set isStatic(value: boolean) { this._isStatic = value; } get update() { return this._update; } get snapPixelsEnabled() { return this._snapPixelsEnabled; } set snapPixelsEnabled(value: boolean) { if (this._snapPixelsEnabled != value) { this._snapPixelsEnabled = value; this._update = true; } } get x() { return this._transfrom.x; } set x(value: number) { if (this._transfrom.x != value) { this._transfrom.x = value; this._update = true; } } get y() { return this._transfrom.y; } set y(value: number) { if (this._transfrom.y != value) { this._transfrom.y = value; this._update = true; } } get width() { return this._transfrom.width; } set width(value: number) { if (this._transfrom.width != value) { this._transfrom.width = value; this._update = true; } } get height() { return this._transfrom.height; } set height(value: number) { if (this._transfrom.height != value) { this._transfrom.height = value; this._update = true; } } get scaleX() { return this._transfrom.scaleX; } set scaleX(value: number) { if (this._transfrom.scaleX != value) { this._transfrom.scaleX = value; this._update = true; } } get scaleY() { return this._transfrom.scaleY; } set scaleY(value: number) { if (this._transfrom.scaleY != value) { this._transfrom.scaleY = value; this._update = true; } } get skewX() { return this._transfrom.skewX; } set skewX(value: number) { if (this._transfrom.skewX != value) { this._transfrom.skewX = value; this._update = true; } } get skewY() { return this._transfrom.skewY; } set skewY(value: number) { if (this._transfrom.skewY != value) { this._transfrom.skewY = value; this._update = true; } } get anchorOffsetX() { return this._transfrom.anchorOffsetX; } set anchorOffsetX(value: number) { if (this._transfrom.anchorOffsetX != value) { this._transfrom.anchorOffsetX = value; this._update = true; } } get anchorOffsetY() { return this._transfrom.anchorOffsetY; } set anchorOffsetY(value: number) { if (this._transfrom.anchorOffsetY != value) { this._transfrom.anchorOffsetY = value; this._update = true; } } get rotation() { return this._transfrom.rotation; } set rotation(value: number) { if (this._transfrom.rotation != value) { this._transfrom.rotation = value; this._update = true; } } get alpha() { return this._alpha; } set alpha(value: number) { if (this._alpha != value) { this._alpha = value; this._update = true; } } get visible() { return this._visible; } set visible(value: boolean) { if (this._visible != value) { this._visible = value; this._update = true; } } get parent() { return this._parent; } get scene() { let _parent:any = this; while (_parent instanceof Scene) { _parent = _parent.parent; } return _parent as Scene; } getTransformedBounds(targetCoordinateSpace: egret.DisplayObject, resultRect?: egret.Rectangle):egret.Rectangle { let _renderer = this.renderer; if (_renderer) { this.__render(); return _renderer.getTransformedBounds(targetCoordinateSpace, resultRect); } } getBounds(resultRect?: egret.Rectangle, calculateAnchor?: boolean) { let _renderer = this.renderer; if (_renderer) { this.__render(); return _renderer.getBounds(resultRect, calculateAnchor); } } getChildAt(index: number): GameObject{ if (index < 0) return null; return this._children[index]; } getChildIndex(child: GameObject): number{ if (!child) return -1; return this._children.indexOf(child); } getChildByName(name: string): GameObject{ for (var i: number = 0, l: number = this._children.length; i < l; i++){ var child: GameObject = this._children[i]; if (child.name === name) return child; } return null; } addChild(child: GameObject): void{ if (!child) return; if (!this.contains(child)) { this._children.push(child); this._numChildren++; } child._parent = this; } removeChild(child: GameObject): void{ if (!child) return; var index: number = this._children.indexOf(child); if (index !== -1) { this._children.splice(index, 1); child._parent = null; this._numChildren--; } } addChildAt(child: GameObject, index: number): GameObject{ if (!child) return null; if (!this.contains(child)) { child._parent = this; this._children.splice(index, 0, child); this._numChildren++; } return child; } /** TODO */ setChildIndex(child: GameObject, index: number): void{ if (!child) return; } swapChildrenAt(index1: number, index2: number): void{ if (index1 < 0 || index2 < 0) return; var child1: GameObject = this._children[index1]; var child2: GameObject = this._children[index2]; if (!child1 || !child2) return; this._children[index1] = child2; this._children[index2] = child1; } swapChildren(child1: GameObject, child2: GameObject): void{ if (!child1 || !child2) return; var index1: number = this._children.indexOf(child1); var index2: number = this._children.indexOf(child2); if (index1 === -1 || index2 === -1) return; this._children[index2] = child1; this._children[index1] = child2; } removeChildren(): void{ for (var i: number = 0, l: number = this._children.length; i < l; i++){ this._children[i]._parent = null; } this._children.length = 0; this._numChildren = 0; } contains(child: GameObject): boolean{ if (!child) return false; for (var i: number = 0, l: number = this._children.length; i < l; i++){ var _child: GameObject = this._children[i]; if (_child.id === child.id) return true; } return false; } getComponentById(id: number): Component{ return this._componentHash[id]; } getComponentsByName(name: string): Component[]{ var comps: Component[] = []; for (let i: number = 0, l: number = this._components.length; i < l; i++) { var comp: Component = this._components[i]; if (comp.name == name) { comps.push(comp); } } return comps; } addComponent(com: Component) { if (!com) return; if (this._componentHash[com.id] === undefined) { if (com instanceof Renderer) { this._rendererComponent = com; this._rendererComponentCount++; if (this._rendererComponentCount > 1) { throw new Error('游戏对象' + this.name + "身上只能绑定一个渲染组件"); } } com.gameobject = this; this._components.push(com); this._componentHash[com.id] = com; } } removeComponent(com: Component) { if (!com) return; var index: number = this._components.indexOf(com); if (index != -1) { if (com instanceof Renderer) { this._rendererComponent = null; this._rendererComponentCount--; } com.gameobject = null; this._components.splice(index, 1); delete this._componentHash[com.id]; } } removeComponentByName(compName: string) { if (!compName) return; for (var i: number = this._components.length - 1; i >= 0; i--){ var _comp: Component = this._components[i]; if (_comp.name === compName) { if (_comp instanceof Renderer) { this._rendererComponent = null; this._rendererComponentCount--; } _comp.gameobject = null; this._components.splice(i, 1); delete this._componentHash[_comp.id]; } } } removeAllComponents() { for (var id in this._componentHash) { this._componentHash[id].gameobject = null; delete this._componentHash[id]; } this._rendererComponent = null; this._rendererComponentCount = 0; this._components.length = 0; } fixedUpdate(): void{ } /** * 渲染 * @private */ private __render(): void{ //渲染挂接在上面的组件 let components: Component[] = this.components; for (let i: number = 0, l: number = components.length; i < l; i++) { let component: Component = components[i]; //不可用,不会执行 if (!component.enabled) continue; //预览模式下,执行组件的tick if (ElsMode.mode === 0) { if(component.enabled) component.tick(); } else { let editorRun: IEditorRun = editorRuns[component.className]; if (editorRun && editorRun.callName === 'tick') { if(component.enabled) component.tick(); } } component.update(); //更新后 component.lateUpdate(); //当准备渲染对象时调用 component.onWillRenderObject(); } if (this.update) { if (this._rendererComponent) this._rendererComponent['__onRendererRender'](); //渲染子子对象 if (this._children) { for (let i: number = 0, l: number = this._children.length; i < l; i++){ let child: GameObject = this._children[i]; child.__render(); } } this._update = false; if (this.onUpdate) this.onUpdate(); } //编辑器模式下调用 for (let i: number = 0, l: number = components.length; i < l; i++) { let component: Component = components[i]; if (ElsMode.mode === ElsModeType.EDIT) { component.onDrawGizmos(); } component.onGUI(); } } //销毁 destroy(): void{ } clone(): GameObject{ let object: GameObject = new GameObject(); object.name = this.name; object.editType = this.editType; object.enabled = this.enabled; object.isStatic = this.isStatic; object.snapPixelsEnabled = this.snapPixelsEnabled; object.x = this.x; object.y = this.y; object.width = this.width; object.height = this.height; object.scaleX = this.scaleX; object.scaleY = this.scaleY; object.skewX = this.skewX; object.skewY = this.skewY; object.anchorOffsetX = this.anchorOffsetX; object.anchorOffsetY = this.anchorOffsetY; object.rotation = this.rotation; object.alpha = this.alpha; object.visible = this.visible; //_transfrom if (this.transform) { for (let key in this.transform) { object.transform[key] = this.transform[key]; } } //组件 if (this.components) { for (let i: number = 0, l: number = this.components.length; i < l; i++){ let component: Component = this.components[i]; object.addComponent(component.clone()); } } //子对象 if (this.children) { for (let i: number = 0, l: number = this.children.length; i < l; i++){ let gameobject: GameObject = this.children[i]; object.addChild(gameobject); } } return object; } } }