新增entityprocessor管理实体解析器
This commit is contained in:
30
source/bin/framework.d.ts
vendored
30
source/bin/framework.d.ts
vendored
@@ -27,6 +27,7 @@ declare abstract class Component {
|
||||
update(): void;
|
||||
bind(displayRender: egret.DisplayObject): this;
|
||||
registerComponent(): void;
|
||||
deregisterComponent(): void;
|
||||
}
|
||||
declare class Entity {
|
||||
name: string;
|
||||
@@ -43,7 +44,7 @@ declare class Entity {
|
||||
setUpdateOrder(updateOrder: number): this;
|
||||
attachToScene(newScene: Scene): void;
|
||||
addComponent<T extends Component>(component: T): T;
|
||||
getComponent<T extends Component>(): T;
|
||||
getComponent<T extends Component>(type: any): T;
|
||||
update(): void;
|
||||
destory(): void;
|
||||
}
|
||||
@@ -53,7 +54,7 @@ declare class Scene extends egret.DisplayObjectContainer {
|
||||
private _projectionMatrix;
|
||||
private _transformMatrix;
|
||||
private _matrixTransformMatrix;
|
||||
readonly entityProcessors: EntitySystem[];
|
||||
readonly entityProcessors: EntityProcessorList;
|
||||
constructor(displayObject: egret.DisplayObject);
|
||||
createEntity(name: string): Entity;
|
||||
addEntity(entity: Entity): Entity;
|
||||
@@ -173,7 +174,7 @@ declare class BitSet {
|
||||
intersects(set: BitSet): boolean;
|
||||
isEmpty(): boolean;
|
||||
nextSetBit(from: number): number;
|
||||
set(pos: number): void;
|
||||
set(pos: number, value?: boolean): void;
|
||||
}
|
||||
declare class ComponentTypeManager {
|
||||
private static _componentTypesMask;
|
||||
@@ -196,6 +197,22 @@ declare class EntityList {
|
||||
removeAllEntities(): 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 {
|
||||
protected allSet: BitSet;
|
||||
protected exclusionSet: BitSet;
|
||||
@@ -203,6 +220,13 @@ declare class Matcher {
|
||||
static empty(): Matcher;
|
||||
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 {
|
||||
static toDegrees(radians: number): number;
|
||||
static toRadians(degrees: number): number;
|
||||
|
||||
@@ -76,7 +76,7 @@ Array.prototype.findAll = function (predicate) {
|
||||
Array.prototype.contains = function (value) {
|
||||
function contains(array, value) {
|
||||
for (var i = 0, len = array.length; i < len; i++) {
|
||||
if (JSON.stringify(array[i]) == JSON.stringify(value)) {
|
||||
if (array[i] == value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -265,9 +265,12 @@ var Component = (function () {
|
||||
return this;
|
||||
};
|
||||
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.scene.entityProcessors.forEach(function (processor) { return processor.onChanged(_this.entity); });
|
||||
this.entity.scene.entityProcessors.onComponentRemoved(this.entity);
|
||||
};
|
||||
return Component;
|
||||
}());
|
||||
@@ -328,8 +331,8 @@ var Entity = (function () {
|
||||
component.initialize();
|
||||
return component;
|
||||
};
|
||||
Entity.prototype.getComponent = function () {
|
||||
return this.components.firstOrDefault(function (component) { return component instanceof Component; });
|
||||
Entity.prototype.getComponent = function (type) {
|
||||
return this.components.firstOrDefault(function (component) { return component instanceof type; });
|
||||
};
|
||||
Entity.prototype.update = function () {
|
||||
this.components.forEach(function (component) { return component.update(); });
|
||||
@@ -351,7 +354,7 @@ var Scene = (function (_super) {
|
||||
var _this = _super.call(this) || this;
|
||||
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);
|
||||
_this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this);
|
||||
@@ -380,14 +383,14 @@ var Scene = (function (_super) {
|
||||
};
|
||||
Scene.prototype.addEntityProcessor = function (processor) {
|
||||
processor.scene = this;
|
||||
this.entityProcessors.push(processor);
|
||||
this.entityProcessors.add(processor);
|
||||
return processor;
|
||||
};
|
||||
Scene.prototype.removeEntityProcessor = function (processor) {
|
||||
this.entityProcessors.remove(processor);
|
||||
};
|
||||
Scene.prototype.getEntityProcessor = function () {
|
||||
return this.entityProcessors.firstOrDefault(function (processor) { return processor instanceof EntitySystem; });
|
||||
return this.entityProcessors.getProcessor();
|
||||
};
|
||||
Scene.prototype.setActive = function () {
|
||||
SceneManager.setActiveScene(this);
|
||||
@@ -395,17 +398,21 @@ var Scene = (function (_super) {
|
||||
};
|
||||
Scene.prototype.initialize = function () {
|
||||
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.onDeactive = function () {
|
||||
};
|
||||
Scene.prototype.update = function () {
|
||||
Time.update(egret.getTimer());
|
||||
this.entities.updateLists();
|
||||
this.entityProcessors.forEach(function (processor) { return processor.update(); });
|
||||
if (this.entityProcessors)
|
||||
this.entityProcessors.update();
|
||||
this.entities.update();
|
||||
this.entityProcessors.forEach(function (processor) { return processor.lateUpdate(); });
|
||||
if (this.entityProcessors)
|
||||
this.entityProcessors.lateUpdate();
|
||||
};
|
||||
Scene.prototype.prepRenderState = function () {
|
||||
this._projectionMatrix.m11 = 2 / this.stage.width;
|
||||
@@ -784,7 +791,7 @@ var BitSet = (function () {
|
||||
};
|
||||
BitSet.prototype.isEmpty = function () {
|
||||
for (var i = this._bits.length - 1; i >= 0; i--) {
|
||||
if (this._bits[i] != 0)
|
||||
if (this._bits[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -805,10 +812,16 @@ var BitSet = (function () {
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
BitSet.prototype.set = function (pos) {
|
||||
var offset = pos >> 6;
|
||||
this.ensure(offset);
|
||||
this._bits[offset] |= 1 << pos;
|
||||
BitSet.prototype.set = function (pos, value) {
|
||||
if (value === void 0) { value = true; }
|
||||
if (value) {
|
||||
var offset = pos >> 6;
|
||||
this.ensure(offset);
|
||||
this._bits[offset] |= 1 << pos;
|
||||
}
|
||||
else {
|
||||
this.clear(pos);
|
||||
}
|
||||
};
|
||||
BitSet.LONG_MASK = 0x3f;
|
||||
return BitSet;
|
||||
@@ -895,7 +908,7 @@ var EntityList = (function () {
|
||||
this._tempEntityList.forEach(function (entity) {
|
||||
_this._entities.remove(entity);
|
||||
entity.scene = null;
|
||||
_this.scene.entityProcessors.forEach(function (processor) { return processor.remove(entity); });
|
||||
_this.scene.entityProcessors.onEntityRemoved(entity);
|
||||
});
|
||||
this._tempEntityList.length = 0;
|
||||
}
|
||||
@@ -906,13 +919,69 @@ var EntityList = (function () {
|
||||
this._tempEntityList.forEach(function (entity) {
|
||||
_this._entities.push(entity);
|
||||
entity.scene = _this.scene;
|
||||
_this.scene.entityProcessors.forEach(function (processor) { return processor.onChanged(entity); });
|
||||
_this.scene.entityProcessors.onEntityAdded(entity);
|
||||
});
|
||||
this._tempEntityList.length = 0;
|
||||
}
|
||||
};
|
||||
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 () {
|
||||
function Matcher() {
|
||||
this.allSet = new BitSet();
|
||||
@@ -937,6 +1006,19 @@ var Matcher = (function () {
|
||||
};
|
||||
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 () {
|
||||
function MathHelper() {
|
||||
}
|
||||
|
||||
2
source/bin/framework.min.js
vendored
2
source/bin/framework.min.js
vendored
File diff suppressed because one or more lines are too long
2
source/package-lock.json
generated
2
source/package-lock.json
generated
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "egret-ecs",
|
||||
"name": "@esengine/egret-framework",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
|
||||
@@ -28,5 +28,7 @@
|
||||
"watchify": "^3.9.0",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -174,7 +174,7 @@ Array.prototype.findAll = function (predicate) {
|
||||
Array.prototype.contains = function (value) {
|
||||
function contains(array, value) {
|
||||
for (let i = 0, len = array.length; i < len; i++) {
|
||||
if (JSON.stringify(array[i]) == JSON.stringify(value)) {
|
||||
if (array[i] == value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user