新增entityprocessor管理实体解析器
This commit is contained in:
@@ -33,7 +33,12 @@ abstract class Component {
|
||||
|
||||
/** 内部使用 运行时不应该调用 */
|
||||
public registerComponent(){
|
||||
this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this), false);
|
||||
this.entity.scene.entityProcessors.onComponentAdded(this.entity);
|
||||
}
|
||||
|
||||
public deregisterComponent(){
|
||||
this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this));
|
||||
this.entity.scene.entityProcessors.forEach(processor => processor.onChanged(this.entity));
|
||||
this.entity.scene.entityProcessors.onComponentRemoved(this.entity);
|
||||
}
|
||||
}
|
||||
@@ -70,8 +70,8 @@ class Entity {
|
||||
return component;
|
||||
}
|
||||
|
||||
public getComponent<T extends Component>(): T{
|
||||
return this.components.firstOrDefault(component => component instanceof Component) as T;
|
||||
public getComponent<T extends Component>(type): T{
|
||||
return this.components.firstOrDefault(component => component instanceof type) as T;
|
||||
}
|
||||
|
||||
public update(){
|
||||
|
||||
@@ -7,13 +7,13 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
private _transformMatrix: Matrix2D;
|
||||
private _matrixTransformMatrix: Matrix2D;
|
||||
|
||||
public readonly entityProcessors: EntitySystem[];
|
||||
public readonly entityProcessors: EntityProcessorList;
|
||||
|
||||
constructor(displayObject: egret.DisplayObject){
|
||||
super();
|
||||
displayObject.stage.addChild(this);
|
||||
this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
|
||||
this.entityProcessors = [];
|
||||
this.entityProcessors = new EntityProcessorList();
|
||||
this.entities = new EntityList(this);
|
||||
|
||||
this.addEventListener(egret.Event.ACTIVATE, this.onActive, this);
|
||||
@@ -53,7 +53,7 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
*/
|
||||
public addEntityProcessor(processor: EntitySystem){
|
||||
processor.scene = this;
|
||||
this.entityProcessors.push(processor);
|
||||
this.entityProcessors.add(processor);
|
||||
return processor;
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
}
|
||||
|
||||
public getEntityProcessor<T extends EntitySystem>(): T {
|
||||
return this.entityProcessors.firstOrDefault(processor => processor instanceof EntitySystem) as T;
|
||||
return this.entityProcessors.getProcessor<T>();
|
||||
}
|
||||
|
||||
public setActive(): Scene{
|
||||
@@ -75,7 +75,9 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
public initialize(){
|
||||
/** 初始化默认相机 */
|
||||
this.camera = this.createEntity("camera").addComponent(new Camera());
|
||||
this.entityProcessors.forEach(processor => processor.initialize());
|
||||
|
||||
if (this.entityProcessors)
|
||||
this.entityProcessors.begin();
|
||||
}
|
||||
|
||||
/** 场景激活 */
|
||||
@@ -89,11 +91,17 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
}
|
||||
|
||||
public update(){
|
||||
Time.update(egret.getTimer());
|
||||
|
||||
this.entities.updateLists();
|
||||
|
||||
this.entityProcessors.forEach(processor => processor.update());
|
||||
if (this.entityProcessors)
|
||||
this.entityProcessors.update()
|
||||
|
||||
this.entities.update();
|
||||
this.entityProcessors.forEach(processor => processor.lateUpdate());
|
||||
|
||||
if (this.entityProcessors)
|
||||
this.entityProcessors.lateUpdate();
|
||||
}
|
||||
|
||||
public prepRenderState(){
|
||||
|
||||
@@ -94,7 +94,7 @@ class BitSet{
|
||||
|
||||
public isEmpty(): boolean{
|
||||
for (let i = this._bits.length - 1; i >= 0; i --){
|
||||
if (this._bits[i] != 0)
|
||||
if (this._bits[i])
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -121,9 +121,13 @@ class BitSet{
|
||||
return -1;
|
||||
}
|
||||
|
||||
public set(pos: number){
|
||||
let offset = pos >> 6;
|
||||
this.ensure(offset);
|
||||
this._bits[offset] |= 1 << pos;
|
||||
public set(pos: number, value: boolean = true){
|
||||
if (value){
|
||||
let offset = pos >> 6;
|
||||
this.ensure(offset);
|
||||
this._bits[offset] |= 1 << pos;
|
||||
}else{
|
||||
this.clear(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,7 @@ class EntityList{
|
||||
this._entities.remove(entity);
|
||||
entity.scene = null;
|
||||
|
||||
this.scene.entityProcessors.forEach(processor => processor.remove(entity));
|
||||
this.scene.entityProcessors.onEntityRemoved(entity);
|
||||
});
|
||||
|
||||
this._tempEntityList.length = 0;
|
||||
@@ -83,7 +83,7 @@ class EntityList{
|
||||
this._entities.push(entity);
|
||||
entity.scene = this.scene;
|
||||
|
||||
this.scene.entityProcessors.forEach(processor => processor.onChanged(entity));
|
||||
this.scene.entityProcessors.onEntityAdded(entity)
|
||||
});
|
||||
|
||||
this._tempEntityList.length = 0;
|
||||
|
||||
69
source/src/ECS/Utils/EntityProcessorList.ts
Normal file
69
source/src/ECS/Utils/EntityProcessorList.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
class EntityProcessorList {
|
||||
private _processors: EntitySystem[] = [];
|
||||
|
||||
public add(processor: EntitySystem){
|
||||
this._processors.push(processor);
|
||||
}
|
||||
|
||||
public remove(processor: EntitySystem){
|
||||
this._processors.remove(processor);
|
||||
}
|
||||
|
||||
public onComponentAdded(entity: Entity){
|
||||
this.notifyEntityChanged(entity);
|
||||
}
|
||||
|
||||
public onComponentRemoved(entity: Entity){
|
||||
this.notifyEntityChanged(entity);
|
||||
}
|
||||
|
||||
public onEntityAdded(entity: Entity){
|
||||
this.notifyEntityChanged(entity);
|
||||
}
|
||||
|
||||
public onEntityRemoved(entity: Entity){
|
||||
this.removeFromProcessors(entity);
|
||||
}
|
||||
|
||||
protected notifyEntityChanged(entity: Entity){
|
||||
for (let i = 0; i < this._processors.length; i ++){
|
||||
this._processors[i].onChanged(entity);
|
||||
}
|
||||
}
|
||||
|
||||
protected removeFromProcessors(entity: Entity){
|
||||
for (let i = 0; i < this._processors.length; i ++){
|
||||
this._processors[i].remove(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public begin(){
|
||||
|
||||
}
|
||||
|
||||
public update(){
|
||||
for (let i = 0; i < this._processors.length; i++){
|
||||
this._processors[i].update();
|
||||
}
|
||||
}
|
||||
|
||||
public lateUpdate(){
|
||||
for (let i = 0; i < this._processors.length; i ++){
|
||||
this._processors[i].lateUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public end(){
|
||||
|
||||
}
|
||||
|
||||
public getProcessor<T extends EntitySystem>(): T{
|
||||
for (let i = 0; i < this._processors.length; i ++){
|
||||
let processor = this._processors[i];
|
||||
if (processor instanceof EntitySystem)
|
||||
return processor as T;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
15
source/src/ECS/Utils/Time.ts
Normal file
15
source/src/ECS/Utils/Time.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
class Time {
|
||||
public static unscaledDeltaTime;
|
||||
public static deltaTime: number;
|
||||
public static timeScale = 1;
|
||||
|
||||
private static _lastTime = 0;
|
||||
|
||||
public static update(currentTime: number){
|
||||
let dt = (currentTime - this._lastTime) / 1000;
|
||||
this.deltaTime = dt * this.timeScale;
|
||||
this.unscaledDeltaTime = dt;
|
||||
|
||||
this._lastTime = currentTime;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user