Skip to content

Commit

Permalink
组件新增onAdded和onRemoved生命周期
Browse files Browse the repository at this point in the history
  • Loading branch information
esengine committed May 9, 2023
1 parent 8054253 commit 487eec4
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class VelocityComponent extends gs.Component {
this.y = 0;
}
}


```

> 注意,需要实现reset方法,当组件回收时会调用该方法
Expand Down
8 changes: 8 additions & 0 deletions source/bin/gs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ declare module gs {
* 通过增加 _version 的值来表示组件已更新
*/
markUpdated(): void;
/**
* 当组件被添加到实体上时执行
*/
onAdded(): void;
/**
* 当组件从实体上被移除时执行
*/
onRemoved(): void;
serialize(): any;
deserialize(data: any): void;
/**
Expand Down
10 changes: 10 additions & 0 deletions source/bin/gs.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ var gs;
Component.prototype.markUpdated = function () {
this._version++;
};
/**
* 当组件被添加到实体上时执行
*/
Component.prototype.onAdded = function () { };
/**
* 当组件从实体上被移除时执行
*/
Component.prototype.onRemoved = function () { };
Component.prototype.serialize = function () {
var e_2, _a;
var data = {};
Expand Down Expand Up @@ -271,6 +279,7 @@ var gs;
throw new Error("\u7EC4\u4EF6\u7C7B\u578B\u4E3A " + componentType.name + " \u7684\u7EC4\u4EF6\u7BA1\u7406\u5668\u672A\u627E\u5230.");
}
var component = manager.create(this.id);
component.onAdded();
return component;
};
/**
Expand All @@ -297,6 +306,7 @@ var gs;
}
var component = this.getComponent(componentType);
if (component) {
component.onRemoved();
manager.remove(this.id);
}
};
Expand Down
2 changes: 1 addition & 1 deletion source/bin/gs.min.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions source/src/Core/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ module gs {
this._version++;
}

/**
* 当组件被添加到实体上时执行
*/
onAdded(): void { }

/**
* 当组件从实体上被移除时执行
*/
onRemoved(): void { }

serialize(): any {
const data: any = {};
Expand Down
2 changes: 2 additions & 0 deletions source/src/Core/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module gs {
throw new Error(`组件类型为 ${componentType.name} 的组件管理器未找到.`);
}
const component = manager.create(this.id) as T;
component.onAdded();
return component;
}

Expand Down Expand Up @@ -55,6 +56,7 @@ module gs {
}
const component = this.getComponent(componentType);
if (component) {
component.onRemoved();
manager.remove(this.id);
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/performanceTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class PositionComponent extends Component {
this.y = this.random.next() + id;
id++;
}
onAdded() {
console.log('position 组件被添加');
}
onRemoved() {
console.log('position 组件被移除');
}
reset() {
this.x = 0;
this.y = 0;
Expand Down
8 changes: 8 additions & 0 deletions test/performanceTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class PositionComponent extends Component {
id ++;
}

onAdded(): void {
console.log('position 组件被添加');
}

onRemoved(): void {
console.log('position 组件被移除');
}

public reset(): void {
this.x = 0;
this.y = 0;
Expand Down

0 comments on commit 487eec4

Please sign in to comment.