新增Sprite用于控制纹理
This commit is contained in:
@@ -29,6 +29,13 @@ abstract class Component {
|
||||
return this;
|
||||
}
|
||||
|
||||
public get stage(){
|
||||
if (!this.entity)
|
||||
return null;
|
||||
|
||||
return this.entity.stage;
|
||||
}
|
||||
|
||||
public initialize(){
|
||||
|
||||
}
|
||||
|
||||
@@ -19,9 +19,9 @@ class Camera extends Component {
|
||||
this.updateMatrixes();
|
||||
|
||||
if (this._areBoundsDirty){
|
||||
let stage = this.entity.scene.stage;
|
||||
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 stage = this.stage;
|
||||
let topLeft = this.screenToWorldPoint(new Vector2(stage.x + this._inset.left, stage.y + this._inset.top));
|
||||
let bottomRight = this.screenToWorldPoint(new Vector2(stage.x + stage.stageWidth - this._inset.right, stage.y + stage.stageHeight - this._inset.bottom));
|
||||
|
||||
if (this.entity.transform.rotation != 0){
|
||||
let topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
|
||||
@@ -163,7 +163,7 @@ class Camera extends Component {
|
||||
this._areMatrixesDirty = true;
|
||||
}
|
||||
|
||||
public updateMatrixes(){
|
||||
protected updateMatrixes(){
|
||||
if (!this._areMatrixesDirty)
|
||||
return;
|
||||
|
||||
@@ -208,8 +208,8 @@ class Camera extends Component {
|
||||
}
|
||||
|
||||
class CameraInset {
|
||||
public left;
|
||||
public right;
|
||||
public top;
|
||||
public bottom;
|
||||
public left = 0;
|
||||
public right = 0;
|
||||
public top = 0;
|
||||
public bottom = 0;
|
||||
}
|
||||
@@ -29,7 +29,13 @@ abstract class RenderableComponent extends Component implements IRenderable {
|
||||
}
|
||||
|
||||
public get bounds(): Rectangle{
|
||||
return this.getBounds();
|
||||
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;
|
||||
}
|
||||
|
||||
protected getWidth(){
|
||||
@@ -40,16 +46,6 @@ abstract class RenderableComponent extends Component implements IRenderable {
|
||||
return this.bounds.height;
|
||||
}
|
||||
|
||||
protected getBounds(){
|
||||
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;
|
||||
}
|
||||
|
||||
protected onBecameVisible(){}
|
||||
|
||||
protected onBecameInvisible(){}
|
||||
@@ -57,10 +53,8 @@ abstract class RenderableComponent extends Component implements IRenderable {
|
||||
public abstract render(camera: Camera);
|
||||
|
||||
public isVisibleFromCamera(camera: Camera): boolean{
|
||||
// this.isVisible = camera.bounds.intersects(this.bounds);
|
||||
// return this.isVisible;
|
||||
|
||||
return true;
|
||||
this.isVisible = camera.bounds.intersects(this.bounds);
|
||||
return this.isVisible;
|
||||
}
|
||||
|
||||
public onEntityTransformChanged(comp: ComponentTransform){
|
||||
|
||||
24
source/src/ECS/Components/Sprite.ts
Normal file
24
source/src/ECS/Components/Sprite.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
class Sprite {
|
||||
public texture2D: egret.Texture;
|
||||
public readonly sourceRect: Rectangle;
|
||||
public readonly center: Vector2;
|
||||
public origin: Vector2;
|
||||
public readonly uvs: Rectangle = new Rectangle();
|
||||
|
||||
constructor(texture: egret.Texture,
|
||||
sourceRect: Rectangle = new Rectangle(texture.textureWidth, texture.textureHeight),
|
||||
origin: Vector2 = sourceRect.getHalfSize()){
|
||||
this.texture2D = texture;
|
||||
this.sourceRect = sourceRect;
|
||||
this.center = new Vector2(sourceRect.width * 0.5, sourceRect.height * 0.5);
|
||||
this.origin = origin;
|
||||
|
||||
let inverseTexW = 1 / texture.textureWidth;
|
||||
let inverseTexH = 1 / texture.textureHeight
|
||||
|
||||
this.uvs.x = sourceRect.x * inverseTexW;
|
||||
this.uvs.y = sourceRect.y * inverseTexH;
|
||||
this.uvs.width = sourceRect.width * inverseTexW;
|
||||
this.uvs.height = sourceRect.height * inverseTexH;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
enum SpriteEffects {
|
||||
none = 0,
|
||||
flipHorizontally = 1,
|
||||
flipVertically = 2
|
||||
}
|
||||
@@ -1,32 +1,36 @@
|
||||
class SpriteRenderer extends RenderableComponent {
|
||||
private _sprite: egret.DisplayObject;
|
||||
private _sprite: Sprite;
|
||||
private _origin: Vector2;
|
||||
private _bitmap: egret.Bitmap;
|
||||
|
||||
protected getBounds(){
|
||||
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.width,
|
||||
this._sprite.height);
|
||||
this.entity.transform.scale, this.entity.transform.rotation, this._sprite.texture2D.textureWidth,
|
||||
this._sprite.texture2D.textureHeight);
|
||||
this._areBoundsDirty = false;
|
||||
}
|
||||
|
||||
return this.bounds;
|
||||
}
|
||||
|
||||
return this._bounds;
|
||||
}
|
||||
|
||||
public get sprite(){
|
||||
return this._sprite;
|
||||
}
|
||||
|
||||
public set sprite(value: egret.DisplayObject){
|
||||
public set sprite(value: Sprite){
|
||||
this.setSprite(value);
|
||||
}
|
||||
|
||||
public setSprite(sprite: egret.DisplayObject): SpriteRenderer{
|
||||
public setSprite(sprite: Sprite): SpriteRenderer{
|
||||
this._sprite = sprite;
|
||||
if (this._sprite)
|
||||
this._origin = new Vector2(this._sprite.anchorOffsetX, this._sprite.anchorOffsetY);
|
||||
this._origin = sprite.origin;
|
||||
|
||||
this._bitmap = new egret.Bitmap(sprite.texture2D);
|
||||
this.stage.addChild(this._bitmap);
|
||||
|
||||
return this;
|
||||
}
|
||||
@@ -45,16 +49,22 @@ class SpriteRenderer extends RenderableComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public render(camera: Camera) {
|
||||
public isVisibleFromCamera(camera: Camera): boolean{
|
||||
this.isVisible = new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds);
|
||||
this._bitmap.visible = this.isVisible;
|
||||
return this.isVisible;
|
||||
}
|
||||
|
||||
public render(camera: Camera){
|
||||
if (!this.sprite)
|
||||
return;
|
||||
|
||||
this.sprite.x = this.entity.transform.position.x - camera.transform.position.x;
|
||||
this.sprite.y = this.entity.transform.position.y - camera.transform.position.y;
|
||||
this.sprite.rotation = this.entity.transform.rotation;
|
||||
this.sprite.anchorOffsetX = this._origin.x;
|
||||
this.sprite.anchorOffsetY = this._origin.y;
|
||||
this.sprite.scaleX = this.entity.transform.scale.x;
|
||||
this.sprite.scaleY = this.entity.transform.scale.y;
|
||||
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;
|
||||
this._bitmap.anchorOffsetX = this._origin.x;
|
||||
this._bitmap.anchorOffsetY = this._origin.y;
|
||||
this._bitmap.scaleX = this.entity.transform.scale.x;
|
||||
this._bitmap.scaleY = this.entity.transform.scale.y;
|
||||
}
|
||||
}
|
||||
@@ -128,6 +128,13 @@ class Entity {
|
||||
this.setTag(value);
|
||||
}
|
||||
|
||||
public get stage(){
|
||||
if (!this.scene)
|
||||
return null;
|
||||
|
||||
return this.scene.stage;
|
||||
}
|
||||
|
||||
constructor(name: string){
|
||||
this.name = name;
|
||||
this.transform = new Transform(this);
|
||||
|
||||
@@ -25,9 +25,9 @@ class Transform {
|
||||
private _rotationMatrix: Matrix2D;
|
||||
private _scaleMatrix: Matrix2D;
|
||||
|
||||
private _worldTransform = new Matrix2D();
|
||||
private _worldToLocalTransform = new Matrix2D();
|
||||
private _worldInverseTransform = new Matrix2D();
|
||||
private _worldTransform = Matrix2D.identity;
|
||||
private _worldToLocalTransform = Matrix2D.identity;
|
||||
private _worldInverseTransform = Matrix2D.identity;
|
||||
|
||||
private _rotation: number = 0;
|
||||
private _position: Vector2;
|
||||
@@ -130,13 +130,18 @@ class Transform {
|
||||
|
||||
public get position(){
|
||||
this.updateTransform();
|
||||
if (!this.parent){
|
||||
this._position = this._localPosition;
|
||||
}else{
|
||||
this.parent.updateTransform();
|
||||
this._position = Vector2Ext.transformR(this._localPosition, this.parent._worldTransform);
|
||||
if (this._positionDirty){
|
||||
if (!this.parent){
|
||||
this._position = this._localPosition;
|
||||
}else{
|
||||
this.parent.updateTransform();
|
||||
this._position = Vector2Ext.transformR(this._localPosition, this.parent._worldTransform);
|
||||
}
|
||||
|
||||
this._positionDirty = false;
|
||||
}
|
||||
|
||||
|
||||
return this._position;
|
||||
}
|
||||
|
||||
@@ -255,6 +260,8 @@ class Transform {
|
||||
this.localPosition = position;
|
||||
}
|
||||
|
||||
this._positionDirty = false;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -303,13 +310,11 @@ class Transform {
|
||||
this._localRotationDirty = false;
|
||||
}
|
||||
|
||||
|
||||
if (this._localScaleDirty){
|
||||
this._scaleMatrix = Matrix2D.createScale(this._localScale.x, this._localScale.y);
|
||||
this._localScaleDirty = false;
|
||||
}
|
||||
|
||||
|
||||
this._localTransform = Matrix2D.multiply(this._scaleMatrix, this._rotationMatrix);
|
||||
this._localTransform = Matrix2D.multiply(this._localTransform, this._translationMatrix);
|
||||
|
||||
|
||||
@@ -71,6 +71,10 @@ class Rectangle {
|
||||
(value.y < (this.y + this.height)));
|
||||
}
|
||||
|
||||
public getHalfSize(){
|
||||
return new Vector2(this.width * 0.5, this.height * 0.5);
|
||||
}
|
||||
|
||||
public static fromMinMax(minX: number, minY: number, maxX: number, maxY: number) {
|
||||
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
|
||||
}
|
||||
@@ -144,10 +148,10 @@ class Rectangle {
|
||||
let bottomLeft = new Vector2(worldPosX, worldPosY + height);
|
||||
let bottomRight = new Vector2(worldPosX + width, worldPosY + height);
|
||||
|
||||
topLeft = Vector2.transform(topLeft, this._transformMat);
|
||||
topRight = Vector2.transform(topRight, this._transformMat);
|
||||
bottomLeft = Vector2.transform(bottomLeft, this._transformMat);
|
||||
bottomRight = Vector2.transform(bottomRight, this._transformMat);
|
||||
topLeft = Vector2Ext.transformR(topLeft, this._transformMat);
|
||||
topRight = Vector2Ext.transformR(topRight, this._transformMat);
|
||||
bottomLeft = Vector2Ext.transformR(bottomLeft, this._transformMat);
|
||||
bottomRight = Vector2Ext.transformR(bottomRight, this._transformMat);
|
||||
|
||||
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
|
||||
Reference in New Issue
Block a user