新增poolcomponent 对象池组件

This commit is contained in:
yhh
2020-06-30 22:22:52 +08:00
parent 099aa749e1
commit 088891c5ca
11 changed files with 217 additions and 60 deletions
+13 -3
View File
@@ -343,6 +343,13 @@ declare class CameraInset {
top: number; top: number;
bottom: number; bottom: number;
} }
declare class ComponentPool<T extends PooledComponent> {
private _cache;
private _type;
constructor(typeClass: any);
obtain(): T;
free(component: T): void;
}
declare class FollowCamera extends Component { declare class FollowCamera extends Component {
camera: Camera; camera: Camera;
followLerp: number; followLerp: number;
@@ -386,6 +393,9 @@ declare class VertexPosition {
declare class PolygonMesh extends Mesh { declare class PolygonMesh extends Mesh {
constructor(points: Vector2[], arePointsCCW?: boolean); constructor(points: Vector2[], arePointsCCW?: boolean);
} }
declare abstract class PooledComponent extends Component {
abstract reset(): any;
}
declare abstract class RenderableComponent extends Component implements IRenderable { declare abstract class RenderableComponent extends Component implements IRenderable {
private _isVisible; private _isVisible;
protected _areBoundsDirty: boolean; protected _areBoundsDirty: boolean;
@@ -711,7 +721,7 @@ declare abstract class SceneTransition {
constructor(sceneLoadAction: Function); constructor(sceneLoadAction: Function);
preRender(): void; preRender(): void;
render(): void; render(): void;
onBeginTransition(): void; onBeginTransition(): Promise<void>;
protected transitionComplete(): void; protected transitionComplete(): void;
protected loadNextScene(): Promise<void>; protected loadNextScene(): Promise<void>;
tickEffectProgressProperty(filter: egret.CustomFilter, duration: number, easeType: Function, reverseDirection?: boolean): Promise<{}>; tickEffectProgressProperty(filter: egret.CustomFilter, duration: number, easeType: Function, reverseDirection?: boolean): Promise<{}>;
@@ -724,7 +734,7 @@ declare class FadeTransition extends SceneTransition {
private _mask; private _mask;
private _alpha; private _alpha;
constructor(sceneLoadAction: Function); constructor(sceneLoadAction: Function);
onBeginTransition(): void; onBeginTransition(): Promise<void>;
render(): void; render(): void;
} }
declare class WindTransition extends SceneTransition { declare class WindTransition extends SceneTransition {
@@ -735,7 +745,7 @@ declare class WindTransition extends SceneTransition {
size: number; size: number;
easeType: (t: number) => number; easeType: (t: number) => number;
constructor(sceneLoadAction: Function); constructor(sceneLoadAction: Function);
onBeginTransition(): void; onBeginTransition(): Promise<void>;
} }
declare class BaseView extends egret.DisplayObjectContainer { declare class BaseView extends egret.DisplayObjectContainer {
protected _data: any; protected _data: any;
+73 -25
View File
@@ -1782,6 +1782,25 @@ var CameraInset = (function () {
} }
return CameraInset; return CameraInset;
}()); }());
var ComponentPool = (function () {
function ComponentPool(typeClass) {
this._type = typeClass;
this._cache = [];
}
ComponentPool.prototype.obtain = function () {
try {
return this._cache.length > 0 ? this._cache.shift() : new this._type();
}
catch (err) {
throw new Error(this._type + err);
}
};
ComponentPool.prototype.free = function (component) {
component.reset();
this._cache.push(component);
};
return ComponentPool;
}());
var FollowCamera = (function (_super) { var FollowCamera = (function (_super) {
__extends(FollowCamera, _super); __extends(FollowCamera, _super);
function FollowCamera(targetEntity, cameraStyle) { function FollowCamera(targetEntity, cameraStyle) {
@@ -1937,6 +1956,13 @@ var PolygonMesh = (function (_super) {
} }
return PolygonMesh; return PolygonMesh;
}(Mesh)); }(Mesh));
var PooledComponent = (function (_super) {
__extends(PooledComponent, _super);
function PooledComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
return PooledComponent;
}(Component));
var RenderableComponent = (function (_super) { var RenderableComponent = (function (_super) {
__extends(RenderableComponent, _super); __extends(RenderableComponent, _super);
function RenderableComponent() { function RenderableComponent() {
@@ -3236,8 +3262,17 @@ var SceneTransition = (function () {
SceneTransition.prototype.render = function () { SceneTransition.prototype.render = function () {
}; };
SceneTransition.prototype.onBeginTransition = function () { SceneTransition.prototype.onBeginTransition = function () {
this.loadNextScene(); return __awaiter(this, void 0, void 0, function () {
this.transitionComplete(); return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.loadNextScene()];
case 1:
_a.sent();
this.transitionComplete();
return [2];
}
});
});
}; };
SceneTransition.prototype.transitionComplete = function () { SceneTransition.prototype.transitionComplete = function () {
SceneManager.sceneTransition = null; SceneManager.sceneTransition = null;
@@ -3291,25 +3326,30 @@ var FadeTransition = (function (_super) {
return _this; return _this;
} }
FadeTransition.prototype.onBeginTransition = function () { FadeTransition.prototype.onBeginTransition = function () {
var _this = this; return __awaiter(this, void 0, void 0, function () {
this._mask.graphics.beginFill(this.fadeToColor, 1); var _this = this;
this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
this._mask.graphics.endFill();
SceneManager.stage.addChild(this._mask);
egret.Tween.get(this).to({ _alpha: 1 }, this.fadeOutDuration * 1000, this.fadeEaseType)
.call(function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { this._mask.graphics.beginFill(this.fadeToColor, 1);
case 0: return [4, this.loadNextScene()]; this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
case 1: this._mask.graphics.endFill();
_a.sent(); SceneManager.stage.addChild(this._mask);
return [2]; egret.Tween.get(this).to({ _alpha: 1 }, this.fadeOutDuration * 1000, this.fadeEaseType)
} .call(function () { return __awaiter(_this, void 0, void 0, function () {
}); return __generator(this, function (_a) {
}); }).wait(this.delayBeforeFadeInDuration).call(function () { switch (_a.label) {
egret.Tween.get(_this).to({ _alpha: 0 }, _this.fadeOutDuration * 1000, _this.fadeEaseType).call(function () { case 0: return [4, this.loadNextScene()];
_this.transitionComplete(); case 1:
SceneManager.stage.removeChild(_this._mask); _a.sent();
return [2];
}
});
}); }).wait(this.delayBeforeFadeInDuration).call(function () {
egret.Tween.get(_this).to({ _alpha: 0 }, _this.fadeOutDuration * 1000, _this.fadeEaseType).call(function () {
_this.transitionComplete();
SceneManager.stage.removeChild(_this._mask);
});
});
return [2];
}); });
}); });
}; };
@@ -3378,11 +3418,19 @@ var WindTransition = (function (_super) {
configurable: true configurable: true
}); });
WindTransition.prototype.onBeginTransition = function () { WindTransition.prototype.onBeginTransition = function () {
var _this = this; return __awaiter(this, void 0, void 0, function () {
this.loadNextScene(); return __generator(this, function (_a) {
this.tickEffectProgressProperty(this._windEffect, this.duration, this.easeType).then(function () { switch (_a.label) {
_this.transitionComplete(); case 0:
SceneManager.stage.removeChild(_this._mask); this.loadNextScene();
return [4, this.tickEffectProgressProperty(this._windEffect, this.duration, this.easeType)];
case 1:
_a.sent();
this.transitionComplete();
SceneManager.stage.removeChild(this._mask);
return [2];
}
});
}); });
}; };
return WindTransition; return WindTransition;
File diff suppressed because one or more lines are too long
+3 -2
View File
@@ -11,14 +11,15 @@
"libs/long/long.js" "libs/long/long.js"
], ],
"game": [ "game": [
"bin-debug/game/CoreEmitterType.js",
"bin-debug/AssetAdapter.js", "bin-debug/AssetAdapter.js",
"bin-debug/LoadingUI.js",
"bin-debug/Main.js", "bin-debug/Main.js",
"bin-debug/Platform.js", "bin-debug/Platform.js",
"bin-debug/ThemeAdapter.js", "bin-debug/ThemeAdapter.js",
"bin-debug/game/CoreEmitterType.js", "bin-debug/LoadingUI.js",
"bin-debug/game/MainScene.js", "bin-debug/game/MainScene.js",
"bin-debug/game/PlayerController.js", "bin-debug/game/PlayerController.js",
"bin-debug/game/SimplePooled.js",
"bin-debug/game/SpawnerComponent.js", "bin-debug/game/SpawnerComponent.js",
"bin-debug/game/SpawnerSystem.js" "bin-debug/game/SpawnerSystem.js"
] ]
+9
View File
@@ -23,6 +23,15 @@ class MainScene extends Scene {
player2.addComponent(new BoxCollider()); player2.addComponent(new BoxCollider());
} }
let pool = new ComponentPool<SimplePooled>(SimplePooled);
let c1 = pool.obtain();
let c2 = pool.obtain();
pool.free(c1);
let c1b = pool.obtain();
console.log(c1 != c2);
console.log(c1 == c1b);
let button = new eui.Button(); let button = new eui.Button();
button.label = "切换场景"; button.label = "切换场景";
this.addChild(button); this.addChild(button);
+5
View File
@@ -0,0 +1,5 @@
class SimplePooled extends PooledComponent {
public reset(){
}
}
+13 -3
View File
@@ -343,6 +343,13 @@ declare class CameraInset {
top: number; top: number;
bottom: number; bottom: number;
} }
declare class ComponentPool<T extends PooledComponent> {
private _cache;
private _type;
constructor(typeClass: any);
obtain(): T;
free(component: T): void;
}
declare class FollowCamera extends Component { declare class FollowCamera extends Component {
camera: Camera; camera: Camera;
followLerp: number; followLerp: number;
@@ -386,6 +393,9 @@ declare class VertexPosition {
declare class PolygonMesh extends Mesh { declare class PolygonMesh extends Mesh {
constructor(points: Vector2[], arePointsCCW?: boolean); constructor(points: Vector2[], arePointsCCW?: boolean);
} }
declare abstract class PooledComponent extends Component {
abstract reset(): any;
}
declare abstract class RenderableComponent extends Component implements IRenderable { declare abstract class RenderableComponent extends Component implements IRenderable {
private _isVisible; private _isVisible;
protected _areBoundsDirty: boolean; protected _areBoundsDirty: boolean;
@@ -711,7 +721,7 @@ declare abstract class SceneTransition {
constructor(sceneLoadAction: Function); constructor(sceneLoadAction: Function);
preRender(): void; preRender(): void;
render(): void; render(): void;
onBeginTransition(): void; onBeginTransition(): Promise<void>;
protected transitionComplete(): void; protected transitionComplete(): void;
protected loadNextScene(): Promise<void>; protected loadNextScene(): Promise<void>;
tickEffectProgressProperty(filter: egret.CustomFilter, duration: number, easeType: Function, reverseDirection?: boolean): Promise<{}>; tickEffectProgressProperty(filter: egret.CustomFilter, duration: number, easeType: Function, reverseDirection?: boolean): Promise<{}>;
@@ -724,7 +734,7 @@ declare class FadeTransition extends SceneTransition {
private _mask; private _mask;
private _alpha; private _alpha;
constructor(sceneLoadAction: Function); constructor(sceneLoadAction: Function);
onBeginTransition(): void; onBeginTransition(): Promise<void>;
render(): void; render(): void;
} }
declare class WindTransition extends SceneTransition { declare class WindTransition extends SceneTransition {
@@ -735,7 +745,7 @@ declare class WindTransition extends SceneTransition {
size: number; size: number;
easeType: (t: number) => number; easeType: (t: number) => number;
constructor(sceneLoadAction: Function); constructor(sceneLoadAction: Function);
onBeginTransition(): void; onBeginTransition(): Promise<void>;
} }
declare class BaseView extends egret.DisplayObjectContainer { declare class BaseView extends egret.DisplayObjectContainer {
protected _data: any; protected _data: any;
+73 -25
View File
@@ -1782,6 +1782,25 @@ var CameraInset = (function () {
} }
return CameraInset; return CameraInset;
}()); }());
var ComponentPool = (function () {
function ComponentPool(typeClass) {
this._type = typeClass;
this._cache = [];
}
ComponentPool.prototype.obtain = function () {
try {
return this._cache.length > 0 ? this._cache.shift() : new this._type();
}
catch (err) {
throw new Error(this._type + err);
}
};
ComponentPool.prototype.free = function (component) {
component.reset();
this._cache.push(component);
};
return ComponentPool;
}());
var FollowCamera = (function (_super) { var FollowCamera = (function (_super) {
__extends(FollowCamera, _super); __extends(FollowCamera, _super);
function FollowCamera(targetEntity, cameraStyle) { function FollowCamera(targetEntity, cameraStyle) {
@@ -1937,6 +1956,13 @@ var PolygonMesh = (function (_super) {
} }
return PolygonMesh; return PolygonMesh;
}(Mesh)); }(Mesh));
var PooledComponent = (function (_super) {
__extends(PooledComponent, _super);
function PooledComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
return PooledComponent;
}(Component));
var RenderableComponent = (function (_super) { var RenderableComponent = (function (_super) {
__extends(RenderableComponent, _super); __extends(RenderableComponent, _super);
function RenderableComponent() { function RenderableComponent() {
@@ -3236,8 +3262,17 @@ var SceneTransition = (function () {
SceneTransition.prototype.render = function () { SceneTransition.prototype.render = function () {
}; };
SceneTransition.prototype.onBeginTransition = function () { SceneTransition.prototype.onBeginTransition = function () {
this.loadNextScene(); return __awaiter(this, void 0, void 0, function () {
this.transitionComplete(); return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.loadNextScene()];
case 1:
_a.sent();
this.transitionComplete();
return [2];
}
});
});
}; };
SceneTransition.prototype.transitionComplete = function () { SceneTransition.prototype.transitionComplete = function () {
SceneManager.sceneTransition = null; SceneManager.sceneTransition = null;
@@ -3291,25 +3326,30 @@ var FadeTransition = (function (_super) {
return _this; return _this;
} }
FadeTransition.prototype.onBeginTransition = function () { FadeTransition.prototype.onBeginTransition = function () {
var _this = this; return __awaiter(this, void 0, void 0, function () {
this._mask.graphics.beginFill(this.fadeToColor, 1); var _this = this;
this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
this._mask.graphics.endFill();
SceneManager.stage.addChild(this._mask);
egret.Tween.get(this).to({ _alpha: 1 }, this.fadeOutDuration * 1000, this.fadeEaseType)
.call(function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { this._mask.graphics.beginFill(this.fadeToColor, 1);
case 0: return [4, this.loadNextScene()]; this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
case 1: this._mask.graphics.endFill();
_a.sent(); SceneManager.stage.addChild(this._mask);
return [2]; egret.Tween.get(this).to({ _alpha: 1 }, this.fadeOutDuration * 1000, this.fadeEaseType)
} .call(function () { return __awaiter(_this, void 0, void 0, function () {
}); return __generator(this, function (_a) {
}); }).wait(this.delayBeforeFadeInDuration).call(function () { switch (_a.label) {
egret.Tween.get(_this).to({ _alpha: 0 }, _this.fadeOutDuration * 1000, _this.fadeEaseType).call(function () { case 0: return [4, this.loadNextScene()];
_this.transitionComplete(); case 1:
SceneManager.stage.removeChild(_this._mask); _a.sent();
return [2];
}
});
}); }).wait(this.delayBeforeFadeInDuration).call(function () {
egret.Tween.get(_this).to({ _alpha: 0 }, _this.fadeOutDuration * 1000, _this.fadeEaseType).call(function () {
_this.transitionComplete();
SceneManager.stage.removeChild(_this._mask);
});
});
return [2];
}); });
}); });
}; };
@@ -3378,11 +3418,19 @@ var WindTransition = (function (_super) {
configurable: true configurable: true
}); });
WindTransition.prototype.onBeginTransition = function () { WindTransition.prototype.onBeginTransition = function () {
var _this = this; return __awaiter(this, void 0, void 0, function () {
this.loadNextScene(); return __generator(this, function (_a) {
this.tickEffectProgressProperty(this._windEffect, this.duration, this.easeType).then(function () { switch (_a.label) {
_this.transitionComplete(); case 0:
SceneManager.stage.removeChild(_this._mask); this.loadNextScene();
return [4, this.tickEffectProgressProperty(this._windEffect, this.duration, this.easeType)];
case 1:
_a.sent();
this.transitionComplete();
SceneManager.stage.removeChild(this._mask);
return [2];
}
});
}); });
}; };
return WindTransition; return WindTransition;
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -0,0 +1,22 @@
class ComponentPool<T extends PooledComponent>{
private _cache: T[];
private _type: any;
constructor(typeClass: any){
this._type = typeClass;
this._cache = [];
}
public obtain(): T{
try {
return this._cache.length > 0 ? this._cache.shift() : new this._type();
} catch(err){
throw new Error(this._type + err);
}
}
public free(component: T){
component.reset();
this._cache.push(component);
}
}
@@ -0,0 +1,4 @@
/** 回收实例的组件类型。 */
abstract class PooledComponent extends Component {
public abstract reset();
}