新增基础实体系统
This commit is contained in:
38
demo/libs/framework/framework.d.ts
vendored
38
demo/libs/framework/framework.d.ts
vendored
@@ -20,6 +20,9 @@ declare interface Array<T> {
|
|||||||
declare abstract class Component {
|
declare abstract class Component {
|
||||||
entity: Entity;
|
entity: Entity;
|
||||||
displayRender: egret.DisplayObject;
|
displayRender: egret.DisplayObject;
|
||||||
|
private _enabled;
|
||||||
|
enabled: boolean;
|
||||||
|
setEnabled(isEnabled: boolean): this;
|
||||||
abstract initialize(): any;
|
abstract initialize(): any;
|
||||||
update(): void;
|
update(): void;
|
||||||
bind(displayRender: egret.DisplayObject): this;
|
bind(displayRender: egret.DisplayObject): this;
|
||||||
@@ -30,11 +33,15 @@ declare class Entity {
|
|||||||
readonly transform: Transform;
|
readonly transform: Transform;
|
||||||
readonly components: Component[];
|
readonly components: Component[];
|
||||||
private _updateOrder;
|
private _updateOrder;
|
||||||
|
private _enabled;
|
||||||
|
enabled: boolean;
|
||||||
|
setEnabled(isEnabled: boolean): this;
|
||||||
constructor(name: string);
|
constructor(name: string);
|
||||||
updateOrder: number;
|
updateOrder: number;
|
||||||
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;
|
||||||
update(): void;
|
update(): void;
|
||||||
destory(): void;
|
destory(): void;
|
||||||
}
|
}
|
||||||
@@ -44,9 +51,15 @@ declare class Scene extends egret.DisplayObjectContainer {
|
|||||||
private _projectionMatrix;
|
private _projectionMatrix;
|
||||||
private _transformMatrix;
|
private _transformMatrix;
|
||||||
private _matrixTransformMatrix;
|
private _matrixTransformMatrix;
|
||||||
|
readonly entityProcessors: EntitySystem[];
|
||||||
constructor(displayObject: egret.DisplayObject);
|
constructor(displayObject: egret.DisplayObject);
|
||||||
createEntity(name: string): Entity;
|
createEntity(name: string): Entity;
|
||||||
addEntity(entity: Entity): 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;
|
setActive(): Scene;
|
||||||
initialize(): void;
|
initialize(): void;
|
||||||
onActive(): void;
|
onActive(): void;
|
||||||
@@ -118,6 +131,31 @@ declare class Camera extends Component {
|
|||||||
updateMatrixes(): void;
|
updateMatrixes(): void;
|
||||||
destory(): 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 {
|
declare class MathHelper {
|
||||||
static toDegrees(radians: number): number;
|
static toDegrees(radians: number): number;
|
||||||
static toRadians(degrees: number): number;
|
static toRadians(degrees: number): number;
|
||||||
|
|||||||
@@ -240,7 +240,24 @@ Array.prototype.sum = function (selector) {
|
|||||||
};
|
};
|
||||||
var Component = (function () {
|
var Component = (function () {
|
||||||
function Component() {
|
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.update = function () {
|
||||||
};
|
};
|
||||||
Component.prototype.bind = function (displayRender) {
|
Component.prototype.bind = function (displayRender) {
|
||||||
@@ -252,10 +269,27 @@ var Component = (function () {
|
|||||||
var Entity = (function () {
|
var Entity = (function () {
|
||||||
function Entity(name) {
|
function Entity(name) {
|
||||||
this._updateOrder = 0;
|
this._updateOrder = 0;
|
||||||
|
this._enabled = true;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.transform = new Transform(this);
|
this.transform = new Transform(this);
|
||||||
this.components = [];
|
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", {
|
Object.defineProperty(Entity.prototype, "updateOrder", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._updateOrder;
|
return this._updateOrder;
|
||||||
@@ -287,6 +321,9 @@ var Entity = (function () {
|
|||||||
component.initialize();
|
component.initialize();
|
||||||
return component;
|
return component;
|
||||||
};
|
};
|
||||||
|
Entity.prototype.getComponent = function () {
|
||||||
|
return this.components.firstOrDefault(function (component) { return component instanceof Component; });
|
||||||
|
};
|
||||||
Entity.prototype.update = function () {
|
Entity.prototype.update = function () {
|
||||||
this.components.forEach(function (component) { return component.update(); });
|
this.components.forEach(function (component) { return component.update(); });
|
||||||
this.transform.updateTransform();
|
this.transform.updateTransform();
|
||||||
@@ -307,8 +344,8 @@ var Scene = (function (_super) {
|
|||||||
var _this = _super.call(this) || this;
|
var _this = _super.call(this) || this;
|
||||||
_this.entities = [];
|
_this.entities = [];
|
||||||
displayObject.stage.addChild(_this);
|
displayObject.stage.addChild(_this);
|
||||||
_this.camera = _this.createEntity("camera").addComponent(new Camera());
|
|
||||||
_this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
|
_this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
|
||||||
|
_this.entityProcessors = [];
|
||||||
_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);
|
||||||
_this.addEventListener(egret.Event.ENTER_FRAME, _this.update, _this);
|
_this.addEventListener(egret.Event.ENTER_FRAME, _this.update, _this);
|
||||||
@@ -324,18 +361,39 @@ var Scene = (function (_super) {
|
|||||||
entity.scene = this;
|
entity.scene = this;
|
||||||
return entity;
|
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 () {
|
Scene.prototype.setActive = function () {
|
||||||
SceneManager.setActiveScene(this);
|
SceneManager.setActiveScene(this);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Scene.prototype.initialize = function () {
|
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.onActive = function () {
|
||||||
};
|
};
|
||||||
Scene.prototype.onDeactive = function () {
|
Scene.prototype.onDeactive = function () {
|
||||||
};
|
};
|
||||||
Scene.prototype.update = function () {
|
Scene.prototype.update = function () {
|
||||||
|
this.entityProcessors.forEach(function (processor) { return processor.update(); });
|
||||||
this.entities.forEach(function (entity) { return entity.update(); });
|
this.entities.forEach(function (entity) { return entity.update(); });
|
||||||
|
this.entityProcessors.forEach(function (processor) { return processor.lateUpdate(); });
|
||||||
};
|
};
|
||||||
Scene.prototype.prepRenderState = function () {
|
Scene.prototype.prepRenderState = function () {
|
||||||
this._projectionMatrix.m11 = 2 / this.stage.width;
|
this._projectionMatrix.m11 = 2 / this.stage.width;
|
||||||
@@ -561,6 +619,74 @@ var Camera = (function (_super) {
|
|||||||
};
|
};
|
||||||
return Camera;
|
return Camera;
|
||||||
}(Component));
|
}(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 () {
|
var MathHelper = (function () {
|
||||||
function MathHelper() {
|
function MathHelper() {
|
||||||
}
|
}
|
||||||
|
|||||||
2
demo/libs/framework/framework.min.js
vendored
2
demo/libs/framework/framework.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -15,6 +15,8 @@
|
|||||||
"bin-debug/Main.js",
|
"bin-debug/Main.js",
|
||||||
"bin-debug/Platform.js",
|
"bin-debug/Platform.js",
|
||||||
"bin-debug/ThemeAdapter.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"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -91,12 +91,9 @@ class Main extends eui.UILayer {
|
|||||||
* Create scene interface
|
* Create scene interface
|
||||||
*/
|
*/
|
||||||
protected createGameScene(): void {
|
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 player = scene.createEntity("player");
|
||||||
let texture = RES.getRes("checkbox_select_down_png");
|
player.addComponent(new SpawnComponent(EnemyType.worm));
|
||||||
player.addComponent(new TestComponent(new eui.Image(texture)));
|
|
||||||
scene.camera.setPosition(new Vector2(-100, -50));
|
|
||||||
player.transform.setPosition(new Vector2(10, 10));
|
|
||||||
// console.log(player.transform.position);
|
// console.log(player.transform.position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
demo/src/game/MainScene.ts
Normal file
7
demo/src/game/MainScene.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
class MainScene extends Scene {
|
||||||
|
constructor(displayContent: egret.DisplayObject){
|
||||||
|
super(displayContent);
|
||||||
|
|
||||||
|
this.addEntityProcessor(new SpawnerSystem(new Matcher()));
|
||||||
|
}
|
||||||
|
}
|
||||||
25
demo/src/game/SpawnerComponent.ts
Normal file
25
demo/src/game/SpawnerComponent.ts
Normal 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
|
||||||
|
}
|
||||||
29
demo/src/game/SpawnerSystem.ts
Normal file
29
demo/src/game/SpawnerSystem.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
38
source/bin/framework.d.ts
vendored
38
source/bin/framework.d.ts
vendored
@@ -20,6 +20,9 @@ declare interface Array<T> {
|
|||||||
declare abstract class Component {
|
declare abstract class Component {
|
||||||
entity: Entity;
|
entity: Entity;
|
||||||
displayRender: egret.DisplayObject;
|
displayRender: egret.DisplayObject;
|
||||||
|
private _enabled;
|
||||||
|
enabled: boolean;
|
||||||
|
setEnabled(isEnabled: boolean): this;
|
||||||
abstract initialize(): any;
|
abstract initialize(): any;
|
||||||
update(): void;
|
update(): void;
|
||||||
bind(displayRender: egret.DisplayObject): this;
|
bind(displayRender: egret.DisplayObject): this;
|
||||||
@@ -30,11 +33,15 @@ declare class Entity {
|
|||||||
readonly transform: Transform;
|
readonly transform: Transform;
|
||||||
readonly components: Component[];
|
readonly components: Component[];
|
||||||
private _updateOrder;
|
private _updateOrder;
|
||||||
|
private _enabled;
|
||||||
|
enabled: boolean;
|
||||||
|
setEnabled(isEnabled: boolean): this;
|
||||||
constructor(name: string);
|
constructor(name: string);
|
||||||
updateOrder: number;
|
updateOrder: number;
|
||||||
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;
|
||||||
update(): void;
|
update(): void;
|
||||||
destory(): void;
|
destory(): void;
|
||||||
}
|
}
|
||||||
@@ -44,9 +51,15 @@ declare class Scene extends egret.DisplayObjectContainer {
|
|||||||
private _projectionMatrix;
|
private _projectionMatrix;
|
||||||
private _transformMatrix;
|
private _transformMatrix;
|
||||||
private _matrixTransformMatrix;
|
private _matrixTransformMatrix;
|
||||||
|
readonly entityProcessors: EntitySystem[];
|
||||||
constructor(displayObject: egret.DisplayObject);
|
constructor(displayObject: egret.DisplayObject);
|
||||||
createEntity(name: string): Entity;
|
createEntity(name: string): Entity;
|
||||||
addEntity(entity: Entity): 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;
|
setActive(): Scene;
|
||||||
initialize(): void;
|
initialize(): void;
|
||||||
onActive(): void;
|
onActive(): void;
|
||||||
@@ -118,6 +131,31 @@ declare class Camera extends Component {
|
|||||||
updateMatrixes(): void;
|
updateMatrixes(): void;
|
||||||
destory(): 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 {
|
declare class MathHelper {
|
||||||
static toDegrees(radians: number): number;
|
static toDegrees(radians: number): number;
|
||||||
static toRadians(degrees: number): number;
|
static toRadians(degrees: number): number;
|
||||||
|
|||||||
@@ -240,7 +240,24 @@ Array.prototype.sum = function (selector) {
|
|||||||
};
|
};
|
||||||
var Component = (function () {
|
var Component = (function () {
|
||||||
function Component() {
|
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.update = function () {
|
||||||
};
|
};
|
||||||
Component.prototype.bind = function (displayRender) {
|
Component.prototype.bind = function (displayRender) {
|
||||||
@@ -252,10 +269,27 @@ var Component = (function () {
|
|||||||
var Entity = (function () {
|
var Entity = (function () {
|
||||||
function Entity(name) {
|
function Entity(name) {
|
||||||
this._updateOrder = 0;
|
this._updateOrder = 0;
|
||||||
|
this._enabled = true;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.transform = new Transform(this);
|
this.transform = new Transform(this);
|
||||||
this.components = [];
|
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", {
|
Object.defineProperty(Entity.prototype, "updateOrder", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._updateOrder;
|
return this._updateOrder;
|
||||||
@@ -287,6 +321,9 @@ var Entity = (function () {
|
|||||||
component.initialize();
|
component.initialize();
|
||||||
return component;
|
return component;
|
||||||
};
|
};
|
||||||
|
Entity.prototype.getComponent = function () {
|
||||||
|
return this.components.firstOrDefault(function (component) { return component instanceof Component; });
|
||||||
|
};
|
||||||
Entity.prototype.update = function () {
|
Entity.prototype.update = function () {
|
||||||
this.components.forEach(function (component) { return component.update(); });
|
this.components.forEach(function (component) { return component.update(); });
|
||||||
this.transform.updateTransform();
|
this.transform.updateTransform();
|
||||||
@@ -307,8 +344,8 @@ var Scene = (function (_super) {
|
|||||||
var _this = _super.call(this) || this;
|
var _this = _super.call(this) || this;
|
||||||
_this.entities = [];
|
_this.entities = [];
|
||||||
displayObject.stage.addChild(_this);
|
displayObject.stage.addChild(_this);
|
||||||
_this.camera = _this.createEntity("camera").addComponent(new Camera());
|
|
||||||
_this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
|
_this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
|
||||||
|
_this.entityProcessors = [];
|
||||||
_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);
|
||||||
_this.addEventListener(egret.Event.ENTER_FRAME, _this.update, _this);
|
_this.addEventListener(egret.Event.ENTER_FRAME, _this.update, _this);
|
||||||
@@ -324,18 +361,39 @@ var Scene = (function (_super) {
|
|||||||
entity.scene = this;
|
entity.scene = this;
|
||||||
return entity;
|
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 () {
|
Scene.prototype.setActive = function () {
|
||||||
SceneManager.setActiveScene(this);
|
SceneManager.setActiveScene(this);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Scene.prototype.initialize = function () {
|
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.onActive = function () {
|
||||||
};
|
};
|
||||||
Scene.prototype.onDeactive = function () {
|
Scene.prototype.onDeactive = function () {
|
||||||
};
|
};
|
||||||
Scene.prototype.update = function () {
|
Scene.prototype.update = function () {
|
||||||
|
this.entityProcessors.forEach(function (processor) { return processor.update(); });
|
||||||
this.entities.forEach(function (entity) { return entity.update(); });
|
this.entities.forEach(function (entity) { return entity.update(); });
|
||||||
|
this.entityProcessors.forEach(function (processor) { return processor.lateUpdate(); });
|
||||||
};
|
};
|
||||||
Scene.prototype.prepRenderState = function () {
|
Scene.prototype.prepRenderState = function () {
|
||||||
this._projectionMatrix.m11 = 2 / this.stage.width;
|
this._projectionMatrix.m11 = 2 / this.stage.width;
|
||||||
@@ -561,6 +619,74 @@ var Camera = (function (_super) {
|
|||||||
};
|
};
|
||||||
return Camera;
|
return Camera;
|
||||||
}(Component));
|
}(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 () {
|
var MathHelper = (function () {
|
||||||
function MathHelper() {
|
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
@@ -1,6 +1,22 @@
|
|||||||
abstract class Component {
|
abstract class Component {
|
||||||
public entity: Entity;
|
public entity: Entity;
|
||||||
public displayRender: egret.DisplayObject;
|
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 abstract initialize();
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,23 @@ class Entity {
|
|||||||
/** 当前附加到此实体的所有组件的列表 */
|
/** 当前附加到此实体的所有组件的列表 */
|
||||||
public readonly components: Component[];
|
public readonly components: Component[];
|
||||||
private _updateOrder: number = 0;
|
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){
|
constructor(name: string){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@@ -49,6 +66,10 @@ class Entity {
|
|||||||
return component;
|
return component;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getComponent<T extends Component>(): T{
|
||||||
|
return this.components.firstOrDefault(component => component instanceof Component) as T;
|
||||||
|
}
|
||||||
|
|
||||||
public update(){
|
public update(){
|
||||||
this.components.forEach(component => component.update());
|
this.components.forEach(component => component.update());
|
||||||
this.transform.updateTransform();
|
this.transform.updateTransform();
|
||||||
|
|||||||
@@ -7,12 +7,14 @@ class Scene extends egret.DisplayObjectContainer {
|
|||||||
private _transformMatrix: Matrix2D;
|
private _transformMatrix: Matrix2D;
|
||||||
private _matrixTransformMatrix: Matrix2D;
|
private _matrixTransformMatrix: Matrix2D;
|
||||||
|
|
||||||
|
public readonly entityProcessors: EntitySystem[];
|
||||||
|
|
||||||
constructor(displayObject: egret.DisplayObject){
|
constructor(displayObject: egret.DisplayObject){
|
||||||
super();
|
super();
|
||||||
displayObject.stage.addChild(this);
|
displayObject.stage.addChild(this);
|
||||||
/** 初始化默认相机 */
|
|
||||||
this.camera = this.createEntity("camera").addComponent(new Camera());
|
|
||||||
this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
|
this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
|
||||||
|
this.entityProcessors = [];
|
||||||
|
|
||||||
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);
|
||||||
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
|
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
|
||||||
@@ -31,6 +33,32 @@ class Scene extends egret.DisplayObjectContainer {
|
|||||||
return entity;
|
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{
|
public setActive(): Scene{
|
||||||
SceneManager.setActiveScene(this);
|
SceneManager.setActiveScene(this);
|
||||||
|
|
||||||
@@ -39,7 +67,9 @@ class Scene extends egret.DisplayObjectContainer {
|
|||||||
|
|
||||||
/** 初始化场景 */
|
/** 初始化场景 */
|
||||||
public initialize(){
|
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(){
|
public update(){
|
||||||
|
this.entityProcessors.forEach(processor => processor.update());
|
||||||
this.entities.forEach(entity => entity.update());
|
this.entities.forEach(entity => entity.update());
|
||||||
|
this.entityProcessors.forEach(processor => processor.lateUpdate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public prepRenderState(){
|
public prepRenderState(){
|
||||||
|
|||||||
23
source/src/ECS/Systems/EntityProcessingSystem.ts
Normal file
23
source/src/ECS/Systems/EntityProcessingSystem.ts
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
52
source/src/ECS/Systems/EntitySystem.ts
Normal file
52
source/src/ECS/Systems/EntitySystem.ts
Normal 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(){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
5
source/src/ECS/Utils/Matcher.ts
Normal file
5
source/src/ECS/Utils/Matcher.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
class Matcher{
|
||||||
|
public static empty(){
|
||||||
|
return new Matcher();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user