Skip to content

Commit

Permalink
fix: duplicated class name (#341)
Browse files Browse the repository at this point in the history
set/get component & post by class, not class name
  • Loading branch information
lslzl3000 committed Nov 26, 2023
1 parent 7a32817 commit fe73994
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
24 changes: 12 additions & 12 deletions src/components/post/PostProcessingComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { Ctor } from "../../util/Global";
import { ComponentBase } from "../ComponentBase";

export class PostProcessingComponent extends ComponentBase {
private _postList: Map<string, PostBase>;
private _postList: Map<any, PostBase>;


public init(param?: any): void {
this._postList = new Map<string, PostBase>();
this._postList = new Map<any, PostBase>();
}

public start(): void {
Expand Down Expand Up @@ -45,35 +45,35 @@ export class PostProcessingComponent extends ComponentBase {
}

public addPost<T extends PostBase>(c: Ctor<T>): T {
if (this._postList.has(c.name)) return;
if (!this._postList.has(FXAAPost.name)) {
if (this._postList.has(c)) return;
if (!this._postList.has(FXAAPost)) {
let post = new FXAAPost();
this._postList.set(post.constructor.name, post);
this._postList.set(FXAAPost, post);
if (this._enable)
this.activePost();
if (c.name === FXAAPost.name) {
if (c === FXAAPost) {
return post as T;
}
}
let post = new c();
this._postList.set(c.name, post);
this._postList.set(c, post);
if (this._enable)
this.activePost();
return post;
}

public removePost<T extends PostBase>(c: Ctor<T>) {
if (!this._postList.has(c.name)) return;
let post = this._postList.get(c.name);
this._postList.delete(c.name);
if (!this._postList.has(c)) return;
let post = this._postList.get(c);
this._postList.delete(c);

let view = this.transform.view3D;
let job = Engine3D.getRenderJob(view);
job.removePost(post);
}

public getPost<T extends PostBase>(c: Ctor<T>): T {
if (!this._postList.has(c.name)) return null;
return this._postList.get(c.name) as T;
if (!this._postList.has(c)) return null;
return this._postList.get(c) as T;
}
}
28 changes: 11 additions & 17 deletions src/core/entities/Object3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ export class Object3D extends Entity {
* @return result component
*/
public addComponent<T extends IComponent>(c: Ctor<T>, param?: any): T {
let className = c.name;
if (!this.components.has(className)) {
if (!this.components.has(c)) {
let instance: T = new c() as T;
instance.object3D = this;
this.components.set(className, instance);
this.components.set(c, instance);
instance[`__init`](param);
ComponentCollect.appendWaitStart(instance);
return instance;
Expand All @@ -75,8 +74,7 @@ export class Object3D extends Entity {
* @returns result component
*/
public getOrAddComponent<T extends IComponent>(c: Ctor<T>): T {
let className = c.name;
let component = this.components.get(className);
let component = this.components.get(c);
if (!component) {
component = this.addComponent(c);
}
Expand All @@ -89,11 +87,10 @@ export class Object3D extends Entity {
* @param c class of component
*/
public removeComponent<T extends IComponent>(c: Ctor<T>) {
let className = c.name;
if (this.components.has(className)) {
let component = this.components.get(className);
if (this.components.has(c)) {
let component = this.components.get(c);
ComponentCollect.removeWaitStart(this, component);
this.components.delete(className);
this.components.delete(c);
component[`__stop`]();
component.beforeDestroy();
component.destroy();
Expand All @@ -107,8 +104,7 @@ export class Object3D extends Entity {
* @returns boolean
*/
public hasComponent<T extends IComponent>(c: Ctor<T>): boolean {
let className = c.name;
return this.components.has(className);
return this.components.has(c);
}

/**
Expand All @@ -118,8 +114,7 @@ export class Object3D extends Entity {
* @returns result component
*/
public getComponent<T extends IComponent>(c: Ctor<T>): T {
let className = c.name;
return this.components.get(className) as T;
return this.components.get(c) as T;
}

/**
Expand Down Expand Up @@ -152,8 +147,7 @@ export class Object3D extends Entity {
*/
public getComponentsInChild<T extends IComponent>(c: Ctor<T>): T[] {
let list: T[] = [];
let className = c.name;
let component = this.components.get(className);
let component = this.components.get(c);
if (component) {
list.push(component as T);
}
Expand Down Expand Up @@ -201,7 +195,7 @@ export class Object3D extends Entity {
*/
public getComponentsExt<T extends IComponent>(c: Ctor<T>, ret?: T[], includeInactive?: boolean): T[] {
ret ||= [];
let component = this.components.get(c.name);
let component = this.components.get(c);
if (component && (component.enable || includeInactive)) {
ret.push(component as T);
} else {
Expand All @@ -219,7 +213,7 @@ export class Object3D extends Entity {
let findComponent;
for (const component of this.components.values()) {
if (component && (component.enable || includeInactive)) {
if (component[key] == value) {
if (component[key] === value) {
ret.push(component as T);
findComponent = true;
}
Expand Down

0 comments on commit fe73994

Please sign in to comment.