init framework
This commit is contained in:
11
source/src/ECS/Components/Camera.ts
Normal file
11
source/src/ECS/Components/Camera.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
class Camera {
|
||||
private _displayContent: egret.DisplayObject;
|
||||
|
||||
constructor(displayObject: egret.DisplayObject){
|
||||
this._displayContent = displayObject;
|
||||
}
|
||||
|
||||
public destory(){
|
||||
this._displayContent = null;
|
||||
}
|
||||
}
|
||||
31
source/src/ECS/Entity.ts
Normal file
31
source/src/ECS/Entity.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
class Entity {
|
||||
public name: string;
|
||||
/** 当前实体所属的场景 */
|
||||
public scene: Scene;
|
||||
/** 封装实体的位置/旋转/缩放,并允许设置一个高层结构 */
|
||||
public readonly transform: Transform;
|
||||
|
||||
constructor(name: string){
|
||||
this.name = name;
|
||||
this.transform = new Transform(this);
|
||||
}
|
||||
|
||||
public attachToScene(newScene: Scene){
|
||||
this.scene = newScene;
|
||||
newScene.entities.push(this);
|
||||
|
||||
for (let i = 0; i < this.transform.childCount; i ++){
|
||||
this.transform.getChild(i).entity.attachToScene(newScene);
|
||||
}
|
||||
}
|
||||
|
||||
public destory(){
|
||||
this.scene.entities.remove(this);
|
||||
this.transform.parent = null;
|
||||
|
||||
for (let i = this.transform.childCount - 1; i >= 0; i --){
|
||||
let child = this.transform.getChild(i);
|
||||
child.entity.destory();
|
||||
}
|
||||
}
|
||||
}
|
||||
58
source/src/ECS/Scene.ts
Normal file
58
source/src/ECS/Scene.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/** 场景 */
|
||||
class Scene extends egret.DisplayObjectContainer {
|
||||
public camera: Camera;
|
||||
public entities: Entity[] = [];
|
||||
|
||||
constructor(displayObject: egret.DisplayObject){
|
||||
super();
|
||||
/** 初始化默认相机 */
|
||||
this.camera = new Camera(displayObject);
|
||||
this.addEventListener(egret.Event.ACTIVATE, this.onActive, this);
|
||||
this.addEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
|
||||
}
|
||||
|
||||
public createEntity(name: string){
|
||||
let entity = new Entity(name);
|
||||
|
||||
return this.addEntity(entity);
|
||||
}
|
||||
|
||||
public addEntity(entity: Entity){
|
||||
this.entities.push(entity);
|
||||
entity.scene = this;
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
public setActive(): Scene{
|
||||
SceneManager.setActiveScene(this);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/** 初始化场景 */
|
||||
public initialize(){
|
||||
|
||||
}
|
||||
|
||||
/** 场景激活 */
|
||||
public onActive(){
|
||||
|
||||
}
|
||||
|
||||
/** 场景失去焦点 */
|
||||
public onDeactive(){
|
||||
|
||||
}
|
||||
|
||||
public destory(){
|
||||
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
|
||||
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
|
||||
|
||||
this.camera.destory();
|
||||
this.camera = null;
|
||||
|
||||
this.entities.forEach(entity => entity.destory());
|
||||
this.entities.length = 0;
|
||||
}
|
||||
}
|
||||
36
source/src/ECS/SceneManager.ts
Normal file
36
source/src/ECS/SceneManager.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/** 运行时的场景管理。 */
|
||||
class SceneManager {
|
||||
private static _loadedScenes: Map<string, Scene> = new Map();
|
||||
/** 上一个场景 */
|
||||
private static _lastScene: Scene;
|
||||
/** 当前激活的场景 */
|
||||
private static _activeScene: Scene;
|
||||
|
||||
/**
|
||||
* 使用给定的名称在运行时创建一个空的新场景。
|
||||
* 新场景将与当前打开的任何现有场景一起被添加到层次结构中。
|
||||
* 这个函数用于在运行时创建场景。
|
||||
* @param name
|
||||
* @param scene
|
||||
*/
|
||||
public static createScene(name: string, scene: Scene){
|
||||
scene.name = name;
|
||||
this._loadedScenes.set(name, scene);
|
||||
return scene;
|
||||
}
|
||||
|
||||
public static setActiveScene(scene: Scene){
|
||||
if (this._activeScene){
|
||||
// 如果场景相同则不进行切换
|
||||
if (this._activeScene == scene)
|
||||
return;
|
||||
|
||||
this._lastScene = this._activeScene;
|
||||
this._activeScene.destory();
|
||||
}
|
||||
|
||||
this._activeScene = scene;
|
||||
this._activeScene.initialize();
|
||||
return scene;
|
||||
}
|
||||
}
|
||||
42
source/src/ECS/Transform.ts
Normal file
42
source/src/ECS/Transform.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
class Transform {
|
||||
/** 相关联的实体 */
|
||||
public readonly entity: Entity;
|
||||
private _children: Transform[];
|
||||
private _parent: Transform;
|
||||
|
||||
public get childCount(){
|
||||
return this._children.length;
|
||||
}
|
||||
|
||||
constructor(entity: Entity){
|
||||
this.entity = entity;
|
||||
this._children = [];
|
||||
}
|
||||
|
||||
public getChild(index: number){
|
||||
return this._children[index];
|
||||
}
|
||||
|
||||
public get parent(){
|
||||
return this._parent;
|
||||
}
|
||||
|
||||
public set parent(value: Transform){
|
||||
this.setParent(value);
|
||||
}
|
||||
|
||||
public setParent(parent: Transform){
|
||||
if (this._parent == parent)
|
||||
return this;
|
||||
|
||||
if (this._parent)
|
||||
this._parent._children.remove(this);
|
||||
|
||||
if (parent)
|
||||
parent._children.push(this);
|
||||
|
||||
this._parent = parent;
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user