新增基础实体系统

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

@@ -20,6 +20,9 @@ declare interface Array<T> {
declare abstract class Component {
entity: Entity;
displayRender: egret.DisplayObject;
private _enabled;
enabled: boolean;
setEnabled(isEnabled: boolean): this;
abstract initialize(): any;
update(): void;
bind(displayRender: egret.DisplayObject): this;
@@ -30,11 +33,15 @@ declare class Entity {
readonly transform: Transform;
readonly components: Component[];
private _updateOrder;
private _enabled;
enabled: boolean;
setEnabled(isEnabled: boolean): this;
constructor(name: string);
updateOrder: number;
setUpdateOrder(updateOrder: number): this;
attachToScene(newScene: Scene): void;
addComponent<T extends Component>(component: T): T;
getComponent<T extends Component>(): T;
update(): void;
destory(): void;
}
@@ -44,9 +51,15 @@ declare class Scene extends egret.DisplayObjectContainer {
private _projectionMatrix;
private _transformMatrix;
private _matrixTransformMatrix;
readonly entityProcessors: EntitySystem[];
constructor(displayObject: egret.DisplayObject);
createEntity(name: string): Entity;
addEntity(entity: Entity): Entity;
destoryAllEntities(): void;
findEntity(name: string): Entity;
addEntityProcessor(processor: EntitySystem): EntitySystem;
removeEntityProcessor(processor: EntitySystem): void;
getEntityProcessor<T extends EntitySystem>(): T;
setActive(): Scene;
initialize(): void;
onActive(): void;
@@ -118,6 +131,31 @@ declare class Camera extends Component {
updateMatrixes(): void;
destory(): void;
}
declare class EntitySystem {
private _scene;
private _entities;
private _matcher;
readonly matcher: Matcher;
scene: Scene;
constructor(matcher?: Matcher);
initialize(): void;
update(): void;
lateUpdate(): void;
protected begin(): void;
protected process(entities: Entity[]): void;
protected lateProcess(entities: Entity[]): void;
protected end(): void;
}
declare abstract class EntityProcessingSystem extends EntitySystem {
constructor(matcher: Matcher);
abstract processEntity(entity: Entity): any;
lateProcessEntity(entity: Entity): void;
protected process(entities: Entity[]): void;
protected lateProcess(entities: Entity[]): void;
}
declare class Matcher {
static empty(): Matcher;
}
declare class MathHelper {
static toDegrees(radians: number): number;
static toRadians(degrees: number): number;

View File

@@ -240,7 +240,24 @@ Array.prototype.sum = function (selector) {
};
var Component = (function () {
function Component() {
this._enabled = true;
}
Object.defineProperty(Component.prototype, "enabled", {
get: function () {
return this.entity ? this.entity.enabled && this._enabled : this._enabled;
},
set: function (value) {
this.setEnabled(value);
},
enumerable: true,
configurable: true
});
Component.prototype.setEnabled = function (isEnabled) {
if (this._enabled != isEnabled) {
this._enabled = isEnabled;
}
return this;
};
Component.prototype.update = function () {
};
Component.prototype.bind = function (displayRender) {
@@ -252,10 +269,27 @@ var Component = (function () {
var Entity = (function () {
function Entity(name) {
this._updateOrder = 0;
this._enabled = true;
this.name = name;
this.transform = new Transform(this);
this.components = [];
}
Object.defineProperty(Entity.prototype, "enabled", {
get: function () {
return this._enabled;
},
set: function (value) {
this.setEnabled(value);
},
enumerable: true,
configurable: true
});
Entity.prototype.setEnabled = function (isEnabled) {
if (this._enabled != isEnabled) {
this._enabled = isEnabled;
}
return this;
};
Object.defineProperty(Entity.prototype, "updateOrder", {
get: function () {
return this._updateOrder;
@@ -287,6 +321,9 @@ var Entity = (function () {
component.initialize();
return component;
};
Entity.prototype.getComponent = function () {
return this.components.firstOrDefault(function (component) { return component instanceof Component; });
};
Entity.prototype.update = function () {
this.components.forEach(function (component) { return component.update(); });
this.transform.updateTransform();
@@ -307,8 +344,8 @@ var Scene = (function (_super) {
var _this = _super.call(this) || this;
_this.entities = [];
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);
@@ -324,18 +361,39 @@ var Scene = (function (_super) {
entity.scene = this;
return entity;
};
Scene.prototype.destoryAllEntities = function () {
this.entities.forEach(function (entity) { return entity.destory(); });
};
Scene.prototype.findEntity = function (name) {
return this.entities.firstOrDefault(function (entity) { return entity.name == name; });
};
Scene.prototype.addEntityProcessor = function (processor) {
processor.scene = this;
this.entityProcessors.push(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; });
};
Scene.prototype.setActive = function () {
SceneManager.setActiveScene(this);
return this;
};
Scene.prototype.initialize = function () {
this.camera = this.createEntity("camera").addComponent(new Camera());
this.entityProcessors.forEach(function (processor) { return processor.initialize(); });
};
Scene.prototype.onActive = function () {
};
Scene.prototype.onDeactive = function () {
};
Scene.prototype.update = function () {
this.entityProcessors.forEach(function (processor) { return processor.update(); });
this.entities.forEach(function (entity) { return entity.update(); });
this.entityProcessors.forEach(function (processor) { return processor.lateUpdate(); });
};
Scene.prototype.prepRenderState = function () {
this._projectionMatrix.m11 = 2 / this.stage.width;
@@ -561,6 +619,74 @@ var Camera = (function (_super) {
};
return Camera;
}(Component));
var EntitySystem = (function () {
function EntitySystem(matcher) {
this._entities = [];
this._matcher = matcher ? matcher : Matcher.empty();
}
Object.defineProperty(EntitySystem.prototype, "matcher", {
get: function () {
return this._matcher;
},
enumerable: true,
configurable: true
});
Object.defineProperty(EntitySystem.prototype, "scene", {
get: function () {
return this._scene;
},
set: function (value) {
this._scene = value;
this._entities = [];
},
enumerable: true,
configurable: true
});
EntitySystem.prototype.initialize = function () {
};
EntitySystem.prototype.update = function () {
this.begin();
this.process(this._entities);
};
EntitySystem.prototype.lateUpdate = function () {
this.lateProcess(this._entities);
this.end();
};
EntitySystem.prototype.begin = function () {
};
EntitySystem.prototype.process = function (entities) {
};
EntitySystem.prototype.lateProcess = function (entities) {
};
EntitySystem.prototype.end = function () {
};
return EntitySystem;
}());
var EntityProcessingSystem = (function (_super) {
__extends(EntityProcessingSystem, _super);
function EntityProcessingSystem(matcher) {
return _super.call(this, matcher) || this;
}
EntityProcessingSystem.prototype.lateProcessEntity = function (entity) {
};
EntityProcessingSystem.prototype.process = function (entities) {
var _this = this;
entities.forEach(function (entity) { return _this.processEntity(entity); });
};
EntityProcessingSystem.prototype.lateProcess = function (entities) {
var _this = this;
entities.forEach(function (entity) { return _this.lateProcessEntity(entity); });
};
return EntityProcessingSystem;
}(EntitySystem));
var Matcher = (function () {
function Matcher() {
}
Matcher.empty = function () {
return new Matcher();
};
return Matcher;
}());
var MathHelper = (function () {
function MathHelper() {
}

File diff suppressed because one or more lines are too long

View File

@@ -15,6 +15,8 @@
"bin-debug/Main.js",
"bin-debug/Platform.js",
"bin-debug/ThemeAdapter.js",
"bin-debug/game/TestComponent.js"
"bin-debug/game/MainScene.js",
"bin-debug/game/SpawnerComponent.js",
"bin-debug/game/SpawnerSystem.js"
]
}

View File

@@ -91,12 +91,9 @@ class Main extends eui.UILayer {
* Create scene interface
*/
protected createGameScene(): void {
let scene = SceneManager.createScene("main", new Scene(this)).setActive();
let scene = SceneManager.createScene("main", new MainScene(this)).setActive();
let player = scene.createEntity("player");
let texture = RES.getRes("checkbox_select_down_png");
player.addComponent(new TestComponent(new eui.Image(texture)));
scene.camera.setPosition(new Vector2(-100, -50));
player.transform.setPosition(new Vector2(10, 10));
player.addComponent(new SpawnComponent(EnemyType.worm));
// console.log(player.transform.position);
}
}

View File

@@ -0,0 +1,7 @@
class MainScene extends Scene {
constructor(displayContent: egret.DisplayObject){
super(displayContent);
this.addEntityProcessor(new SpawnerSystem(new Matcher()));
}
}

View File

@@ -0,0 +1,25 @@
class SpawnComponent extends Component {
public cooldown = -1;
public minInterval = 2;
public maxInterval = 60;
public enemyType = EnemyType.worm;
public numSpawned = 0;
public numAlive = 0;
constructor(enemyType: EnemyType) {
super();
this.enemyType = enemyType;
}
public initialize() {
// console.log("initialize");
}
public update() {
// console.log("update");
}
}
enum EnemyType {
worm
}

View File

@@ -0,0 +1,29 @@
class SpawnerSystem extends EntityProcessingSystem {
constructor(matcher: Matcher){
super(matcher);
}
public processEntity(entity: Entity){
let spawner = entity.getComponent<SpawnComponent>();
if (spawner.numAlive <= 0)
spawner.enabled = true;
if (!spawner.enabled)
return;
console.log("cooldown", spawner.cooldown);
if (spawner.cooldown == -1){
spawner.cooldown /= 4;
}
spawner.cooldown -= 0.001;
if (spawner.cooldown <= 0){
// CreateEnemy
spawner.numSpawned ++;
spawner.numAlive ++;
if (spawner.numAlive > 0)
spawner.enabled = false;
}
}
}

View File

@@ -1,14 +0,0 @@
class TestComponent extends Component {
constructor(displayRender: egret.DisplayObject){
super();
this.bind(displayRender);
}
public initialize(){
// console.log("initialize");
}
public update(){
// console.log("update");
}
}

View File

@@ -20,6 +20,9 @@ declare interface Array<T> {
declare abstract class Component {
entity: Entity;
displayRender: egret.DisplayObject;
private _enabled;
enabled: boolean;
setEnabled(isEnabled: boolean): this;
abstract initialize(): any;
update(): void;
bind(displayRender: egret.DisplayObject): this;
@@ -30,11 +33,15 @@ declare class Entity {
readonly transform: Transform;
readonly components: Component[];
private _updateOrder;
private _enabled;
enabled: boolean;
setEnabled(isEnabled: boolean): this;
constructor(name: string);
updateOrder: number;
setUpdateOrder(updateOrder: number): this;
attachToScene(newScene: Scene): void;
addComponent<T extends Component>(component: T): T;
getComponent<T extends Component>(): T;
update(): void;
destory(): void;
}
@@ -44,9 +51,15 @@ declare class Scene extends egret.DisplayObjectContainer {
private _projectionMatrix;
private _transformMatrix;
private _matrixTransformMatrix;
readonly entityProcessors: EntitySystem[];
constructor(displayObject: egret.DisplayObject);
createEntity(name: string): Entity;
addEntity(entity: Entity): Entity;
destoryAllEntities(): void;
findEntity(name: string): Entity;
addEntityProcessor(processor: EntitySystem): EntitySystem;
removeEntityProcessor(processor: EntitySystem): void;
getEntityProcessor<T extends EntitySystem>(): T;
setActive(): Scene;
initialize(): void;
onActive(): void;
@@ -118,6 +131,31 @@ declare class Camera extends Component {
updateMatrixes(): void;
destory(): void;
}
declare class EntitySystem {
private _scene;
private _entities;
private _matcher;
readonly matcher: Matcher;
scene: Scene;
constructor(matcher?: Matcher);
initialize(): void;
update(): void;
lateUpdate(): void;
protected begin(): void;
protected process(entities: Entity[]): void;
protected lateProcess(entities: Entity[]): void;
protected end(): void;
}
declare abstract class EntityProcessingSystem extends EntitySystem {
constructor(matcher: Matcher);
abstract processEntity(entity: Entity): any;
lateProcessEntity(entity: Entity): void;
protected process(entities: Entity[]): void;
protected lateProcess(entities: Entity[]): void;
}
declare class Matcher {
static empty(): Matcher;
}
declare class MathHelper {
static toDegrees(radians: number): number;
static toRadians(degrees: number): number;

View File

@@ -240,7 +240,24 @@ Array.prototype.sum = function (selector) {
};
var Component = (function () {
function Component() {
this._enabled = true;
}
Object.defineProperty(Component.prototype, "enabled", {
get: function () {
return this.entity ? this.entity.enabled && this._enabled : this._enabled;
},
set: function (value) {
this.setEnabled(value);
},
enumerable: true,
configurable: true
});
Component.prototype.setEnabled = function (isEnabled) {
if (this._enabled != isEnabled) {
this._enabled = isEnabled;
}
return this;
};
Component.prototype.update = function () {
};
Component.prototype.bind = function (displayRender) {
@@ -252,10 +269,27 @@ var Component = (function () {
var Entity = (function () {
function Entity(name) {
this._updateOrder = 0;
this._enabled = true;
this.name = name;
this.transform = new Transform(this);
this.components = [];
}
Object.defineProperty(Entity.prototype, "enabled", {
get: function () {
return this._enabled;
},
set: function (value) {
this.setEnabled(value);
},
enumerable: true,
configurable: true
});
Entity.prototype.setEnabled = function (isEnabled) {
if (this._enabled != isEnabled) {
this._enabled = isEnabled;
}
return this;
};
Object.defineProperty(Entity.prototype, "updateOrder", {
get: function () {
return this._updateOrder;
@@ -287,6 +321,9 @@ var Entity = (function () {
component.initialize();
return component;
};
Entity.prototype.getComponent = function () {
return this.components.firstOrDefault(function (component) { return component instanceof Component; });
};
Entity.prototype.update = function () {
this.components.forEach(function (component) { return component.update(); });
this.transform.updateTransform();
@@ -307,8 +344,8 @@ var Scene = (function (_super) {
var _this = _super.call(this) || this;
_this.entities = [];
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);
@@ -324,18 +361,39 @@ var Scene = (function (_super) {
entity.scene = this;
return entity;
};
Scene.prototype.destoryAllEntities = function () {
this.entities.forEach(function (entity) { return entity.destory(); });
};
Scene.prototype.findEntity = function (name) {
return this.entities.firstOrDefault(function (entity) { return entity.name == name; });
};
Scene.prototype.addEntityProcessor = function (processor) {
processor.scene = this;
this.entityProcessors.push(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; });
};
Scene.prototype.setActive = function () {
SceneManager.setActiveScene(this);
return this;
};
Scene.prototype.initialize = function () {
this.camera = this.createEntity("camera").addComponent(new Camera());
this.entityProcessors.forEach(function (processor) { return processor.initialize(); });
};
Scene.prototype.onActive = function () {
};
Scene.prototype.onDeactive = function () {
};
Scene.prototype.update = function () {
this.entityProcessors.forEach(function (processor) { return processor.update(); });
this.entities.forEach(function (entity) { return entity.update(); });
this.entityProcessors.forEach(function (processor) { return processor.lateUpdate(); });
};
Scene.prototype.prepRenderState = function () {
this._projectionMatrix.m11 = 2 / this.stage.width;
@@ -561,6 +619,74 @@ var Camera = (function (_super) {
};
return Camera;
}(Component));
var EntitySystem = (function () {
function EntitySystem(matcher) {
this._entities = [];
this._matcher = matcher ? matcher : Matcher.empty();
}
Object.defineProperty(EntitySystem.prototype, "matcher", {
get: function () {
return this._matcher;
},
enumerable: true,
configurable: true
});
Object.defineProperty(EntitySystem.prototype, "scene", {
get: function () {
return this._scene;
},
set: function (value) {
this._scene = value;
this._entities = [];
},
enumerable: true,
configurable: true
});
EntitySystem.prototype.initialize = function () {
};
EntitySystem.prototype.update = function () {
this.begin();
this.process(this._entities);
};
EntitySystem.prototype.lateUpdate = function () {
this.lateProcess(this._entities);
this.end();
};
EntitySystem.prototype.begin = function () {
};
EntitySystem.prototype.process = function (entities) {
};
EntitySystem.prototype.lateProcess = function (entities) {
};
EntitySystem.prototype.end = function () {
};
return EntitySystem;
}());
var EntityProcessingSystem = (function (_super) {
__extends(EntityProcessingSystem, _super);
function EntityProcessingSystem(matcher) {
return _super.call(this, matcher) || this;
}
EntityProcessingSystem.prototype.lateProcessEntity = function (entity) {
};
EntityProcessingSystem.prototype.process = function (entities) {
var _this = this;
entities.forEach(function (entity) { return _this.processEntity(entity); });
};
EntityProcessingSystem.prototype.lateProcess = function (entities) {
var _this = this;
entities.forEach(function (entity) { return _this.lateProcessEntity(entity); });
};
return EntityProcessingSystem;
}(EntitySystem));
var Matcher = (function () {
function Matcher() {
}
Matcher.empty = function () {
return new Matcher();
};
return Matcher;
}());
var MathHelper = (function () {
function MathHelper() {
}

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,22 @@
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();

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();
}
}