ecs适配egret

This commit is contained in:
yhh
2020-06-29 15:41:02 +08:00
parent a63d8598d8
commit a4f1ae351f
33 changed files with 647 additions and 871 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/source/node_modules /source/node_modules
/demo/bin-debug /demo/bin-debug
/demo/bin-release

View File

@@ -77,7 +77,7 @@
* "calculateCanvasScaleFactor": //a function return canvas scale factor * "calculateCanvasScaleFactor": //a function return canvas scale factor
* } * }
**/ **/
egret.runEgret({ renderMode: "webgl", audioType: 0, calculateCanvasScaleFactor:function(context) { egret.runEgret({ renderMode: "canvas", audioType: 0, calculateCanvasScaleFactor:function(context) {
var backingStore = context.backingStorePixelRatio || var backingStore = context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio || context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio || context.mozBackingStorePixelRatio ||

View File

@@ -139,11 +139,10 @@ declare class DebugDefaults {
static verletParticle: number; static verletParticle: number;
static verletConstraintEdge: number; static verletConstraintEdge: number;
} }
declare abstract class Component { declare abstract class Component extends egret.DisplayObjectContainer {
entity: Entity; entity: Entity;
private _enabled; private _enabled;
updateInterval: number; updateInterval: number;
readonly transform: Transform;
enabled: boolean; enabled: boolean;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
readonly stage: egret.Stage; readonly stage: egret.Stage;
@@ -159,37 +158,27 @@ declare abstract class Component {
registerComponent(): void; registerComponent(): void;
deregisterComponent(): void; deregisterComponent(): void;
} }
declare class Entity { declare class Entity extends egret.DisplayObjectContainer {
private static _idGenerator; private static _idGenerator;
name: string; name: string;
readonly id: number; readonly id: number;
scene: Scene; scene: Scene;
readonly transform: Transform;
readonly components: ComponentList; readonly components: ComponentList;
private _updateOrder; private _updateOrder;
private _enabled; private _enabled;
_isDestoryed: boolean; _isDestoryed: boolean;
private _tag; private _tag;
componentBits: BitSet; componentBits: BitSet;
parent: Transform;
position: Vector2;
localPosition: Vector2;
rotation: number;
rotationDegrees: number;
localRotation: number;
localRotationDegrees: number;
scale: Vector2;
localScale: Vector2;
readonly worldInverseTransform: Matrix2D;
readonly localToWorldTransform: Matrix2D;
readonly worldToLocalTransform: Matrix2D;
readonly isDestoryed: boolean; readonly isDestoryed: boolean;
position: Vector2;
scale: Vector2;
enabled: boolean; enabled: boolean;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
tag: number; tag: number;
readonly stage: egret.Stage; readonly stage: egret.Stage;
constructor(name: string); constructor(name: string);
updateOrder: number; updateOrder: number;
roundPosition(): void;
setUpdateOrder(updateOrder: number): this; setUpdateOrder(updateOrder: number): this;
setTag(tag: number): Entity; setTag(tag: number): Entity;
attachToScene(newScene: Scene): void; attachToScene(newScene: Scene): void;
@@ -206,7 +195,7 @@ declare class Entity {
onAddedToScene(): void; onAddedToScene(): void;
onRemovedFromScene(): void; onRemovedFromScene(): void;
onTransformChanged(comp: ComponentTransform): void; onTransformChanged(comp: ComponentTransform): void;
destory(): void; destroy(): void;
} }
declare class Scene extends egret.DisplayObjectContainer { declare class Scene extends egret.DisplayObjectContainer {
camera: Camera; camera: Camera;
@@ -214,9 +203,6 @@ declare class Scene extends egret.DisplayObjectContainer {
readonly renderableComponents: RenderableComponentList; readonly renderableComponents: RenderableComponentList;
readonly content: ContentManager; readonly content: ContentManager;
enablePostProcessing: boolean; enablePostProcessing: boolean;
private _projectionMatrix;
private _transformMatrix;
private _matrixTransformMatrix;
private _renderers; private _renderers;
private _postProcessors; private _postProcessors;
private _didSceneBegin; private _didSceneBegin;
@@ -324,14 +310,12 @@ declare class Camera extends Component {
private _origin; private _origin;
private _transformMatrix; private _transformMatrix;
private _inverseTransformMatrix; private _inverseTransformMatrix;
private _projectionMatrix;
private _minimumZoom; private _minimumZoom;
private _maximumZoom; private _maximumZoom;
private _areMatrixesDirty; private _areMatrixesDirty;
private _inset; private _inset;
private _bounds; private _bounds;
private _areBoundsDirty; private _areBoundsDirty;
private _isProjectionMatrixDirty;
readonly bounds: Rectangle; readonly bounds: Rectangle;
zoom: number; zoom: number;
minimumZoom: number; minimumZoom: number;
@@ -432,14 +416,10 @@ declare class Sprite {
constructor(texture: egret.Texture, sourceRect?: Rectangle, origin?: Vector2); constructor(texture: egret.Texture, sourceRect?: Rectangle, origin?: Vector2);
} }
declare class SpriteRenderer extends RenderableComponent { declare class SpriteRenderer extends RenderableComponent {
private _sprite;
private _origin; private _origin;
private _bitmap;
readonly bounds: Rectangle;
sprite: Sprite;
setSprite(sprite: Sprite): SpriteRenderer;
origin: Vector2; origin: Vector2;
setOrigin(origin: Vector2): this; setOrigin(origin: Vector2): this;
setSprite(sprite: Sprite): void;
setColor(color: number): void; setColor(color: number): void;
isVisibleFromCamera(camera: Camera): boolean; isVisibleFromCamera(camera: Camera): boolean;
render(camera: Camera): void; render(camera: Camera): void;
@@ -619,6 +599,39 @@ declare class Time {
private static _lastTime; private static _lastTime;
static update(currentTime: number): void; static update(currentTime: number): void;
} }
declare class GraphicsCapabilities {
supportsTextureFilterAnisotropic: boolean;
supportsNonPowerOfTwo: boolean;
supportsDepth24: boolean;
supportsPackedDepthStencil: boolean;
supportsDepthNonLinear: boolean;
supportsTextureMaxLevel: boolean;
supportsS3tc: boolean;
supportsDxt1: boolean;
supportsPvrtc: boolean;
supportsAtitc: boolean;
supportsFramebufferObjectARB: boolean;
initialize(device: GraphicsDevice): void;
private platformInitialize;
}
declare class GraphicsDevice {
private viewport;
graphicsCapabilities: GraphicsCapabilities;
constructor();
}
declare class Viewport {
private _x;
private _y;
private _width;
private _height;
private _minDepth;
private _maxDepth;
readonly aspectRatio: number;
bounds: Rectangle;
constructor(x: number, y: number, width: number, height: number);
}
declare abstract class GraphicsResource {
}
declare class GaussianBlurEffect extends egret.CustomFilter { declare class GaussianBlurEffect extends egret.CustomFilter {
private static blur_frag; private static blur_frag;
constructor(); constructor();
@@ -767,6 +780,7 @@ declare class Matrix2D {
static createTranslation(xPosition: number, yPosition: number, result?: Matrix2D): Matrix2D; static createTranslation(xPosition: number, yPosition: number, result?: Matrix2D): Matrix2D;
static createRotation(radians: number, result?: Matrix2D): Matrix2D; static createRotation(radians: number, result?: Matrix2D): Matrix2D;
static createScale(xScale: number, yScale: number, result?: Matrix2D): Matrix2D; static createScale(xScale: number, yScale: number, result?: Matrix2D): Matrix2D;
toEgretMatrix(): egret.Matrix;
} }
declare class Rectangle { declare class Rectangle {
x: number; x: number;
@@ -823,6 +837,12 @@ declare class Vector2 {
static distance(value1: Vector2, value2: Vector2): number; static distance(value1: Vector2, value2: Vector2): number;
static negate(value: Vector2): Vector2; static negate(value: Vector2): Vector2;
} }
declare class Vector3 {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z: number);
}
declare class ColliderTriggerHelper { declare class ColliderTriggerHelper {
private _entity; private _entity;
private _activeTriggerIntersections; private _activeTriggerIntersections;
@@ -1060,10 +1080,5 @@ declare class Vector2Ext {
static transformA(sourceArray: Vector2[], sourceIndex: number, matrix: Matrix2D, destinationArray: Vector2[], destinationIndex: number, length: number): void; static transformA(sourceArray: Vector2[], sourceIndex: number, matrix: Matrix2D, destinationArray: Vector2[], destinationIndex: number, length: number): void;
static transformR(position: Vector2, matrix: Matrix2D): Vector2; static transformR(position: Vector2, matrix: Matrix2D): Vector2;
static transform(sourceArray: Vector2[], matrix: Matrix2D, destinationArray: Vector2[]): void; static transform(sourceArray: Vector2[], matrix: Matrix2D, destinationArray: Vector2[]): void;
} static round(vec: Vector2): Vector2;
declare class WebGLUtils {
static getWebGL(): WebGLRenderingContext;
static drawUserIndexPrimitives<T>(primitiveType: number, vertexData: T[], vertexOffset: number, numVertices: number, indexData: number[], indexOffset: number, primitiveCount: number): void;
private static getElementCountArray;
static checkGLError(): void;
} }

View File

@@ -737,18 +737,14 @@ var DebugDefaults = (function () {
DebugDefaults.verletConstraintEdge = 0x433E36; DebugDefaults.verletConstraintEdge = 0x433E36;
return DebugDefaults; return DebugDefaults;
}()); }());
var Component = (function () { var Component = (function (_super) {
__extends(Component, _super);
function Component() { function Component() {
this._enabled = true; var _this = _super !== null && _super.apply(this, arguments) || this;
this.updateInterval = 1; _this._enabled = true;
_this.updateInterval = 1;
return _this;
} }
Object.defineProperty(Component.prototype, "transform", {
get: function () {
return this.entity.transform;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Component.prototype, "enabled", { Object.defineProperty(Component.prototype, "enabled", {
get: function () { get: function () {
return this.entity ? this.entity.enabled && this._enabled : this._enabled; return this.entity ? this.entity.enabled && this._enabled : this._enabled;
@@ -814,132 +810,45 @@ var Component = (function () {
this.entity.scene.entityProcessors.onComponentRemoved(this.entity); this.entity.scene.entityProcessors.onComponentRemoved(this.entity);
}; };
return Component; return Component;
}()); }(egret.DisplayObjectContainer));
var Entity = (function () { var Entity = (function (_super) {
__extends(Entity, _super);
function Entity(name) { function Entity(name) {
this._updateOrder = 0; var _this = _super.call(this) || this;
this._enabled = true; _this._updateOrder = 0;
this._tag = 0; _this._enabled = true;
this.name = name; _this._tag = 0;
this.transform = new Transform(this); _this.name = name;
this.components = new ComponentList(this); _this.components = new ComponentList(_this);
this.id = Entity._idGenerator++; _this.id = Entity._idGenerator++;
this.componentBits = new BitSet(); _this.componentBits = new BitSet();
return _this;
} }
Object.defineProperty(Entity.prototype, "parent", { Object.defineProperty(Entity.prototype, "isDestoryed", {
get: function () { get: function () {
return this.transform.parent; return this._isDestoryed;
},
set: function (value) {
this.transform.setParent(value);
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Entity.prototype, "position", { Object.defineProperty(Entity.prototype, "position", {
get: function () { get: function () {
return this.transform.position; return new Vector2(this.x, this.y);
}, },
set: function (value) { set: function (value) {
this.transform.setPosition(value); this.x = value.x;
}, this.y = value.y;
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "localPosition", {
get: function () {
return this.transform.localPosition;
},
set: function (value) {
this.transform.setLocalPosition(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "rotation", {
get: function () {
return this.transform.rotation;
},
set: function (value) {
this.transform.setRotation(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "rotationDegrees", {
get: function () {
return this.transform.rotationDegrees;
},
set: function (value) {
this.transform.setRotationDegrees(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "localRotation", {
get: function () {
return this.transform.localRotation;
},
set: function (value) {
this.transform.setLocalRotation(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "localRotationDegrees", {
get: function () {
return this.transform.localRotationDegrees;
},
set: function (value) {
this.transform.setLocalRotationDegrees(value);
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Entity.prototype, "scale", { Object.defineProperty(Entity.prototype, "scale", {
get: function () { get: function () {
return this.transform.scale; return new Vector2(this.scaleX, this.scaleY);
}, },
set: function (value) { set: function (value) {
this.transform.setScale(value); this.scaleX = value.x;
}, this.scaleY = value.y;
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "localScale", {
get: function () {
return this.transform.scale;
},
set: function (value) {
this.transform.setScale(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "worldInverseTransform", {
get: function () {
return this.transform.worldInverseTransform;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "localToWorldTransform", {
get: function () {
return this.transform.localToWorldTransform;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "worldToLocalTransform", {
get: function () {
return this.transform.worldToLocalTransform;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "isDestoryed", {
get: function () {
return this._isDestoryed;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -989,6 +898,9 @@ var Entity = (function () {
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Entity.prototype.roundPosition = function () {
this.position = Vector2Ext.round(this.position);
};
Entity.prototype.setUpdateOrder = function (updateOrder) { Entity.prototype.setUpdateOrder = function (updateOrder) {
if (this._updateOrder != updateOrder) { if (this._updateOrder != updateOrder) {
this._updateOrder = updateOrder; this._updateOrder = updateOrder;
@@ -1013,19 +925,20 @@ var Entity = (function () {
this.scene = newScene; this.scene = newScene;
newScene.entities.add(this); newScene.entities.add(this);
this.components.registerAllComponents(); this.components.registerAllComponents();
for (var i = 0; i < this.transform.childCount; i++) { for (var i = 0; i < this.numChildren; i++) {
this.transform.getChild(i).entity.attachToScene(newScene); this.getChildAt(i).entity.attachToScene(newScene);
} }
}; };
Entity.prototype.detachFromScene = function () { Entity.prototype.detachFromScene = function () {
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.components.deregisterAllComponents(); this.components.deregisterAllComponents();
for (var i = 0; i < this.transform.childCount; i++) for (var i = 0; i < this.numChildren; i++)
this.transform.getChild(i).entity.detachFromScene(); this.getChildAt(i).entity.detachFromScene();
}; };
Entity.prototype.addComponent = function (component) { Entity.prototype.addComponent = function (component) {
component.entity = this; component.entity = this;
this.components.add(component); this.components.add(component);
this.addChild(component);
component.initialize(); component.initialize();
return component; return component;
}; };
@@ -1073,17 +986,17 @@ var Entity = (function () {
Entity.prototype.onTransformChanged = function (comp) { Entity.prototype.onTransformChanged = function (comp) {
this.components.onEntityTransformChanged(comp); this.components.onEntityTransformChanged(comp);
}; };
Entity.prototype.destory = function () { Entity.prototype.destroy = function () {
this._isDestoryed = true; this._isDestoryed = true;
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.transform.parent = null; this.removeChildren();
for (var i = this.transform.childCount - 1; i >= 0; i--) { for (var i = this.numChildren - 1; i >= 0; i--) {
var child = this.transform.getChild(i); var child = this.getChildAt(i);
child.entity.destory(); child.entity.destroy();
} }
}; };
return Entity; return Entity;
}()); }(egret.DisplayObjectContainer));
var Scene = (function (_super) { var Scene = (function (_super) {
__extends(Scene, _super); __extends(Scene, _super);
function Scene() { function Scene() {
@@ -1091,7 +1004,6 @@ var Scene = (function (_super) {
_this.enablePostProcessing = true; _this.enablePostProcessing = true;
_this._renderers = []; _this._renderers = [];
_this._postProcessors = []; _this._postProcessors = [];
_this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
_this.entityProcessors = new EntityProcessorList(); _this.entityProcessors = new EntityProcessorList();
_this.renderableComponents = new RenderableComponentList(); _this.renderableComponents = new RenderableComponentList();
_this.entities = new EntityList(_this); _this.entities = new EntityList(_this);
@@ -1102,19 +1014,20 @@ var Scene = (function (_super) {
} }
Scene.prototype.createEntity = function (name) { Scene.prototype.createEntity = function (name) {
var entity = new Entity(name); var entity = new Entity(name);
entity.transform.position = new Vector2(0, 0); entity.position = new Vector2(0, 0);
return this.addEntity(entity); return this.addEntity(entity);
}; };
Scene.prototype.addEntity = function (entity) { Scene.prototype.addEntity = function (entity) {
this.entities.add(entity); this.entities.add(entity);
entity.scene = this; entity.scene = this;
for (var i = 0; i < entity.transform.childCount; i++) this.addChild(entity);
this.addEntity(entity.transform.getChild(i).entity); for (var i = 0; i < entity.numChildren; i++)
this.addEntity(entity.getChildAt(i).entity);
return entity; return entity;
}; };
Scene.prototype.destroyAllEntities = function () { Scene.prototype.destroyAllEntities = function () {
for (var i = 0; i < this.entities.count; i++) { for (var i = 0; i < this.entities.count; i++) {
this.entities.buffer[i].destory(); this.entities.buffer[i].destroy();
} }
}; };
Scene.prototype.findEntity = function (name) { Scene.prototype.findEntity = function (name) {
@@ -1178,6 +1091,7 @@ var Scene = (function (_super) {
this._postProcessors[i].unload(); this._postProcessors[i].unload();
} }
this.entities.removeAllEntities(); this.entities.removeAllEntities();
this.removeChildren();
Physics.clear(); Physics.clear();
this.camera.destory(); this.camera.destory();
this.camera = null; this.camera = null;
@@ -1275,7 +1189,7 @@ var SceneManager = (function () {
SceneManager._scene.end(); SceneManager._scene.end();
for (var i = 0; i < SceneManager._scene.entities.buffer.length; i++) { for (var i = 0; i < SceneManager._scene.entities.buffer.length; i++) {
var entity = SceneManager._scene.entities.buffer[i]; var entity = SceneManager._scene.entities.buffer[i];
entity.destory(); entity.destroy();
} }
SceneManager._scene = SceneManager._nextScene; SceneManager._scene = SceneManager._nextScene;
SceneManager._nextScene = null; SceneManager._nextScene = null;
@@ -1632,14 +1546,12 @@ var Camera = (function (_super) {
_this._origin = Vector2.zero; _this._origin = Vector2.zero;
_this._transformMatrix = new Matrix2D(); _this._transformMatrix = new Matrix2D();
_this._inverseTransformMatrix = new Matrix2D(); _this._inverseTransformMatrix = new Matrix2D();
_this._projectionMatrix = new Matrix2D();
_this._minimumZoom = 0.3; _this._minimumZoom = 0.3;
_this._maximumZoom = 3; _this._maximumZoom = 3;
_this._areMatrixesDirty = true; _this._areMatrixesDirty = true;
_this._inset = new CameraInset(); _this._inset = new CameraInset();
_this._bounds = new Rectangle(); _this._bounds = new Rectangle();
_this._areBoundsDirty = true; _this._areBoundsDirty = true;
_this._isProjectionMatrixDirty = true;
_this.setZoom(0); _this.setZoom(0);
return _this; return _this;
} }
@@ -1651,7 +1563,7 @@ var Camera = (function (_super) {
var stage = this.stage; var stage = this.stage;
var topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top)); var topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top));
var bottomRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, stage.stageHeight - this._inset.bottom)); var bottomRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, stage.stageHeight - this._inset.bottom));
if (this.entity.transform.rotation != 0) { if (this.entity.rotation != 0) {
var topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top)); var topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
var bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom)); var bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom));
var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x); var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
@@ -1723,10 +1635,10 @@ var Camera = (function (_super) {
}); });
Object.defineProperty(Camera.prototype, "position", { Object.defineProperty(Camera.prototype, "position", {
get: function () { get: function () {
return this.entity.transform.position; return this.entity.position;
}, },
set: function (value) { set: function (value) {
this.entity.transform.position = value; this.entity.position = value;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -1750,10 +1662,9 @@ var Camera = (function (_super) {
configurable: true configurable: true
}); });
Camera.prototype.onSceneSizeChanged = function (newWidth, newHeight) { Camera.prototype.onSceneSizeChanged = function (newWidth, newHeight) {
this._isProjectionMatrixDirty = true;
var oldOrigin = this._origin; var oldOrigin = this._origin;
this.origin = new Vector2(newWidth / 2, newHeight / 2); this.origin = new Vector2(newWidth / 2, newHeight / 2);
this.entity.transform.position = Vector2.add(this.entity.transform.position, Vector2.subtract(this._origin, oldOrigin)); this.entity.position = Vector2.add(this.entity.position, Vector2.subtract(this._origin, oldOrigin));
}; };
Camera.prototype.setMinimumZoom = function (minZoom) { Camera.prototype.setMinimumZoom = function (minZoom) {
if (this._zoom < minZoom) if (this._zoom < minZoom)
@@ -1782,7 +1693,7 @@ var Camera = (function (_super) {
return this; return this;
}; };
Camera.prototype.setPosition = function (position) { Camera.prototype.setPosition = function (position) {
this.entity.transform.setPosition(position); this.entity.position = position;
return this; return this;
}; };
Camera.prototype.forceMatrixUpdate = function () { Camera.prototype.forceMatrixUpdate = function () {
@@ -1792,12 +1703,12 @@ var Camera = (function (_super) {
if (!this._areMatrixesDirty) if (!this._areMatrixesDirty)
return; return;
var tempMat; var tempMat;
this._transformMatrix = Matrix2D.createTranslation(-this.entity.transform.position.x, -this.entity.transform.position.y); this._transformMatrix = Matrix2D.createTranslation(-this.entity.position.x, -this.entity.position.y);
if (this._zoom != 1) { if (this._zoom != 1) {
tempMat = Matrix2D.createScale(this._zoom, this._zoom); tempMat = Matrix2D.createScale(this._zoom, this._zoom);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
} }
if (this.entity.transform.rotation != 0) { if (this.entity.rotation != 0) {
tempMat = Matrix2D.createRotation(this.entity.rotation); tempMat = Matrix2D.createRotation(this.entity.rotation);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
} }
@@ -1877,10 +1788,10 @@ var FollowCamera = (function (_super) {
if (this._targetEntity) if (this._targetEntity)
this.updateFollow(); this.updateFollow();
this.camera.position = Vector2.lerp(this.camera.position, Vector2.add(this.camera.position, this._desiredPositionDelta), this.followLerp); this.camera.position = Vector2.lerp(this.camera.position, Vector2.add(this.camera.position, this._desiredPositionDelta), this.followLerp);
this.camera.entity.transform.roundPosition(); this.camera.entity.roundPosition();
if (this.mapLockEnabled) { if (this.mapLockEnabled) {
this.camera.position = this.clampToMapSize(this.camera.position); this.camera.position = this.clampToMapSize(this.camera.position);
this.camera.entity.transform.roundPosition(); this.camera.entity.roundPosition();
} }
}; };
FollowCamera.prototype.clampToMapSize = function (position) { FollowCamera.prototype.clampToMapSize = function (position) {
@@ -1891,8 +1802,8 @@ var FollowCamera = (function (_super) {
FollowCamera.prototype.updateFollow = function () { FollowCamera.prototype.updateFollow = function () {
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0; this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
if (this._cameraStyle == CameraStyle.lockOn) { if (this._cameraStyle == CameraStyle.lockOn) {
var targetX = this._targetEntity.transform.position.x; var targetX = this._targetEntity.position.x;
var targetY = this._targetEntity.transform.position.y; var targetY = this._targetEntity.position.y;
if (this._worldSpaceDeadZone.x > targetX) if (this._worldSpaceDeadZone.x > targetX)
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x; this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
else if (this._worldSpaceDeadZone.x < targetX) else if (this._worldSpaceDeadZone.x < targetX)
@@ -2026,11 +1937,7 @@ var RenderableComponent = (function (_super) {
}); });
Object.defineProperty(RenderableComponent.prototype, "bounds", { Object.defineProperty(RenderableComponent.prototype, "bounds", {
get: function () { get: function () {
if (this._areBoundsDirty) { return new Rectangle(this.getBounds().x, this.getBounds().y, this.getBounds().width, this.getBounds().height);
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new Vector2(0, 0), this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height);
this._areBoundsDirty = false;
}
return this._bounds;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -2084,37 +1991,6 @@ var SpriteRenderer = (function (_super) {
function SpriteRenderer() { function SpriteRenderer() {
return _super !== null && _super.apply(this, arguments) || this; return _super !== null && _super.apply(this, arguments) || this;
} }
Object.defineProperty(SpriteRenderer.prototype, "bounds", {
get: function () {
if (this._areBoundsDirty) {
if (this._sprite) {
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin, this.entity.transform.scale, this.entity.transform.rotation, this._sprite.texture2D.textureWidth, this._sprite.texture2D.textureHeight);
this._areBoundsDirty = false;
}
}
return this._bounds;
},
enumerable: true,
configurable: true
});
Object.defineProperty(SpriteRenderer.prototype, "sprite", {
get: function () {
return this._sprite;
},
set: function (value) {
this.setSprite(value);
},
enumerable: true,
configurable: true
});
SpriteRenderer.prototype.setSprite = function (sprite) {
this._sprite = sprite;
if (this._sprite)
this._origin = sprite.origin;
this._bitmap = new egret.Bitmap(sprite.texture2D);
this.scene.addChild(this._bitmap);
return this;
};
Object.defineProperty(SpriteRenderer.prototype, "origin", { Object.defineProperty(SpriteRenderer.prototype, "origin", {
get: function () { get: function () {
return this._origin; return this._origin;
@@ -2132,6 +2008,10 @@ var SpriteRenderer = (function (_super) {
} }
return this; return this;
}; };
SpriteRenderer.prototype.setSprite = function (sprite) {
this.removeChildren();
this.addChild(new egret.Bitmap(sprite.texture2D));
};
SpriteRenderer.prototype.setColor = function (color) { SpriteRenderer.prototype.setColor = function (color) {
var colorMatrix = [ var colorMatrix = [
1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
@@ -2143,28 +2023,19 @@ var SpriteRenderer = (function (_super) {
colorMatrix[6] = Math.floor(color / 256 % 256) / 255; colorMatrix[6] = Math.floor(color / 256 % 256) / 255;
colorMatrix[12] = color % 256 / 255; colorMatrix[12] = color % 256 / 255;
var colorFilter = new egret.ColorMatrixFilter(colorMatrix); var colorFilter = new egret.ColorMatrixFilter(colorMatrix);
this._bitmap.filters = [colorFilter]; this.filters = [colorFilter];
}; };
SpriteRenderer.prototype.isVisibleFromCamera = function (camera) { SpriteRenderer.prototype.isVisibleFromCamera = function (camera) {
var topLeft = camera.screenToWorldPoint(new Vector2(0, 0)); var topLeft = camera.screenToWorldPoint(new Vector2(0, 0));
this.isVisible = new Rectangle(topLeft.x, topLeft.y, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds); this.isVisible = new Rectangle(topLeft.x, topLeft.y, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds);
this._bitmap.visible = this.isVisible; this.visible = this.isVisible;
return this.isVisible; return this.isVisible;
}; };
SpriteRenderer.prototype.render = function (camera) { SpriteRenderer.prototype.render = function (camera) {
if (!this.sprite)
return;
this._bitmap.x = this.entity.transform.position.x - camera.transform.position.x + camera.origin.x;
this._bitmap.y = this.entity.transform.position.y - camera.transform.position.y + camera.origin.y;
this._bitmap.rotation = this.entity.transform.rotation + camera.transform.rotation;
this._bitmap.anchorOffsetX = this._origin.x;
this._bitmap.anchorOffsetY = this._origin.y;
this._bitmap.scaleX = this.entity.transform.scale.x * camera.transform.scale.x;
this._bitmap.scaleY = this.entity.transform.scale.y * camera.transform.scale.y;
}; };
SpriteRenderer.prototype.onRemovedFromEntity = function () { SpriteRenderer.prototype.onRemovedFromEntity = function () {
if (this._bitmap) if (this.parent)
this.scene.removeChild(this._bitmap); this.parent.removeChild(this);
}; };
return SpriteRenderer; return SpriteRenderer;
}(RenderableComponent)); }(RenderableComponent));
@@ -2207,7 +2078,7 @@ var Mover = (function (_super) {
return collisionResult; return collisionResult;
}; };
Mover.prototype.applyMovement = function (motion) { Mover.prototype.applyMovement = function (motion) {
this.entity.transform.position = Vector2.add(this.entity.transform.position, motion); this.entity.position = Vector2.add(this.entity.position, motion);
if (this._triggerHelper) if (this._triggerHelper)
this._triggerHelper.update(); this._triggerHelper.update();
}; };
@@ -2292,13 +2163,13 @@ var Collider = (function (_super) {
var renderable = this.entity.getComponent(RenderableComponent); var renderable = this.entity.getComponent(RenderableComponent);
if (renderable) { if (renderable) {
var renderbaleBounds = renderable.bounds; var renderbaleBounds = renderable.bounds;
var width = renderbaleBounds.width / this.entity.transform.scale.x; var width = renderbaleBounds.width / this.entity.scale.x;
var height = renderbaleBounds.height / this.entity.transform.scale.y; var height = renderbaleBounds.height / this.entity.scale.y;
if (this instanceof BoxCollider) { if (this instanceof BoxCollider) {
var boxCollider = this; var boxCollider = this;
boxCollider.width = width; boxCollider.width = width;
boxCollider.height = height; boxCollider.height = height;
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.transform.position); this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.position);
} }
} }
} }
@@ -2672,6 +2543,8 @@ var ComponentList = (function () {
} }
}; };
ComponentList.prototype.handleRemove = function (component) { ComponentList.prototype.handleRemove = function (component) {
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
component.onRemovedFromEntity(); component.onRemovedFromEntity();
@@ -3004,6 +2877,74 @@ var Time = (function () {
Time._lastTime = 0; Time._lastTime = 0;
return Time; return Time;
}()); }());
var GraphicsCapabilities = (function () {
function GraphicsCapabilities() {
}
GraphicsCapabilities.prototype.initialize = function (device) {
this.platformInitialize(device);
};
GraphicsCapabilities.prototype.platformInitialize = function (device) {
var gl = new egret.sys.RenderBuffer().context.getInstance();
this.supportsNonPowerOfTwo = false;
this.supportsTextureFilterAnisotropic = gl.getExtension("EXT_texture_filter_anisotropic") != null;
this.supportsDepth24 = true;
this.supportsPackedDepthStencil = true;
this.supportsDepthNonLinear = false;
this.supportsTextureMaxLevel = true;
this.supportsS3tc = gl.getExtension("WEBGL_compressed_texture_s3tc") != null ||
gl.getExtension("WEBGL_compressed_texture_s3tc_srgb") != null;
this.supportsDxt1 = this.supportsS3tc;
this.supportsPvrtc = false;
this.supportsAtitc = gl.getExtension("WEBGL_compressed_texture_astc") != null;
this.supportsFramebufferObjectARB = false;
};
return GraphicsCapabilities;
}());
var GraphicsDevice = (function () {
function GraphicsDevice() {
this.graphicsCapabilities = new GraphicsCapabilities();
this.graphicsCapabilities.initialize(this);
}
return GraphicsDevice;
}());
var Viewport = (function () {
function Viewport(x, y, width, height) {
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._minDepth = 0;
this._maxDepth = 1;
}
Object.defineProperty(Viewport.prototype, "aspectRatio", {
get: function () {
if ((this._height != 0) && (this._width != 0))
return (this._width / this._height);
return 0;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Viewport.prototype, "bounds", {
get: function () {
return new Rectangle(this._x, this._y, this._width, this._height);
},
set: function (value) {
this._x = value.x;
this._y = value.y;
this._width = value.width;
this._height = value.height;
},
enumerable: true,
configurable: true
});
return Viewport;
}());
var GraphicsResource = (function () {
function GraphicsResource() {
}
return GraphicsResource;
}());
var GaussianBlurEffect = (function (_super) { var GaussianBlurEffect = (function (_super) {
__extends(GaussianBlurEffect, _super); __extends(GaussianBlurEffect, _super);
function GaussianBlurEffect() { function GaussianBlurEffect() {
@@ -3152,11 +3093,6 @@ var Renderer = (function () {
} }
Renderer.prototype.onAddedToScene = function (scene) { }; Renderer.prototype.onAddedToScene = function (scene) { };
Renderer.prototype.beginRender = function (cam) { Renderer.prototype.beginRender = function (cam) {
cam.transform.updateTransform();
var entities = SceneManager.scene.entities;
for (var i = 0; i < entities.buffer.length; i++) {
entities.buffer[i].transform.updateTransform();
}
}; };
Renderer.prototype.unload = function () { }; Renderer.prototype.unload = function () { };
Renderer.prototype.renderAfterStateCheck = function (renderable, cam) { Renderer.prototype.renderAfterStateCheck = function (renderable, cam) {
@@ -3203,7 +3139,7 @@ var PolyLight = (function (_super) {
Object.defineProperty(PolyLight.prototype, "bounds", { Object.defineProperty(PolyLight.prototype, "bounds", {
get: function () { get: function () {
if (this._areBoundsDirty) { if (this._areBoundsDirty) {
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new Vector2(this._radius), Vector2.one, 0, this._radius * 2, this._radius * 2); this._bounds.calculateBounds(this.entity.position, this._localOffset, new Vector2(this._radius), Vector2.one, 0, this._radius * 2, this._radius * 2);
this._areBoundsDirty = false; this._areBoundsDirty = false;
} }
return this._bounds; return this._bounds;
@@ -3602,6 +3538,10 @@ var Matrix2D = (function () {
result.m32 = 0; result.m32 = 0;
return result; return result;
}; };
Matrix2D.prototype.toEgretMatrix = function () {
var matrix = new egret.Matrix(this.m11, this.m12, this.m21, this.m22, this.m31, this.m32);
return matrix;
};
Matrix2D._identity = new Matrix2D(1, 0, 0, 1, 0, 0); Matrix2D._identity = new Matrix2D(1, 0, 0, 1, 0, 0);
return Matrix2D; return Matrix2D;
}()); }());
@@ -3901,6 +3841,14 @@ var Vector2 = (function () {
Vector2.zeroVector2 = new Vector2(0, 0); Vector2.zeroVector2 = new Vector2(0, 0);
return Vector2; return Vector2;
}()); }());
var Vector3 = (function () {
function Vector3(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
return Vector3;
}());
var ColliderTriggerHelper = (function () { var ColliderTriggerHelper = (function () {
function ColliderTriggerHelper(entity) { function ColliderTriggerHelper(entity) {
this._activeTriggerIntersections = []; this._activeTriggerIntersections = [];
@@ -4292,28 +4240,28 @@ var Polygon = (function (_super) {
var hasUnitScale = true; var hasUnitScale = true;
var tempMat = void 0; var tempMat = void 0;
var combinedMatrix = Matrix2D.createTranslation(-this._polygonCenter.x, -this._polygonCenter.y); var combinedMatrix = Matrix2D.createTranslation(-this._polygonCenter.x, -this._polygonCenter.y);
if (collider.entity.transform.scale != Vector2.one) { if (collider.entity.scale != Vector2.one) {
tempMat = Matrix2D.createScale(collider.entity.transform.scale.x, collider.entity.transform.scale.y); tempMat = Matrix2D.createScale(collider.entity.scale.x, collider.entity.scale.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
hasUnitScale = false; hasUnitScale = false;
var scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.transform.scale); var scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale);
this.center = scaledOffset; this.center = scaledOffset;
} }
if (collider.entity.transform.rotation != 0) { if (collider.entity.rotation != 0) {
tempMat = Matrix2D.createRotation(collider.entity.transform.rotation); tempMat = Matrix2D.createRotation(collider.entity.rotation);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg; var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.transform.scale)).length(); var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, collider.entity.transform.rotationDegrees + offsetAngle); this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle);
} }
tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y); tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points); Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points);
this.isUnrotated = collider.entity.transform.rotation == 0; this.isUnrotated = collider.entity.rotation == 0;
if (collider._isRotationDirty) if (collider._isRotationDirty)
this._areEdgeNormalsDirty = true; this._areEdgeNormalsDirty = true;
} }
this.position = Vector2.add(collider.entity.transform.position, this.center); this.position = Vector2.add(collider.entity.position, this.center);
this.bounds = Rectangle.rectEncompassingPoints(this.points); this.bounds = Rectangle.rectEncompassingPoints(this.points);
this.bounds.location = Vector2.add(this.bounds.location, this.position); this.bounds.location = Vector2.add(this.bounds.location, this.position);
}; };
@@ -4382,17 +4330,17 @@ var Circle = (function (_super) {
Circle.prototype.recalculateBounds = function (collider) { Circle.prototype.recalculateBounds = function (collider) {
this.center = collider.localOffset; this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotationWithTransform) { if (collider.shouldColliderScaleAndRotationWithTransform) {
var scale = collider.entity.transform.scale; var scale = collider.entity.scale;
var hasUnitScale = scale.x == 1 && scale.y == 1; var hasUnitScale = scale.x == 1 && scale.y == 1;
var maxScale = Math.max(scale.x, scale.y); var maxScale = Math.max(scale.x, scale.y);
this.radius = this._originalRadius * maxScale; this.radius = this._originalRadius * maxScale;
if (collider.entity.transform.rotation != 0) { if (collider.entity.rotation != 0) {
var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg; var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.transform.scale)).length(); var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, collider.entity.transform.rotationDegrees + offsetAngle); this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle);
} }
} }
this.position = Vector2.add(collider.entity.transform.position, this.center); this.position = Vector2.add(collider.entity.position, this.center);
this.bounds = new Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2); this.bounds = new Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2);
}; };
Circle.prototype.overlaps = function (other) { Circle.prototype.overlaps = function (other) {
@@ -5166,45 +5114,8 @@ var Vector2Ext = (function () {
Vector2Ext.transform = function (sourceArray, matrix, destinationArray) { Vector2Ext.transform = function (sourceArray, matrix, destinationArray) {
this.transformA(sourceArray, 0, matrix, destinationArray, 0, sourceArray.length); this.transformA(sourceArray, 0, matrix, destinationArray, 0, sourceArray.length);
}; };
Vector2Ext.round = function (vec) {
return new Vector2(Math.round(vec.x), Math.round(vec.y));
};
return Vector2Ext; return Vector2Ext;
}()); }());
var WebGLUtils = (function () {
function WebGLUtils() {
}
WebGLUtils.getWebGL = function () {
if (egret.WebGLUtils.checkCanUseWebGL())
return document.querySelector("canvas").getContext("webgl");
throw new Error("cannot get webgl");
};
WebGLUtils.drawUserIndexPrimitives = function (primitiveType, vertexData, vertexOffset, numVertices, indexData, indexOffset, primitiveCount) {
var GL = this.getWebGL();
GL.bindBuffer(GL.ARRAY_BUFFER, 0);
this.checkGLError();
GL.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, 0);
this.checkGLError();
GL.drawElements(primitiveType, this.getElementCountArray(primitiveType, primitiveCount), GL.UNSIGNED_SHORT, indexOffset * 2);
this.checkGLError();
};
WebGLUtils.getElementCountArray = function (primitiveType, primitiveCount) {
var GL = this.getWebGL();
switch (primitiveType) {
case GL.LINES:
return primitiveCount * 2;
case GL.LINE_STRIP:
return primitiveCount + 1;
case GL.TRIANGLES:
return primitiveCount * 3;
case GL.TRIANGLE_STRIP:
return primitiveCount + 2;
}
throw new Error("not support");
};
WebGLUtils.checkGLError = function () {
var GL = this.getWebGL();
var error = GL.getError();
if (error != GL.NO_ERROR) {
throw new Error("GL.GetError() returned" + error);
}
};
return WebGLUtils;
}());

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,5 @@
class MainScene extends Scene { class MainScene extends Scene {
constructor(){ constructor() {
super(); super();
// this.addEntityProcessor(new SpawnerSystem(new Matcher())); // this.addEntityProcessor(new SpawnerSystem(new Matcher()));
@@ -8,45 +8,34 @@ class MainScene extends Scene {
this.breadthfirstTest(); this.breadthfirstTest();
} }
public onStart(){ public onStart() {
this.content.load("http://www.hyuan.org/123.jpeg", false).then((data)=>{ this.content.load("http://www.hyuan.org/123.jpeg", false).then((data) => {
console.log(data); console.log(data);
let bgSprite = new Sprite(data);
let bg = this.createEntity("bg");
bg.position = new Vector2(0, 0);
bg.addComponent(new SpriteRenderer()).setSprite(bgSprite);
for (let i = 0; i < 20; i++) {
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
let player2 = this.createEntity("player2");
player2.addComponent(new SpriteRenderer()).setSprite(sprite);
player2.position = new Vector2(Math.random() * 100 * i, Math.random() * 100 * i);
player2.addComponent(new BoxCollider());
}
let button = new eui.Button();
button.label = "切换场景";
this.addChild(button);
button.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
SceneManager.startSceneTransition(new FadeTransition(() => {
return new MainScene();
}));
}, this);
}); });
this.camera.setZoom(0.5);
let bgSprite = new Sprite(RES.getRes("bg_jpg"));
let bg = this.createEntity("bg");
bg.position = new Vector2(0, 0);
bg.addComponent(new SpriteRenderer()).setSprite(bgSprite);
for (let i = 0; i < 20; i ++){
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
let player2 = this.createEntity("player2");
player2.addComponent(new SpriteRenderer()).setSprite(sprite);
player2.position = new Vector2(Math.random() * 100 * i, Math.random() * 100 * i);
player2.addComponent(new BoxCollider());
}
let button = new eui.Button();
button.label = "切换场景";
this.addChild(button);
button.addEventListener(egret.TouchEvent.TOUCH_TAP, ()=>{
SceneManager.startSceneTransition(new WindTransition(()=>{
return new MainScene();
}));
}, this);
let cancel = new eui.Button();
cancel.label = "打开高斯模糊";
cancel.y = 100;
this.addChild(cancel);
cancel.addEventListener(egret.TouchEvent.TOUCH_TAP, ()=>{
this.addPostProcessor(new GaussianBlurPostProcessor());
}, this);
} }
public breadthfirstTest(){ public breadthfirstTest() {
let graph = new UnweightedGraph<string>(); let graph = new UnweightedGraph<string>();
graph.addEdgesForNode("a", ["b"]); // a->b graph.addEdgesForNode("a", ["b"]); // a->b
@@ -60,7 +49,7 @@ class MainScene extends Scene {
console.log(path); console.log(path);
} }
public dijkstraTest(){ public dijkstraTest() {
let graph = new WeightedGridGraph(20, 20); let graph = new WeightedGridGraph(20, 20);
graph.weightedNodes.push(new Point(3, 3)); graph.weightedNodes.push(new Point(3, 3));
@@ -72,7 +61,7 @@ class MainScene extends Scene {
console.log(path); console.log(path);
} }
public astarTest(){ public astarTest() {
let graph = new AstarGridGraph(20, 20); let graph = new AstarGridGraph(20, 20);
graph.weightedNodes.push(new Point(3, 3)); graph.weightedNodes.push(new Point(3, 3));

View File

@@ -139,11 +139,10 @@ declare class DebugDefaults {
static verletParticle: number; static verletParticle: number;
static verletConstraintEdge: number; static verletConstraintEdge: number;
} }
declare abstract class Component { declare abstract class Component extends egret.DisplayObjectContainer {
entity: Entity; entity: Entity;
private _enabled; private _enabled;
updateInterval: number; updateInterval: number;
readonly transform: Transform;
enabled: boolean; enabled: boolean;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
readonly stage: egret.Stage; readonly stage: egret.Stage;
@@ -159,37 +158,27 @@ declare abstract class Component {
registerComponent(): void; registerComponent(): void;
deregisterComponent(): void; deregisterComponent(): void;
} }
declare class Entity { declare class Entity extends egret.DisplayObjectContainer {
private static _idGenerator; private static _idGenerator;
name: string; name: string;
readonly id: number; readonly id: number;
scene: Scene; scene: Scene;
readonly transform: Transform;
readonly components: ComponentList; readonly components: ComponentList;
private _updateOrder; private _updateOrder;
private _enabled; private _enabled;
_isDestoryed: boolean; _isDestoryed: boolean;
private _tag; private _tag;
componentBits: BitSet; componentBits: BitSet;
parent: Transform;
position: Vector2;
localPosition: Vector2;
rotation: number;
rotationDegrees: number;
localRotation: number;
localRotationDegrees: number;
scale: Vector2;
localScale: Vector2;
readonly worldInverseTransform: Matrix2D;
readonly localToWorldTransform: Matrix2D;
readonly worldToLocalTransform: Matrix2D;
readonly isDestoryed: boolean; readonly isDestoryed: boolean;
position: Vector2;
scale: Vector2;
enabled: boolean; enabled: boolean;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
tag: number; tag: number;
readonly stage: egret.Stage; readonly stage: egret.Stage;
constructor(name: string); constructor(name: string);
updateOrder: number; updateOrder: number;
roundPosition(): void;
setUpdateOrder(updateOrder: number): this; setUpdateOrder(updateOrder: number): this;
setTag(tag: number): Entity; setTag(tag: number): Entity;
attachToScene(newScene: Scene): void; attachToScene(newScene: Scene): void;
@@ -206,7 +195,7 @@ declare class Entity {
onAddedToScene(): void; onAddedToScene(): void;
onRemovedFromScene(): void; onRemovedFromScene(): void;
onTransformChanged(comp: ComponentTransform): void; onTransformChanged(comp: ComponentTransform): void;
destory(): void; destroy(): void;
} }
declare class Scene extends egret.DisplayObjectContainer { declare class Scene extends egret.DisplayObjectContainer {
camera: Camera; camera: Camera;
@@ -214,9 +203,6 @@ declare class Scene extends egret.DisplayObjectContainer {
readonly renderableComponents: RenderableComponentList; readonly renderableComponents: RenderableComponentList;
readonly content: ContentManager; readonly content: ContentManager;
enablePostProcessing: boolean; enablePostProcessing: boolean;
private _projectionMatrix;
private _transformMatrix;
private _matrixTransformMatrix;
private _renderers; private _renderers;
private _postProcessors; private _postProcessors;
private _didSceneBegin; private _didSceneBegin;
@@ -324,14 +310,12 @@ declare class Camera extends Component {
private _origin; private _origin;
private _transformMatrix; private _transformMatrix;
private _inverseTransformMatrix; private _inverseTransformMatrix;
private _projectionMatrix;
private _minimumZoom; private _minimumZoom;
private _maximumZoom; private _maximumZoom;
private _areMatrixesDirty; private _areMatrixesDirty;
private _inset; private _inset;
private _bounds; private _bounds;
private _areBoundsDirty; private _areBoundsDirty;
private _isProjectionMatrixDirty;
readonly bounds: Rectangle; readonly bounds: Rectangle;
zoom: number; zoom: number;
minimumZoom: number; minimumZoom: number;
@@ -432,14 +416,10 @@ declare class Sprite {
constructor(texture: egret.Texture, sourceRect?: Rectangle, origin?: Vector2); constructor(texture: egret.Texture, sourceRect?: Rectangle, origin?: Vector2);
} }
declare class SpriteRenderer extends RenderableComponent { declare class SpriteRenderer extends RenderableComponent {
private _sprite;
private _origin; private _origin;
private _bitmap;
readonly bounds: Rectangle;
sprite: Sprite;
setSprite(sprite: Sprite): SpriteRenderer;
origin: Vector2; origin: Vector2;
setOrigin(origin: Vector2): this; setOrigin(origin: Vector2): this;
setSprite(sprite: Sprite): void;
setColor(color: number): void; setColor(color: number): void;
isVisibleFromCamera(camera: Camera): boolean; isVisibleFromCamera(camera: Camera): boolean;
render(camera: Camera): void; render(camera: Camera): void;
@@ -619,6 +599,39 @@ declare class Time {
private static _lastTime; private static _lastTime;
static update(currentTime: number): void; static update(currentTime: number): void;
} }
declare class GraphicsCapabilities {
supportsTextureFilterAnisotropic: boolean;
supportsNonPowerOfTwo: boolean;
supportsDepth24: boolean;
supportsPackedDepthStencil: boolean;
supportsDepthNonLinear: boolean;
supportsTextureMaxLevel: boolean;
supportsS3tc: boolean;
supportsDxt1: boolean;
supportsPvrtc: boolean;
supportsAtitc: boolean;
supportsFramebufferObjectARB: boolean;
initialize(device: GraphicsDevice): void;
private platformInitialize;
}
declare class GraphicsDevice {
private viewport;
graphicsCapabilities: GraphicsCapabilities;
constructor();
}
declare class Viewport {
private _x;
private _y;
private _width;
private _height;
private _minDepth;
private _maxDepth;
readonly aspectRatio: number;
bounds: Rectangle;
constructor(x: number, y: number, width: number, height: number);
}
declare abstract class GraphicsResource {
}
declare class GaussianBlurEffect extends egret.CustomFilter { declare class GaussianBlurEffect extends egret.CustomFilter {
private static blur_frag; private static blur_frag;
constructor(); constructor();
@@ -767,6 +780,7 @@ declare class Matrix2D {
static createTranslation(xPosition: number, yPosition: number, result?: Matrix2D): Matrix2D; static createTranslation(xPosition: number, yPosition: number, result?: Matrix2D): Matrix2D;
static createRotation(radians: number, result?: Matrix2D): Matrix2D; static createRotation(radians: number, result?: Matrix2D): Matrix2D;
static createScale(xScale: number, yScale: number, result?: Matrix2D): Matrix2D; static createScale(xScale: number, yScale: number, result?: Matrix2D): Matrix2D;
toEgretMatrix(): egret.Matrix;
} }
declare class Rectangle { declare class Rectangle {
x: number; x: number;
@@ -823,6 +837,12 @@ declare class Vector2 {
static distance(value1: Vector2, value2: Vector2): number; static distance(value1: Vector2, value2: Vector2): number;
static negate(value: Vector2): Vector2; static negate(value: Vector2): Vector2;
} }
declare class Vector3 {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z: number);
}
declare class ColliderTriggerHelper { declare class ColliderTriggerHelper {
private _entity; private _entity;
private _activeTriggerIntersections; private _activeTriggerIntersections;
@@ -1060,10 +1080,5 @@ declare class Vector2Ext {
static transformA(sourceArray: Vector2[], sourceIndex: number, matrix: Matrix2D, destinationArray: Vector2[], destinationIndex: number, length: number): void; static transformA(sourceArray: Vector2[], sourceIndex: number, matrix: Matrix2D, destinationArray: Vector2[], destinationIndex: number, length: number): void;
static transformR(position: Vector2, matrix: Matrix2D): Vector2; static transformR(position: Vector2, matrix: Matrix2D): Vector2;
static transform(sourceArray: Vector2[], matrix: Matrix2D, destinationArray: Vector2[]): void; static transform(sourceArray: Vector2[], matrix: Matrix2D, destinationArray: Vector2[]): void;
} static round(vec: Vector2): Vector2;
declare class WebGLUtils {
static getWebGL(): WebGLRenderingContext;
static drawUserIndexPrimitives<T>(primitiveType: number, vertexData: T[], vertexOffset: number, numVertices: number, indexData: number[], indexOffset: number, primitiveCount: number): void;
private static getElementCountArray;
static checkGLError(): void;
} }

View File

@@ -737,18 +737,14 @@ var DebugDefaults = (function () {
DebugDefaults.verletConstraintEdge = 0x433E36; DebugDefaults.verletConstraintEdge = 0x433E36;
return DebugDefaults; return DebugDefaults;
}()); }());
var Component = (function () { var Component = (function (_super) {
__extends(Component, _super);
function Component() { function Component() {
this._enabled = true; var _this = _super !== null && _super.apply(this, arguments) || this;
this.updateInterval = 1; _this._enabled = true;
_this.updateInterval = 1;
return _this;
} }
Object.defineProperty(Component.prototype, "transform", {
get: function () {
return this.entity.transform;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Component.prototype, "enabled", { Object.defineProperty(Component.prototype, "enabled", {
get: function () { get: function () {
return this.entity ? this.entity.enabled && this._enabled : this._enabled; return this.entity ? this.entity.enabled && this._enabled : this._enabled;
@@ -814,132 +810,45 @@ var Component = (function () {
this.entity.scene.entityProcessors.onComponentRemoved(this.entity); this.entity.scene.entityProcessors.onComponentRemoved(this.entity);
}; };
return Component; return Component;
}()); }(egret.DisplayObjectContainer));
var Entity = (function () { var Entity = (function (_super) {
__extends(Entity, _super);
function Entity(name) { function Entity(name) {
this._updateOrder = 0; var _this = _super.call(this) || this;
this._enabled = true; _this._updateOrder = 0;
this._tag = 0; _this._enabled = true;
this.name = name; _this._tag = 0;
this.transform = new Transform(this); _this.name = name;
this.components = new ComponentList(this); _this.components = new ComponentList(_this);
this.id = Entity._idGenerator++; _this.id = Entity._idGenerator++;
this.componentBits = new BitSet(); _this.componentBits = new BitSet();
return _this;
} }
Object.defineProperty(Entity.prototype, "parent", { Object.defineProperty(Entity.prototype, "isDestoryed", {
get: function () { get: function () {
return this.transform.parent; return this._isDestoryed;
},
set: function (value) {
this.transform.setParent(value);
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Entity.prototype, "position", { Object.defineProperty(Entity.prototype, "position", {
get: function () { get: function () {
return this.transform.position; return new Vector2(this.x, this.y);
}, },
set: function (value) { set: function (value) {
this.transform.setPosition(value); this.x = value.x;
}, this.y = value.y;
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "localPosition", {
get: function () {
return this.transform.localPosition;
},
set: function (value) {
this.transform.setLocalPosition(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "rotation", {
get: function () {
return this.transform.rotation;
},
set: function (value) {
this.transform.setRotation(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "rotationDegrees", {
get: function () {
return this.transform.rotationDegrees;
},
set: function (value) {
this.transform.setRotationDegrees(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "localRotation", {
get: function () {
return this.transform.localRotation;
},
set: function (value) {
this.transform.setLocalRotation(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "localRotationDegrees", {
get: function () {
return this.transform.localRotationDegrees;
},
set: function (value) {
this.transform.setLocalRotationDegrees(value);
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Entity.prototype, "scale", { Object.defineProperty(Entity.prototype, "scale", {
get: function () { get: function () {
return this.transform.scale; return new Vector2(this.scaleX, this.scaleY);
}, },
set: function (value) { set: function (value) {
this.transform.setScale(value); this.scaleX = value.x;
}, this.scaleY = value.y;
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "localScale", {
get: function () {
return this.transform.scale;
},
set: function (value) {
this.transform.setScale(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "worldInverseTransform", {
get: function () {
return this.transform.worldInverseTransform;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "localToWorldTransform", {
get: function () {
return this.transform.localToWorldTransform;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "worldToLocalTransform", {
get: function () {
return this.transform.worldToLocalTransform;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "isDestoryed", {
get: function () {
return this._isDestoryed;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -989,6 +898,9 @@ var Entity = (function () {
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Entity.prototype.roundPosition = function () {
this.position = Vector2Ext.round(this.position);
};
Entity.prototype.setUpdateOrder = function (updateOrder) { Entity.prototype.setUpdateOrder = function (updateOrder) {
if (this._updateOrder != updateOrder) { if (this._updateOrder != updateOrder) {
this._updateOrder = updateOrder; this._updateOrder = updateOrder;
@@ -1013,19 +925,20 @@ var Entity = (function () {
this.scene = newScene; this.scene = newScene;
newScene.entities.add(this); newScene.entities.add(this);
this.components.registerAllComponents(); this.components.registerAllComponents();
for (var i = 0; i < this.transform.childCount; i++) { for (var i = 0; i < this.numChildren; i++) {
this.transform.getChild(i).entity.attachToScene(newScene); this.getChildAt(i).entity.attachToScene(newScene);
} }
}; };
Entity.prototype.detachFromScene = function () { Entity.prototype.detachFromScene = function () {
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.components.deregisterAllComponents(); this.components.deregisterAllComponents();
for (var i = 0; i < this.transform.childCount; i++) for (var i = 0; i < this.numChildren; i++)
this.transform.getChild(i).entity.detachFromScene(); this.getChildAt(i).entity.detachFromScene();
}; };
Entity.prototype.addComponent = function (component) { Entity.prototype.addComponent = function (component) {
component.entity = this; component.entity = this;
this.components.add(component); this.components.add(component);
this.addChild(component);
component.initialize(); component.initialize();
return component; return component;
}; };
@@ -1073,17 +986,17 @@ var Entity = (function () {
Entity.prototype.onTransformChanged = function (comp) { Entity.prototype.onTransformChanged = function (comp) {
this.components.onEntityTransformChanged(comp); this.components.onEntityTransformChanged(comp);
}; };
Entity.prototype.destory = function () { Entity.prototype.destroy = function () {
this._isDestoryed = true; this._isDestoryed = true;
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.transform.parent = null; this.removeChildren();
for (var i = this.transform.childCount - 1; i >= 0; i--) { for (var i = this.numChildren - 1; i >= 0; i--) {
var child = this.transform.getChild(i); var child = this.getChildAt(i);
child.entity.destory(); child.entity.destroy();
} }
}; };
return Entity; return Entity;
}()); }(egret.DisplayObjectContainer));
var Scene = (function (_super) { var Scene = (function (_super) {
__extends(Scene, _super); __extends(Scene, _super);
function Scene() { function Scene() {
@@ -1091,7 +1004,6 @@ var Scene = (function (_super) {
_this.enablePostProcessing = true; _this.enablePostProcessing = true;
_this._renderers = []; _this._renderers = [];
_this._postProcessors = []; _this._postProcessors = [];
_this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
_this.entityProcessors = new EntityProcessorList(); _this.entityProcessors = new EntityProcessorList();
_this.renderableComponents = new RenderableComponentList(); _this.renderableComponents = new RenderableComponentList();
_this.entities = new EntityList(_this); _this.entities = new EntityList(_this);
@@ -1102,19 +1014,20 @@ var Scene = (function (_super) {
} }
Scene.prototype.createEntity = function (name) { Scene.prototype.createEntity = function (name) {
var entity = new Entity(name); var entity = new Entity(name);
entity.transform.position = new Vector2(0, 0); entity.position = new Vector2(0, 0);
return this.addEntity(entity); return this.addEntity(entity);
}; };
Scene.prototype.addEntity = function (entity) { Scene.prototype.addEntity = function (entity) {
this.entities.add(entity); this.entities.add(entity);
entity.scene = this; entity.scene = this;
for (var i = 0; i < entity.transform.childCount; i++) this.addChild(entity);
this.addEntity(entity.transform.getChild(i).entity); for (var i = 0; i < entity.numChildren; i++)
this.addEntity(entity.getChildAt(i).entity);
return entity; return entity;
}; };
Scene.prototype.destroyAllEntities = function () { Scene.prototype.destroyAllEntities = function () {
for (var i = 0; i < this.entities.count; i++) { for (var i = 0; i < this.entities.count; i++) {
this.entities.buffer[i].destory(); this.entities.buffer[i].destroy();
} }
}; };
Scene.prototype.findEntity = function (name) { Scene.prototype.findEntity = function (name) {
@@ -1178,6 +1091,7 @@ var Scene = (function (_super) {
this._postProcessors[i].unload(); this._postProcessors[i].unload();
} }
this.entities.removeAllEntities(); this.entities.removeAllEntities();
this.removeChildren();
Physics.clear(); Physics.clear();
this.camera.destory(); this.camera.destory();
this.camera = null; this.camera = null;
@@ -1275,7 +1189,7 @@ var SceneManager = (function () {
SceneManager._scene.end(); SceneManager._scene.end();
for (var i = 0; i < SceneManager._scene.entities.buffer.length; i++) { for (var i = 0; i < SceneManager._scene.entities.buffer.length; i++) {
var entity = SceneManager._scene.entities.buffer[i]; var entity = SceneManager._scene.entities.buffer[i];
entity.destory(); entity.destroy();
} }
SceneManager._scene = SceneManager._nextScene; SceneManager._scene = SceneManager._nextScene;
SceneManager._nextScene = null; SceneManager._nextScene = null;
@@ -1632,14 +1546,12 @@ var Camera = (function (_super) {
_this._origin = Vector2.zero; _this._origin = Vector2.zero;
_this._transformMatrix = new Matrix2D(); _this._transformMatrix = new Matrix2D();
_this._inverseTransformMatrix = new Matrix2D(); _this._inverseTransformMatrix = new Matrix2D();
_this._projectionMatrix = new Matrix2D();
_this._minimumZoom = 0.3; _this._minimumZoom = 0.3;
_this._maximumZoom = 3; _this._maximumZoom = 3;
_this._areMatrixesDirty = true; _this._areMatrixesDirty = true;
_this._inset = new CameraInset(); _this._inset = new CameraInset();
_this._bounds = new Rectangle(); _this._bounds = new Rectangle();
_this._areBoundsDirty = true; _this._areBoundsDirty = true;
_this._isProjectionMatrixDirty = true;
_this.setZoom(0); _this.setZoom(0);
return _this; return _this;
} }
@@ -1651,7 +1563,7 @@ var Camera = (function (_super) {
var stage = this.stage; var stage = this.stage;
var topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top)); var topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top));
var bottomRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, stage.stageHeight - this._inset.bottom)); var bottomRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, stage.stageHeight - this._inset.bottom));
if (this.entity.transform.rotation != 0) { if (this.entity.rotation != 0) {
var topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top)); var topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
var bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom)); var bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom));
var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x); var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
@@ -1723,10 +1635,10 @@ var Camera = (function (_super) {
}); });
Object.defineProperty(Camera.prototype, "position", { Object.defineProperty(Camera.prototype, "position", {
get: function () { get: function () {
return this.entity.transform.position; return this.entity.position;
}, },
set: function (value) { set: function (value) {
this.entity.transform.position = value; this.entity.position = value;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -1750,10 +1662,9 @@ var Camera = (function (_super) {
configurable: true configurable: true
}); });
Camera.prototype.onSceneSizeChanged = function (newWidth, newHeight) { Camera.prototype.onSceneSizeChanged = function (newWidth, newHeight) {
this._isProjectionMatrixDirty = true;
var oldOrigin = this._origin; var oldOrigin = this._origin;
this.origin = new Vector2(newWidth / 2, newHeight / 2); this.origin = new Vector2(newWidth / 2, newHeight / 2);
this.entity.transform.position = Vector2.add(this.entity.transform.position, Vector2.subtract(this._origin, oldOrigin)); this.entity.position = Vector2.add(this.entity.position, Vector2.subtract(this._origin, oldOrigin));
}; };
Camera.prototype.setMinimumZoom = function (minZoom) { Camera.prototype.setMinimumZoom = function (minZoom) {
if (this._zoom < minZoom) if (this._zoom < minZoom)
@@ -1782,7 +1693,7 @@ var Camera = (function (_super) {
return this; return this;
}; };
Camera.prototype.setPosition = function (position) { Camera.prototype.setPosition = function (position) {
this.entity.transform.setPosition(position); this.entity.position = position;
return this; return this;
}; };
Camera.prototype.forceMatrixUpdate = function () { Camera.prototype.forceMatrixUpdate = function () {
@@ -1792,12 +1703,12 @@ var Camera = (function (_super) {
if (!this._areMatrixesDirty) if (!this._areMatrixesDirty)
return; return;
var tempMat; var tempMat;
this._transformMatrix = Matrix2D.createTranslation(-this.entity.transform.position.x, -this.entity.transform.position.y); this._transformMatrix = Matrix2D.createTranslation(-this.entity.position.x, -this.entity.position.y);
if (this._zoom != 1) { if (this._zoom != 1) {
tempMat = Matrix2D.createScale(this._zoom, this._zoom); tempMat = Matrix2D.createScale(this._zoom, this._zoom);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
} }
if (this.entity.transform.rotation != 0) { if (this.entity.rotation != 0) {
tempMat = Matrix2D.createRotation(this.entity.rotation); tempMat = Matrix2D.createRotation(this.entity.rotation);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
} }
@@ -1877,10 +1788,10 @@ var FollowCamera = (function (_super) {
if (this._targetEntity) if (this._targetEntity)
this.updateFollow(); this.updateFollow();
this.camera.position = Vector2.lerp(this.camera.position, Vector2.add(this.camera.position, this._desiredPositionDelta), this.followLerp); this.camera.position = Vector2.lerp(this.camera.position, Vector2.add(this.camera.position, this._desiredPositionDelta), this.followLerp);
this.camera.entity.transform.roundPosition(); this.camera.entity.roundPosition();
if (this.mapLockEnabled) { if (this.mapLockEnabled) {
this.camera.position = this.clampToMapSize(this.camera.position); this.camera.position = this.clampToMapSize(this.camera.position);
this.camera.entity.transform.roundPosition(); this.camera.entity.roundPosition();
} }
}; };
FollowCamera.prototype.clampToMapSize = function (position) { FollowCamera.prototype.clampToMapSize = function (position) {
@@ -1891,8 +1802,8 @@ var FollowCamera = (function (_super) {
FollowCamera.prototype.updateFollow = function () { FollowCamera.prototype.updateFollow = function () {
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0; this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
if (this._cameraStyle == CameraStyle.lockOn) { if (this._cameraStyle == CameraStyle.lockOn) {
var targetX = this._targetEntity.transform.position.x; var targetX = this._targetEntity.position.x;
var targetY = this._targetEntity.transform.position.y; var targetY = this._targetEntity.position.y;
if (this._worldSpaceDeadZone.x > targetX) if (this._worldSpaceDeadZone.x > targetX)
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x; this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
else if (this._worldSpaceDeadZone.x < targetX) else if (this._worldSpaceDeadZone.x < targetX)
@@ -2026,11 +1937,7 @@ var RenderableComponent = (function (_super) {
}); });
Object.defineProperty(RenderableComponent.prototype, "bounds", { Object.defineProperty(RenderableComponent.prototype, "bounds", {
get: function () { get: function () {
if (this._areBoundsDirty) { return new Rectangle(this.getBounds().x, this.getBounds().y, this.getBounds().width, this.getBounds().height);
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new Vector2(0, 0), this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height);
this._areBoundsDirty = false;
}
return this._bounds;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -2084,37 +1991,6 @@ var SpriteRenderer = (function (_super) {
function SpriteRenderer() { function SpriteRenderer() {
return _super !== null && _super.apply(this, arguments) || this; return _super !== null && _super.apply(this, arguments) || this;
} }
Object.defineProperty(SpriteRenderer.prototype, "bounds", {
get: function () {
if (this._areBoundsDirty) {
if (this._sprite) {
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin, this.entity.transform.scale, this.entity.transform.rotation, this._sprite.texture2D.textureWidth, this._sprite.texture2D.textureHeight);
this._areBoundsDirty = false;
}
}
return this._bounds;
},
enumerable: true,
configurable: true
});
Object.defineProperty(SpriteRenderer.prototype, "sprite", {
get: function () {
return this._sprite;
},
set: function (value) {
this.setSprite(value);
},
enumerable: true,
configurable: true
});
SpriteRenderer.prototype.setSprite = function (sprite) {
this._sprite = sprite;
if (this._sprite)
this._origin = sprite.origin;
this._bitmap = new egret.Bitmap(sprite.texture2D);
this.scene.addChild(this._bitmap);
return this;
};
Object.defineProperty(SpriteRenderer.prototype, "origin", { Object.defineProperty(SpriteRenderer.prototype, "origin", {
get: function () { get: function () {
return this._origin; return this._origin;
@@ -2132,6 +2008,10 @@ var SpriteRenderer = (function (_super) {
} }
return this; return this;
}; };
SpriteRenderer.prototype.setSprite = function (sprite) {
this.removeChildren();
this.addChild(new egret.Bitmap(sprite.texture2D));
};
SpriteRenderer.prototype.setColor = function (color) { SpriteRenderer.prototype.setColor = function (color) {
var colorMatrix = [ var colorMatrix = [
1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
@@ -2143,28 +2023,19 @@ var SpriteRenderer = (function (_super) {
colorMatrix[6] = Math.floor(color / 256 % 256) / 255; colorMatrix[6] = Math.floor(color / 256 % 256) / 255;
colorMatrix[12] = color % 256 / 255; colorMatrix[12] = color % 256 / 255;
var colorFilter = new egret.ColorMatrixFilter(colorMatrix); var colorFilter = new egret.ColorMatrixFilter(colorMatrix);
this._bitmap.filters = [colorFilter]; this.filters = [colorFilter];
}; };
SpriteRenderer.prototype.isVisibleFromCamera = function (camera) { SpriteRenderer.prototype.isVisibleFromCamera = function (camera) {
var topLeft = camera.screenToWorldPoint(new Vector2(0, 0)); var topLeft = camera.screenToWorldPoint(new Vector2(0, 0));
this.isVisible = new Rectangle(topLeft.x, topLeft.y, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds); this.isVisible = new Rectangle(topLeft.x, topLeft.y, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds);
this._bitmap.visible = this.isVisible; this.visible = this.isVisible;
return this.isVisible; return this.isVisible;
}; };
SpriteRenderer.prototype.render = function (camera) { SpriteRenderer.prototype.render = function (camera) {
if (!this.sprite)
return;
this._bitmap.x = this.entity.transform.position.x - camera.transform.position.x + camera.origin.x;
this._bitmap.y = this.entity.transform.position.y - camera.transform.position.y + camera.origin.y;
this._bitmap.rotation = this.entity.transform.rotation + camera.transform.rotation;
this._bitmap.anchorOffsetX = this._origin.x;
this._bitmap.anchorOffsetY = this._origin.y;
this._bitmap.scaleX = this.entity.transform.scale.x * camera.transform.scale.x;
this._bitmap.scaleY = this.entity.transform.scale.y * camera.transform.scale.y;
}; };
SpriteRenderer.prototype.onRemovedFromEntity = function () { SpriteRenderer.prototype.onRemovedFromEntity = function () {
if (this._bitmap) if (this.parent)
this.scene.removeChild(this._bitmap); this.parent.removeChild(this);
}; };
return SpriteRenderer; return SpriteRenderer;
}(RenderableComponent)); }(RenderableComponent));
@@ -2207,7 +2078,7 @@ var Mover = (function (_super) {
return collisionResult; return collisionResult;
}; };
Mover.prototype.applyMovement = function (motion) { Mover.prototype.applyMovement = function (motion) {
this.entity.transform.position = Vector2.add(this.entity.transform.position, motion); this.entity.position = Vector2.add(this.entity.position, motion);
if (this._triggerHelper) if (this._triggerHelper)
this._triggerHelper.update(); this._triggerHelper.update();
}; };
@@ -2292,13 +2163,13 @@ var Collider = (function (_super) {
var renderable = this.entity.getComponent(RenderableComponent); var renderable = this.entity.getComponent(RenderableComponent);
if (renderable) { if (renderable) {
var renderbaleBounds = renderable.bounds; var renderbaleBounds = renderable.bounds;
var width = renderbaleBounds.width / this.entity.transform.scale.x; var width = renderbaleBounds.width / this.entity.scale.x;
var height = renderbaleBounds.height / this.entity.transform.scale.y; var height = renderbaleBounds.height / this.entity.scale.y;
if (this instanceof BoxCollider) { if (this instanceof BoxCollider) {
var boxCollider = this; var boxCollider = this;
boxCollider.width = width; boxCollider.width = width;
boxCollider.height = height; boxCollider.height = height;
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.transform.position); this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.position);
} }
} }
} }
@@ -2672,6 +2543,8 @@ var ComponentList = (function () {
} }
}; };
ComponentList.prototype.handleRemove = function (component) { ComponentList.prototype.handleRemove = function (component) {
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
component.onRemovedFromEntity(); component.onRemovedFromEntity();
@@ -3004,6 +2877,74 @@ var Time = (function () {
Time._lastTime = 0; Time._lastTime = 0;
return Time; return Time;
}()); }());
var GraphicsCapabilities = (function () {
function GraphicsCapabilities() {
}
GraphicsCapabilities.prototype.initialize = function (device) {
this.platformInitialize(device);
};
GraphicsCapabilities.prototype.platformInitialize = function (device) {
var gl = new egret.sys.RenderBuffer().context.getInstance();
this.supportsNonPowerOfTwo = false;
this.supportsTextureFilterAnisotropic = gl.getExtension("EXT_texture_filter_anisotropic") != null;
this.supportsDepth24 = true;
this.supportsPackedDepthStencil = true;
this.supportsDepthNonLinear = false;
this.supportsTextureMaxLevel = true;
this.supportsS3tc = gl.getExtension("WEBGL_compressed_texture_s3tc") != null ||
gl.getExtension("WEBGL_compressed_texture_s3tc_srgb") != null;
this.supportsDxt1 = this.supportsS3tc;
this.supportsPvrtc = false;
this.supportsAtitc = gl.getExtension("WEBGL_compressed_texture_astc") != null;
this.supportsFramebufferObjectARB = false;
};
return GraphicsCapabilities;
}());
var GraphicsDevice = (function () {
function GraphicsDevice() {
this.graphicsCapabilities = new GraphicsCapabilities();
this.graphicsCapabilities.initialize(this);
}
return GraphicsDevice;
}());
var Viewport = (function () {
function Viewport(x, y, width, height) {
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._minDepth = 0;
this._maxDepth = 1;
}
Object.defineProperty(Viewport.prototype, "aspectRatio", {
get: function () {
if ((this._height != 0) && (this._width != 0))
return (this._width / this._height);
return 0;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Viewport.prototype, "bounds", {
get: function () {
return new Rectangle(this._x, this._y, this._width, this._height);
},
set: function (value) {
this._x = value.x;
this._y = value.y;
this._width = value.width;
this._height = value.height;
},
enumerable: true,
configurable: true
});
return Viewport;
}());
var GraphicsResource = (function () {
function GraphicsResource() {
}
return GraphicsResource;
}());
var GaussianBlurEffect = (function (_super) { var GaussianBlurEffect = (function (_super) {
__extends(GaussianBlurEffect, _super); __extends(GaussianBlurEffect, _super);
function GaussianBlurEffect() { function GaussianBlurEffect() {
@@ -3152,11 +3093,6 @@ var Renderer = (function () {
} }
Renderer.prototype.onAddedToScene = function (scene) { }; Renderer.prototype.onAddedToScene = function (scene) { };
Renderer.prototype.beginRender = function (cam) { Renderer.prototype.beginRender = function (cam) {
cam.transform.updateTransform();
var entities = SceneManager.scene.entities;
for (var i = 0; i < entities.buffer.length; i++) {
entities.buffer[i].transform.updateTransform();
}
}; };
Renderer.prototype.unload = function () { }; Renderer.prototype.unload = function () { };
Renderer.prototype.renderAfterStateCheck = function (renderable, cam) { Renderer.prototype.renderAfterStateCheck = function (renderable, cam) {
@@ -3203,7 +3139,7 @@ var PolyLight = (function (_super) {
Object.defineProperty(PolyLight.prototype, "bounds", { Object.defineProperty(PolyLight.prototype, "bounds", {
get: function () { get: function () {
if (this._areBoundsDirty) { if (this._areBoundsDirty) {
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new Vector2(this._radius), Vector2.one, 0, this._radius * 2, this._radius * 2); this._bounds.calculateBounds(this.entity.position, this._localOffset, new Vector2(this._radius), Vector2.one, 0, this._radius * 2, this._radius * 2);
this._areBoundsDirty = false; this._areBoundsDirty = false;
} }
return this._bounds; return this._bounds;
@@ -3602,6 +3538,10 @@ var Matrix2D = (function () {
result.m32 = 0; result.m32 = 0;
return result; return result;
}; };
Matrix2D.prototype.toEgretMatrix = function () {
var matrix = new egret.Matrix(this.m11, this.m12, this.m21, this.m22, this.m31, this.m32);
return matrix;
};
Matrix2D._identity = new Matrix2D(1, 0, 0, 1, 0, 0); Matrix2D._identity = new Matrix2D(1, 0, 0, 1, 0, 0);
return Matrix2D; return Matrix2D;
}()); }());
@@ -3901,6 +3841,14 @@ var Vector2 = (function () {
Vector2.zeroVector2 = new Vector2(0, 0); Vector2.zeroVector2 = new Vector2(0, 0);
return Vector2; return Vector2;
}()); }());
var Vector3 = (function () {
function Vector3(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
return Vector3;
}());
var ColliderTriggerHelper = (function () { var ColliderTriggerHelper = (function () {
function ColliderTriggerHelper(entity) { function ColliderTriggerHelper(entity) {
this._activeTriggerIntersections = []; this._activeTriggerIntersections = [];
@@ -4292,28 +4240,28 @@ var Polygon = (function (_super) {
var hasUnitScale = true; var hasUnitScale = true;
var tempMat = void 0; var tempMat = void 0;
var combinedMatrix = Matrix2D.createTranslation(-this._polygonCenter.x, -this._polygonCenter.y); var combinedMatrix = Matrix2D.createTranslation(-this._polygonCenter.x, -this._polygonCenter.y);
if (collider.entity.transform.scale != Vector2.one) { if (collider.entity.scale != Vector2.one) {
tempMat = Matrix2D.createScale(collider.entity.transform.scale.x, collider.entity.transform.scale.y); tempMat = Matrix2D.createScale(collider.entity.scale.x, collider.entity.scale.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
hasUnitScale = false; hasUnitScale = false;
var scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.transform.scale); var scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale);
this.center = scaledOffset; this.center = scaledOffset;
} }
if (collider.entity.transform.rotation != 0) { if (collider.entity.rotation != 0) {
tempMat = Matrix2D.createRotation(collider.entity.transform.rotation); tempMat = Matrix2D.createRotation(collider.entity.rotation);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg; var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.transform.scale)).length(); var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, collider.entity.transform.rotationDegrees + offsetAngle); this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle);
} }
tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y); tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points); Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points);
this.isUnrotated = collider.entity.transform.rotation == 0; this.isUnrotated = collider.entity.rotation == 0;
if (collider._isRotationDirty) if (collider._isRotationDirty)
this._areEdgeNormalsDirty = true; this._areEdgeNormalsDirty = true;
} }
this.position = Vector2.add(collider.entity.transform.position, this.center); this.position = Vector2.add(collider.entity.position, this.center);
this.bounds = Rectangle.rectEncompassingPoints(this.points); this.bounds = Rectangle.rectEncompassingPoints(this.points);
this.bounds.location = Vector2.add(this.bounds.location, this.position); this.bounds.location = Vector2.add(this.bounds.location, this.position);
}; };
@@ -4382,17 +4330,17 @@ var Circle = (function (_super) {
Circle.prototype.recalculateBounds = function (collider) { Circle.prototype.recalculateBounds = function (collider) {
this.center = collider.localOffset; this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotationWithTransform) { if (collider.shouldColliderScaleAndRotationWithTransform) {
var scale = collider.entity.transform.scale; var scale = collider.entity.scale;
var hasUnitScale = scale.x == 1 && scale.y == 1; var hasUnitScale = scale.x == 1 && scale.y == 1;
var maxScale = Math.max(scale.x, scale.y); var maxScale = Math.max(scale.x, scale.y);
this.radius = this._originalRadius * maxScale; this.radius = this._originalRadius * maxScale;
if (collider.entity.transform.rotation != 0) { if (collider.entity.rotation != 0) {
var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg; var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.transform.scale)).length(); var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, collider.entity.transform.rotationDegrees + offsetAngle); this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle);
} }
} }
this.position = Vector2.add(collider.entity.transform.position, this.center); this.position = Vector2.add(collider.entity.position, this.center);
this.bounds = new Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2); this.bounds = new Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2);
}; };
Circle.prototype.overlaps = function (other) { Circle.prototype.overlaps = function (other) {
@@ -5166,45 +5114,8 @@ var Vector2Ext = (function () {
Vector2Ext.transform = function (sourceArray, matrix, destinationArray) { Vector2Ext.transform = function (sourceArray, matrix, destinationArray) {
this.transformA(sourceArray, 0, matrix, destinationArray, 0, sourceArray.length); this.transformA(sourceArray, 0, matrix, destinationArray, 0, sourceArray.length);
}; };
Vector2Ext.round = function (vec) {
return new Vector2(Math.round(vec.x), Math.round(vec.y));
};
return Vector2Ext; return Vector2Ext;
}()); }());
var WebGLUtils = (function () {
function WebGLUtils() {
}
WebGLUtils.getWebGL = function () {
if (egret.WebGLUtils.checkCanUseWebGL())
return document.querySelector("canvas").getContext("webgl");
throw new Error("cannot get webgl");
};
WebGLUtils.drawUserIndexPrimitives = function (primitiveType, vertexData, vertexOffset, numVertices, indexData, indexOffset, primitiveCount) {
var GL = this.getWebGL();
GL.bindBuffer(GL.ARRAY_BUFFER, 0);
this.checkGLError();
GL.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, 0);
this.checkGLError();
GL.drawElements(primitiveType, this.getElementCountArray(primitiveType, primitiveCount), GL.UNSIGNED_SHORT, indexOffset * 2);
this.checkGLError();
};
WebGLUtils.getElementCountArray = function (primitiveType, primitiveCount) {
var GL = this.getWebGL();
switch (primitiveType) {
case GL.LINES:
return primitiveCount * 2;
case GL.LINE_STRIP:
return primitiveCount + 1;
case GL.TRIANGLES:
return primitiveCount * 3;
case GL.TRIANGLE_STRIP:
return primitiveCount + 2;
}
throw new Error("not support");
};
WebGLUtils.checkGLError = function () {
var GL = this.getWebGL();
var error = GL.getError();
if (error != GL.NO_ERROR) {
throw new Error("GL.GetError() returned" + error);
}
};
return WebGLUtils;
}());

File diff suppressed because one or more lines are too long

View File

@@ -10967,13 +10967,13 @@ declare namespace egret {
const RUNTIME2 = "runtime2"; const RUNTIME2 = "runtime2";
/** /**
* Running on Alipay * Running on Alipay
* @version Egret 5.2.23 * @version Egret 5.2.33
* @platform All * @platform All
* @language en_US * @language en_US
*/ */
/** /**
* 运行在支付宝小游戏上 * 运行在支付宝小游戏上
* @version Egret 5.2.26 * @version Egret 5.2.33
* @platform All * @platform All
* @language zh_CN * @language zh_CN
*/ */

View File

@@ -1,12 +1,8 @@
abstract class Component { abstract class Component extends egret.DisplayObjectContainer {
public entity: Entity; public entity: Entity;
private _enabled: boolean = true; private _enabled: boolean = true;
public updateInterval: number = 1; public updateInterval: number = 1;
public get transform(){
return this.entity.transform;
}
public get enabled(){ public get enabled(){
return this.entity ? this.entity.enabled && this._enabled : this._enabled; return this.entity ? this.entity.enabled && this._enabled : this._enabled;
} }

View File

@@ -4,7 +4,6 @@ class Camera extends Component {
private _origin: Vector2 = Vector2.zero; private _origin: Vector2 = Vector2.zero;
private _transformMatrix: Matrix2D = new Matrix2D(); private _transformMatrix: Matrix2D = new Matrix2D();
private _inverseTransformMatrix = new Matrix2D(); private _inverseTransformMatrix = new Matrix2D();
private _projectionMatrix = new Matrix2D();
private _minimumZoom = 0.3; private _minimumZoom = 0.3;
private _maximumZoom = 3; private _maximumZoom = 3;
@@ -12,7 +11,6 @@ class Camera extends Component {
private _inset: CameraInset = new CameraInset(); private _inset: CameraInset = new CameraInset();
private _bounds: Rectangle = new Rectangle(); private _bounds: Rectangle = new Rectangle();
private _areBoundsDirty = true; private _areBoundsDirty = true;
private _isProjectionMatrixDirty = true;
public get bounds(){ public get bounds(){
if (this._areMatrixesDirty) if (this._areMatrixesDirty)
@@ -23,7 +21,7 @@ class Camera extends Component {
let topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top)); let topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top));
let bottomRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, stage.stageHeight - this._inset.bottom)); let bottomRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, stage.stageHeight - this._inset.bottom));
if (this.entity.transform.rotation != 0){ if (this.entity.rotation != 0){
let topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top)); let topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
let bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom)); let bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom));
@@ -89,11 +87,11 @@ class Camera extends Component {
} }
public get position(){ public get position(){
return this.entity.transform.position; return this.entity.position;
} }
public set position(value: Vector2){ public set position(value: Vector2){
this.entity.transform.position = value; this.entity.position = value;
} }
public get transformMatrix(){ public get transformMatrix(){
@@ -115,11 +113,10 @@ class Camera extends Component {
} }
public onSceneSizeChanged(newWidth: number, newHeight: number){ public onSceneSizeChanged(newWidth: number, newHeight: number){
this._isProjectionMatrixDirty = true;
let oldOrigin = this._origin; let oldOrigin = this._origin;
this.origin = new Vector2(newWidth / 2, newHeight / 2); this.origin = new Vector2(newWidth / 2, newHeight / 2);
this.entity.transform.position = Vector2.add(this.entity.transform.position, Vector2.subtract(this._origin, oldOrigin)); this.entity.position = Vector2.add(this.entity.position, Vector2.subtract(this._origin, oldOrigin));
} }
public setMinimumZoom(minZoom: number): Camera{ public setMinimumZoom(minZoom: number): Camera{
@@ -154,7 +151,7 @@ class Camera extends Component {
} }
public setPosition(position: Vector2){ public setPosition(position: Vector2){
this.entity.transform.setPosition(position); this.entity.position = position;
return this; return this;
} }
@@ -168,13 +165,13 @@ class Camera extends Component {
return; return;
let tempMat: Matrix2D; let tempMat: Matrix2D;
this._transformMatrix = Matrix2D.createTranslation(-this.entity.transform.position.x, -this.entity.transform.position.y); this._transformMatrix = Matrix2D.createTranslation(-this.entity.position.x, -this.entity.position.y);
if (this._zoom != 1){ if (this._zoom != 1){
tempMat = Matrix2D.createScale(this._zoom, this._zoom); tempMat = Matrix2D.createScale(this._zoom, this._zoom);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
} }
if (this.entity.transform.rotation != 0){ if (this.entity.rotation != 0){
tempMat = Matrix2D.createRotation(this.entity.rotation); tempMat = Matrix2D.createRotation(this.entity.rotation);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
} }

View File

@@ -55,11 +55,11 @@ class FollowCamera extends Component {
this.updateFollow(); this.updateFollow();
this.camera.position = Vector2.lerp(this.camera.position, Vector2.add(this.camera.position, this._desiredPositionDelta), this.followLerp); this.camera.position = Vector2.lerp(this.camera.position, Vector2.add(this.camera.position, this._desiredPositionDelta), this.followLerp);
this.camera.entity.transform.roundPosition(); this.camera.entity.roundPosition();
if (this.mapLockEnabled){ if (this.mapLockEnabled){
this.camera.position = this.clampToMapSize(this.camera.position); this.camera.position = this.clampToMapSize(this.camera.position);
this.camera.entity.transform.roundPosition(); this.camera.entity.roundPosition();
} }
} }
@@ -74,8 +74,8 @@ class FollowCamera extends Component {
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0; this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
if (this._cameraStyle == CameraStyle.lockOn){ if (this._cameraStyle == CameraStyle.lockOn){
let targetX = this._targetEntity.transform.position.x; let targetX = this._targetEntity.position.x;
let targetY = this._targetEntity.transform.position.y; let targetY = this._targetEntity.position.y;
if (this._worldSpaceDeadZone.x > targetX) if (this._worldSpaceDeadZone.x > targetX)
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x; this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;

View File

@@ -82,15 +82,15 @@ abstract class Collider extends Component{
if (renderable){ if (renderable){
let renderbaleBounds = renderable.bounds; let renderbaleBounds = renderable.bounds;
let width = renderbaleBounds.width / this.entity.transform.scale.x; let width = renderbaleBounds.width / this.entity.scale.x;
let height = renderbaleBounds.height / this.entity.transform.scale.y; let height = renderbaleBounds.height / this.entity.scale.y;
if (this instanceof BoxCollider){ if (this instanceof BoxCollider){
let boxCollider = this as BoxCollider; let boxCollider = this as BoxCollider;
boxCollider.width = width; boxCollider.width = width;
boxCollider.height = height; boxCollider.height = height;
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.transform.position); this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.position);
} }
} }
} }

View File

@@ -46,7 +46,7 @@ class Mover extends Component {
} }
public applyMovement(motion: Vector2){ public applyMovement(motion: Vector2){
this.entity.transform.position = Vector2.add(this.entity.transform.position, motion); this.entity.position = Vector2.add(this.entity.position, motion);
if (this._triggerHelper) if (this._triggerHelper)
this._triggerHelper.update(); this._triggerHelper.update();

View File

@@ -31,13 +31,7 @@ abstract class RenderableComponent extends Component implements IRenderable {
} }
public get bounds(): Rectangle{ public get bounds(): Rectangle{
if (this._areBoundsDirty){ return new Rectangle(this.getBounds().x, this.getBounds().y, this.getBounds().width, this.getBounds().height);
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new Vector2(0, 0),
this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height);
this._areBoundsDirty = false;
}
return this._bounds;
} }
protected getWidth(){ protected getWidth(){

View File

@@ -1,39 +1,5 @@
class SpriteRenderer extends RenderableComponent { class SpriteRenderer extends RenderableComponent {
private _sprite: Sprite;
private _origin: Vector2; private _origin: Vector2;
private _bitmap: egret.Bitmap;
public get bounds(){
if (this._areBoundsDirty){
if (this._sprite){
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin,
this.entity.transform.scale, this.entity.transform.rotation, this._sprite.texture2D.textureWidth,
this._sprite.texture2D.textureHeight);
this._areBoundsDirty = false;
}
}
return this._bounds;
}
public get sprite(){
return this._sprite;
}
public set sprite(value: Sprite){
this.setSprite(value);
}
public setSprite(sprite: Sprite): SpriteRenderer{
this._sprite = sprite;
if (this._sprite)
this._origin = sprite.origin;
this._bitmap = new egret.Bitmap(sprite.texture2D);
this.scene.addChild(this._bitmap);
return this;
}
public get origin(){ public get origin(){
return this._origin; return this._origin;
@@ -49,6 +15,11 @@ class SpriteRenderer extends RenderableComponent {
return this; return this;
} }
public setSprite(sprite: Sprite){
this.removeChildren();
this.addChild(new egret.Bitmap(sprite.texture2D));
}
public setColor(color: number){ public setColor(color: number){
let colorMatrix = [ let colorMatrix = [
1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
@@ -60,31 +31,22 @@ class SpriteRenderer extends RenderableComponent {
colorMatrix[6] = Math.floor(color / 256 % 256) / 255; colorMatrix[6] = Math.floor(color / 256 % 256) / 255;
colorMatrix[12] = color % 256 / 255; colorMatrix[12] = color % 256 / 255;
let colorFilter = new egret.ColorMatrixFilter(colorMatrix); let colorFilter = new egret.ColorMatrixFilter(colorMatrix);
this._bitmap.filters = [colorFilter]; this.filters = [colorFilter];
} }
public isVisibleFromCamera(camera: Camera): boolean{ public isVisibleFromCamera(camera: Camera): boolean{
let topLeft = camera.screenToWorldPoint(new Vector2(0, 0)); let topLeft = camera.screenToWorldPoint(new Vector2(0, 0));
this.isVisible = new Rectangle(topLeft.x, topLeft.y, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds); this.isVisible = new Rectangle(topLeft.x, topLeft.y, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds);
this._bitmap.visible = this.isVisible; this.visible = this.isVisible;
return this.isVisible; return this.isVisible;
} }
public render(camera: Camera){ public render(camera: Camera){
if (!this.sprite)
return;
this._bitmap.x = this.entity.transform.position.x - camera.transform.position.x + camera.origin.x;
this._bitmap.y = this.entity.transform.position.y - camera.transform.position.y + camera.origin.y;
this._bitmap.rotation = this.entity.transform.rotation + camera.transform.rotation;
this._bitmap.anchorOffsetX = this._origin.x;
this._bitmap.anchorOffsetY = this._origin.y;
this._bitmap.scaleX = this.entity.transform.scale.x * camera.transform.scale.x;
this._bitmap.scaleY = this.entity.transform.scale.y * camera.transform.scale.y;
} }
public onRemovedFromEntity(){ public onRemovedFromEntity(){
if (this._bitmap) if (this.parent)
this.scene.removeChild(this._bitmap); this.parent.removeChild(this);
} }
} }

View File

@@ -1,12 +1,10 @@
class Entity { class Entity extends egret.DisplayObjectContainer {
private static _idGenerator: number; private static _idGenerator: number;
public name: string; public name: string;
public readonly id: number; public readonly id: number;
/** 当前实体所属的场景 */ /** 当前实体所属的场景 */
public scene: Scene; public scene: Scene;
/** 封装实体的位置/旋转/缩放,并允许设置一个高层结构 */
public readonly transform: Transform;
/** 当前附加到此实体的所有组件的列表 */ /** 当前附加到此实体的所有组件的列表 */
public readonly components: ComponentList; public readonly components: ComponentList;
private _updateOrder: number = 0; private _updateOrder: number = 0;
@@ -16,92 +14,26 @@ class Entity {
public componentBits: BitSet; public componentBits: BitSet;
public get parent(){ public get isDestoryed(){
return this.transform.parent; return this._isDestoryed;
}
public set parent(value: Transform){
this.transform.setParent(value);
} }
public get position(){ public get position(){
return this.transform.position; return new Vector2(this.x, this.y);
} }
public set position(value: Vector2){ public set position(value: Vector2){
this.transform.setPosition(value); this.x = value.x;
} this.y = value.y;
public get localPosition(){
return this.transform.localPosition;
}
public set localPosition(value: Vector2){
this.transform.setLocalPosition(value);
}
public get rotation(){
return this.transform.rotation;
}
public set rotation(value: number){
this.transform.setRotation(value);
}
public get rotationDegrees(){
return this.transform.rotationDegrees;
}
public set rotationDegrees(value: number){
this.transform.setRotationDegrees(value);
}
public get localRotation(){
return this.transform.localRotation;
}
public set localRotation(value: number){
this.transform.setLocalRotation(value);
}
public get localRotationDegrees(){
return this.transform.localRotationDegrees;
}
public set localRotationDegrees(value: number){
this.transform.setLocalRotationDegrees(value);
} }
public get scale(){ public get scale(){
return this.transform.scale; return new Vector2(this.scaleX, this.scaleY);
} }
public set scale(value: Vector2){ public set scale(value: Vector2){
this.transform.setScale(value); this.scaleX = value.x;
} this.scaleY = value.y;
public get localScale(){
return this.transform.scale;
}
public set localScale(value: Vector2){
this.transform.setScale(value);
}
public get worldInverseTransform(){
return this.transform.worldInverseTransform;
}
public get localToWorldTransform(){
return this.transform.localToWorldTransform;
}
public get worldToLocalTransform(){
return this.transform.worldToLocalTransform;
}
public get isDestoryed(){
return this._isDestoryed;
} }
public get enabled(){ public get enabled(){
@@ -136,8 +68,8 @@ class Entity {
} }
constructor(name: string){ constructor(name: string){
super();
this.name = name; this.name = name;
this.transform = new Transform(this);
this.components = new ComponentList(this); this.components = new ComponentList(this);
this.id = Entity._idGenerator ++; this.id = Entity._idGenerator ++;
@@ -152,6 +84,10 @@ class Entity {
this.setUpdateOrder(value); this.setUpdateOrder(value);
} }
public roundPosition(){
this.position = Vector2Ext.round(this.position);
}
public setUpdateOrder(updateOrder: number){ public setUpdateOrder(updateOrder: number){
if (this._updateOrder != updateOrder){ if (this._updateOrder != updateOrder){
this._updateOrder = updateOrder; this._updateOrder = updateOrder;
@@ -182,8 +118,8 @@ class Entity {
newScene.entities.add(this); newScene.entities.add(this);
this.components.registerAllComponents(); this.components.registerAllComponents();
for (let i = 0; i < this.transform.childCount; i ++){ for (let i = 0; i < this.numChildren; i ++){
this.transform.getChild(i).entity.attachToScene(newScene); (this.getChildAt(i) as Component).entity.attachToScene(newScene);
} }
} }
@@ -191,13 +127,14 @@ class Entity {
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.components.deregisterAllComponents(); this.components.deregisterAllComponents();
for (let i = 0; i < this.transform.childCount; i ++) for (let i = 0; i < this.numChildren; i ++)
this.transform.getChild(i).entity.detachFromScene(); (this.getChildAt(i) as Component).entity.detachFromScene();
} }
public addComponent<T extends Component>(component: T): T{ public addComponent<T extends Component>(component: T): T{
component.entity = this; component.entity = this;
this.components.add(component); this.components.add(component);
this.addChild(component);
component.initialize(); component.initialize();
return component; return component;
} }
@@ -260,14 +197,14 @@ class Entity {
this.components.onEntityTransformChanged(comp); this.components.onEntityTransformChanged(comp);
} }
public destory(){ public destroy(){
this._isDestoryed = true; this._isDestoryed = true;
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.transform.parent = null; this.removeChildren();
for (let i = this.transform.childCount - 1; i >= 0; i --){ for (let i = this.numChildren - 1; i >= 0; i --){
let child = this.transform.getChild(i); let child = this.getChildAt(i);
child.entity.destory(); (child as Component).entity.destroy();
} }
} }
} }

View File

@@ -6,9 +6,6 @@ class Scene extends egret.DisplayObjectContainer {
public readonly content: ContentManager; public readonly content: ContentManager;
public enablePostProcessing = true; public enablePostProcessing = true;
private _projectionMatrix: Matrix2D;
private _transformMatrix: Matrix2D;
private _matrixTransformMatrix: Matrix2D;
private _renderers: Renderer[] = []; private _renderers: Renderer[] = [];
private _postProcessors: PostProcessor[] = []; private _postProcessors: PostProcessor[] = [];
private _didSceneBegin; private _didSceneBegin;
@@ -17,7 +14,6 @@ class Scene extends egret.DisplayObjectContainer {
constructor() { constructor() {
super(); super();
this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
this.entityProcessors = new EntityProcessorList(); this.entityProcessors = new EntityProcessorList();
this.renderableComponents = new RenderableComponentList(); this.renderableComponents = new RenderableComponentList();
this.entities = new EntityList(this); this.entities = new EntityList(this);
@@ -29,23 +25,24 @@ class Scene extends egret.DisplayObjectContainer {
public createEntity(name: string) { public createEntity(name: string) {
let entity = new Entity(name); let entity = new Entity(name);
entity.transform.position = new Vector2(0, 0); entity.position = new Vector2(0, 0);
return this.addEntity(entity); return this.addEntity(entity);
} }
public addEntity(entity: Entity) { public addEntity(entity: Entity) {
this.entities.add(entity); this.entities.add(entity);
entity.scene = this; entity.scene = this;
this.addChild(entity);
for (let i = 0; i < entity.transform.childCount; i++) for (let i = 0; i < entity.numChildren; i++)
this.addEntity(entity.transform.getChild(i).entity); this.addEntity((entity.getChildAt(i) as Component).entity);
return entity; return entity;
} }
public destroyAllEntities() { public destroyAllEntities() {
for (let i = 0; i < this.entities.count; i++) { for (let i = 0; i < this.entities.count; i++) {
this.entities.buffer[i].destory(); this.entities.buffer[i].destroy();
} }
} }
@@ -134,6 +131,7 @@ class Scene extends egret.DisplayObjectContainer {
} }
this.entities.removeAllEntities(); this.entities.removeAllEntities();
this.removeChildren();
Physics.clear(); Physics.clear();

View File

@@ -50,7 +50,7 @@ class SceneManager {
for (let i = 0; i < SceneManager._scene.entities.buffer.length; i++) { for (let i = 0; i < SceneManager._scene.entities.buffer.length; i++) {
let entity = SceneManager._scene.entities.buffer[i]; let entity = SceneManager._scene.entities.buffer[i];
entity.destory(); entity.destroy();
} }
SceneManager._scene = SceneManager._nextScene; SceneManager._scene = SceneManager._nextScene;

View File

@@ -102,6 +102,9 @@ class ComponentList {
} }
private handleRemove(component: Component){ private handleRemove(component: Component){
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);

View File

@@ -0,0 +1,3 @@
abstract class GraphicsResource {
}

View File

@@ -0,0 +1,33 @@
class GraphicsCapabilities {
public supportsTextureFilterAnisotropic: boolean;
public supportsNonPowerOfTwo: boolean;
public supportsDepth24: boolean;
public supportsPackedDepthStencil: boolean;
public supportsDepthNonLinear: boolean;
public supportsTextureMaxLevel: boolean;
public supportsS3tc: boolean;
public supportsDxt1: boolean;
public supportsPvrtc: boolean;
public supportsAtitc: boolean;
public supportsFramebufferObjectARB: boolean;
public initialize(device: GraphicsDevice){
this.platformInitialize(device);
}
private platformInitialize(device: GraphicsDevice){
let gl: WebGLRenderingContext = new egret.sys.RenderBuffer().context.getInstance();
this.supportsNonPowerOfTwo = false;
this.supportsTextureFilterAnisotropic = gl.getExtension("EXT_texture_filter_anisotropic") != null;
this.supportsDepth24 = true;
this.supportsPackedDepthStencil = true;
this.supportsDepthNonLinear = false;
this.supportsTextureMaxLevel = true;
this.supportsS3tc = gl.getExtension("WEBGL_compressed_texture_s3tc") != null ||
gl.getExtension("WEBGL_compressed_texture_s3tc_srgb") != null;
this.supportsDxt1 = this.supportsS3tc;
this.supportsPvrtc = false;
this.supportsAtitc = gl.getExtension("WEBGL_compressed_texture_astc") != null;
this.supportsFramebufferObjectARB = false;
}
}

View File

@@ -0,0 +1,10 @@
class GraphicsDevice {
private viewport: Viewport;
public graphicsCapabilities: GraphicsCapabilities;
constructor(){
this.graphicsCapabilities = new GraphicsCapabilities();
this.graphicsCapabilities.initialize(this);
}
}

View File

@@ -6,7 +6,7 @@ class PolyLight extends RenderableComponent {
public get bounds(){ public get bounds(){
if (this._areBoundsDirty){ if (this._areBoundsDirty){
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new Vector2(this._radius), this._bounds.calculateBounds(this.entity.position, this._localOffset, new Vector2(this._radius),
Vector2.one, 0, this._radius * 2, this._radius * 2); Vector2.one, 0, this._radius * 2, this._radius * 2);
this._areBoundsDirty = false; this._areBoundsDirty = false;
} }

View File

@@ -16,12 +16,7 @@ abstract class Renderer {
public onAddedToScene(scene: Scene){} public onAddedToScene(scene: Scene){}
protected beginRender(cam: Camera){ protected beginRender(cam: Camera){
cam.transform.updateTransform();
let entities = SceneManager.scene.entities;
for (let i = 0; i < entities.buffer.length; i ++){
entities.buffer[i].transform.updateTransform();
}
} }
/** /**

View File

@@ -0,0 +1,34 @@
class Viewport {
private _x: number;
private _y: number;
private _width: number;
private _height: number;
private _minDepth: number;
private _maxDepth: number;
public get aspectRatio(){
if ((this._height != 0) && (this._width != 0))
return (this._width / this._height);
return 0;
}
public get bounds(){
return new Rectangle(this._x, this._y, this._width, this._height);
}
public set bounds(value: Rectangle){
this._x = value.x;
this._y = value.y;
this._width = value.width;
this._height = value.height;
}
constructor(x: number, y: number, width: number, height: number){
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._minDepth = 0;
this._maxDepth = 1;
}
}

View File

@@ -196,4 +196,9 @@ class Matrix2D {
return result; return result;
} }
public toEgretMatrix(): egret.Matrix{
let matrix = new egret.Matrix(this.m11, this.m12, this.m21, this.m22, this.m31, this.m32);
return matrix;
}
} }

View File

@@ -0,0 +1,11 @@
class Vector3 {
public x: number;
public y: number;
public z: number;
constructor(x: number, y: number, z: number){
this.x = x;
this.y = y;
this.z = z;
}
}

View File

@@ -33,19 +33,19 @@ class Circle extends Shape {
this.center = collider.localOffset; this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotationWithTransform) { if (collider.shouldColliderScaleAndRotationWithTransform) {
let scale = collider.entity.transform.scale; let scale = collider.entity.scale;
let hasUnitScale = scale.x == 1 && scale.y == 1; let hasUnitScale = scale.x == 1 && scale.y == 1;
let maxScale = Math.max(scale.x, scale.y); let maxScale = Math.max(scale.x, scale.y);
this.radius = this._originalRadius * maxScale; this.radius = this._originalRadius * maxScale;
if (collider.entity.transform.rotation != 0) { if (collider.entity.rotation != 0) {
let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg; let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
let offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.transform.scale)).length(); let offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, collider.entity.transform.rotationDegrees + offsetAngle); this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle);
} }
} }
this.position = Vector2.add(collider.entity.transform.position, this.center); this.position = Vector2.add(collider.entity.position, this.center);
this.bounds = new Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2); this.bounds = new Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2);
} }

View File

@@ -175,35 +175,35 @@ class Polygon extends Shape {
let tempMat: Matrix2D; let tempMat: Matrix2D;
let combinedMatrix = Matrix2D.createTranslation(-this._polygonCenter.x, -this._polygonCenter.y); let combinedMatrix = Matrix2D.createTranslation(-this._polygonCenter.x, -this._polygonCenter.y);
if (collider.entity.transform.scale != Vector2.one){ if (collider.entity.scale != Vector2.one){
tempMat = Matrix2D.createScale(collider.entity.transform.scale.x, collider.entity.transform.scale.y); tempMat = Matrix2D.createScale(collider.entity.scale.x, collider.entity.scale.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
hasUnitScale = false; hasUnitScale = false;
let scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.transform.scale); let scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale);
this.center = scaledOffset; this.center = scaledOffset;
} }
if (collider.entity.transform.rotation != 0){ if (collider.entity.rotation != 0){
tempMat = Matrix2D.createRotation(collider.entity.transform.rotation); tempMat = Matrix2D.createRotation(collider.entity.rotation);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg; let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
let offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.transform.scale)).length(); let offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, collider.entity.transform.rotationDegrees + offsetAngle); this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle);
} }
tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y); tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points); Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points);
this.isUnrotated = collider.entity.transform.rotation == 0; this.isUnrotated = collider.entity.rotation == 0;
if (collider._isRotationDirty) if (collider._isRotationDirty)
this._areEdgeNormalsDirty = true; this._areEdgeNormalsDirty = true;
} }
this.position = Vector2.add(collider.entity.transform.position, this.center); this.position = Vector2.add(collider.entity.position, this.center);
this.bounds = Rectangle.rectEncompassingPoints(this.points); this.bounds = Rectangle.rectEncompassingPoints(this.points);
this.bounds.location = Vector2.add(this.bounds.location, this.position); this.bounds.location = Vector2.add(this.bounds.location, this.position);
} }

View File

@@ -63,4 +63,8 @@ class Vector2Ext {
public static transform(sourceArray: Vector2[], matrix: Matrix2D, destinationArray: Vector2[]) { public static transform(sourceArray: Vector2[], matrix: Matrix2D, destinationArray: Vector2[]) {
this.transformA(sourceArray, 0, matrix, destinationArray, 0, sourceArray.length); this.transformA(sourceArray, 0, matrix, destinationArray, 0, sourceArray.length);
} }
public static round(vec: Vector2){
return new Vector2(Math.round(vec.x), Math.round(vec.y));
}
} }

View File

@@ -1,48 +0,0 @@
class WebGLUtils {
public static getWebGL(): WebGLRenderingContext {
if (egret.WebGLUtils.checkCanUseWebGL())
return document.querySelector("canvas").getContext("webgl");
throw new Error("cannot get webgl");
}
public static drawUserIndexPrimitives<T>(primitiveType: number, vertexData: T[], vertexOffset: number,
numVertices: number, indexData: number[], indexOffset: number, primitiveCount: number) {
let GL = this.getWebGL();
GL.bindBuffer(GL.ARRAY_BUFFER, 0);
this.checkGLError();
GL.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, 0);
this.checkGLError();
GL.drawElements(primitiveType,
this.getElementCountArray(primitiveType, primitiveCount),
GL.UNSIGNED_SHORT,
indexOffset * 2);
this.checkGLError();
}
private static getElementCountArray(primitiveType: number, primitiveCount: number) {
let GL = this.getWebGL();
switch (primitiveType) {
case GL.LINES:
return primitiveCount * 2;
case GL.LINE_STRIP:
return primitiveCount + 1;
case GL.TRIANGLES:
return primitiveCount * 3;
case GL.TRIANGLE_STRIP:
return primitiveCount + 2;
}
throw new Error("not support");
}
public static checkGLError() {
let GL = this.getWebGL();
let error = GL.getError();
if (error != GL.NO_ERROR) {
throw new Error("GL.GetError() returned" + error);
}
}
}