新增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;
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;

View File

@@ -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() {
}

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