新增基础实体系统

This commit is contained in:
yhh
2020-06-08 18:26:05 +08:00
parent 7939253622
commit 57efc5b0e6
18 changed files with 551 additions and 28 deletions

View File

@@ -1,11 +1,27 @@
abstract class Component {
public entity: Entity;
public displayRender: egret.DisplayObject;
private _enabled: boolean = true;
public get enabled(){
return this.entity ? this.entity.enabled && this._enabled : this._enabled;
}
public set enabled(value: boolean){
this.setEnabled(value);
}
public setEnabled(isEnabled: boolean){
if (this._enabled != isEnabled){
this._enabled = isEnabled;
}
return this;
}
public abstract initialize();
public update(){
}
/** 绑定显示对象 */

View File

@@ -7,6 +7,23 @@ class Entity {
/** 当前附加到此实体的所有组件的列表 */
public readonly components: Component[];
private _updateOrder: number = 0;
private _enabled: boolean = true;
public get enabled(){
return this._enabled;
}
public set enabled(value: boolean){
this.setEnabled(value);
}
public setEnabled(isEnabled: boolean){
if (this._enabled != isEnabled){
this._enabled = isEnabled;
}
return this;
}
constructor(name: string){
this.name = name;
@@ -49,6 +66,10 @@ class Entity {
return component;
}
public getComponent<T extends Component>(): T{
return this.components.firstOrDefault(component => component instanceof Component) as T;
}
public update(){
this.components.forEach(component => component.update());
this.transform.updateTransform();

View File

@@ -7,12 +7,14 @@ class Scene extends egret.DisplayObjectContainer {
private _transformMatrix: Matrix2D;
private _matrixTransformMatrix: Matrix2D;
public readonly entityProcessors: EntitySystem[];
constructor(displayObject: egret.DisplayObject){
super();
displayObject.stage.addChild(this);
/** 初始化默认相机 */
this.camera = this.createEntity("camera").addComponent(new Camera());
this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
this.entityProcessors = [];
this.addEventListener(egret.Event.ACTIVATE, this.onActive, this);
this.addEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
@@ -31,6 +33,32 @@ class Scene extends egret.DisplayObjectContainer {
return entity;
}
public destoryAllEntities(){
this.entities.forEach(entity => entity.destory());
}
public findEntity(name: string): Entity{
return this.entities.firstOrDefault(entity => entity.name == name);
}
/**
* 在场景中添加一个EntitySystem处理器
* @param processor 处理器
*/
public addEntityProcessor(processor: EntitySystem){
processor.scene = this;
this.entityProcessors.push(processor);
return processor;
}
public removeEntityProcessor(processor: EntitySystem){
this.entityProcessors.remove(processor);
}
public getEntityProcessor<T extends EntitySystem>(): T {
return this.entityProcessors.firstOrDefault(processor => processor instanceof EntitySystem) as T;
}
public setActive(): Scene{
SceneManager.setActiveScene(this);
@@ -39,7 +67,9 @@ class Scene extends egret.DisplayObjectContainer {
/** 初始化场景 */
public initialize(){
/** 初始化默认相机 */
this.camera = this.createEntity("camera").addComponent(new Camera());
this.entityProcessors.forEach(processor => processor.initialize());
}
/** 场景激活 */
@@ -53,7 +83,9 @@ class Scene extends egret.DisplayObjectContainer {
}
public update(){
this.entityProcessors.forEach(processor => processor.update());
this.entities.forEach(entity => entity.update());
this.entityProcessors.forEach(processor => processor.lateUpdate());
}
public prepRenderState(){

View File

@@ -0,0 +1,23 @@
///<reference path="./EntitySystem.ts" />
/**
* 基本实体处理系统。将其用作处理具有特定组件的许多实体的基础
*/
abstract class EntityProcessingSystem extends EntitySystem {
constructor(matcher: Matcher) {
super(matcher);
}
public abstract processEntity(entity: Entity);
public lateProcessEntity(entity: Entity) {
}
protected process(entities: Entity[]) {
entities.forEach(entity => this.processEntity(entity));
}
protected lateProcess(entities: Entity[]) {
entities.forEach(entity => this.lateProcessEntity(entity));
}
}

View File

@@ -0,0 +1,52 @@
class EntitySystem {
private _scene: Scene;
private _entities: Entity[] = [];
private _matcher: Matcher;
public get matcher(){
return this._matcher;
}
public get scene(){
return this._scene;
}
public set scene(value: Scene){
this._scene = value;
this._entities = [];
}
constructor(matcher?: Matcher){
this._matcher = matcher ? matcher : Matcher.empty();
}
public initialize(){
}
public update(){
this.begin();
this.process(this._entities);
}
public lateUpdate(){
this.lateProcess(this._entities);
this.end();
}
protected begin(){
}
protected process(entities: Entity[]){
}
protected lateProcess(entities: Entity[]){
}
protected end(){
}
}

View File

@@ -0,0 +1,5 @@
class Matcher{
public static empty(){
return new Matcher();
}
}