#16 新增createEntityAsync方法

修复entity.id为null
This commit is contained in:
YHH
2020-08-08 09:50:40 +08:00
parent 6a3622a5ef
commit 7cd38ea54e
9 changed files with 42 additions and 13 deletions

View File

@@ -370,6 +370,7 @@ declare module es {
getPostProcessor<T extends PostProcessor>(type: any): T;
removePostProcessor(postProcessor: PostProcessor): void;
createEntity(name: string): Entity;
createEntityAsync(name: string): Promise<Entity>;
addEntity(entity: Entity): Entity;
destroyAllEntities(): void;
findEntity(name: string): Entity;

View File

@@ -1603,6 +1603,7 @@ var es;
childClone.transform.parent = this.transform;
}
};
Entity._idGenerator = 0;
return Entity;
}());
es.Entity = Entity;
@@ -1755,9 +1756,15 @@ var es;
var entity = new es.Entity(name);
return this.addEntity(entity);
};
Scene.prototype.createEntityAsync = function (name) {
var _this = this;
return new Promise(function (resolve) {
resolve(_this.createEntity(name));
});
};
Scene.prototype.addEntity = function (entity) {
if (this.entities.buffer.contains(entity))
console.warn("You are attempting to add the same entity to a scene twice: " + entity);
console.warn("\u60A8\u8BD5\u56FE\u5C06\u540C\u4E00\u5B9E\u4F53\u6DFB\u52A0\u5230\u573A\u666F\u4E24\u6B21: " + entity);
this.entities.add(entity);
entity.scene = this;
for (var i = 0; i < entity.transform.childCount; i++)

File diff suppressed because one or more lines are too long

View File

@@ -11,14 +11,18 @@ module scene {
public async onStart() {
let sprite = new es.Sprite(RES.getRes("checkbox_select_disabled_png"));
let bg = this.createEntity("bg");
// bg.addComponent(new es.SpriteRenderer()).setSprite(sprite).setColor(0xff0000);
this.createEntityAsync("bg").then(bg => {
bg.addComponent(new component.PlayerController());
bg.addComponent(new es.Mover());
bg.addComponent(new es.ScrollingSpriteRenderer(sprite)).setGapXY(new es.Vector2(10, 0));
bg.addComponent(new es.BoxCollider());
bg.position = new es.Vector2(Math.random() * 200, Math.random() * 200);
this.camera.follow(bg, es.CameraStyle.lockOn);
});
// bg.addComponent(new es.SpriteRenderer()).setSprite(sprite).setColor(0xff0000);
for (let i = 0; i < 20; i++) {
let sprite = new es.Sprite(RES.getRes("checkbox_select_disabled_png"));
let player2 = this.createEntity("player2");
@@ -27,7 +31,6 @@ module scene {
player2.addComponent(new es.BoxCollider());
}
this.camera.follow(bg, es.CameraStyle.lockOn);
let pool = new es.ComponentPool<component.SimplePooled>(component.SimplePooled);
let c1 = pool.obtain();

View File

@@ -370,6 +370,7 @@ declare module es {
getPostProcessor<T extends PostProcessor>(type: any): T;
removePostProcessor(postProcessor: PostProcessor): void;
createEntity(name: string): Entity;
createEntityAsync(name: string): Promise<Entity>;
addEntity(entity: Entity): Entity;
destroyAllEntities(): void;
findEntity(name: string): Entity;

View File

@@ -1603,6 +1603,7 @@ var es;
childClone.transform.parent = this.transform;
}
};
Entity._idGenerator = 0;
return Entity;
}());
es.Entity = Entity;
@@ -1755,9 +1756,15 @@ var es;
var entity = new es.Entity(name);
return this.addEntity(entity);
};
Scene.prototype.createEntityAsync = function (name) {
var _this = this;
return new Promise(function (resolve) {
resolve(_this.createEntity(name));
});
};
Scene.prototype.addEntity = function (entity) {
if (this.entities.buffer.contains(entity))
console.warn("You are attempting to add the same entity to a scene twice: " + entity);
console.warn("\u60A8\u8BD5\u56FE\u5C06\u540C\u4E00\u5B9E\u4F53\u6DFB\u52A0\u5230\u573A\u666F\u4E24\u6B21: " + entity);
this.entities.add(entity);
entity.scene = this;
for (var i = 0; i < entity.transform.childCount; i++)

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,6 @@
module es {
export class Entity {
public static _idGenerator: number;
public static _idGenerator: number = 0;
/**
* 当前实体所属的场景

View File

@@ -262,13 +262,23 @@ module es {
return this.addEntity(entity);
}
/**
* 将实体添加到此场景,并返回它
* @param name
*/
public createEntityAsync(name: string): Promise<Entity> {
return new Promise<Entity>(resolve => {
resolve(this.createEntity(name));
});
}
/**
* 在场景的实体列表中添加一个实体
* @param entity
*/
public addEntity(entity: Entity) {
if (this.entities.buffer.contains(entity))
console.warn(`You are attempting to add the same entity to a scene twice: ${entity}`);
console.warn(`您试图将同一实体添加到场景两次: ${entity}`);
this.entities.add(entity);
entity.scene = this;