import { Component } from "./Component" ; import * as PIXI from "pixi.js" ; import { GameApplication } from "./GameApplication" ; import { Log } from "../log/Log" ; import { Image } from "../gui/components/Image" ; /**
 * 游戏场景
 */ export class GameScene { container : PIXI . Container = new PIXI . Container ( ) ; rootElement : Component = new Component ( ) ; isDestroy : boolean = false ; set x ( x ) { this . rootElement . x = x ; } get x ( ) : number { return this . rootElement . x ; } set y ( y ) { this . rootElement . y = y ; } get y ( ) : number { return this . rootElement . y ; } get sceneWidth ( ) : number { return GameApplication . getSceneWidth ( ) ; } get sceneHeight ( ) : number { return GameApplication . getSceneHeight ( ) ; } update = ( deltaMS ) => { this . onUpdate ( deltaMS ) ; this . rootElement . update ( deltaMS ) ; } ; resize = ( ) => { this . onResize ( ) ; this . rootElement . resize ( ) ; } ; onResize ( ) { } param deltaMS onUpdate ( deltaMS ) { } /**
     * 被添加之后调用
     */ onInit ( ... obj ) { } /**
     * 场景被销毁调用
     */ onDestroy ( ) { } init ( ... obj ) { this . rootElement . active = true ; this . container . addChild ( < any > this . rootElement ) ; this . onInit ( ... obj ) ; } /**
     * 添加元素
     */ addComponent ( element ) { this . rootElement . addChild ( element ) ; } /**
     * 删除元素
     */ removeComponent ( element ) { this . rootElement . removeChild ( < any > element ) ; } param callback param time schedule ( callback , time ) { this . rootElement . schedule ( callback , time ) ; } param callback param time scheduleOne ( callback , time ) { this . rootElement . scheduleOne ( callback , time ) ; } param callback unschedule ( callback ) { this . rootElement . unschedule ( callback ) ; } /**
     * 移除全部计时器
     */ unscheduleAll ( ) { this . rootElement . unscheduleAll ( ) ; } on ( event , fn , context ? ) { this . rootElement . on ( event , fn , context ) ; } off ( event , fn ? , context ? ) { this . rootElement . off ( event , fn , context ) ; } toLocal ( position , from ? , point ? , skipUpdate ? ) { return this . rootElement . toLocal ( position , from , point , skipUpdate ) ; } /**
     * 清除显卡内图片所占用的缓存
     */ dispose ( ) { this . rootElement . children . forEach ( v => { this . disposeChildren ( < any > v ) ; } ) ; } private disposeChildren ( view ) { if ( view . isSprite ) { let img = < Image > < unknown > view ; if ( img . texture && img . texture . valid ) { img . texture . baseTexture . dispose ( ) ; } } if ( Array . isArray ( view . children ) ) { view . children . forEach ( v => { this . disposeChildren ( < any > v ) ; } ) ; } } /**
     * 销毁场景
     */ destroy ( ) { if ( this . isDestroy ) { return ; } this . dispose ( ) ; this . isDestroy = true ; this . unscheduleAll ( ) ; this . onDestroy ( ) ; if ( this . container . parent ) { this . container . parent . removeChild ( this . container ) ; } this . rootElement . destroy ( { children : true } ) ; this . container . destroy ( { children : true } ) ; } }