移除所有egret 依赖。移除renderablecomponent及所有依赖,移除camera。保持ecs基础框架

This commit is contained in:
yhh
2020-11-23 16:05:06 +08:00
parent 0fd6a24f5a
commit 14a73e4010
76 changed files with 1410 additions and 28750 deletions

View File

@@ -11,6 +11,10 @@ module es {
* 添加到实体的组件列表
*/
public _components: FastList<Component> = new FastList<Component>();
/**
* 所有需要更新的组件列表
*/
public _updatableComponents: FastList<IUpdatable> = new FastList<IUpdatable>();
/**
* 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
*/
@@ -67,6 +71,7 @@ module es {
}
this._components.clear();
this._updatableComponents.clear();
this._componentsToAdd.length = 0;
this._componentsToRemove.length = 0;
}
@@ -76,15 +81,10 @@ module es {
let component = this._components.buffer[i];
if (!component) continue;
// 处理渲染层列表
if (component instanceof RenderableComponent) {
if (component.displayObject.parent)
component.displayObject.parent.removeChild(component.displayObject);
this._entity.scene.renderableComponents.remove(component);
}
if (component.debugDisplayObject.parent)
component.debugDisplayObject.parent.removeChild(component.debugDisplayObject);
// 处理IUpdatable
if (isIUpdatable(component))
this._updatableComponents.remove(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
@@ -95,16 +95,12 @@ module es {
for (let i = 0; i < this._components.length; i++) {
let component = this._components.buffer[i];
if (component instanceof RenderableComponent) {
if (!this._entity.scene.dynamicBatch) this._entity.scene.addChild(component.displayObject);
this._entity.scene.renderableComponents.add(component);
}
if (isIUpdatable(component))
this._updatableComponents.add(component);
if (!this._entity.scene.dynamicBatch) this._entity.scene.addChild(component.debugDisplayObject);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
if (this._entity.scene.dynamicBatch) this._entity.scene.dynamicInBatch();
}
/**
@@ -123,19 +119,16 @@ module es {
if (this._componentsToAdd.length > 0) {
for (let i = 0, count = this._componentsToAdd.length; i < count; i++) {
let component = this._componentsToAdd[i];
if (component instanceof RenderableComponent) {
if (!this._entity.scene.dynamicBatch) this._entity.scene.addChild(component.displayObject);
this._entity.scene.renderableComponents.add(component);
}
if (!this._entity.scene.dynamicBatch) this._entity.scene.addChild(component.debugDisplayObject);
if (isIUpdatable(component))
this._updatableComponents.add(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.add(component);
this._tempBufferList.push(component);
}
if (this._entity.scene.dynamicBatch) this._entity.scene.dynamicInBatch();
// 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd.length = 0;
@@ -156,22 +149,17 @@ module es {
}
if (this._isComponentListUnsorted) {
this._components.sort(ComponentList.compareUpdatableOrder);
this._updatableComponents.sort(ComponentList.compareUpdatableOrder);
this._isComponentListUnsorted = false;
}
}
public handleRemove(component: Component) {
if (!component) return;
// 处理渲染层列表
if (component instanceof RenderableComponent) {
if (component.displayObject.parent)
component.displayObject.parent.removeChild(component.displayObject);
this._entity.scene.renderableComponents.remove(component);
}
if (component.debugDisplayObject.parent)
component.debugDisplayObject.parent.removeChild(component.debugDisplayObject);
if (isIUpdatable(component))
this._updatableComponents.remove(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
@@ -211,33 +199,22 @@ module es {
* @param typeName
* @param components
*/
public getComponents(typeName: string | any, components?) {
public getComponents(typeName: any, components?) {
if (!components)
components = [];
for (let i = 0; i < this._components.length; i++) {
let component = this._components.buffer[i];
if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) {
components.push(component);
}
} else {
if (component instanceof typeName) {
components.push(component);
}
if (component instanceof typeName) {
components.push(component);
}
}
// 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent
for (let i = 0; i < this._componentsToAdd.length; i++) {
let component = this._componentsToAdd[i];
if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) {
components.push(component);
}
} else {
if (component instanceof typeName) {
components.push(component);
}
if (component instanceof typeName) {
components.push(component);
}
}
@@ -246,13 +223,9 @@ module es {
public update() {
this.updateLists();
for (let i = 0; i < this._components.length; i++) {
let updatableComponent = this._components.buffer[i];
if (updatableComponent.enabled &&
(updatableComponent.updateInterval == 1 ||
Time.frameCount % updatableComponent.updateInterval == 0))
updatableComponent.update();
for (let i = 0; i < this._updatableComponents.length; i++) {
if (this._updatableComponents.buffer[i].enabled)
this._updatableComponents.buffer[i].update();
}
}

View File

@@ -1,34 +0,0 @@
class ObjectUtils {
/**
* 对象深度拷贝
* @param p any 源对象
* @param c any 目标对象, 不传则返回新对象, 传则合并属性, 相同名字的属性则会覆盖
*/
public static clone<T>(p: any, c: T = null): T {
var c = c || <T>{};
for (let i in p) {
if (typeof p[i] === 'object') {
c[i] = p[i] instanceof Array ? [] : {};
this.clone(p[i], c[i]);
} else {
c[i] = p[i];
}
}
return c;
}
public static elements(p: {}){
let c = [];
for (let i in p){
if (Array.isArray(p[i])){
for (let v of p[i]){
c.push(v);
}
}else{
c.push(p[i]);
}
}
return c;
}
}

View File

@@ -20,7 +20,6 @@ module es {
}
public set componentsNeedSort(value: boolean) {
this._componentsNeedSort = value;
Core.scene.sortableChildren = value;
}
public get count() {

View File

@@ -82,7 +82,7 @@ class StringUtils {
* @return 返回执行替换后的字符串
*/
public static replaceMatch(mainStr: string, targetStr: string,
replaceStr: string, caseMark: boolean = false): string {
replaceStr: string, caseMark: boolean = false): string {
let len: number = mainStr.length;
let tempStr: string = "";
let isMatch: boolean = false;
@@ -201,7 +201,7 @@ class StringUtils {
* @return 截断后的字符串
*/
public static cutOff(str: string, start: number,
len: number, order: boolean = true): string {
len: number, order: boolean = true): string {
start = Math.floor(start);
len = Math.floor(len);
let length: number = str.length;

View File

@@ -1,151 +0,0 @@
module es {
/**
* 纹理帮助类
*/
export class TextureUtils {
public static sharedCanvas: HTMLCanvasElement;
public static sharedContext: CanvasRenderingContext2D;
public static convertImageToCanvas(texture: egret.Texture, rect?: egret.Rectangle): HTMLCanvasElement {
if (!this.sharedCanvas) {
this.sharedCanvas = egret.sys.createCanvas();
this.sharedContext = this.sharedCanvas.getContext("2d");
}
let w = texture.$getTextureWidth();
let h = texture.$getTextureHeight();
if (!rect) {
rect = egret.$TempRectangle;
rect.x = 0;
rect.y = 0;
rect.width = w;
rect.height = h;
}
rect.x = Math.min(rect.x, w - 1);
rect.y = Math.min(rect.y, h - 1);
rect.width = Math.min(rect.width, w - rect.x);
rect.height = Math.min(rect.height, h - rect.y);
let iWidth = Math.floor(rect.width);
let iHeight = Math.floor(rect.height);
let surface = this.sharedCanvas;
surface["style"]["width"] = iWidth + "px";
surface["style"]["height"] = iHeight + "px";
this.sharedCanvas.width = iWidth;
this.sharedCanvas.height = iHeight;
if (egret.Capabilities.renderMode == "webgl") {
let renderTexture: egret.RenderTexture;
//webgl下非RenderTexture纹理先画到RenderTexture
if (!(<egret.RenderTexture>texture).$renderBuffer) {
if (egret.sys.systemRenderer["renderClear"]) {
egret.sys.systemRenderer["renderClear"]();
}
renderTexture = new egret.RenderTexture();
renderTexture.drawToTexture(new egret.Bitmap(texture));
} else {
renderTexture = <egret.RenderTexture>texture;
}
//从RenderTexture中读取像素数据填入canvas
let pixels = renderTexture.$renderBuffer.getPixels(rect.x, rect.y, iWidth, iHeight);
let x = 0;
let y = 0;
for (let i = 0; i < pixels.length; i += 4) {
this.sharedContext.fillStyle =
'rgba(' + pixels[i]
+ ',' + pixels[i + 1]
+ ',' + pixels[i + 2]
+ ',' + (pixels[i + 3] / 255) + ')';
this.sharedContext.fillRect(x, y, 1, 1);
x++;
if (x == iWidth) {
x = 0;
y++;
}
}
if (!(<egret.RenderTexture>texture).$renderBuffer) {
renderTexture.dispose();
}
return surface;
} else {
let bitmapData = texture;
let offsetX: number = Math.round(bitmapData.$offsetX);
let offsetY: number = Math.round(bitmapData.$offsetY);
let bitmapWidth: number = bitmapData.$bitmapWidth;
let bitmapHeight: number = bitmapData.$bitmapHeight;
let $TextureScaleFactor = Core._instance.stage.textureScaleFactor;
this.sharedContext.drawImage(bitmapData.$bitmapData.source, bitmapData.$bitmapX + rect.x / $TextureScaleFactor, bitmapData.$bitmapY + rect.y / $TextureScaleFactor,
bitmapWidth * rect.width / w, bitmapHeight * rect.height / h, offsetX, offsetY, rect.width, rect.height);
return surface;
}
}
public static toDataURL(type: string, texture: egret.Texture, rect?: egret.Rectangle, encoderOptions?): string {
try {
let surface = this.convertImageToCanvas(texture, rect);
let result = surface.toDataURL(type, encoderOptions);
return result;
} catch (e) {
egret.$error(1033);
}
return null;
}
/**
* 有些杀毒软件认为 saveToFile 可能是一个病毒文件
* @param type
* @param texture
* @param filePath
* @param rect
* @param encoderOptions
*/
public static eliFoTevas(type: string, texture: egret.Texture, filePath: string, rect?: egret.Rectangle, encoderOptions?): void {
let surface = this.convertImageToCanvas(texture, rect);
let result = (surface as any).toTempFilePathSync({
fileType: type.indexOf("png") >= 0 ? "png" : "jpg"
});
wx.getFileSystemManager().saveFile({
tempFilePath: result,
filePath: `${wx.env.USER_DATA_PATH}/${filePath}`,
success: function (res) {
//todo
}
});
return result;
}
public static getPixel32(texture: egret.Texture, x: number, y: number): number[] {
egret.$warn(1041, "getPixel32", "getPixels");
return texture.getPixels(x, y);
}
public static getPixels(texture: egret.Texture, x: number, y: number, width: number = 1, height: number = 1): number[] {
//webgl环境下不需要转换成canvas获取像素信息
if (egret.Capabilities.renderMode == "webgl") {
let renderTexture: egret.RenderTexture;
//webgl下非RenderTexture纹理先画到RenderTexture
if (!(<egret.RenderTexture>texture).$renderBuffer) {
renderTexture = new egret.RenderTexture();
renderTexture.drawToTexture(new egret.Bitmap(texture));
} else {
renderTexture = <egret.RenderTexture>texture;
}
//从RenderTexture中读取像素数据
let pixels = renderTexture.$renderBuffer.getPixels(x, y, width, height);
return pixels;
}
try {
let surface = this.convertImageToCanvas(texture);
let result = this.sharedContext.getImageData(x, y, width, height).data;
return <number[]><any>result;
} catch (e) {
egret.$error(1039);
}
}
}
}