diff --git a/examples/src/image.ts b/examples/src/image.ts index a8548e5b..a588593b 100644 --- a/examples/src/image.ts +++ b/examples/src/image.ts @@ -16,6 +16,8 @@ export async function init(canvas) { preload: true, }, ]); + + const game = new Game({ systems: [ new RendererSystem({ @@ -27,6 +29,7 @@ export async function init(canvas) { ], }); + const image = new GameObject('image', { size: { width: 750, height: 1319 }, origin: { x: 0, y: 0 }, @@ -45,6 +48,7 @@ export async function init(canvas) { resource: 'imageName', }), ); + game.scene.addChild(image); diff --git a/packages/eva.js/lib/core/GameObject.ts b/packages/eva.js/lib/core/GameObject.ts index f2fb91ca..8935e542 100644 --- a/packages/eva.js/lib/core/GameObject.ts +++ b/packages/eva.js/lib/core/GameObject.ts @@ -32,6 +32,8 @@ class GameObject { /** GameObject has been destroyed */ public destroyed: boolean = false; + static gameObjects: GameObject[] = [] + /** * Consruct a new gameObject * @param name - the name of this gameObject @@ -40,6 +42,7 @@ class GameObject { constructor(name: string, obj?: TransformParams) { this._name = name; this.id = getId(); + GameObject.gameObjects.push(this) this.addComponent(Transform, obj); } @@ -251,6 +254,8 @@ class GameObject { } this.components.length = 0; this.destroyed = true + const index = GameObject.gameObjects.indexOf(this); + index > -1 && GameObject.gameObjects.splice(index, 1); } } diff --git a/packages/eva.js/lib/core/observer.ts b/packages/eva.js/lib/core/observer.ts index 66fba856..f0970d6e 100644 --- a/packages/eva.js/lib/core/observer.ts +++ b/packages/eva.js/lib/core/observer.ts @@ -294,6 +294,23 @@ export function observer(component: Component, componentName: string = component } } + +export function observerWithSystem(component: Component, componentName: string = component.name, systemName: string) { + const observerInfo = observerInfos[systemName]?.[componentName] + if (observerInfo) { + for (const item of observerInfo) { + const { property, key } = getObjectCache(component, item.prop); + defineProperty({ + obj: property, + key, + prop: item, + component, + componentName, + }); + } + } +} + /** * Push a `Add` event to componentObserver * @param component @@ -301,15 +318,19 @@ export function observer(component: Component, componentName: string = component */ export function observerAdded(component: Component, componentName: string = component.name) { for (const systemName in observerInfos) { - const observerInfo = observerInfos[systemName] || {}; - const info = observerInfo[componentName]; - if (info) { - systemInstance[systemName]?.componentObserver?.add({ - component, - type: ObserverType.ADD, - componentName, - }); - } + observerAddedWithSystem(component, componentName, systemName) + } +} + +export function observerAddedWithSystem(component: Component, componentName: string = component.name, systemName: string) { + const observerInfo = observerInfos[systemName] || {}; + const info = observerInfo[componentName]; + if (info) { + systemInstance[systemName]?.componentObserver?.add({ + component, + type: ObserverType.ADD, + componentName, + }); } } diff --git a/packages/eva.js/lib/game/Game.ts b/packages/eva.js/lib/game/Game.ts index 1f14d0e8..c82374b9 100644 --- a/packages/eva.js/lib/game/Game.ts +++ b/packages/eva.js/lib/game/Game.ts @@ -3,8 +3,9 @@ import Scene from './Scene'; import type { SystemConstructor } from '../core/System'; import System from '../core/System'; import Component from '../core/Component'; -import { setSystemObserver, initObserver } from '../core/observer'; +import { setSystemObserver, initObserver, observerAddedWithSystem, observerWithSystem } from '../core/observer'; import EventEmitter from 'eventemitter3'; +import GameObject from '../core/GameObject'; /** eva plugin struct */ @@ -224,6 +225,13 @@ class Game extends EventEmitter { setSystemObserver(system, system.constructor); initObserver(system.constructor); + for (const gameObject of GameObject.gameObjects) { + for (const component of gameObject.components) { + observerAddedWithSystem(component, component.name, system.name); + observerWithSystem(component, component.name, system.name); + } + } + try { system.awake && system.awake(); } catch (e) { @@ -252,7 +260,7 @@ class Game extends EventEmitter { } if (index > -1) { - this.systems[index].destroy && this.systems[index].destroy(); + this.systems[index]?.destroy(); this.systems.splice(index, 1); } } @@ -374,6 +382,7 @@ class Game extends EventEmitter { this.scene = null; this.canvas = null; this.multiScenes = null; + GameObject.gameObjects.length = 0 } loadScene({ scene, mode = LOAD_SCENE_MODE.SINGLE, params = {} }: LoadSceneParams) { diff --git a/packages/plugin-renderer-img/lib/system.ts b/packages/plugin-renderer-img/lib/system.ts index eaca6eec..7636e8ec 100644 --- a/packages/plugin-renderer-img/lib/system.ts +++ b/packages/plugin-renderer-img/lib/system.ts @@ -71,5 +71,13 @@ export default class Img extends Renderer { } } } - + destroy(): void { + console.log('destroy') + for (const key in this.imgs) { + const sprite = this.imgs[key] + this.containerManager?.getContainer(parseInt(key))?.removeChild(sprite.sprite); + sprite.sprite.destroy({ children: true }); + delete this.imgs[key]; + } + } }