ecs适配egret
This commit is contained in:
@@ -4,7 +4,6 @@ class Camera extends Component {
|
||||
private _origin: Vector2 = Vector2.zero;
|
||||
private _transformMatrix: Matrix2D = new Matrix2D();
|
||||
private _inverseTransformMatrix = new Matrix2D();
|
||||
private _projectionMatrix = new Matrix2D();
|
||||
|
||||
private _minimumZoom = 0.3;
|
||||
private _maximumZoom = 3;
|
||||
@@ -12,7 +11,6 @@ class Camera extends Component {
|
||||
private _inset: CameraInset = new CameraInset();
|
||||
private _bounds: Rectangle = new Rectangle();
|
||||
private _areBoundsDirty = true;
|
||||
private _isProjectionMatrixDirty = true;
|
||||
|
||||
public get bounds(){
|
||||
if (this._areMatrixesDirty)
|
||||
@@ -23,7 +21,7 @@ class Camera extends Component {
|
||||
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));
|
||||
|
||||
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 bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom));
|
||||
|
||||
@@ -89,11 +87,11 @@ class Camera extends Component {
|
||||
}
|
||||
|
||||
public get position(){
|
||||
return this.entity.transform.position;
|
||||
return this.entity.position;
|
||||
}
|
||||
|
||||
public set position(value: Vector2){
|
||||
this.entity.transform.position = value;
|
||||
this.entity.position = value;
|
||||
}
|
||||
|
||||
public get transformMatrix(){
|
||||
@@ -115,11 +113,10 @@ class Camera extends Component {
|
||||
}
|
||||
|
||||
public onSceneSizeChanged(newWidth: number, newHeight: number){
|
||||
this._isProjectionMatrixDirty = true;
|
||||
let oldOrigin = this._origin;
|
||||
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{
|
||||
@@ -154,7 +151,7 @@ class Camera extends Component {
|
||||
}
|
||||
|
||||
public setPosition(position: Vector2){
|
||||
this.entity.transform.setPosition(position);
|
||||
this.entity.position = position;
|
||||
|
||||
return this;
|
||||
}
|
||||
@@ -168,13 +165,13 @@ class Camera extends Component {
|
||||
return;
|
||||
|
||||
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){
|
||||
tempMat = Matrix2D.createScale(this._zoom, this._zoom);
|
||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
||||
}
|
||||
|
||||
if (this.entity.transform.rotation != 0){
|
||||
if (this.entity.rotation != 0){
|
||||
tempMat = Matrix2D.createRotation(this.entity.rotation);
|
||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
||||
}
|
||||
|
||||
@@ -55,11 +55,11 @@ class FollowCamera extends Component {
|
||||
this.updateFollow();
|
||||
|
||||
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){
|
||||
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;
|
||||
|
||||
if (this._cameraStyle == CameraStyle.lockOn){
|
||||
let targetX = this._targetEntity.transform.position.x;
|
||||
let targetY = this._targetEntity.transform.position.y;
|
||||
let targetX = this._targetEntity.position.x;
|
||||
let targetY = this._targetEntity.position.y;
|
||||
|
||||
if (this._worldSpaceDeadZone.x > targetX)
|
||||
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
||||
|
||||
@@ -82,15 +82,15 @@ abstract class Collider extends Component{
|
||||
if (renderable){
|
||||
let renderbaleBounds = renderable.bounds;
|
||||
|
||||
let width = renderbaleBounds.width / this.entity.transform.scale.x;
|
||||
let height = renderbaleBounds.height / this.entity.transform.scale.y;
|
||||
let width = renderbaleBounds.width / this.entity.scale.x;
|
||||
let height = renderbaleBounds.height / this.entity.scale.y;
|
||||
|
||||
if (this instanceof BoxCollider){
|
||||
let boxCollider = this as BoxCollider;
|
||||
boxCollider.width = width;
|
||||
boxCollider.height = height;
|
||||
|
||||
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.transform.position);
|
||||
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class Mover extends Component {
|
||||
}
|
||||
|
||||
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)
|
||||
this._triggerHelper.update();
|
||||
|
||||
@@ -31,13 +31,7 @@ abstract class RenderableComponent extends Component implements IRenderable {
|
||||
}
|
||||
|
||||
public get bounds(): Rectangle{
|
||||
if (this._areBoundsDirty){
|
||||
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;
|
||||
return new Rectangle(this.getBounds().x, this.getBounds().y, this.getBounds().width, this.getBounds().height);
|
||||
}
|
||||
|
||||
protected getWidth(){
|
||||
|
||||
@@ -1,39 +1,5 @@
|
||||
class SpriteRenderer extends RenderableComponent {
|
||||
private _sprite: Sprite;
|
||||
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(){
|
||||
return this._origin;
|
||||
@@ -49,6 +15,11 @@ class SpriteRenderer extends RenderableComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public setSprite(sprite: Sprite){
|
||||
this.removeChildren();
|
||||
this.addChild(new egret.Bitmap(sprite.texture2D));
|
||||
}
|
||||
|
||||
public setColor(color: number){
|
||||
let colorMatrix = [
|
||||
1, 0, 0, 0, 0,
|
||||
@@ -60,31 +31,22 @@ class SpriteRenderer extends RenderableComponent {
|
||||
colorMatrix[6] = Math.floor(color / 256 % 256) / 255;
|
||||
colorMatrix[12] = color % 256 / 255;
|
||||
let colorFilter = new egret.ColorMatrixFilter(colorMatrix);
|
||||
this._bitmap.filters = [colorFilter];
|
||||
this.filters = [colorFilter];
|
||||
}
|
||||
|
||||
public isVisibleFromCamera(camera: Camera): boolean{
|
||||
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._bitmap.visible = this.isVisible;
|
||||
this.visible = this.isVisible;
|
||||
return this.isVisible;
|
||||
}
|
||||
|
||||
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(){
|
||||
if (this._bitmap)
|
||||
this.scene.removeChild(this._bitmap);
|
||||
if (this.parent)
|
||||
this.parent.removeChild(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user