Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #188 #194

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export async function init(canvas) {
preload: true,
},
]);


const game = new Game({
systems: [
new RendererSystem({
Expand All @@ -27,6 +29,7 @@ export async function init(canvas) {
],
});


const image = new GameObject('image', {
size: { width: 750, height: 1319 },
origin: { x: 0, y: 0 },
Expand All @@ -45,6 +48,7 @@ export async function init(canvas) {
resource: 'imageName',
}),
);


game.scene.addChild(image);

Expand Down
5 changes: 5 additions & 0 deletions packages/eva.js/lib/core/GameObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
39 changes: 30 additions & 9 deletions packages/eva.js/lib/core/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,43 @@ 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
* @param componentName - default value is `component.name`, it will be deprecated
*/
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,
});
}
}

Expand Down
13 changes: 11 additions & 2 deletions packages/eva.js/lib/game/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion packages/plugin-renderer-img/lib/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
}