移除渲染模块,提供更纯净的ecs
This commit is contained in:
@@ -83,8 +83,6 @@ module es {
|
||||
public onEntityTransformChanged(comp: ComponentTransform) {
|
||||
}
|
||||
|
||||
public debugRender(batcher: IBatcher) {}
|
||||
|
||||
/**
|
||||
*当父实体或此组件启用时调用
|
||||
*/
|
||||
|
||||
@@ -11,11 +11,7 @@ module es {
|
||||
constructor(x: number = 0, y: number = 0, width: number = 1, height: number = 1) {
|
||||
super();
|
||||
|
||||
if (width == 1 && height == 1) {
|
||||
this._colliderRequiresAutoSizing = true;
|
||||
} else {
|
||||
this._localOffset = new Vector2(x + width / 2, y + height / 2);
|
||||
}
|
||||
this._localOffset = new Vector2(x + width / 2, y + height / 2);
|
||||
|
||||
this.shape = new Box(width, height);
|
||||
}
|
||||
@@ -42,7 +38,6 @@ 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) {
|
||||
// 更新框,改变边界,如果我们需要更新物理系统中的边界
|
||||
@@ -60,7 +55,6 @@ module es {
|
||||
* @param width
|
||||
*/
|
||||
public setWidth(width: number): BoxCollider {
|
||||
this._colliderRequiresAutoSizing = false;
|
||||
let box = this.shape as Box;
|
||||
if (width != box.width) {
|
||||
// 更新框,改变边界,如果我们需要更新物理系统中的边界
|
||||
@@ -78,7 +72,6 @@ module es {
|
||||
* @param height
|
||||
*/
|
||||
public setHeight(height: number) {
|
||||
this._colliderRequiresAutoSizing = false;
|
||||
let box = this.shape as Box;
|
||||
if (height != box.height) {
|
||||
// 更新框,改变边界,如果我们需要更新物理系统中的边界
|
||||
@@ -89,18 +82,6 @@ module es {
|
||||
}
|
||||
}
|
||||
|
||||
public debugRender(batcher: IBatcher) {
|
||||
let poly = this.shape as Polygon;
|
||||
batcher.drawHollowRect(this.bounds.x, this.bounds.y, this.bounds.width, this.bounds.height, new Color(76, 76, 76, 76), 2);
|
||||
batcher.end();
|
||||
batcher.drawPolygon(this.shape.position, poly.points, new Color(139, 0, 0, 255), true, 2);
|
||||
batcher.end();
|
||||
batcher.drawPixel(this.entity.position, new Color(255, 255, 0), 4);
|
||||
batcher.end();
|
||||
batcher.drawPixel(es.Vector2.add(this.transform.position, this.shape.center), new Color(255, 0, 0), 2);
|
||||
batcher.end();
|
||||
}
|
||||
|
||||
public toString() {
|
||||
return `[BoxCollider: bounds: ${this.bounds}]`;
|
||||
}
|
||||
|
||||
@@ -11,9 +11,6 @@ module es {
|
||||
super();
|
||||
|
||||
this.shape = new Circle(radius);
|
||||
if (radius == 1) {
|
||||
this._colliderRequiresAutoSizing = true;
|
||||
}
|
||||
}
|
||||
|
||||
public get radius(): number {
|
||||
@@ -29,7 +26,6 @@ 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;
|
||||
@@ -43,17 +39,6 @@ module es {
|
||||
return this;
|
||||
}
|
||||
|
||||
public debugRender(batcher: IBatcher) {
|
||||
batcher.drawHollowRect(this.bounds.x, this.bounds.y, this.bounds.width, this.bounds.height, new Color(76, 76, 76, 76), 2);
|
||||
batcher.end();
|
||||
batcher.drawCircle(this.shape.position, this.radius, new Color(139, 0, 0), 2);
|
||||
batcher.end();
|
||||
batcher.drawPixel(this.entity.transform.position, new Color(255, 255, 0), 4);
|
||||
batcher.end();
|
||||
batcher.drawPixel(this.shape.position, new Color(255, 0, 0), 2);
|
||||
batcher.end();
|
||||
}
|
||||
|
||||
public toString() {
|
||||
return `[CircleCollider: bounds: ${this.bounds}, radius: ${(this.shape as Circle).radius}]`
|
||||
}
|
||||
|
||||
@@ -29,8 +29,6 @@ module es {
|
||||
*/
|
||||
public registeredPhysicsBounds: Rectangle = new Rectangle();
|
||||
|
||||
protected _colliderRequiresAutoSizing: boolean;
|
||||
|
||||
public _localOffsetLength: number;
|
||||
public _isPositionDirty: boolean = true;
|
||||
public _isRotationDirty: boolean = true;
|
||||
@@ -116,33 +114,6 @@ module es {
|
||||
}
|
||||
|
||||
public onAddedToEntity() {
|
||||
if (this._colliderRequiresAutoSizing) {
|
||||
let renderable = null;
|
||||
for (let i = 0; i < this.entity.components.buffer.length; i ++) {
|
||||
let component = this.entity.components.buffer[i];
|
||||
if (component instanceof RenderableComponent){
|
||||
renderable = component;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (renderable != null) {
|
||||
let renderableBounds = renderable.bounds.clone();
|
||||
|
||||
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;
|
||||
this.localOffset = renderableBounds.center.sub(this.entity.transform.position);
|
||||
} else if (this instanceof BoxCollider) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
this.localOffset = renderableBounds.center.sub(this.entity.transform.position);
|
||||
}
|
||||
}
|
||||
}
|
||||
this._isParentEntityAddedToScene = true;
|
||||
this.registerColliderWithPhysicsSystem();
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
module es {
|
||||
export interface IRenderable {
|
||||
enabled: boolean;
|
||||
renderLayer: number;
|
||||
isVisibleFromCamera(camera: ICamera): boolean;
|
||||
render(batcher: IBatcher, camera: ICamera): void;
|
||||
debugRender(batcher: IBatcher): void;
|
||||
}
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
module es {
|
||||
export abstract class RenderableComponent extends es.Component implements IRenderable {
|
||||
public getwidth() {
|
||||
return this.bounds.width;
|
||||
}
|
||||
|
||||
public getheight() {
|
||||
return this.bounds.height;
|
||||
}
|
||||
|
||||
protected _bounds: es.Rectangle = new es.Rectangle();
|
||||
public getbounds(): es.Rectangle {
|
||||
if (this._areBoundsDirty) {
|
||||
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new es.Vector2(this.getwidth() / 2, this.getheight() / 2),
|
||||
this.entity.transform.scale, this.entity.transform.rotation, this.getwidth(), this.getheight());
|
||||
this._areBoundsDirty = false;
|
||||
}
|
||||
return this._bounds;
|
||||
}
|
||||
public get bounds() {
|
||||
return this.getbounds();
|
||||
}
|
||||
protected _areBoundsDirty: boolean = true;
|
||||
public color: Color = Color.White;
|
||||
|
||||
public get renderLayer() {
|
||||
return this._renderLayer;
|
||||
}
|
||||
public set renderLayer(value: number) {
|
||||
this.setRenderLayer(value);
|
||||
}
|
||||
|
||||
protected _renderLayer: number = 0;
|
||||
|
||||
public onEntityTransformChanged(comp: ComponentTransform) {
|
||||
this._areBoundsDirty = true;
|
||||
}
|
||||
|
||||
public get localOffset() {
|
||||
return this._localOffset;
|
||||
}
|
||||
public set localOffset(value: es.Vector2) {
|
||||
this.setLocalOffset(value);
|
||||
}
|
||||
|
||||
public setLocalOffset(offset: es.Vector2) {
|
||||
if (!this._localOffset.equals(offset)) {
|
||||
this._localOffset = offset;
|
||||
this._areBoundsDirty = true;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public get isVisible() {
|
||||
return this._isVisible;
|
||||
}
|
||||
|
||||
public set isVisible(value: boolean) {
|
||||
if (this._isVisible != value) {
|
||||
this._isVisible = value;
|
||||
|
||||
if (this._isVisible) {
|
||||
this.onBecameVisible();
|
||||
} else {
|
||||
this.onBecameInvisible();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public debugRenderEnabled: boolean = true;
|
||||
|
||||
protected _isVisible: boolean = false;
|
||||
protected _localOffset: es.Vector2 = new es.Vector2();
|
||||
|
||||
public abstract render(batcher: IBatcher, camera: ICamera): void;
|
||||
|
||||
protected onBecameVisible() {
|
||||
|
||||
}
|
||||
|
||||
protected onBecameInvisible() {
|
||||
|
||||
}
|
||||
|
||||
public setRenderLayer(renderLayer: number): RenderableComponent {
|
||||
if (renderLayer != this._renderLayer) {
|
||||
let oldRenderLayer = this._renderLayer;
|
||||
this._renderLayer = renderLayer;
|
||||
|
||||
if (this.entity != null && this.entity.scene != null)
|
||||
es.Core.scene.renderableComponents.updateRenderableRenderLayer(this, oldRenderLayer, this._renderLayer);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public isVisibleFromCamera(cam: ICamera): boolean {
|
||||
this.isVisible = cam.bounds.intersects(this.bounds);
|
||||
|
||||
return this.isVisible;
|
||||
}
|
||||
|
||||
public debugRender(batcher: IBatcher) {
|
||||
if (!this.debugRenderEnabled)
|
||||
return;
|
||||
|
||||
let collider = null;
|
||||
for (let i = 0; i < this.entity.components.buffer.length; i++) {
|
||||
let component = this.entity.components.buffer[i];
|
||||
if (component instanceof Collider) {
|
||||
collider = component;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (collider == null) {
|
||||
batcher.drawHollowRect(this.bounds.x, this.bounds.y, this.bounds.width, this.bounds.height, new Color(255, 255, 0));
|
||||
batcher.end();
|
||||
}
|
||||
|
||||
batcher.drawPixel(es.Vector2.add(this.entity.transform.position, this._localOffset), new Color(153, 50, 204), 4);
|
||||
batcher.end();
|
||||
}
|
||||
|
||||
public tweenColorTo(to: Color, duration: number) {
|
||||
const tween = Pool.obtain(RenderableColorTween);
|
||||
tween.setTarget(this);
|
||||
tween.initialize(tween, to, duration);
|
||||
return tween;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -376,11 +376,6 @@ module es {
|
||||
this.components.update();
|
||||
}
|
||||
|
||||
public debugRender(batcher: IBatcher) {
|
||||
if (!batcher) return;
|
||||
this.components.debugRender(batcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建组件的新实例。返回实例组件
|
||||
* @param componentType
|
||||
|
||||
@@ -2,21 +2,17 @@
|
||||
module es {
|
||||
/** 场景 */
|
||||
export class Scene {
|
||||
public camera: ICamera;
|
||||
/** 这个场景中的实体列表 */
|
||||
public readonly entities: EntityList;
|
||||
public readonly renderableComponents: RenderableComponentList;
|
||||
/** 管理所有实体处理器 */
|
||||
public readonly entityProcessors: EntityProcessorList;
|
||||
|
||||
public readonly _sceneComponents: SceneComponent[] = [];
|
||||
public _renderers: Renderer[] = [];
|
||||
public readonly identifierPool: IdentifierPool;
|
||||
private _didSceneBegin: boolean;
|
||||
|
||||
constructor() {
|
||||
this.entities = new EntityList(this);
|
||||
this.renderableComponents = new RenderableComponentList();
|
||||
|
||||
this.entityProcessors = new EntityProcessorList();
|
||||
this.identifierPool = new IdentifierPool();
|
||||
@@ -45,10 +41,6 @@ module es {
|
||||
}
|
||||
|
||||
public begin() {
|
||||
if (this._renderers.length == 0) {
|
||||
this.addRenderer(new DefaultRenderer());
|
||||
}
|
||||
|
||||
Physics.reset();
|
||||
|
||||
if (this.entityProcessors != null)
|
||||
@@ -62,9 +54,6 @@ module es {
|
||||
public end() {
|
||||
this._didSceneBegin = false;
|
||||
|
||||
for (let i = 0; i < this._renderers.length; i ++)
|
||||
this._renderers[i].unload();
|
||||
|
||||
this.entities.removeAllEntities();
|
||||
|
||||
for (let i = 0; i < this._sceneComponents.length; i++) {
|
||||
@@ -72,7 +61,6 @@ module es {
|
||||
}
|
||||
this._sceneComponents.length = 0;
|
||||
|
||||
this.camera = null;
|
||||
Physics.clear();
|
||||
|
||||
if (this.entityProcessors)
|
||||
@@ -99,38 +87,6 @@ module es {
|
||||
|
||||
if (this.entityProcessors != null)
|
||||
this.entityProcessors.lateUpdate();
|
||||
|
||||
this.renderableComponents.updateLists();
|
||||
this.render();
|
||||
}
|
||||
|
||||
public render() {
|
||||
for (let i = 0; i < this._renderers.length; i ++) {
|
||||
this._renderers[i].render(this);
|
||||
}
|
||||
}
|
||||
|
||||
public addRenderer<T extends Renderer>(renderer: T): T {
|
||||
this._renderers.push(renderer);
|
||||
this._renderers.sort((self, other) => self.renderOrder - other.renderOrder);
|
||||
|
||||
renderer.onAddedToScene(this);
|
||||
|
||||
return renderer;
|
||||
}
|
||||
|
||||
public getRenderer<T extends Renderer>(type: new (...args: any[]) => T): T {
|
||||
for (let i = 0; i < this._renderers.length; i ++) {
|
||||
if (this._renderers[i] instanceof type)
|
||||
return this._renderers[i] as T;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public removeRenderer(renderer: Renderer) {
|
||||
new List(this._renderers).remove(renderer);
|
||||
renderer.unload();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -94,9 +94,6 @@ module es {
|
||||
for (let i = 0, s = this._components.length; i < s; ++ i) {
|
||||
let component = this._components[i];
|
||||
if (!component) continue;
|
||||
|
||||
if (component instanceof RenderableComponent)
|
||||
this._entity.scene.renderableComponents.remove(component);
|
||||
|
||||
// 处理IUpdatable
|
||||
if (isIUpdatable(component))
|
||||
@@ -112,9 +109,6 @@ module es {
|
||||
if (this._components.length > 0) {
|
||||
for (let i = 0, s = this._components.length; i < s; ++ i) {
|
||||
let component = this._components[i];
|
||||
if (component instanceof RenderableComponent)
|
||||
this._entity.scene.renderableComponents.add(component);
|
||||
|
||||
if (isIUpdatable(component))
|
||||
this._updatableComponents.push(component);
|
||||
|
||||
@@ -157,8 +151,6 @@ module es {
|
||||
if (this._componentsToAddList.length > 0) {
|
||||
for (let i = 0, l = this._componentsToAddList.length; i < l; ++ i) {
|
||||
let component = this._componentsToAddList[i];
|
||||
if (component instanceof RenderableComponent)
|
||||
this._entity.scene.renderableComponents.add(component);
|
||||
|
||||
if (isIUpdatable(component))
|
||||
this._updatableComponents.push(component);
|
||||
@@ -195,9 +187,6 @@ module es {
|
||||
}
|
||||
|
||||
public handleRemove(component: Component) {
|
||||
if (component instanceof RenderableComponent)
|
||||
this._entity.scene.renderableComponents.remove(component);
|
||||
|
||||
if (isIUpdatable(component) && this._updatableComponents.length > 0) {
|
||||
let index = this._updatableComponents.findIndex((c) => (<any>c as Component).id == component.id);
|
||||
if (index != -1)
|
||||
@@ -325,14 +314,5 @@ module es {
|
||||
this._components[i].onDisabled();
|
||||
}
|
||||
}
|
||||
|
||||
public debugRender(batcher: IBatcher) {
|
||||
if (!batcher) return;
|
||||
for (let i = 0; i < this._components.length; i ++) {
|
||||
if (this._components[i].enabled) {
|
||||
this._components[i].debugRender(batcher);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
module es {
|
||||
export class RenderableComponentList {
|
||||
private _components: IRenderable[] = [];
|
||||
private _componentsByRenderLayer: Map<number, IRenderable[]> = new Map();
|
||||
private _unsortedRenderLayers: number[] = [];
|
||||
private _componentsNeedSort = true;
|
||||
|
||||
public get count() {
|
||||
return this._components.length;
|
||||
}
|
||||
|
||||
public get(index: number) {
|
||||
return this._components[index];
|
||||
}
|
||||
|
||||
public add(component: IRenderable) {
|
||||
this._components.push(component);
|
||||
this.addToRenderLayerList(component, component.renderLayer);
|
||||
}
|
||||
|
||||
public remove(component: IRenderable) {
|
||||
new List(this._components).remove(component);
|
||||
new List(this._componentsByRenderLayer.get(component.renderLayer)).remove(component);
|
||||
}
|
||||
|
||||
public updateRenderableRenderLayer(component: IRenderable, oldRenderLayer: number, newRenderLayer: number) {
|
||||
if (this._componentsByRenderLayer.has(oldRenderLayer) && new List(this._componentsByRenderLayer.get(oldRenderLayer)).contains(component)) {
|
||||
new List(this._componentsByRenderLayer.get(oldRenderLayer)).remove(component);
|
||||
this.addToRenderLayerList(component, newRenderLayer);
|
||||
}
|
||||
}
|
||||
|
||||
public setRenderLayerNeedsComponentSort(renderLayer: number) {
|
||||
const unsortedRenderLayersList = new List(this._unsortedRenderLayers);
|
||||
if (!unsortedRenderLayersList.contains(renderLayer))
|
||||
unsortedRenderLayersList.add(renderLayer);
|
||||
this._componentsNeedSort = true;
|
||||
}
|
||||
|
||||
public setNeedsComponentSort() {
|
||||
this._componentsNeedSort = true;
|
||||
}
|
||||
|
||||
private addToRenderLayerList(component: IRenderable, renderLayer: number) {
|
||||
let list = this.componentsWithRenderLayer(renderLayer);
|
||||
es.Insist.isFalse(!!list.find(c => c == component), "组件renderLayer列表已包含此组件");
|
||||
|
||||
list.push(component);
|
||||
const unsortedRenderLayersList = new List(this._unsortedRenderLayers);
|
||||
if (!unsortedRenderLayersList.contains(renderLayer))
|
||||
unsortedRenderLayersList.add(renderLayer);
|
||||
this._componentsNeedSort = true;
|
||||
}
|
||||
|
||||
public componentsWithRenderLayer(renderLayer: number) {
|
||||
if (!this._componentsByRenderLayer.get(renderLayer)) {
|
||||
this._componentsByRenderLayer.set(renderLayer, []);
|
||||
}
|
||||
|
||||
return this._componentsByRenderLayer.get(renderLayer);
|
||||
}
|
||||
|
||||
public updateLists() {
|
||||
if (this._componentsNeedSort) {
|
||||
this._components.sort((self, other) => other.renderLayer - self.renderLayer);
|
||||
this._componentsNeedSort = false;
|
||||
}
|
||||
|
||||
if (this._unsortedRenderLayers.length > 0) {
|
||||
for (let i = 0, count = this._unsortedRenderLayers.length; i < count; i ++) {
|
||||
const renderLayerComponents = this._componentsByRenderLayer.get(this._unsortedRenderLayers[i]);
|
||||
if (renderLayerComponents) {
|
||||
renderLayerComponents.sort((self, other) => other.renderLayer - self.renderLayer);
|
||||
}
|
||||
|
||||
this._unsortedRenderLayers.length = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user