移除Core,新增各接口用于sdk扩展

This commit is contained in:
yhh
2020-12-30 16:28:07 +08:00
parent f934890fac
commit d84ffcc2b7
28 changed files with 2008 additions and 653 deletions

View File

@@ -78,6 +78,9 @@ module es {
public onEntityTransformChanged(comp: transform.Component) {
}
public debugRender(batcher: IBatcher) {
}
/**
*当父实体或此组件启用时调用
*/
@@ -89,7 +92,7 @@ module es {
*/
public onDisabled() {
}
public setEnabled(isEnabled: boolean) {
if (this._enabled != isEnabled) {
this._enabled = isEnabled;

View File

@@ -1,6 +1,13 @@
///<reference path="./Collider.ts" />
module es {
export class BoxCollider extends Collider {
/**
* 创建一个BoxCollider并使用x/y组件作为局部Offset
* @param x
* @param y
* @param width
* @param height
*/
constructor(x: number, y: number, width: number, height: number) {
super();
@@ -30,10 +37,12 @@ module es {
* @param height
*/
public setSize(width: number, height: number) {
this._colliderRequiresAutoSizing = false;
let box = this.shape as Box;
if (width != box.width || height != box.height) {
// 更新框,改变边界,如果我们需要更新物理系统中的边界
box.updateBox(width, height);
this._isPositionDirty = true;
if (this.entity && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
@@ -46,10 +55,12 @@ module es {
* @param width
*/
public setWidth(width: number): BoxCollider {
this._colliderRequiresAutoSizing = false;
let box = this.shape as Box;
if (width != box.width) {
// 更新框,改变边界,如果我们需要更新物理系统中的边界
box.updateBox(width, box.height);
this._isPositionDirty = true;
if (this.entity && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
@@ -62,15 +73,25 @@ module es {
* @param height
*/
public setHeight(height: number) {
this._colliderRequiresAutoSizing = false;
let box = this.shape as Box;
if (height != box.height) {
// 更新框,改变边界,如果我们需要更新物理系统中的边界
box.updateBox(box.width, height);
this._isPositionDirty = true;
if (this.entity && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
}
public debugRender(batcher: IBatcher) {
let poly = this.shape as Polygon;
batcher.drawHollowRect(this.bounds, Debug.colliderBounds, 1);
batcher.drawPolygon(this.shape.position, poly.points, Debug.colliderEdge, true, 1);
batcher.drawPixel(this.entity.transform.position, Debug.colliderPosition, 4);
batcher.drawPixel(Vector2.add(this.entity.transform.position, this.shape.center), Debug.colliderCenter, 2);
}
public toString() {
return `[BoxCollider: bounds: ${this.bounds}]`;
}

View File

@@ -1,7 +1,9 @@
module es {
export class CircleCollider extends Collider {
/**
* 创建一个有半径的
* 创建一个有半径的CircleCollider。
* 请注意当指定半径时如果在实体上使用RenderableComponent您将需要设置原点来对齐CircleCollider。
* 例如如果RenderableComponent有一个0,0的原点并且创建了一个半径为1.5f * renderable.width的CircleCollider你可以通过设置originNormalied为中心除以缩放尺寸来偏移原点
*
* @param radius
*/
@@ -24,18 +26,27 @@ module es {
* @param radius
*/
public setRadius(radius: number): CircleCollider {
this._colliderRequiresAutoSizing = false;
let circle = this.shape as Circle;
if (radius != circle.radius) {
circle.radius = radius;
circle._originalRadius = radius;
this._isPositionDirty = true;
if (this.entity && this._isParentEntityAddedToScene)
if (this.entity != null && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
return this;
}
public debugRender(batcher: IBatcher) {
batcher.drawHollowRect(this.bounds, Debug.colliderBounds, 1);
batcher.drawCircle(this.shape.position, (this.shape as Circle).radius, Debug.colliderEdge, 1);
batcher.drawPixel(this.entity.transform.position, Debug.colliderPosition, 4);
batcher.drawPixel(this.shape.position, Debug.colliderCenter, 2);
}
public toString() {
return `[CircleCollider: bounds: ${this.bounds}, radius: ${(this.shape as Circle).radius}]`
}

View File

@@ -116,6 +116,28 @@ module es {
public onAddedToEntity() {
if (this._colliderRequiresAutoSizing) {
Insist.isTrue(this instanceof BoxCollider || this instanceof CircleCollider, "只有框和圆的碰撞器可以自动创建");
let renderable = this.entity.getComponent<RenderableComponent>(RenderableComponent);
if (renderable == null)
console.warn("Collider没有形状也没有RenderableComponent。不知道如何确定它的大小。");
if (renderable != null) {
let renderableBounds = renderable.bounds.clone();
// 我们在这里需要大小*反比例因为当我们自动调整Collider的大小时它需要没有一个缩放的Renderable
let width = renderableBounds.width / this.entity.transform.scale.x;
let height = renderableBounds.height / this.entity.transform.scale.y;
if (this instanceof CircleCollider) {
this.radius = Math.max(width, height) * 0.5;
// 获取Renderable的中心将其转移到本地坐标并将其作为我们碰撞器的localOffset
this.localOffset = Vector2.subtract(renderableBounds.center, this.entity.transform.position);
} else if (this instanceof BoxCollider) {
this.width = width;
this.height = height;
this.localOffset = Vector2.subtract(renderableBounds.center, this.entity.transform.position);
}
}
}
this._isParentEntityAddedToScene = true;

View File

@@ -23,5 +23,13 @@ module es {
Polygon.recenterPolygonVerts(points);
this.shape = new Polygon(points);
}
public debugRender(batcher: IBatcher) {
let poly = this.shape as Polygon;
batcher.drawHollowRect(this.bounds, Debug.colliderBounds, 1);
batcher.drawPolygon(this.shape.position, poly.points, Debug.colliderEdge, true, 1);
batcher.drawPixel(this.entity.transform.position, Debug.colliderPosition, 4);
batcher.drawPixel(this.shape.position, Debug.colliderCenter, 2);
}
}
}

View File

@@ -0,0 +1,80 @@
module es {
/**
* 接口当应用于一个Component时它将被注册到场景渲染器中。
* 请仔细实现这个功能 改变像layerDepth/renderLayer/material这样的东西需要更新Scene RenderableComponentList
*/
export interface IRenderable {
/** 包裹此对象的AABB。用来进行相机筛选 */
bounds: Rectangle;
/** 这个IRenderable是否应该被渲染 */
enabled: boolean;
/**
* 标准的Batcher图层深度0为前面1为后面。
* 改变这个值会触发场景中可渲染组件列表的排序
*/
layerDepth: number;
/**
* 较低的renderLayers在前面较高的在後面就像layerDepth一样但不是限制在0-1。
* 请注意这意味着较高的renderLayers首先被发送到Batcher。在使用模板缓冲区时这是一个重要的事实
*/
renderLayer: number;
/**
* 由渲染器使用,用于指定该精灵应如何渲染。
* 如果非空,当组件从实体中移除时,它会被自动处理。
*/
material;
/**
* 这个Renderable的可见性。
* 状态的改变最终会调用onBecameVisible/onBecameInvisible方法
*/
isVisible: boolean;
/**
* 用于检索一个已经铸造的Material子类的帮助程序
*/
getMaterial<T extends IMaterial>(): T;
/**
* 如果Renderables的边界与Camera.bounds相交则返回true。
* 处理isVisible标志的状态切换。
* 在你的渲染方法中使用这个方法来决定你是否应该渲染
* @param camera
*/
isVisibleFromCamera(camera: ICamera): boolean;
/**
* 被渲染器调用。摄像机可以用来进行裁剪并使用Batcher实例进行绘制
* @param batcher
* @param camera
*/
render(batcher: IBatcher, camera: ICamera);
/**
* 只有在没有对撞机的情况下才会渲染边界。
* 始终在原点上渲染一个正方形
* @param batcher
*/
debugRender(batcher: IBatcher);
}
/**
* 对IRenderables进行排序的比较器。
* 首先按 RenderLayer 排序,然后按 LayerDepth 排序。
* 如果出现平局,则使用材料作为平局的断定器,以避免渲染状态的改变
*/
export class RenderableComparer implements IComparer<IRenderable> {
public compare(self: IRenderable, other: IRenderable): number {
let res = other.renderLayer - self.renderLayer;
if (res == 0) {
res = other.layerDepth - self.layerDepth;
if (res == 0) {
if (self.material == other.material)
return 0;
if (other.material == null)
return -1;
return 1;
}
}
return res;
}
}
}

View File

@@ -0,0 +1,232 @@
module es {
/**
* IRenderable的具体实现。包含方便的方法。
* 非常重要子类必须覆盖width/height或bounds! 子类必须覆盖width/height或bounds!
*/
export abstract class RenderableComponent extends Component implements IRenderable, IComparer<RenderableComponent> {
/**
* 不重写bounds属性的子类必须实现这个RenderableComponent的宽度。
*/
public get width() {
return this.bounds.width;
}
/**
* 不重写bounds属性的子类必须实现这个!
*/
public get height() {
return this.bounds.height;
}
/**
* 包裹此对象的AABB。用来进行相机筛选。
*/
public get bounds(): Rectangle {
if (this._areBoundsDirty) {
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, Vector2.zero,
this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height);
this._areBoundsDirty = false;
}
return this._bounds;
}
/**
* 标准的Batcher图层深度0为前面1为后面。
* 改变这个值会触发场景中可渲染组件列表的排序。
*/
public get layerDepth() {
return this._layerDepth;
}
public set layerDepth(value: number) {
this.setLayerDepth(value);
}
/**
* 较低的renderLayers在前面较高的在后面就像layerDepth一樣但不是限制在0-1。
* 请注意这意味着更高的renderLayers首先被发送到Batcher。
*/
public get renderLayer() {
return this._renderLayer;
}
public set renderLayer(value: number) {
this.setRenderLayer(value);
}
/**
* 由渲染器使用,用于指定该精灵的渲染方式
*/
public material: IMaterial;
/**
* 偏移。用于将多个Renderables添加到需要特定定位的实体
*/
public get localOffset(): Vector2 {
return this._localOffset;
}
public set localOffset(value: Vector2) {
this.setLocalOffset(value);
}
/**
* 这个Renderable的可见性。
* 状态的改变最终会调用onBecameVisible/onBecameInvisible方法
*/
public get isVisible() {
return this._isVisble;
}
public set isVisible(value: boolean) {
if (this._isVisble != value) {
this._isVisble = value;
if (this._isVisble)
this.onBecameVisible();
else
this.onBecameInvisible();
}
}
public debugRenderEnabled: boolean = true;
protected _localOffset: Vector2;
protected _layerDepth: number;
protected _renderLayer: number;
protected _bounds: Rectangle;
protected _isVisble: boolean;
protected _areBoundsDirty: boolean = true;
public onEntityTransformChanged(comp: transform.Component) {
this._areBoundsDirty = true;
}
/**
* 被渲染器调用。摄像机可以用来进行裁剪并使用Batcher实例进行绘制
* @param batcher
* @param camera
*/
public abstract render(batcher: IBatcher, camera: ICamera);
/**
* 只有在没有对撞机的情况下才会渲染边界。始终在原点上渲染一个正方形
* @param batcher
*/
public debugRender(batcher: IBatcher) {
if (!this.debugRenderEnabled)
return;
// 如果我们没有对撞机,我们就画出我们的范围
if (this.entity.getComponent<Collider>(Collider) == null)
batcher.drawHollowRect(this.bounds, 0xFFFF00);
batcher.drawPixel(this.entity.transform.position.add(this._localOffset), 0xcc3299, 4);
}
/**
* 当Renderable进入相机帧时被调用。
* 请注意如果您的Renderer没有使用isVisibleFromCamera来进行裁剪检查这些方法将不会被调用。
* 所有默认的Renderer都会这样做
*/
protected onBecameVisible() {
}
/**
* 当渲染器退出相机帧时,将调用这些方法。
* 请注意如果你的Renderer没有使用isVisibleFromCamera来进行Culling检查这些方法将不会被调用。
* 所有默认的Renderer都会这样做
*/
protected onBecameInvisible() {
}
public onRemovedFromEntity() {
}
/**
* 如果Renderables的边界与Camera.bounds相交则返回true。
* 处理isVisible标志的状态切换。在你的渲染方法中使用这个方法来决定你是否应该渲染
* @param camera
*/
public isVisibleFromCamera(camera: ICamera) {
this.isVisible = camera.bounds.intersects(this.bounds);
return this.isVisible;
}
public setMaterial(material: IMaterial) {
this.material = material;
if (this.entity != null && this.entity.scene != null)
this.entity.scene.renderableComponents.setRenderLayerNeedsComponentSort(this.renderLayer);
return this;
}
/**
* 标准的Batcher图层深度0为前面1为后面。
* 改变这个值会触发一种类似于renderableComponents的方法
* @param layerDepth
*/
public setLayerDepth(layerDepth: number): RenderableComponent {
this._layerDepth = MathHelper.clamp01(layerDepth);
if (this.entity != null && this.entity.scene != null)
this.entity.scene.renderableComponents.setRenderLayerNeedsComponentSort(this.renderLayer);
return this;
}
/**
* 较低的渲染层在前面,较高的在后面
* @param renderLayer
*/
public setRenderLayer(renderLayer: number): RenderableComponent {
if (renderLayer != this._renderLayer) {
let oldRenderLayer = this._renderLayer;
this._renderLayer = renderLayer;
// 如果该组件拥有一个实体那么是由ComponentList管理需要通知它改变了渲染层
if (this.entity && this.entity.scene)
this.entity.scene.renderableComponents.updateRenderableRenderLayer(this, oldRenderLayer, this._renderLayer);
}
return this;
}
/**
* 偏移。用于将多个Renderables添加到需要特定定位的实体
* @param offset
*/
public setLocalOffset(offset: Vector2): RenderableComponent {
if (!this._localOffset.equals(offset)) {
this._localOffset = offset;
this._areBoundsDirty = true;
}
return this;
}
/**
* 用于检索一个已经铸造的Material子类的帮助程序
*/
public getMaterial<T extends IMaterial>(): T {
return this.material as T;
}
/**
* 先按renderLayer排序再按layerDepth排序最后按材质排序
* @param other
*/
public compare(other: RenderableComponent) {
let res = other.renderLayer - this.renderLayer;
if (res == 0) {
res = other.layerDepth - this.layerDepth;
if (res == 0) {
if (this.material == other.material)
return 0;
if (other.material == null)
return -1;
return 1;
}
}
}
}
}

View File

@@ -8,7 +8,7 @@ module es {
/**
* 如果启用了SceneComponent则为true。状态的改变会导致调用onEnabled/onDisable。
*/
public get enabled(){
public get enabled() {
return this._enabled;
}
@@ -16,7 +16,7 @@ module es {
* 如果启用了SceneComponent则为true。状态的改变会导致调用onEnabled/onDisable。
* @param value
*/
public set enabled(value: boolean){
public set enabled(value: boolean) {
this.setEnabled(value);
}
@@ -30,38 +30,38 @@ module es {
/**
* 在启用此SceneComponent时调用
*/
public onEnabled(){
public onEnabled() {
}
/**
* 当禁用此SceneComponent时调用
*/
public onDisabled(){
public onDisabled() {
}
/**
* 当该SceneComponent从场景中移除时调用
*/
public onRemovedFromScene(){
public onRemovedFromScene() {
}
/**
* 在实体更新之前每一帧调用
*/
public update(){
public update() {
}
/**
* 启用/禁用这个SceneComponent
* @param isEnabled
*/
public setEnabled(isEnabled: boolean): SceneComponent{
if (this._enabled != isEnabled){
public setEnabled(isEnabled: boolean): SceneComponent {
if (this._enabled != isEnabled) {
this._enabled = isEnabled;
if (this._enabled){
if (this._enabled) {
this.onEnabled();
}else{
} else {
this.onDisabled();
}
}
@@ -73,16 +73,15 @@ module es {
* 设置SceneComponent的updateOrder并触发某种SceneComponent
* @param updateOrder
*/
public setUpdateOrder(updateOrder: number){
if (this.updateOrder != updateOrder){
public setUpdateOrder(updateOrder: number) {
if (this.updateOrder != updateOrder) {
this.updateOrder = updateOrder;
Core.scene._sceneComponents.sort(this.compare);
}
return this;
}
public compare(other: SceneComponent): number{
public compare(other: SceneComponent): number {
return this.updateOrder - other.updateOrder;
}
}

View File

@@ -1,217 +0,0 @@
module es {
/**
* 全局核心类
*/
export class Core {
/**
* 核心发射器。只发出核心级别的事件
*/
public static emitter: Emitter<CoreEvents>;
/**
* 启用/禁用焦点丢失时的暂停。如果为真,则不调用更新或渲染方法
*/
public static pauseOnFocusLost = true;
/**
* 是否启用调试渲染
*/
public static debugRenderEndabled = false;
/**
* 简化对内部类的全局内容实例的访问
*/
public static _instance: Core;
/**
* 用于确定是否应该使用EntitySystems
*/
public static entitySystemsEnabled: boolean;
public _nextScene: Scene;
/**
* 用于凝聚GraphicsDeviceReset事件
*/
public _graphicsDeviceChangeTimer: ITimer;
/**
* 全局访问系统
*/
public _globalManagers: GlobalManager[] = [];
public _coroutineManager: CoroutineManager = new CoroutineManager();
public _timerManager: TimerManager = new TimerManager();
public width: number;
public height: number;
constructor(width: number, height: number, enableEntitySystems: boolean = true) {
this.width = width;
this.height = height;
Core._instance = this;
Core.emitter = new Emitter<CoreEvents>();
Core.emitter.addObserver(CoreEvents.FrameUpdated, this.update, this);
Core.registerGlobalManager(this._coroutineManager);
Core.registerGlobalManager(this._timerManager);
Core.entitySystemsEnabled = enableEntitySystems;
this.initialize();
}
/**
* 提供对单例/游戏实例的访问
* @constructor
*/
public static get Instance() {
return this._instance;
}
public _frameCounterElapsedTime: number = 0;
public _frameCounter: number = 0;
public _totalMemory: number = 0;
public _titleMemory: (totalMemory: number, frameCounter: number) => void;
public _scene: Scene;
/**
* 当前活动的场景。注意,如果设置了该设置,在更新结束之前场景实际上不会改变
*/
public static get scene() {
if (!this._instance)
return null;
return this._instance._scene;
}
/**
* 当前活动的场景。注意,如果设置了该设置,在更新结束之前场景实际上不会改变
* @param value
*/
public static set scene(value: Scene) {
if (!value) {
console.error("场景不能为空");
return;
}
if (this._instance._scene == null) {
this._instance._scene = value;
this._instance.onSceneChanged();
this._instance._scene.begin();
} else {
this._instance._nextScene = value;
}
}
/**
* 添加一个全局管理器对象,它的更新方法将调用场景前的每一帧。
* @param manager
*/
public static registerGlobalManager(manager: es.GlobalManager) {
this._instance._globalManagers.push(manager);
manager.enabled = true;
}
/**
* 删除全局管理器对象
* @param manager
*/
public static unregisterGlobalManager(manager: es.GlobalManager) {
new linq.List(this._instance._globalManagers).remove(manager);
manager.enabled = false;
}
/**
* 获取类型为T的全局管理器
* @param type
*/
public static getGlobalManager<T extends es.GlobalManager>(type): T {
for (let i = 0; i < this._instance._globalManagers.length; i++) {
if (this._instance._globalManagers[i] instanceof type)
return this._instance._globalManagers[i] as T;
}
return null;
}
/**
* 启动一个coroutine。Coroutine可以将number延时几秒或延时到其他startCoroutine.Yielding
* null将使coroutine在下一帧被执行。
* @param enumerator
*/
public static startCoroutine(enumerator): ICoroutine {
return this._instance._coroutineManager.startCoroutine(enumerator);
}
/**
* 调度一个一次性或重复的计时器,该计时器将调用已传递的动作
* @param timeInSeconds
* @param repeats
* @param context
* @param onTime
*/
public static schedule(timeInSeconds: number, repeats: boolean = false, context: any = null, onTime: (timer: ITimer) => void) {
return this._instance._timerManager.schedule(timeInSeconds, repeats, context, onTime);
}
public onOrientationChanged() {
Core.emitter.emit(CoreEvents.OrientationChanged);
}
public startDebugDraw() {
this._frameCounter++;
this._frameCounterElapsedTime += Time.deltaTime;
if (this._frameCounterElapsedTime >= 1) {
let memoryInfo = window.performance["memory"];
if (memoryInfo != null) {
this._totalMemory = Number((memoryInfo.totalJSHeapSize / 1048576).toFixed(2));
}
if (this._titleMemory) this._titleMemory(this._totalMemory, this._frameCounter);
this._frameCounter = 0;
this._frameCounterElapsedTime -= 1;
}
}
/**
* 在一个场景结束后,下一个场景开始之前调用
*/
public onSceneChanged() {
Core.emitter.emit(CoreEvents.SceneChanged);
Time.sceneChanged();
}
/**
* 当屏幕大小发生改变时调用
*/
protected onGraphicsDeviceReset() {
// 我们用这些来避免垃圾事件的发生
if (this._graphicsDeviceChangeTimer != null) {
this._graphicsDeviceChangeTimer.reset();
} else {
this._graphicsDeviceChangeTimer = Core.schedule(0.05, false, this, t => {
(t.context as Core)._graphicsDeviceChangeTimer = null;
Core.emitter.emit(CoreEvents.GraphicsDeviceReset);
});
}
}
protected initialize() {
}
protected async update(currentTime?: number) {
if (currentTime != null) Time.update(currentTime);
if (this._scene != null) {
for (let i = this._globalManagers.length - 1; i >= 0; i--) {
if (this._globalManagers[i].enabled)
this._globalManagers[i].update();
}
this._scene.update();
if (this._nextScene != null) {
this._scene.end();
this._scene = this._nextScene;
this._nextScene = null;
this.onSceneChanged();
this._scene.begin();
}
}
this.startDebugDraw();
}
}
}

View File

@@ -16,5 +16,17 @@ module es {
* 当每帧事件触发时
*/
FrameUpdated,
/**
* 当Core.useCustomUpdate为true时则派发该事件
*/
SceneUpdated,
/**
* 当场景需要绘制时
*/
CallDraw,
/**
* 当需要GC时
*/
CallGC,
}
}

View File

@@ -46,8 +46,7 @@ module es {
this.name = name;
this.id = Entity._idGenerator++;
if (Core.entitySystemsEnabled)
this.componentBits = new BitSet();
this.componentBits = new BitSet();
}
public _isDestroyed: boolean;
@@ -211,11 +210,11 @@ module es {
} else if (parent instanceof Entity) {
this.transform.setParent(parent.transform);
}
return this;
}
public setPosition(x: number, y: number){
public setPosition(x: number, y: number) {
this.transform.setPosition(x, y);
return this;
}
@@ -225,22 +224,22 @@ module es {
return this;
}
public setRotation(radians: number){
public setRotation(radians: number) {
this.transform.setRotation(radians);
return this;
}
public setRotationDegrees(degrees: number){
public setRotationDegrees(degrees: number) {
this.transform.setRotationDegrees(degrees);
return this;
}
public setLocalRotation(radians: number){
public setLocalRotation(radians: number) {
this.transform.setLocalRotation(radians);
return this;
}
public setLocalRotationDegrees(degrees: number){
public setLocalRotationDegrees(degrees: number) {
this.transform.setLocalRotationDegrees(degrees);
return this;
}
@@ -253,7 +252,7 @@ module es {
} else {
this.transform.setScale(new Vector2(scale));
}
return this;
}
@@ -265,7 +264,7 @@ module es {
} else {
this.transform.setLocalScale(new Vector2(scale));
}
return this;
}
@@ -381,6 +380,14 @@ module es {
this.components.update();
}
/**
* 自定义渲染器可以选择是否调用它
* @param batcher
*/
public debugRender(batcher: IBatcher) {
this.components.debugRender(batcher);
}
/**
* 将组件添加到组件列表中。返回组件。
* @param component

View File

@@ -1,23 +1,51 @@
module es {
/** 场景 */
export class Scene {
/**
* 默认场景 摄像机
*/
public camera: ICamera;
/**
* 这个场景中的实体列表
*/
public readonly entities: EntityList;
/** 管理当前在场景中的所有RenderableComponents的列表 Entitys */
public readonly renderableComponents: RenderableComponentList;
/**
* 如果设置了,最终渲染到屏幕上的时间可以推迟到这个委托。
* 这实际上只在最终渲染可能需要全屏大小效果的情况下有用,即使使用了一个小的后置缓冲区
*/
public set finalRenderDelegate(value: IFinalRenderDelegate) {
if (this._finalRenderDelegate != null)
this._finalRenderDelegate.unload();
this._finalRenderDelegate = value;
if (this._finalRenderDelegate != null)
this._finalRenderDelegate.onAddedToScene(this);
}
public get finalRenderDelegate() {
return this._finalRenderDelegate;
}
private _finalRenderDelegate: IFinalRenderDelegate;
/**
* 管理所有实体处理器
*/
public readonly entityProcessors: EntityProcessorList;
private _screenshotRequestCallback: (texture) => void;
public readonly _sceneComponents: SceneComponent[] = [];
public _didSceneBegin;
public _renderers: IRenderer[] = [];
public readonly _afterPostProcessorRenderers: IRenderer[] = [];
public _didSceneBegin: boolean;
constructor() {
this.entities = new EntityList(this);
if (Core.entitySystemsEnabled)
this.entityProcessors = new EntityProcessorList();
this.renderableComponents = new RenderableComponentList();
this.entityProcessors = new EntityProcessorList();
this.initialize();
}
@@ -43,15 +71,16 @@ module es {
}
public begin() {
if (this._renderers.length == 0) {
console.warn("场景开始时没有渲染器");
}
Physics.reset();
this.updateResolutionScaler();
if (this.entityProcessors != null)
this.entityProcessors.begin();
Core.emitter.addObserver(CoreEvents.GraphicsDeviceReset,this.updateResolutionScaler, this);
Core.emitter.addObserver(CoreEvents.OrientationChanged, this.updateResolutionScaler, this);
this._didSceneBegin = true;
this.onStart();
@@ -60,8 +89,8 @@ module es {
public end() {
this._didSceneBegin = false;
Core.emitter.removeObserver(CoreEvents.GraphicsDeviceReset, this.updateResolutionScaler);
Core.emitter.removeObserver(CoreEvents.OrientationChanged, this.updateResolutionScaler);
for (let i = 0; i < this._renderers.length; i++)
this._renderers[i].unload();
this.entities.removeAllEntities();
@@ -70,6 +99,7 @@ module es {
}
this._sceneComponents.length = 0;
this.camera = null;
Physics.clear();
if (this.entityProcessors)
@@ -78,10 +108,19 @@ module es {
this.unload();
}
public updateResolutionScaler(){
public updateResolutionScaler() {
}
/**
* 下一次绘制完成后这将克隆回缓冲区并调用回调与clone。
* 注意当使用完Texture后你必须处理掉它
* @param callback
*/
public requestScreenshot(callback: (texture) => void) {
this._screenshotRequestCallback = callback;
}
public update() {
// 更新我们的列表,以防它们有任何变化
this.entities.updateLists();
@@ -98,10 +137,60 @@ module es {
// 更新我们的实体组
this.entities.update();
// 我们在entity.update之后更新我们的renderables以防止任何新的Renderables被添加
this.renderableComponents.updateList();
if (this.entityProcessors != null)
this.entityProcessors.lateUpdate();
}
public render() {
if (this._renderers.length == 0) {
console.error("场景中没有渲染器!");
return;
}
let lastRendererHadRenderTarget = false;
for (let i = 0; i < this._renderers.length; i++) {
if (lastRendererHadRenderTarget && this._renderers[i].wantsToRenderToSceneRenderTarget) {
// 强制更新相机矩阵,以考虑到新的视口尺寸
if (this._renderers[i].camera != null)
this._renderers[i].camera.forceMatrixUpdate();
this.camera && this.camera.forceMatrixUpdate();
}
this._renderers[i].render(this);
lastRendererHadRenderTarget = this._renderers[i].renderTexture != null;
}
}
/**
* 任何存在的PostProcessors都可以进行处理然后我们对RenderTarget进行最后的渲染。
* 几乎在所有情况下finalRenderTarget都是空的。
* 只有在场景转换的第一帧中,如果转换请求渲染,它才会有一个值。
* @param finalRenderTarget
*/
public postRender(finalRenderTarget = null) {
for (let i = 0; i < this._afterPostProcessorRenderers.length; i++) {
if (this._afterPostProcessorRenderers[i].camera != null)
this._afterPostProcessorRenderers[i].camera.forceMatrixUpdate();
this._afterPostProcessorRenderers[i].render(this);
}
// 如果我们有一个截图请求,在最终渲染到回缓冲区之前处理它
if (this._screenshotRequestCallback != null) {
// TODO: 实现各平台的截图方式
this._screenshotRequestCallback = null;
}
// 将我们的最终结果渲染到后置缓冲区,或者让我们的委托来做
if (this._finalRenderDelegate != null) {
} else {
}
}
/**
* 向组件列表添加并返回SceneComponent
* @param component
@@ -154,6 +243,62 @@ module es {
component.onRemovedFromScene();
}
/**
* 添加一个渲染器到场景中
* @param renderer
*/
public addRenderer<T extends IRenderer>(renderer: T): T {
if (renderer.wantsToRenderAfterPostProcessors) {
this._afterPostProcessorRenderers.push(renderer);
this._afterPostProcessorRenderers.sort((a, b) => {
return a.compare(b);
});
} else {
this._renderers.push(renderer);
this._renderers.sort((a, b) => {
return a.compare(b);
});
}
renderer.onAddedToScene(this);
return renderer;
}
/**
* 得到第一个T型的渲染器
* @param type
*/
public getRenderer<T extends IRenderer>(type): T {
for (let i = 0; i < this._renderers.length; i++) {
if (this._renderers[i] instanceof type)
return this._renderers[i] as T;
}
for (let i = 0; i < this._afterPostProcessorRenderers.length; i++) {
if (this._afterPostProcessorRenderers[i] instanceof type)
return this._afterPostProcessorRenderers[i] as T;
}
return null;
}
/**
* 从场景中移除渲染器
* @param renderer
*/
public removeRenderer(renderer: IRenderer) {
Insist.isTrue(new linq.List(this._renderers).contains(renderer) ||
new linq.List(this._afterPostProcessorRenderers).contains(renderer));
if (renderer.wantsToRenderAfterPostProcessors)
new linq.List(this._afterPostProcessorRenderers).remove(renderer);
else
new linq.List(this._renderers).remove(renderer);
renderer.unload();
}
/**
* 将实体添加到此场景,并返回它
* @param name
@@ -168,8 +313,7 @@ module es {
* @param entity
*/
public addEntity(entity: Entity) {
if (new linq.List(this.entities.buffer).contains(entity))
console.warn(`您试图将同一实体添加到场景两次: ${entity}`);
Insist.isFalse(new linq.List(this.entities.buffer).contains(entity), `您试图将同一实体添加到场景两次: ${entity}`);
this.entities.add(entity);
entity.scene = this;

View File

@@ -86,10 +86,8 @@ module es {
if (isIUpdatable(component))
new linq.List(this._updatableComponents).remove(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}
}
@@ -98,10 +96,8 @@ module es {
if (isIUpdatable(component))
this._updatableComponents.push(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
}
@@ -125,10 +121,8 @@ module es {
if (isIUpdatable(component))
this._updatableComponents.push(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component);
this._tempBufferList.push(component);
@@ -164,10 +158,8 @@ module es {
if (isIUpdatable(component))
new linq.List(this._updatableComponents).remove(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
component.onRemovedFromEntity();
component.entity = null;
@@ -252,5 +244,12 @@ module es {
for (let i = 0; i < this._components.length; i++)
this._components[i].onDisabled();
}
public debugRender(batcher: IBatcher) {
for (let i = 0; i < this._components.length; i++) {
if (this._components[i].enabled)
this._components[i].debugRender(batcher);
}
}
}
}

View File

@@ -136,16 +136,15 @@ module es {
public updateLists() {
if (this._entitiesToRemove.getCount() > 0) {
this._entitiesToRemove.toArray().forEach(entity => {
// 处理标签列表
this.removeFromTagList(entity);
// 处理标签列表
this.removeFromTagList(entity);
// 处理常规实体列表
new linq.List(this._entities).remove(entity);
entity.onRemovedFromScene();
entity.scene = null;
if (Core.entitySystemsEnabled)
this.scene.entityProcessors.onEntityRemoved(entity);
// 处理常规实体列表
new linq.List(this._entities).remove(entity);
entity.onRemovedFromScene();
entity.scene = null;
this.scene.entityProcessors.onEntityRemoved(entity);
});
this._entitiesToRemove.clear();
}
@@ -157,8 +156,7 @@ module es {
this.addToTagList(entity);
if (Core.entitySystemsEnabled)
this.scene.entityProcessors.onEntityAdded(entity);
this.scene.entityProcessors.onEntityAdded(entity);
});
this._entitiesToAdded.toArray().forEach(entity => {
@@ -177,7 +175,7 @@ module es {
// 根据需要对标签列表进行排序
if (this._unsortedTags.size > 0) {
this._unsortedTags.forEach(value => this._entityDict.get(value).sort((a, b) => a.compareTo(b)));
this._unsortedTags.clear();
}
}
@@ -192,12 +190,12 @@ module es {
return this._entities[i];
}
for (let i = 0; i < this._entitiesToAdded.getCount(); i ++){
for (let i = 0; i < this._entitiesToAdded.getCount(); i++) {
let entity = this._entitiesToAdded.toArray()[i];
if (entity.name == name)
return entity;
}
return null;
}
@@ -229,7 +227,7 @@ module es {
list.push(this._entities[i] as T);
}
for (let i = 0; i < this._entitiesToAdded.getCount(); i ++){
for (let i = 0; i < this._entitiesToAdded.getCount(); i++) {
let entity = this._entitiesToAdded.toArray()[i];
if (TypeUtils.getType(entity) instanceof type) {
list.push(entity as T);

View File

@@ -0,0 +1,90 @@
module es {
export class RenderableComponentList {
// IRenderable列表的全局updateOrder排序
public static compareUpdatableOrder: IComparer<IRenderable> = new RenderableComparer();
/**
* 添加到实体的组件列表
*/
private _components: IRenderable[] = [];
/**
* 通过renderLayer跟踪组件便于检索
*/
private _componentsByRenderLayer: Map<number, IRenderable[]> = new Map();
private _unsortedRenderLayers: number[] = [];
private _componentsNeedSort: boolean = true;
public get count() {
return this._components.length;
}
public get(index: number): IRenderable {
return this._components[index];
}
public add(component: IRenderable) {
this._components.push(component);
this.addToRenderLayerList(component, component.renderLayer);
}
public remove(component: IRenderable) {
new linq.List(this._components).remove(component);
new linq.List(this._componentsByRenderLayer.get(component.renderLayer)).remove(component);
}
public updateRenderableRenderLayer(component: IRenderable, oldRenderLayer: number, newRenderLayer: number) {
// 需要注意的是,在组件 "上线 "之前renderLayer可能会发生变化
if (this._componentsByRenderLayer.has(oldRenderLayer) &&
new linq.List(this._componentsByRenderLayer.get(oldRenderLayer)).contains(component)) {
new linq.List(this._componentsByRenderLayer.get(oldRenderLayer)).remove(component);
this.addToRenderLayerList(component, newRenderLayer);
}
}
/**
* 弄脏RenderLayers排序标志导致所有组件的重新排序
* @param renderLayer
*/
public setRenderLayerNeedsComponentSort(renderLayer: number) {
if (!new linq.List(this._unsortedRenderLayers).contains(renderLayer))
this._unsortedRenderLayers.push(renderLayer);
this._componentsNeedSort = true;
}
private addToRenderLayerList(component: IRenderable, renderLayer: number) {
let list = this.componentsWithRenderLayer(renderLayer);
Insist.isFalse(new linq.List(list).contains(component), "组件renderLayer列表已经包含这个组件");
}
/**
* 获取所有给定renderLayer的组件。组件列表是预先排序的。
* @param renderLayer
*/
public componentsWithRenderLayer(renderLayer: number) {
if (!this._componentsByRenderLayer.has(renderLayer)) {
this._componentsByRenderLayer.set(renderLayer, []);
}
return this._componentsByRenderLayer.get(renderLayer);
}
public updateList() {
if (this._componentsNeedSort) {
this._components.sort(RenderableComponentList.compareUpdatableOrder.compare);
this._componentsNeedSort = false;
}
if (this._unsortedRenderLayers.length > 0) {
for (let i = 0, count = this._unsortedRenderLayers.length; i < count; i++) {
let renderLayerComponents = this._componentsByRenderLayer.get(this._unsortedRenderLayers[i]);
if (renderLayerComponents) {
renderLayerComponents.sort(RenderableComponentList.compareUpdatableOrder.compare);
}
}
this._unsortedRenderLayers.length = 0;
}
}
}
}