新增entityprocessor管理实体解析器

This commit is contained in:
YHH
2020-06-08 21:53:09 +08:00
parent a048a8ac29
commit 11af0a31a7
17 changed files with 386 additions and 66 deletions

View File

@@ -27,6 +27,7 @@ declare abstract class Component {
update(): void; update(): void;
bind(displayRender: egret.DisplayObject): this; bind(displayRender: egret.DisplayObject): this;
registerComponent(): void; registerComponent(): void;
deregisterComponent(): void;
} }
declare class Entity { declare class Entity {
name: string; name: string;
@@ -43,7 +44,7 @@ declare class Entity {
setUpdateOrder(updateOrder: number): this; setUpdateOrder(updateOrder: number): this;
attachToScene(newScene: Scene): void; attachToScene(newScene: Scene): void;
addComponent<T extends Component>(component: T): T; addComponent<T extends Component>(component: T): T;
getComponent<T extends Component>(): T; getComponent<T extends Component>(type: any): T;
update(): void; update(): void;
destory(): void; destory(): void;
} }
@@ -53,7 +54,7 @@ declare class Scene extends egret.DisplayObjectContainer {
private _projectionMatrix; private _projectionMatrix;
private _transformMatrix; private _transformMatrix;
private _matrixTransformMatrix; private _matrixTransformMatrix;
readonly entityProcessors: EntitySystem[]; readonly entityProcessors: EntityProcessorList;
constructor(displayObject: egret.DisplayObject); constructor(displayObject: egret.DisplayObject);
createEntity(name: string): Entity; createEntity(name: string): Entity;
addEntity(entity: Entity): Entity; addEntity(entity: Entity): Entity;
@@ -173,7 +174,7 @@ declare class BitSet {
intersects(set: BitSet): boolean; intersects(set: BitSet): boolean;
isEmpty(): boolean; isEmpty(): boolean;
nextSetBit(from: number): number; nextSetBit(from: number): number;
set(pos: number): void; set(pos: number, value?: boolean): void;
} }
declare class ComponentTypeManager { declare class ComponentTypeManager {
private static _componentTypesMask; private static _componentTypesMask;
@@ -196,6 +197,22 @@ declare class EntityList {
removeAllEntities(): void; removeAllEntities(): void;
updateLists(): void; updateLists(): void;
} }
declare class EntityProcessorList {
private _processors;
add(processor: EntitySystem): void;
remove(processor: EntitySystem): void;
onComponentAdded(entity: Entity): void;
onComponentRemoved(entity: Entity): void;
onEntityAdded(entity: Entity): void;
onEntityRemoved(entity: Entity): void;
protected notifyEntityChanged(entity: Entity): void;
protected removeFromProcessors(entity: Entity): void;
begin(): void;
update(): void;
lateUpdate(): void;
end(): void;
getProcessor<T extends EntitySystem>(): T;
}
declare class Matcher { declare class Matcher {
protected allSet: BitSet; protected allSet: BitSet;
protected exclusionSet: BitSet; protected exclusionSet: BitSet;
@@ -203,6 +220,13 @@ declare class Matcher {
static empty(): Matcher; static empty(): Matcher;
IsIntersted(e: Entity): boolean; IsIntersted(e: Entity): boolean;
} }
declare class Time {
static unscaledDeltaTime: any;
static deltaTime: number;
static timeScale: number;
private static _lastTime;
static update(currentTime: number): void;
}
declare class MathHelper { declare class MathHelper {
static toDegrees(radians: number): number; static toDegrees(radians: number): number;
static toRadians(degrees: number): number; static toRadians(degrees: number): number;

View File

@@ -76,7 +76,7 @@ Array.prototype.findAll = function (predicate) {
Array.prototype.contains = function (value) { Array.prototype.contains = function (value) {
function contains(array, value) { function contains(array, value) {
for (var i = 0, len = array.length; i < len; i++) { for (var i = 0, len = array.length; i < len; i++) {
if (JSON.stringify(array[i]) == JSON.stringify(value)) { if (array[i] == value) {
return true; return true;
} }
} }
@@ -265,9 +265,12 @@ var Component = (function () {
return this; return this;
}; };
Component.prototype.registerComponent = function () { Component.prototype.registerComponent = function () {
var _this = this; this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this), false);
this.entity.scene.entityProcessors.onComponentAdded(this.entity);
};
Component.prototype.deregisterComponent = function () {
this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this)); this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this));
this.entity.scene.entityProcessors.forEach(function (processor) { return processor.onChanged(_this.entity); }); this.entity.scene.entityProcessors.onComponentRemoved(this.entity);
}; };
return Component; return Component;
}()); }());
@@ -328,8 +331,8 @@ var Entity = (function () {
component.initialize(); component.initialize();
return component; return component;
}; };
Entity.prototype.getComponent = function () { Entity.prototype.getComponent = function (type) {
return this.components.firstOrDefault(function (component) { return component instanceof Component; }); return this.components.firstOrDefault(function (component) { return component instanceof type; });
}; };
Entity.prototype.update = function () { Entity.prototype.update = function () {
this.components.forEach(function (component) { return component.update(); }); this.components.forEach(function (component) { return component.update(); });
@@ -351,7 +354,7 @@ var Scene = (function (_super) {
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
displayObject.stage.addChild(_this); displayObject.stage.addChild(_this);
_this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); _this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
_this.entityProcessors = []; _this.entityProcessors = new EntityProcessorList();
_this.entities = new EntityList(_this); _this.entities = new EntityList(_this);
_this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this); _this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this);
_this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this); _this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this);
@@ -380,14 +383,14 @@ var Scene = (function (_super) {
}; };
Scene.prototype.addEntityProcessor = function (processor) { Scene.prototype.addEntityProcessor = function (processor) {
processor.scene = this; processor.scene = this;
this.entityProcessors.push(processor); this.entityProcessors.add(processor);
return processor; return processor;
}; };
Scene.prototype.removeEntityProcessor = function (processor) { Scene.prototype.removeEntityProcessor = function (processor) {
this.entityProcessors.remove(processor); this.entityProcessors.remove(processor);
}; };
Scene.prototype.getEntityProcessor = function () { Scene.prototype.getEntityProcessor = function () {
return this.entityProcessors.firstOrDefault(function (processor) { return processor instanceof EntitySystem; }); return this.entityProcessors.getProcessor();
}; };
Scene.prototype.setActive = function () { Scene.prototype.setActive = function () {
SceneManager.setActiveScene(this); SceneManager.setActiveScene(this);
@@ -395,17 +398,21 @@ var Scene = (function (_super) {
}; };
Scene.prototype.initialize = function () { Scene.prototype.initialize = function () {
this.camera = this.createEntity("camera").addComponent(new Camera()); this.camera = this.createEntity("camera").addComponent(new Camera());
this.entityProcessors.forEach(function (processor) { return processor.initialize(); }); if (this.entityProcessors)
this.entityProcessors.begin();
}; };
Scene.prototype.onActive = function () { Scene.prototype.onActive = function () {
}; };
Scene.prototype.onDeactive = function () { Scene.prototype.onDeactive = function () {
}; };
Scene.prototype.update = function () { Scene.prototype.update = function () {
Time.update(egret.getTimer());
this.entities.updateLists(); this.entities.updateLists();
this.entityProcessors.forEach(function (processor) { return processor.update(); }); if (this.entityProcessors)
this.entityProcessors.update();
this.entities.update(); this.entities.update();
this.entityProcessors.forEach(function (processor) { return processor.lateUpdate(); }); if (this.entityProcessors)
this.entityProcessors.lateUpdate();
}; };
Scene.prototype.prepRenderState = function () { Scene.prototype.prepRenderState = function () {
this._projectionMatrix.m11 = 2 / this.stage.width; this._projectionMatrix.m11 = 2 / this.stage.width;
@@ -784,7 +791,7 @@ var BitSet = (function () {
}; };
BitSet.prototype.isEmpty = function () { BitSet.prototype.isEmpty = function () {
for (var i = this._bits.length - 1; i >= 0; i--) { for (var i = this._bits.length - 1; i >= 0; i--) {
if (this._bits[i] != 0) if (this._bits[i])
return false; return false;
} }
return true; return true;
@@ -805,10 +812,16 @@ var BitSet = (function () {
} }
return -1; return -1;
}; };
BitSet.prototype.set = function (pos) { BitSet.prototype.set = function (pos, value) {
if (value === void 0) { value = true; }
if (value) {
var offset = pos >> 6; var offset = pos >> 6;
this.ensure(offset); this.ensure(offset);
this._bits[offset] |= 1 << pos; this._bits[offset] |= 1 << pos;
}
else {
this.clear(pos);
}
}; };
BitSet.LONG_MASK = 0x3f; BitSet.LONG_MASK = 0x3f;
return BitSet; return BitSet;
@@ -895,7 +908,7 @@ var EntityList = (function () {
this._tempEntityList.forEach(function (entity) { this._tempEntityList.forEach(function (entity) {
_this._entities.remove(entity); _this._entities.remove(entity);
entity.scene = null; entity.scene = null;
_this.scene.entityProcessors.forEach(function (processor) { return processor.remove(entity); }); _this.scene.entityProcessors.onEntityRemoved(entity);
}); });
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
} }
@@ -906,13 +919,69 @@ var EntityList = (function () {
this._tempEntityList.forEach(function (entity) { this._tempEntityList.forEach(function (entity) {
_this._entities.push(entity); _this._entities.push(entity);
entity.scene = _this.scene; entity.scene = _this.scene;
_this.scene.entityProcessors.forEach(function (processor) { return processor.onChanged(entity); }); _this.scene.entityProcessors.onEntityAdded(entity);
}); });
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
} }
}; };
return EntityList; return EntityList;
}()); }());
var EntityProcessorList = (function () {
function EntityProcessorList() {
this._processors = [];
}
EntityProcessorList.prototype.add = function (processor) {
this._processors.push(processor);
};
EntityProcessorList.prototype.remove = function (processor) {
this._processors.remove(processor);
};
EntityProcessorList.prototype.onComponentAdded = function (entity) {
this.notifyEntityChanged(entity);
};
EntityProcessorList.prototype.onComponentRemoved = function (entity) {
this.notifyEntityChanged(entity);
};
EntityProcessorList.prototype.onEntityAdded = function (entity) {
this.notifyEntityChanged(entity);
};
EntityProcessorList.prototype.onEntityRemoved = function (entity) {
this.removeFromProcessors(entity);
};
EntityProcessorList.prototype.notifyEntityChanged = function (entity) {
for (var i = 0; i < this._processors.length; i++) {
this._processors[i].onChanged(entity);
}
};
EntityProcessorList.prototype.removeFromProcessors = function (entity) {
for (var i = 0; i < this._processors.length; i++) {
this._processors[i].remove(entity);
}
};
EntityProcessorList.prototype.begin = function () {
};
EntityProcessorList.prototype.update = function () {
for (var i = 0; i < this._processors.length; i++) {
this._processors[i].update();
}
};
EntityProcessorList.prototype.lateUpdate = function () {
for (var i = 0; i < this._processors.length; i++) {
this._processors[i].lateUpdate();
}
};
EntityProcessorList.prototype.end = function () {
};
EntityProcessorList.prototype.getProcessor = function () {
for (var i = 0; i < this._processors.length; i++) {
var processor = this._processors[i];
if (processor instanceof EntitySystem)
return processor;
}
return null;
};
return EntityProcessorList;
}());
var Matcher = (function () { var Matcher = (function () {
function Matcher() { function Matcher() {
this.allSet = new BitSet(); this.allSet = new BitSet();
@@ -937,6 +1006,19 @@ var Matcher = (function () {
}; };
return Matcher; return Matcher;
}()); }());
var Time = (function () {
function Time() {
}
Time.update = function (currentTime) {
var dt = (currentTime - this._lastTime) / 1000;
this.deltaTime = dt * this.timeScale;
this.unscaledDeltaTime = dt;
this._lastTime = currentTime;
};
Time.timeScale = 1;
Time._lastTime = 0;
return Time;
}());
var MathHelper = (function () { var MathHelper = (function () {
function MathHelper() { function MathHelper() {
} }

File diff suppressed because one or more lines are too long

View File

@@ -4,7 +4,10 @@ class SpawnerSystem extends EntityProcessingSystem {
} }
public processEntity(entity: Entity){ public processEntity(entity: Entity){
let spawner = entity.getComponent<SpawnComponent>(); let spawner = entity.getComponent<SpawnComponent>(SpawnComponent);
if (!spawner)
return;
if (spawner.numAlive <= 0) if (spawner.numAlive <= 0)
spawner.enabled = true; spawner.enabled = true;
@@ -13,11 +16,13 @@ class SpawnerSystem extends EntityProcessingSystem {
console.log("cooldown", spawner.cooldown); console.log("cooldown", spawner.cooldown);
if (spawner.cooldown == -1){ if (spawner.cooldown == -1){
spawner.cooldown = Math.random() * 60;
spawner.cooldown /= 4; spawner.cooldown /= 4;
} }
spawner.cooldown -= 0.001; spawner.cooldown -= Time.deltaTime;
if (spawner.cooldown <= 0){ if (spawner.cooldown <= 0){
spawner.cooldown = Math.random() * 60;
// CreateEnemy // CreateEnemy
spawner.numSpawned ++; spawner.numSpawned ++;
spawner.numAlive ++; spawner.numAlive ++;

View File

@@ -27,6 +27,7 @@ declare abstract class Component {
update(): void; update(): void;
bind(displayRender: egret.DisplayObject): this; bind(displayRender: egret.DisplayObject): this;
registerComponent(): void; registerComponent(): void;
deregisterComponent(): void;
} }
declare class Entity { declare class Entity {
name: string; name: string;
@@ -43,7 +44,7 @@ declare class Entity {
setUpdateOrder(updateOrder: number): this; setUpdateOrder(updateOrder: number): this;
attachToScene(newScene: Scene): void; attachToScene(newScene: Scene): void;
addComponent<T extends Component>(component: T): T; addComponent<T extends Component>(component: T): T;
getComponent<T extends Component>(): T; getComponent<T extends Component>(type: any): T;
update(): void; update(): void;
destory(): void; destory(): void;
} }
@@ -53,7 +54,7 @@ declare class Scene extends egret.DisplayObjectContainer {
private _projectionMatrix; private _projectionMatrix;
private _transformMatrix; private _transformMatrix;
private _matrixTransformMatrix; private _matrixTransformMatrix;
readonly entityProcessors: EntitySystem[]; readonly entityProcessors: EntityProcessorList;
constructor(displayObject: egret.DisplayObject); constructor(displayObject: egret.DisplayObject);
createEntity(name: string): Entity; createEntity(name: string): Entity;
addEntity(entity: Entity): Entity; addEntity(entity: Entity): Entity;
@@ -173,7 +174,7 @@ declare class BitSet {
intersects(set: BitSet): boolean; intersects(set: BitSet): boolean;
isEmpty(): boolean; isEmpty(): boolean;
nextSetBit(from: number): number; nextSetBit(from: number): number;
set(pos: number): void; set(pos: number, value?: boolean): void;
} }
declare class ComponentTypeManager { declare class ComponentTypeManager {
private static _componentTypesMask; private static _componentTypesMask;
@@ -196,6 +197,22 @@ declare class EntityList {
removeAllEntities(): void; removeAllEntities(): void;
updateLists(): void; updateLists(): void;
} }
declare class EntityProcessorList {
private _processors;
add(processor: EntitySystem): void;
remove(processor: EntitySystem): void;
onComponentAdded(entity: Entity): void;
onComponentRemoved(entity: Entity): void;
onEntityAdded(entity: Entity): void;
onEntityRemoved(entity: Entity): void;
protected notifyEntityChanged(entity: Entity): void;
protected removeFromProcessors(entity: Entity): void;
begin(): void;
update(): void;
lateUpdate(): void;
end(): void;
getProcessor<T extends EntitySystem>(): T;
}
declare class Matcher { declare class Matcher {
protected allSet: BitSet; protected allSet: BitSet;
protected exclusionSet: BitSet; protected exclusionSet: BitSet;
@@ -203,6 +220,13 @@ declare class Matcher {
static empty(): Matcher; static empty(): Matcher;
IsIntersted(e: Entity): boolean; IsIntersted(e: Entity): boolean;
} }
declare class Time {
static unscaledDeltaTime: any;
static deltaTime: number;
static timeScale: number;
private static _lastTime;
static update(currentTime: number): void;
}
declare class MathHelper { declare class MathHelper {
static toDegrees(radians: number): number; static toDegrees(radians: number): number;
static toRadians(degrees: number): number; static toRadians(degrees: number): number;

View File

@@ -76,7 +76,7 @@ Array.prototype.findAll = function (predicate) {
Array.prototype.contains = function (value) { Array.prototype.contains = function (value) {
function contains(array, value) { function contains(array, value) {
for (var i = 0, len = array.length; i < len; i++) { for (var i = 0, len = array.length; i < len; i++) {
if (JSON.stringify(array[i]) == JSON.stringify(value)) { if (array[i] == value) {
return true; return true;
} }
} }
@@ -265,9 +265,12 @@ var Component = (function () {
return this; return this;
}; };
Component.prototype.registerComponent = function () { Component.prototype.registerComponent = function () {
var _this = this; this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this), false);
this.entity.scene.entityProcessors.onComponentAdded(this.entity);
};
Component.prototype.deregisterComponent = function () {
this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this)); this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this));
this.entity.scene.entityProcessors.forEach(function (processor) { return processor.onChanged(_this.entity); }); this.entity.scene.entityProcessors.onComponentRemoved(this.entity);
}; };
return Component; return Component;
}()); }());
@@ -328,8 +331,8 @@ var Entity = (function () {
component.initialize(); component.initialize();
return component; return component;
}; };
Entity.prototype.getComponent = function () { Entity.prototype.getComponent = function (type) {
return this.components.firstOrDefault(function (component) { return component instanceof Component; }); return this.components.firstOrDefault(function (component) { return component instanceof type; });
}; };
Entity.prototype.update = function () { Entity.prototype.update = function () {
this.components.forEach(function (component) { return component.update(); }); this.components.forEach(function (component) { return component.update(); });
@@ -351,7 +354,7 @@ var Scene = (function (_super) {
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
displayObject.stage.addChild(_this); displayObject.stage.addChild(_this);
_this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); _this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
_this.entityProcessors = []; _this.entityProcessors = new EntityProcessorList();
_this.entities = new EntityList(_this); _this.entities = new EntityList(_this);
_this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this); _this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this);
_this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this); _this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this);
@@ -380,14 +383,14 @@ var Scene = (function (_super) {
}; };
Scene.prototype.addEntityProcessor = function (processor) { Scene.prototype.addEntityProcessor = function (processor) {
processor.scene = this; processor.scene = this;
this.entityProcessors.push(processor); this.entityProcessors.add(processor);
return processor; return processor;
}; };
Scene.prototype.removeEntityProcessor = function (processor) { Scene.prototype.removeEntityProcessor = function (processor) {
this.entityProcessors.remove(processor); this.entityProcessors.remove(processor);
}; };
Scene.prototype.getEntityProcessor = function () { Scene.prototype.getEntityProcessor = function () {
return this.entityProcessors.firstOrDefault(function (processor) { return processor instanceof EntitySystem; }); return this.entityProcessors.getProcessor();
}; };
Scene.prototype.setActive = function () { Scene.prototype.setActive = function () {
SceneManager.setActiveScene(this); SceneManager.setActiveScene(this);
@@ -395,17 +398,21 @@ var Scene = (function (_super) {
}; };
Scene.prototype.initialize = function () { Scene.prototype.initialize = function () {
this.camera = this.createEntity("camera").addComponent(new Camera()); this.camera = this.createEntity("camera").addComponent(new Camera());
this.entityProcessors.forEach(function (processor) { return processor.initialize(); }); if (this.entityProcessors)
this.entityProcessors.begin();
}; };
Scene.prototype.onActive = function () { Scene.prototype.onActive = function () {
}; };
Scene.prototype.onDeactive = function () { Scene.prototype.onDeactive = function () {
}; };
Scene.prototype.update = function () { Scene.prototype.update = function () {
Time.update(egret.getTimer());
this.entities.updateLists(); this.entities.updateLists();
this.entityProcessors.forEach(function (processor) { return processor.update(); }); if (this.entityProcessors)
this.entityProcessors.update();
this.entities.update(); this.entities.update();
this.entityProcessors.forEach(function (processor) { return processor.lateUpdate(); }); if (this.entityProcessors)
this.entityProcessors.lateUpdate();
}; };
Scene.prototype.prepRenderState = function () { Scene.prototype.prepRenderState = function () {
this._projectionMatrix.m11 = 2 / this.stage.width; this._projectionMatrix.m11 = 2 / this.stage.width;
@@ -784,7 +791,7 @@ var BitSet = (function () {
}; };
BitSet.prototype.isEmpty = function () { BitSet.prototype.isEmpty = function () {
for (var i = this._bits.length - 1; i >= 0; i--) { for (var i = this._bits.length - 1; i >= 0; i--) {
if (this._bits[i] != 0) if (this._bits[i])
return false; return false;
} }
return true; return true;
@@ -805,10 +812,16 @@ var BitSet = (function () {
} }
return -1; return -1;
}; };
BitSet.prototype.set = function (pos) { BitSet.prototype.set = function (pos, value) {
if (value === void 0) { value = true; }
if (value) {
var offset = pos >> 6; var offset = pos >> 6;
this.ensure(offset); this.ensure(offset);
this._bits[offset] |= 1 << pos; this._bits[offset] |= 1 << pos;
}
else {
this.clear(pos);
}
}; };
BitSet.LONG_MASK = 0x3f; BitSet.LONG_MASK = 0x3f;
return BitSet; return BitSet;
@@ -895,7 +908,7 @@ var EntityList = (function () {
this._tempEntityList.forEach(function (entity) { this._tempEntityList.forEach(function (entity) {
_this._entities.remove(entity); _this._entities.remove(entity);
entity.scene = null; entity.scene = null;
_this.scene.entityProcessors.forEach(function (processor) { return processor.remove(entity); }); _this.scene.entityProcessors.onEntityRemoved(entity);
}); });
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
} }
@@ -906,13 +919,69 @@ var EntityList = (function () {
this._tempEntityList.forEach(function (entity) { this._tempEntityList.forEach(function (entity) {
_this._entities.push(entity); _this._entities.push(entity);
entity.scene = _this.scene; entity.scene = _this.scene;
_this.scene.entityProcessors.forEach(function (processor) { return processor.onChanged(entity); }); _this.scene.entityProcessors.onEntityAdded(entity);
}); });
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
} }
}; };
return EntityList; return EntityList;
}()); }());
var EntityProcessorList = (function () {
function EntityProcessorList() {
this._processors = [];
}
EntityProcessorList.prototype.add = function (processor) {
this._processors.push(processor);
};
EntityProcessorList.prototype.remove = function (processor) {
this._processors.remove(processor);
};
EntityProcessorList.prototype.onComponentAdded = function (entity) {
this.notifyEntityChanged(entity);
};
EntityProcessorList.prototype.onComponentRemoved = function (entity) {
this.notifyEntityChanged(entity);
};
EntityProcessorList.prototype.onEntityAdded = function (entity) {
this.notifyEntityChanged(entity);
};
EntityProcessorList.prototype.onEntityRemoved = function (entity) {
this.removeFromProcessors(entity);
};
EntityProcessorList.prototype.notifyEntityChanged = function (entity) {
for (var i = 0; i < this._processors.length; i++) {
this._processors[i].onChanged(entity);
}
};
EntityProcessorList.prototype.removeFromProcessors = function (entity) {
for (var i = 0; i < this._processors.length; i++) {
this._processors[i].remove(entity);
}
};
EntityProcessorList.prototype.begin = function () {
};
EntityProcessorList.prototype.update = function () {
for (var i = 0; i < this._processors.length; i++) {
this._processors[i].update();
}
};
EntityProcessorList.prototype.lateUpdate = function () {
for (var i = 0; i < this._processors.length; i++) {
this._processors[i].lateUpdate();
}
};
EntityProcessorList.prototype.end = function () {
};
EntityProcessorList.prototype.getProcessor = function () {
for (var i = 0; i < this._processors.length; i++) {
var processor = this._processors[i];
if (processor instanceof EntitySystem)
return processor;
}
return null;
};
return EntityProcessorList;
}());
var Matcher = (function () { var Matcher = (function () {
function Matcher() { function Matcher() {
this.allSet = new BitSet(); this.allSet = new BitSet();
@@ -937,6 +1006,19 @@ var Matcher = (function () {
}; };
return Matcher; return Matcher;
}()); }());
var Time = (function () {
function Time() {
}
Time.update = function (currentTime) {
var dt = (currentTime - this._lastTime) / 1000;
this.deltaTime = dt * this.timeScale;
this.unscaledDeltaTime = dt;
this._lastTime = currentTime;
};
Time.timeScale = 1;
Time._lastTime = 0;
return Time;
}());
var MathHelper = (function () { var MathHelper = (function () {
function MathHelper() { function MathHelper() {
} }

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,5 @@
{ {
"name": "egret-ecs", "name": "@esengine/egret-framework",
"version": "1.0.0", "version": "1.0.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,

View File

@@ -28,5 +28,7 @@
"watchify": "^3.9.0", "watchify": "^3.9.0",
"gulp-inject-string": "^1.1.2" "gulp-inject-string": "^1.1.2"
}, },
"publishConfig": { "registry": "https://npm.pkg.github.com/359807859@qq.com" } "publishConfig": {
"registry": "https://npm.pkg.github.com/359807859@qq.com"
}
} }

View File

@@ -33,7 +33,12 @@ abstract class Component {
/** 内部使用 运行时不应该调用 */ /** 内部使用 运行时不应该调用 */
public registerComponent(){ 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.componentBits.set(ComponentTypeManager.getIndexFor(this));
this.entity.scene.entityProcessors.forEach(processor => processor.onChanged(this.entity)); this.entity.scene.entityProcessors.onComponentRemoved(this.entity);
} }
} }

View File

@@ -70,8 +70,8 @@ class Entity {
return component; return component;
} }
public getComponent<T extends Component>(): T{ public getComponent<T extends Component>(type): T{
return this.components.firstOrDefault(component => component instanceof Component) as T; return this.components.firstOrDefault(component => component instanceof type) as T;
} }
public update(){ public update(){

View File

@@ -7,13 +7,13 @@ class Scene extends egret.DisplayObjectContainer {
private _transformMatrix: Matrix2D; private _transformMatrix: Matrix2D;
private _matrixTransformMatrix: Matrix2D; private _matrixTransformMatrix: Matrix2D;
public readonly entityProcessors: EntitySystem[]; public readonly entityProcessors: EntityProcessorList;
constructor(displayObject: egret.DisplayObject){ constructor(displayObject: egret.DisplayObject){
super(); super();
displayObject.stage.addChild(this); displayObject.stage.addChild(this);
this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
this.entityProcessors = []; this.entityProcessors = new EntityProcessorList();
this.entities = new EntityList(this); this.entities = new EntityList(this);
this.addEventListener(egret.Event.ACTIVATE, this.onActive, this); this.addEventListener(egret.Event.ACTIVATE, this.onActive, this);
@@ -53,7 +53,7 @@ class Scene extends egret.DisplayObjectContainer {
*/ */
public addEntityProcessor(processor: EntitySystem){ public addEntityProcessor(processor: EntitySystem){
processor.scene = this; processor.scene = this;
this.entityProcessors.push(processor); this.entityProcessors.add(processor);
return processor; return processor;
} }
@@ -62,7 +62,7 @@ class Scene extends egret.DisplayObjectContainer {
} }
public getEntityProcessor<T extends EntitySystem>(): T { public getEntityProcessor<T extends EntitySystem>(): T {
return this.entityProcessors.firstOrDefault(processor => processor instanceof EntitySystem) as T; return this.entityProcessors.getProcessor<T>();
} }
public setActive(): Scene{ public setActive(): Scene{
@@ -75,7 +75,9 @@ class Scene extends egret.DisplayObjectContainer {
public initialize(){ public initialize(){
/** 初始化默认相机 */ /** 初始化默认相机 */
this.camera = this.createEntity("camera").addComponent(new Camera()); 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(){ public update(){
Time.update(egret.getTimer());
this.entities.updateLists(); this.entities.updateLists();
this.entityProcessors.forEach(processor => processor.update()); if (this.entityProcessors)
this.entityProcessors.update()
this.entities.update(); this.entities.update();
this.entityProcessors.forEach(processor => processor.lateUpdate());
if (this.entityProcessors)
this.entityProcessors.lateUpdate();
} }
public prepRenderState(){ public prepRenderState(){

View File

@@ -94,7 +94,7 @@ class BitSet{
public isEmpty(): boolean{ public isEmpty(): boolean{
for (let i = this._bits.length - 1; i >= 0; i --){ for (let i = this._bits.length - 1; i >= 0; i --){
if (this._bits[i] != 0) if (this._bits[i])
return false; return false;
} }
@@ -121,9 +121,13 @@ class BitSet{
return -1; return -1;
} }
public set(pos: number){ public set(pos: number, value: boolean = true){
if (value){
let offset = pos >> 6; let offset = pos >> 6;
this.ensure(offset); this.ensure(offset);
this._bits[offset] |= 1 << pos; this._bits[offset] |= 1 << pos;
}else{
this.clear(pos);
}
} }
} }

View File

@@ -69,7 +69,7 @@ class EntityList{
this._entities.remove(entity); this._entities.remove(entity);
entity.scene = null; entity.scene = null;
this.scene.entityProcessors.forEach(processor => processor.remove(entity)); this.scene.entityProcessors.onEntityRemoved(entity);
}); });
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
@@ -83,7 +83,7 @@ class EntityList{
this._entities.push(entity); this._entities.push(entity);
entity.scene = this.scene; entity.scene = this.scene;
this.scene.entityProcessors.forEach(processor => processor.onChanged(entity)); this.scene.entityProcessors.onEntityAdded(entity)
}); });
this._tempEntityList.length = 0; this._tempEntityList.length = 0;

View 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;
}
}

View 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;
}
}

View File

@@ -174,7 +174,7 @@ Array.prototype.findAll = function (predicate) {
Array.prototype.contains = function (value) { Array.prototype.contains = function (value) {
function contains(array, value) { function contains(array, value) {
for (let i = 0, len = array.length; i < len; i++) { for (let i = 0, len = array.length; i < len; i++) {
if (JSON.stringify(array[i]) == JSON.stringify(value)) { if (array[i] == value) {
return true; return true;
} }
} }