更新相机强制刷新矩阵
This commit is contained in:
8
demo/libs/framework/framework.d.ts
vendored
8
demo/libs/framework/framework.d.ts
vendored
@@ -330,12 +330,12 @@ declare class Camera extends Component {
|
|||||||
setMinimumZoom(minZoom: number): Camera;
|
setMinimumZoom(minZoom: number): Camera;
|
||||||
setMaximumZoom(maxZoom: number): Camera;
|
setMaximumZoom(maxZoom: number): Camera;
|
||||||
setZoom(zoom: number): this;
|
setZoom(zoom: number): this;
|
||||||
initialize(): void;
|
|
||||||
update(): void;
|
|
||||||
setPosition(position: Vector2): this;
|
setPosition(position: Vector2): this;
|
||||||
|
forceMatrixUpdate(): void;
|
||||||
updateMatrixes(): void;
|
updateMatrixes(): void;
|
||||||
screenToWorldPoint(screenPosition: Vector2): Vector2;
|
screenToWorldPoint(screenPosition: Vector2): Vector2;
|
||||||
worldToScreenPoint(worldPosition: Vector2): Vector2;
|
worldToScreenPoint(worldPosition: Vector2): Vector2;
|
||||||
|
onEntityTransformChanged(comp: ComponentTransform): void;
|
||||||
destory(): void;
|
destory(): void;
|
||||||
}
|
}
|
||||||
declare class CameraInset {
|
declare class CameraInset {
|
||||||
@@ -574,6 +574,7 @@ declare class Time {
|
|||||||
declare abstract class Renderer {
|
declare abstract class Renderer {
|
||||||
camera: Camera;
|
camera: Camera;
|
||||||
onAddedToScene(scene: Scene): void;
|
onAddedToScene(scene: Scene): void;
|
||||||
|
protected beginRender(cam: Camera): void;
|
||||||
abstract render(scene: Scene): any;
|
abstract render(scene: Scene): any;
|
||||||
protected renderAfterStateCheck(renderable: IRenderable, cam: Camera): void;
|
protected renderAfterStateCheck(renderable: IRenderable, cam: Camera): void;
|
||||||
}
|
}
|
||||||
@@ -618,7 +619,7 @@ declare class Matrix2D {
|
|||||||
m32: number;
|
m32: number;
|
||||||
private static _identity;
|
private static _identity;
|
||||||
static readonly identity: Matrix2D;
|
static readonly identity: Matrix2D;
|
||||||
constructor(m11: number, m12: number, m21: number, m22: number, m31: number, m32: number);
|
constructor(m11?: number, m12?: number, m21?: number, m22?: number, m31?: number, m32?: number);
|
||||||
translation: Vector2;
|
translation: Vector2;
|
||||||
rotation: number;
|
rotation: number;
|
||||||
rotationDegrees: number;
|
rotationDegrees: number;
|
||||||
@@ -880,6 +881,7 @@ declare class Vector2Ext {
|
|||||||
static perpendicular(first: Vector2, second: Vector2): Vector2;
|
static perpendicular(first: Vector2, second: Vector2): Vector2;
|
||||||
static normalize(vec: Vector2): Vector2;
|
static normalize(vec: Vector2): Vector2;
|
||||||
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 transform(sourceArray: Vector2[], matrix: Matrix2D, destinationArray: Vector2[]): void;
|
static transform(sourceArray: Vector2[], matrix: Matrix2D, destinationArray: Vector2[]): void;
|
||||||
}
|
}
|
||||||
declare class WebGLUtils {
|
declare class WebGLUtils {
|
||||||
|
|||||||
@@ -1036,7 +1036,6 @@ var Entity = (function () {
|
|||||||
};
|
};
|
||||||
Entity.prototype.update = function () {
|
Entity.prototype.update = function () {
|
||||||
this.components.update();
|
this.components.update();
|
||||||
this.transform.updateTransform();
|
|
||||||
};
|
};
|
||||||
Entity.prototype.onAddedToScene = function () {
|
Entity.prototype.onAddedToScene = function () {
|
||||||
};
|
};
|
||||||
@@ -1156,6 +1155,9 @@ var Scene = (function (_super) {
|
|||||||
};
|
};
|
||||||
Scene.prototype.render = function () {
|
Scene.prototype.render = function () {
|
||||||
for (var i = 0; i < this._renderers.length; i++) {
|
for (var i = 0; i < this._renderers.length; i++) {
|
||||||
|
if (this._renderers[i].camera)
|
||||||
|
this._renderers[i].camera.forceMatrixUpdate();
|
||||||
|
this.camera.forceMatrixUpdate();
|
||||||
this._renderers[i].render(this);
|
this._renderers[i].render(this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1215,9 +1217,9 @@ var ComponentTransform;
|
|||||||
var Transform = (function () {
|
var Transform = (function () {
|
||||||
function Transform(entity) {
|
function Transform(entity) {
|
||||||
this._localRotation = 0;
|
this._localRotation = 0;
|
||||||
this._worldTransform = Matrix2D.identity;
|
this._worldTransform = new Matrix2D();
|
||||||
this._worldToLocalTransform = Matrix2D.identity;
|
this._worldToLocalTransform = new Matrix2D();
|
||||||
this._worldInverseTransform = Matrix2D.identity;
|
this._worldInverseTransform = new Matrix2D();
|
||||||
this._rotation = 0;
|
this._rotation = 0;
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
this._scale = this._localScale = Vector2.one;
|
this._scale = this._localScale = Vector2.one;
|
||||||
@@ -1257,7 +1259,7 @@ var Transform = (function () {
|
|||||||
get: function () {
|
get: function () {
|
||||||
if (this._worldToLocalDirty) {
|
if (this._worldToLocalDirty) {
|
||||||
if (!this.parent) {
|
if (!this.parent) {
|
||||||
this._worldInverseTransform = Matrix2D.identity;
|
this._worldInverseTransform = new Matrix2D();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.parent.updateTransform();
|
this.parent.updateTransform();
|
||||||
@@ -1320,7 +1322,7 @@ var Transform = (function () {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.parent.updateTransform();
|
this.parent.updateTransform();
|
||||||
this._position = Vector2.transform(this._localPosition, this.parent._worldTransform);
|
this._position = Vector2Ext.transformR(this._localPosition, this.parent._worldTransform);
|
||||||
}
|
}
|
||||||
return this._position;
|
return this._position;
|
||||||
},
|
},
|
||||||
@@ -1507,9 +1509,9 @@ var Camera = (function (_super) {
|
|||||||
function Camera() {
|
function Camera() {
|
||||||
var _this = _super.call(this) || this;
|
var _this = _super.call(this) || this;
|
||||||
_this._origin = Vector2.zero;
|
_this._origin = Vector2.zero;
|
||||||
_this._transformMatrix = Matrix2D.identity;
|
_this._transformMatrix = new Matrix2D();
|
||||||
_this._inverseTransformMatrix = Matrix2D.identity;
|
_this._inverseTransformMatrix = new Matrix2D();
|
||||||
_this._projectionMatrix = Matrix2D.identity;
|
_this._projectionMatrix = new Matrix2D();
|
||||||
_this._minimumZoom = 0.3;
|
_this._minimumZoom = 0.3;
|
||||||
_this._maximumZoom = 3;
|
_this._maximumZoom = 3;
|
||||||
_this._areMatrixesDirty = true;
|
_this._areMatrixesDirty = true;
|
||||||
@@ -1648,14 +1650,13 @@ var Camera = (function (_super) {
|
|||||||
this._areMatrixesDirty = true;
|
this._areMatrixesDirty = true;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Camera.prototype.initialize = function () {
|
|
||||||
};
|
|
||||||
Camera.prototype.update = function () {
|
|
||||||
};
|
|
||||||
Camera.prototype.setPosition = function (position) {
|
Camera.prototype.setPosition = function (position) {
|
||||||
this.entity.transform.setPosition(position);
|
this.entity.transform.setPosition(position);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
Camera.prototype.forceMatrixUpdate = function () {
|
||||||
|
this._areMatrixesDirty = true;
|
||||||
|
};
|
||||||
Camera.prototype.updateMatrixes = function () {
|
Camera.prototype.updateMatrixes = function () {
|
||||||
if (!this._areMatrixesDirty)
|
if (!this._areMatrixesDirty)
|
||||||
return;
|
return;
|
||||||
@@ -1665,6 +1666,10 @@ var Camera = (function (_super) {
|
|||||||
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) {
|
||||||
|
tempMat = Matrix2D.createRotation(this.entity.rotation);
|
||||||
|
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
||||||
|
}
|
||||||
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
|
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
||||||
this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix);
|
this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix);
|
||||||
@@ -1673,11 +1678,14 @@ var Camera = (function (_super) {
|
|||||||
};
|
};
|
||||||
Camera.prototype.screenToWorldPoint = function (screenPosition) {
|
Camera.prototype.screenToWorldPoint = function (screenPosition) {
|
||||||
this.updateMatrixes();
|
this.updateMatrixes();
|
||||||
return Vector2.transform(screenPosition, this._inverseTransformMatrix);
|
return Vector2Ext.transformR(screenPosition, this._inverseTransformMatrix);
|
||||||
};
|
};
|
||||||
Camera.prototype.worldToScreenPoint = function (worldPosition) {
|
Camera.prototype.worldToScreenPoint = function (worldPosition) {
|
||||||
this.updateMatrixes();
|
this.updateMatrixes();
|
||||||
return Vector2.transform(worldPosition, this._transformMatrix);
|
return Vector2Ext.transformR(worldPosition, this._transformMatrix);
|
||||||
|
};
|
||||||
|
Camera.prototype.onEntityTransformChanged = function (comp) {
|
||||||
|
this._areMatrixesDirty = true;
|
||||||
};
|
};
|
||||||
Camera.prototype.destory = function () {
|
Camera.prototype.destory = function () {
|
||||||
};
|
};
|
||||||
@@ -1869,8 +1877,8 @@ var SpriteRenderer = (function (_super) {
|
|||||||
SpriteRenderer.prototype.render = function (camera) {
|
SpriteRenderer.prototype.render = function (camera) {
|
||||||
if (!this.sprite)
|
if (!this.sprite)
|
||||||
return;
|
return;
|
||||||
this.sprite.x = this.entity.transform.position.x;
|
this.sprite.x = this.entity.transform.position.x - camera.transform.position.x;
|
||||||
this.sprite.y = this.entity.transform.position.y;
|
this.sprite.y = this.entity.transform.position.y - camera.transform.position.y;
|
||||||
this.sprite.rotation = this.entity.transform.rotation;
|
this.sprite.rotation = this.entity.transform.rotation;
|
||||||
this.sprite.anchorOffsetX = this._origin.x;
|
this.sprite.anchorOffsetX = this._origin.x;
|
||||||
this.sprite.anchorOffsetY = this._origin.y;
|
this.sprite.anchorOffsetY = this._origin.y;
|
||||||
@@ -2717,6 +2725,13 @@ var Renderer = (function () {
|
|||||||
function Renderer() {
|
function Renderer() {
|
||||||
}
|
}
|
||||||
Renderer.prototype.onAddedToScene = function (scene) { };
|
Renderer.prototype.onAddedToScene = function (scene) { };
|
||||||
|
Renderer.prototype.beginRender = function (cam) {
|
||||||
|
cam.transform.updateTransform();
|
||||||
|
var entities = SceneManager.getActiveScene().entities;
|
||||||
|
for (var i = 0; i < entities.buffer.length; i++) {
|
||||||
|
entities.buffer[i].transform.updateTransform();
|
||||||
|
}
|
||||||
|
};
|
||||||
Renderer.prototype.renderAfterStateCheck = function (renderable, cam) {
|
Renderer.prototype.renderAfterStateCheck = function (renderable, cam) {
|
||||||
renderable.render(cam);
|
renderable.render(cam);
|
||||||
};
|
};
|
||||||
@@ -2729,6 +2744,7 @@ var DefaultRenderer = (function (_super) {
|
|||||||
}
|
}
|
||||||
DefaultRenderer.prototype.render = function (scene) {
|
DefaultRenderer.prototype.render = function (scene) {
|
||||||
var cam = this.camera ? this.camera : scene.camera;
|
var cam = this.camera ? this.camera : scene.camera;
|
||||||
|
this.beginRender(cam);
|
||||||
for (var i = 0; i < scene.renderableComponents.count; i++) {
|
for (var i = 0; i < scene.renderableComponents.count; i++) {
|
||||||
var renderable = scene.renderableComponents.buffer[i];
|
var renderable = scene.renderableComponents.buffer[i];
|
||||||
if (renderable.enabled && renderable.isVisibleFromCamera(cam))
|
if (renderable.enabled && renderable.isVisibleFromCamera(cam))
|
||||||
@@ -2810,12 +2826,12 @@ var Matrix2D = (function () {
|
|||||||
this.m22 = 0;
|
this.m22 = 0;
|
||||||
this.m31 = 0;
|
this.m31 = 0;
|
||||||
this.m32 = 0;
|
this.m32 = 0;
|
||||||
this.m11 = m11;
|
this.m11 = m11 ? m11 : 1;
|
||||||
this.m12 = m12;
|
this.m12 = m12 ? m12 : 0;
|
||||||
this.m21 = m21;
|
this.m21 = m21 ? m21 : 0;
|
||||||
this.m22 = m22;
|
this.m22 = m22 ? m22 : 1;
|
||||||
this.m31 = m31;
|
this.m31 = m31 ? m31 : 0;
|
||||||
this.m32 = m32;
|
this.m32 = m32 ? m32 : 0;
|
||||||
}
|
}
|
||||||
Object.defineProperty(Matrix2D, "identity", {
|
Object.defineProperty(Matrix2D, "identity", {
|
||||||
get: function () {
|
get: function () {
|
||||||
@@ -2890,19 +2906,20 @@ var Matrix2D = (function () {
|
|||||||
return matrix1;
|
return matrix1;
|
||||||
};
|
};
|
||||||
Matrix2D.multiply = function (matrix1, matrix2) {
|
Matrix2D.multiply = function (matrix1, matrix2) {
|
||||||
|
var result = new Matrix2D();
|
||||||
var m11 = (matrix1.m11 * matrix2.m11) + (matrix1.m12 * matrix2.m21);
|
var m11 = (matrix1.m11 * matrix2.m11) + (matrix1.m12 * matrix2.m21);
|
||||||
var m12 = (matrix1.m11 * matrix2.m12) + (matrix1.m12 * matrix2.m22);
|
var m12 = (matrix1.m11 * matrix2.m12) + (matrix1.m12 * matrix2.m22);
|
||||||
var m21 = (matrix1.m21 * matrix2.m11) + (matrix1.m22 * matrix2.m21);
|
var m21 = (matrix1.m21 * matrix2.m11) + (matrix1.m22 * matrix2.m21);
|
||||||
var m22 = (matrix1.m21 * matrix2.m12) + (matrix1.m22 * matrix2.m22);
|
var m22 = (matrix1.m21 * matrix2.m12) + (matrix1.m22 * matrix2.m22);
|
||||||
var m31 = (matrix1.m31 * matrix2.m11) + (matrix1.m32 * matrix2.m21) + matrix2.m31;
|
var m31 = (matrix1.m31 * matrix2.m11) + (matrix1.m32 * matrix2.m21) + matrix2.m31;
|
||||||
var m32 = (matrix1.m31 * matrix2.m12) + (matrix1.m32 * matrix2.m22) + matrix2.m32;
|
var m32 = (matrix1.m31 * matrix2.m12) + (matrix1.m32 * matrix2.m22) + matrix2.m32;
|
||||||
matrix1.m11 = m11;
|
result.m11 = m11;
|
||||||
matrix1.m12 = m12;
|
result.m12 = m12;
|
||||||
matrix1.m21 = m21;
|
result.m21 = m21;
|
||||||
matrix1.m22 = m22;
|
result.m22 = m22;
|
||||||
matrix1.m31 = m31;
|
result.m31 = m31;
|
||||||
matrix1.m32 = m32;
|
result.m32 = m32;
|
||||||
return matrix1;
|
return result;
|
||||||
};
|
};
|
||||||
Matrix2D.multiplyTranslation = function (matrix, x, y) {
|
Matrix2D.multiplyTranslation = function (matrix, x, y) {
|
||||||
var trans = Matrix2D.createTranslation(x, y);
|
var trans = Matrix2D.createTranslation(x, y);
|
||||||
@@ -2912,7 +2929,7 @@ var Matrix2D = (function () {
|
|||||||
return this.m11 * this.m22 - this.m12 * this.m21;
|
return this.m11 * this.m22 - this.m12 * this.m21;
|
||||||
};
|
};
|
||||||
Matrix2D.invert = function (matrix, result) {
|
Matrix2D.invert = function (matrix, result) {
|
||||||
if (result === void 0) { result = Matrix2D.identity; }
|
if (result === void 0) { result = new Matrix2D(); }
|
||||||
var det = 1 / matrix.determinant();
|
var det = 1 / matrix.determinant();
|
||||||
result.m11 = matrix.m22 * det;
|
result.m11 = matrix.m22 * det;
|
||||||
result.m12 = -matrix.m12 * det;
|
result.m12 = -matrix.m12 * det;
|
||||||
@@ -2923,7 +2940,7 @@ var Matrix2D = (function () {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
Matrix2D.createTranslation = function (xPosition, yPosition, result) {
|
Matrix2D.createTranslation = function (xPosition, yPosition, result) {
|
||||||
if (result === void 0) { result = Matrix2D.identity; }
|
result = result ? result : new Matrix2D();
|
||||||
result.m11 = 1;
|
result.m11 = 1;
|
||||||
result.m12 = 0;
|
result.m12 = 0;
|
||||||
result.m21 = 0;
|
result.m21 = 0;
|
||||||
@@ -2933,7 +2950,7 @@ var Matrix2D = (function () {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
Matrix2D.createRotation = function (radians, result) {
|
Matrix2D.createRotation = function (radians, result) {
|
||||||
result = Matrix2D.identity;
|
result = new Matrix2D();
|
||||||
var val1 = Math.cos(radians);
|
var val1 = Math.cos(radians);
|
||||||
var val2 = Math.sin(radians);
|
var val2 = Math.sin(radians);
|
||||||
result.m11 = val1;
|
result.m11 = val1;
|
||||||
@@ -2943,7 +2960,7 @@ var Matrix2D = (function () {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
Matrix2D.createScale = function (xScale, yScale, result) {
|
Matrix2D.createScale = function (xScale, yScale, result) {
|
||||||
if (result === void 0) { result = Matrix2D.identity; }
|
if (result === void 0) { result = new Matrix2D(); }
|
||||||
result.m11 = xScale;
|
result.m11 = xScale;
|
||||||
result.m12 = 0;
|
result.m12 = 0;
|
||||||
result.m21 = 0;
|
result.m21 = 0;
|
||||||
@@ -4282,6 +4299,11 @@ var Vector2Ext = (function () {
|
|||||||
destinationArray[destinationIndex + i] = destination;
|
destinationArray[destinationIndex + i] = destination;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Vector2Ext.transformR = function (position, matrix) {
|
||||||
|
var x = (position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31;
|
||||||
|
var y = (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32;
|
||||||
|
return new Vector2(x, y);
|
||||||
|
};
|
||||||
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);
|
||||||
};
|
};
|
||||||
|
|||||||
2
demo/libs/framework/framework.min.js
vendored
2
demo/libs/framework/framework.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -18,6 +18,7 @@
|
|||||||
"bin-debug/ThemeAdapter.js",
|
"bin-debug/ThemeAdapter.js",
|
||||||
"bin-debug/game/CoreEmitterType.js",
|
"bin-debug/game/CoreEmitterType.js",
|
||||||
"bin-debug/game/MainScene.js",
|
"bin-debug/game/MainScene.js",
|
||||||
|
"bin-debug/game/PlayerController.js",
|
||||||
"bin-debug/game/SpawnerComponent.js",
|
"bin-debug/game/SpawnerComponent.js",
|
||||||
"bin-debug/game/SpawnerSystem.js"
|
"bin-debug/game/SpawnerSystem.js"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -59,14 +59,6 @@ class Main extends eui.UILayer {
|
|||||||
|
|
||||||
private updateFrame(evt: egret.Event){
|
private updateFrame(evt: egret.Event){
|
||||||
Main.emitter.emit(CoreEmitterType.Update, evt);
|
Main.emitter.emit(CoreEmitterType.Update, evt);
|
||||||
let activeScene = SceneManager.getActiveScene();
|
|
||||||
if (activeScene){
|
|
||||||
let player = activeScene.findEntity("player");
|
|
||||||
if (player){
|
|
||||||
let mover = player.getComponent<Mover>(Mover);
|
|
||||||
mover.move(new Vector2(0, 0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async runGame() {
|
private async runGame() {
|
||||||
@@ -109,15 +101,15 @@ class Main extends eui.UILayer {
|
|||||||
this.addChild(image);
|
this.addChild(image);
|
||||||
let scene = SceneManager.createScene("main", new MainScene(this)).setActive();
|
let scene = SceneManager.createScene("main", new MainScene(this)).setActive();
|
||||||
let player = scene.createEntity("player");
|
let player = scene.createEntity("player");
|
||||||
player.addComponent(new SpriteRenderer()).setSprite(image)
|
player.addComponent(new SpriteRenderer()).setSprite(image);
|
||||||
player.addComponent(new SpawnComponent(EnemyType.worm));
|
player.addComponent(new SpawnComponent(EnemyType.worm));
|
||||||
player.addComponent(new BoxCollider()).setSize(100, 100).isTrigger = true;
|
player.addComponent(new PlayerController());
|
||||||
player.addComponent(new Mover());
|
|
||||||
player.position = new Vector2(100, 100);
|
|
||||||
|
|
||||||
let player2 = scene.createEntity("player2");
|
let player2 = scene.createEntity("player2");
|
||||||
player2.addComponent(new BoxCollider()).setSize(99, 99);
|
player2.addComponent(new BoxCollider()).setSize(99, 99);
|
||||||
|
|
||||||
|
scene.camera.setPosition(new Vector2(-200, -200));
|
||||||
|
|
||||||
// Main.emitter.addObserver(CoreEmitterType.Update, ()=>{
|
// Main.emitter.addObserver(CoreEmitterType.Update, ()=>{
|
||||||
// console.log("update emitter");
|
// console.log("update emitter");
|
||||||
// });
|
// });
|
||||||
|
|||||||
29
demo/src/game/PlayerController.ts
Normal file
29
demo/src/game/PlayerController.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
class PlayerController extends Component {
|
||||||
|
private down: boolean = false;
|
||||||
|
private touchPoint: Vector2 = Vector2.zero;
|
||||||
|
|
||||||
|
public onAddedToEntity(){
|
||||||
|
this.entity.scene.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchBegin, this);
|
||||||
|
this.entity.scene.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchBegin, this);
|
||||||
|
this.entity.scene.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private touchBegin(evt: egret.TouchEvent){
|
||||||
|
this.down = true;
|
||||||
|
this.touchPoint = new Vector2(evt.stageX, evt.stageY);
|
||||||
|
}
|
||||||
|
|
||||||
|
private touchEnd(evt: egret.TouchEvent){
|
||||||
|
this.down = false;
|
||||||
|
this.touchPoint = new Vector2(evt.stageX, evt.stageY);
|
||||||
|
}
|
||||||
|
|
||||||
|
public update(){
|
||||||
|
if (this.down){
|
||||||
|
let camera = SceneManager.getActiveScene().camera;
|
||||||
|
let worldVec = camera.screenToWorldPoint(this.touchPoint);
|
||||||
|
console.log(worldVec, camera.transform.position);
|
||||||
|
camera.transform.position = Vector2.lerp(camera.transform.position, worldVec, Time.deltaTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@ class SpawnComponent extends Component implements ITriggerListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public update() {
|
public update() {
|
||||||
console.log("update");
|
// console.log("update");
|
||||||
}
|
}
|
||||||
|
|
||||||
public onTriggerEnter(other: Collider, local: Collider){
|
public onTriggerEnter(other: Collider, local: Collider){
|
||||||
|
|||||||
8
source/bin/framework.d.ts
vendored
8
source/bin/framework.d.ts
vendored
@@ -330,12 +330,12 @@ declare class Camera extends Component {
|
|||||||
setMinimumZoom(minZoom: number): Camera;
|
setMinimumZoom(minZoom: number): Camera;
|
||||||
setMaximumZoom(maxZoom: number): Camera;
|
setMaximumZoom(maxZoom: number): Camera;
|
||||||
setZoom(zoom: number): this;
|
setZoom(zoom: number): this;
|
||||||
initialize(): void;
|
|
||||||
update(): void;
|
|
||||||
setPosition(position: Vector2): this;
|
setPosition(position: Vector2): this;
|
||||||
|
forceMatrixUpdate(): void;
|
||||||
updateMatrixes(): void;
|
updateMatrixes(): void;
|
||||||
screenToWorldPoint(screenPosition: Vector2): Vector2;
|
screenToWorldPoint(screenPosition: Vector2): Vector2;
|
||||||
worldToScreenPoint(worldPosition: Vector2): Vector2;
|
worldToScreenPoint(worldPosition: Vector2): Vector2;
|
||||||
|
onEntityTransformChanged(comp: ComponentTransform): void;
|
||||||
destory(): void;
|
destory(): void;
|
||||||
}
|
}
|
||||||
declare class CameraInset {
|
declare class CameraInset {
|
||||||
@@ -574,6 +574,7 @@ declare class Time {
|
|||||||
declare abstract class Renderer {
|
declare abstract class Renderer {
|
||||||
camera: Camera;
|
camera: Camera;
|
||||||
onAddedToScene(scene: Scene): void;
|
onAddedToScene(scene: Scene): void;
|
||||||
|
protected beginRender(cam: Camera): void;
|
||||||
abstract render(scene: Scene): any;
|
abstract render(scene: Scene): any;
|
||||||
protected renderAfterStateCheck(renderable: IRenderable, cam: Camera): void;
|
protected renderAfterStateCheck(renderable: IRenderable, cam: Camera): void;
|
||||||
}
|
}
|
||||||
@@ -618,7 +619,7 @@ declare class Matrix2D {
|
|||||||
m32: number;
|
m32: number;
|
||||||
private static _identity;
|
private static _identity;
|
||||||
static readonly identity: Matrix2D;
|
static readonly identity: Matrix2D;
|
||||||
constructor(m11: number, m12: number, m21: number, m22: number, m31: number, m32: number);
|
constructor(m11?: number, m12?: number, m21?: number, m22?: number, m31?: number, m32?: number);
|
||||||
translation: Vector2;
|
translation: Vector2;
|
||||||
rotation: number;
|
rotation: number;
|
||||||
rotationDegrees: number;
|
rotationDegrees: number;
|
||||||
@@ -880,6 +881,7 @@ declare class Vector2Ext {
|
|||||||
static perpendicular(first: Vector2, second: Vector2): Vector2;
|
static perpendicular(first: Vector2, second: Vector2): Vector2;
|
||||||
static normalize(vec: Vector2): Vector2;
|
static normalize(vec: Vector2): Vector2;
|
||||||
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 transform(sourceArray: Vector2[], matrix: Matrix2D, destinationArray: Vector2[]): void;
|
static transform(sourceArray: Vector2[], matrix: Matrix2D, destinationArray: Vector2[]): void;
|
||||||
}
|
}
|
||||||
declare class WebGLUtils {
|
declare class WebGLUtils {
|
||||||
|
|||||||
@@ -1036,7 +1036,6 @@ var Entity = (function () {
|
|||||||
};
|
};
|
||||||
Entity.prototype.update = function () {
|
Entity.prototype.update = function () {
|
||||||
this.components.update();
|
this.components.update();
|
||||||
this.transform.updateTransform();
|
|
||||||
};
|
};
|
||||||
Entity.prototype.onAddedToScene = function () {
|
Entity.prototype.onAddedToScene = function () {
|
||||||
};
|
};
|
||||||
@@ -1156,6 +1155,9 @@ var Scene = (function (_super) {
|
|||||||
};
|
};
|
||||||
Scene.prototype.render = function () {
|
Scene.prototype.render = function () {
|
||||||
for (var i = 0; i < this._renderers.length; i++) {
|
for (var i = 0; i < this._renderers.length; i++) {
|
||||||
|
if (this._renderers[i].camera)
|
||||||
|
this._renderers[i].camera.forceMatrixUpdate();
|
||||||
|
this.camera.forceMatrixUpdate();
|
||||||
this._renderers[i].render(this);
|
this._renderers[i].render(this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1215,9 +1217,9 @@ var ComponentTransform;
|
|||||||
var Transform = (function () {
|
var Transform = (function () {
|
||||||
function Transform(entity) {
|
function Transform(entity) {
|
||||||
this._localRotation = 0;
|
this._localRotation = 0;
|
||||||
this._worldTransform = Matrix2D.identity;
|
this._worldTransform = new Matrix2D();
|
||||||
this._worldToLocalTransform = Matrix2D.identity;
|
this._worldToLocalTransform = new Matrix2D();
|
||||||
this._worldInverseTransform = Matrix2D.identity;
|
this._worldInverseTransform = new Matrix2D();
|
||||||
this._rotation = 0;
|
this._rotation = 0;
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
this._scale = this._localScale = Vector2.one;
|
this._scale = this._localScale = Vector2.one;
|
||||||
@@ -1257,7 +1259,7 @@ var Transform = (function () {
|
|||||||
get: function () {
|
get: function () {
|
||||||
if (this._worldToLocalDirty) {
|
if (this._worldToLocalDirty) {
|
||||||
if (!this.parent) {
|
if (!this.parent) {
|
||||||
this._worldInverseTransform = Matrix2D.identity;
|
this._worldInverseTransform = new Matrix2D();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.parent.updateTransform();
|
this.parent.updateTransform();
|
||||||
@@ -1320,7 +1322,7 @@ var Transform = (function () {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.parent.updateTransform();
|
this.parent.updateTransform();
|
||||||
this._position = Vector2.transform(this._localPosition, this.parent._worldTransform);
|
this._position = Vector2Ext.transformR(this._localPosition, this.parent._worldTransform);
|
||||||
}
|
}
|
||||||
return this._position;
|
return this._position;
|
||||||
},
|
},
|
||||||
@@ -1507,9 +1509,9 @@ var Camera = (function (_super) {
|
|||||||
function Camera() {
|
function Camera() {
|
||||||
var _this = _super.call(this) || this;
|
var _this = _super.call(this) || this;
|
||||||
_this._origin = Vector2.zero;
|
_this._origin = Vector2.zero;
|
||||||
_this._transformMatrix = Matrix2D.identity;
|
_this._transformMatrix = new Matrix2D();
|
||||||
_this._inverseTransformMatrix = Matrix2D.identity;
|
_this._inverseTransformMatrix = new Matrix2D();
|
||||||
_this._projectionMatrix = Matrix2D.identity;
|
_this._projectionMatrix = new Matrix2D();
|
||||||
_this._minimumZoom = 0.3;
|
_this._minimumZoom = 0.3;
|
||||||
_this._maximumZoom = 3;
|
_this._maximumZoom = 3;
|
||||||
_this._areMatrixesDirty = true;
|
_this._areMatrixesDirty = true;
|
||||||
@@ -1648,14 +1650,13 @@ var Camera = (function (_super) {
|
|||||||
this._areMatrixesDirty = true;
|
this._areMatrixesDirty = true;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Camera.prototype.initialize = function () {
|
|
||||||
};
|
|
||||||
Camera.prototype.update = function () {
|
|
||||||
};
|
|
||||||
Camera.prototype.setPosition = function (position) {
|
Camera.prototype.setPosition = function (position) {
|
||||||
this.entity.transform.setPosition(position);
|
this.entity.transform.setPosition(position);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
Camera.prototype.forceMatrixUpdate = function () {
|
||||||
|
this._areMatrixesDirty = true;
|
||||||
|
};
|
||||||
Camera.prototype.updateMatrixes = function () {
|
Camera.prototype.updateMatrixes = function () {
|
||||||
if (!this._areMatrixesDirty)
|
if (!this._areMatrixesDirty)
|
||||||
return;
|
return;
|
||||||
@@ -1665,6 +1666,10 @@ var Camera = (function (_super) {
|
|||||||
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) {
|
||||||
|
tempMat = Matrix2D.createRotation(this.entity.rotation);
|
||||||
|
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
||||||
|
}
|
||||||
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
|
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
||||||
this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix);
|
this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix);
|
||||||
@@ -1673,11 +1678,14 @@ var Camera = (function (_super) {
|
|||||||
};
|
};
|
||||||
Camera.prototype.screenToWorldPoint = function (screenPosition) {
|
Camera.prototype.screenToWorldPoint = function (screenPosition) {
|
||||||
this.updateMatrixes();
|
this.updateMatrixes();
|
||||||
return Vector2.transform(screenPosition, this._inverseTransformMatrix);
|
return Vector2Ext.transformR(screenPosition, this._inverseTransformMatrix);
|
||||||
};
|
};
|
||||||
Camera.prototype.worldToScreenPoint = function (worldPosition) {
|
Camera.prototype.worldToScreenPoint = function (worldPosition) {
|
||||||
this.updateMatrixes();
|
this.updateMatrixes();
|
||||||
return Vector2.transform(worldPosition, this._transformMatrix);
|
return Vector2Ext.transformR(worldPosition, this._transformMatrix);
|
||||||
|
};
|
||||||
|
Camera.prototype.onEntityTransformChanged = function (comp) {
|
||||||
|
this._areMatrixesDirty = true;
|
||||||
};
|
};
|
||||||
Camera.prototype.destory = function () {
|
Camera.prototype.destory = function () {
|
||||||
};
|
};
|
||||||
@@ -1869,8 +1877,8 @@ var SpriteRenderer = (function (_super) {
|
|||||||
SpriteRenderer.prototype.render = function (camera) {
|
SpriteRenderer.prototype.render = function (camera) {
|
||||||
if (!this.sprite)
|
if (!this.sprite)
|
||||||
return;
|
return;
|
||||||
this.sprite.x = this.entity.transform.position.x;
|
this.sprite.x = this.entity.transform.position.x - camera.transform.position.x;
|
||||||
this.sprite.y = this.entity.transform.position.y;
|
this.sprite.y = this.entity.transform.position.y - camera.transform.position.y;
|
||||||
this.sprite.rotation = this.entity.transform.rotation;
|
this.sprite.rotation = this.entity.transform.rotation;
|
||||||
this.sprite.anchorOffsetX = this._origin.x;
|
this.sprite.anchorOffsetX = this._origin.x;
|
||||||
this.sprite.anchorOffsetY = this._origin.y;
|
this.sprite.anchorOffsetY = this._origin.y;
|
||||||
@@ -2717,6 +2725,13 @@ var Renderer = (function () {
|
|||||||
function Renderer() {
|
function Renderer() {
|
||||||
}
|
}
|
||||||
Renderer.prototype.onAddedToScene = function (scene) { };
|
Renderer.prototype.onAddedToScene = function (scene) { };
|
||||||
|
Renderer.prototype.beginRender = function (cam) {
|
||||||
|
cam.transform.updateTransform();
|
||||||
|
var entities = SceneManager.getActiveScene().entities;
|
||||||
|
for (var i = 0; i < entities.buffer.length; i++) {
|
||||||
|
entities.buffer[i].transform.updateTransform();
|
||||||
|
}
|
||||||
|
};
|
||||||
Renderer.prototype.renderAfterStateCheck = function (renderable, cam) {
|
Renderer.prototype.renderAfterStateCheck = function (renderable, cam) {
|
||||||
renderable.render(cam);
|
renderable.render(cam);
|
||||||
};
|
};
|
||||||
@@ -2729,6 +2744,7 @@ var DefaultRenderer = (function (_super) {
|
|||||||
}
|
}
|
||||||
DefaultRenderer.prototype.render = function (scene) {
|
DefaultRenderer.prototype.render = function (scene) {
|
||||||
var cam = this.camera ? this.camera : scene.camera;
|
var cam = this.camera ? this.camera : scene.camera;
|
||||||
|
this.beginRender(cam);
|
||||||
for (var i = 0; i < scene.renderableComponents.count; i++) {
|
for (var i = 0; i < scene.renderableComponents.count; i++) {
|
||||||
var renderable = scene.renderableComponents.buffer[i];
|
var renderable = scene.renderableComponents.buffer[i];
|
||||||
if (renderable.enabled && renderable.isVisibleFromCamera(cam))
|
if (renderable.enabled && renderable.isVisibleFromCamera(cam))
|
||||||
@@ -2810,12 +2826,12 @@ var Matrix2D = (function () {
|
|||||||
this.m22 = 0;
|
this.m22 = 0;
|
||||||
this.m31 = 0;
|
this.m31 = 0;
|
||||||
this.m32 = 0;
|
this.m32 = 0;
|
||||||
this.m11 = m11;
|
this.m11 = m11 ? m11 : 1;
|
||||||
this.m12 = m12;
|
this.m12 = m12 ? m12 : 0;
|
||||||
this.m21 = m21;
|
this.m21 = m21 ? m21 : 0;
|
||||||
this.m22 = m22;
|
this.m22 = m22 ? m22 : 1;
|
||||||
this.m31 = m31;
|
this.m31 = m31 ? m31 : 0;
|
||||||
this.m32 = m32;
|
this.m32 = m32 ? m32 : 0;
|
||||||
}
|
}
|
||||||
Object.defineProperty(Matrix2D, "identity", {
|
Object.defineProperty(Matrix2D, "identity", {
|
||||||
get: function () {
|
get: function () {
|
||||||
@@ -2890,19 +2906,20 @@ var Matrix2D = (function () {
|
|||||||
return matrix1;
|
return matrix1;
|
||||||
};
|
};
|
||||||
Matrix2D.multiply = function (matrix1, matrix2) {
|
Matrix2D.multiply = function (matrix1, matrix2) {
|
||||||
|
var result = new Matrix2D();
|
||||||
var m11 = (matrix1.m11 * matrix2.m11) + (matrix1.m12 * matrix2.m21);
|
var m11 = (matrix1.m11 * matrix2.m11) + (matrix1.m12 * matrix2.m21);
|
||||||
var m12 = (matrix1.m11 * matrix2.m12) + (matrix1.m12 * matrix2.m22);
|
var m12 = (matrix1.m11 * matrix2.m12) + (matrix1.m12 * matrix2.m22);
|
||||||
var m21 = (matrix1.m21 * matrix2.m11) + (matrix1.m22 * matrix2.m21);
|
var m21 = (matrix1.m21 * matrix2.m11) + (matrix1.m22 * matrix2.m21);
|
||||||
var m22 = (matrix1.m21 * matrix2.m12) + (matrix1.m22 * matrix2.m22);
|
var m22 = (matrix1.m21 * matrix2.m12) + (matrix1.m22 * matrix2.m22);
|
||||||
var m31 = (matrix1.m31 * matrix2.m11) + (matrix1.m32 * matrix2.m21) + matrix2.m31;
|
var m31 = (matrix1.m31 * matrix2.m11) + (matrix1.m32 * matrix2.m21) + matrix2.m31;
|
||||||
var m32 = (matrix1.m31 * matrix2.m12) + (matrix1.m32 * matrix2.m22) + matrix2.m32;
|
var m32 = (matrix1.m31 * matrix2.m12) + (matrix1.m32 * matrix2.m22) + matrix2.m32;
|
||||||
matrix1.m11 = m11;
|
result.m11 = m11;
|
||||||
matrix1.m12 = m12;
|
result.m12 = m12;
|
||||||
matrix1.m21 = m21;
|
result.m21 = m21;
|
||||||
matrix1.m22 = m22;
|
result.m22 = m22;
|
||||||
matrix1.m31 = m31;
|
result.m31 = m31;
|
||||||
matrix1.m32 = m32;
|
result.m32 = m32;
|
||||||
return matrix1;
|
return result;
|
||||||
};
|
};
|
||||||
Matrix2D.multiplyTranslation = function (matrix, x, y) {
|
Matrix2D.multiplyTranslation = function (matrix, x, y) {
|
||||||
var trans = Matrix2D.createTranslation(x, y);
|
var trans = Matrix2D.createTranslation(x, y);
|
||||||
@@ -2912,7 +2929,7 @@ var Matrix2D = (function () {
|
|||||||
return this.m11 * this.m22 - this.m12 * this.m21;
|
return this.m11 * this.m22 - this.m12 * this.m21;
|
||||||
};
|
};
|
||||||
Matrix2D.invert = function (matrix, result) {
|
Matrix2D.invert = function (matrix, result) {
|
||||||
if (result === void 0) { result = Matrix2D.identity; }
|
if (result === void 0) { result = new Matrix2D(); }
|
||||||
var det = 1 / matrix.determinant();
|
var det = 1 / matrix.determinant();
|
||||||
result.m11 = matrix.m22 * det;
|
result.m11 = matrix.m22 * det;
|
||||||
result.m12 = -matrix.m12 * det;
|
result.m12 = -matrix.m12 * det;
|
||||||
@@ -2923,7 +2940,7 @@ var Matrix2D = (function () {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
Matrix2D.createTranslation = function (xPosition, yPosition, result) {
|
Matrix2D.createTranslation = function (xPosition, yPosition, result) {
|
||||||
if (result === void 0) { result = Matrix2D.identity; }
|
result = result ? result : new Matrix2D();
|
||||||
result.m11 = 1;
|
result.m11 = 1;
|
||||||
result.m12 = 0;
|
result.m12 = 0;
|
||||||
result.m21 = 0;
|
result.m21 = 0;
|
||||||
@@ -2933,7 +2950,7 @@ var Matrix2D = (function () {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
Matrix2D.createRotation = function (radians, result) {
|
Matrix2D.createRotation = function (radians, result) {
|
||||||
result = Matrix2D.identity;
|
result = new Matrix2D();
|
||||||
var val1 = Math.cos(radians);
|
var val1 = Math.cos(radians);
|
||||||
var val2 = Math.sin(radians);
|
var val2 = Math.sin(radians);
|
||||||
result.m11 = val1;
|
result.m11 = val1;
|
||||||
@@ -2943,7 +2960,7 @@ var Matrix2D = (function () {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
Matrix2D.createScale = function (xScale, yScale, result) {
|
Matrix2D.createScale = function (xScale, yScale, result) {
|
||||||
if (result === void 0) { result = Matrix2D.identity; }
|
if (result === void 0) { result = new Matrix2D(); }
|
||||||
result.m11 = xScale;
|
result.m11 = xScale;
|
||||||
result.m12 = 0;
|
result.m12 = 0;
|
||||||
result.m21 = 0;
|
result.m21 = 0;
|
||||||
@@ -4282,6 +4299,11 @@ var Vector2Ext = (function () {
|
|||||||
destinationArray[destinationIndex + i] = destination;
|
destinationArray[destinationIndex + i] = destination;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Vector2Ext.transformR = function (position, matrix) {
|
||||||
|
var x = (position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31;
|
||||||
|
var y = (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32;
|
||||||
|
return new Vector2(x, y);
|
||||||
|
};
|
||||||
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);
|
||||||
};
|
};
|
||||||
|
|||||||
2
source/bin/framework.min.js
vendored
2
source/bin/framework.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -2,9 +2,9 @@
|
|||||||
class Camera extends Component {
|
class Camera extends Component {
|
||||||
private _zoom;
|
private _zoom;
|
||||||
private _origin: Vector2 = Vector2.zero;
|
private _origin: Vector2 = Vector2.zero;
|
||||||
private _transformMatrix: Matrix2D = Matrix2D.identity;
|
private _transformMatrix: Matrix2D = new Matrix2D();
|
||||||
private _inverseTransformMatrix = Matrix2D.identity;
|
private _inverseTransformMatrix = new Matrix2D();
|
||||||
private _projectionMatrix = Matrix2D.identity;
|
private _projectionMatrix = new Matrix2D();
|
||||||
|
|
||||||
private _minimumZoom = 0.3;
|
private _minimumZoom = 0.3;
|
||||||
private _maximumZoom = 3;
|
private _maximumZoom = 3;
|
||||||
@@ -145,20 +145,16 @@ class Camera extends Component {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public initialize() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public update(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public setPosition(position: Vector2){
|
public setPosition(position: Vector2){
|
||||||
this.entity.transform.setPosition(position);
|
this.entity.transform.setPosition(position);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public forceMatrixUpdate(){
|
||||||
|
this._areMatrixesDirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
public updateMatrixes(){
|
public updateMatrixes(){
|
||||||
if (!this._areMatrixesDirty)
|
if (!this._areMatrixesDirty)
|
||||||
return;
|
return;
|
||||||
@@ -170,6 +166,11 @@ class Camera extends Component {
|
|||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.entity.transform.rotation != 0){
|
||||||
|
tempMat = Matrix2D.createRotation(this.entity.rotation);
|
||||||
|
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
||||||
|
}
|
||||||
|
|
||||||
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
|
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
||||||
|
|
||||||
@@ -181,12 +182,16 @@ class Camera extends Component {
|
|||||||
|
|
||||||
public screenToWorldPoint(screenPosition: Vector2){
|
public screenToWorldPoint(screenPosition: Vector2){
|
||||||
this.updateMatrixes();
|
this.updateMatrixes();
|
||||||
return Vector2.transform(screenPosition, this._inverseTransformMatrix);
|
return Vector2Ext.transformR(screenPosition, this._inverseTransformMatrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
public worldToScreenPoint(worldPosition: Vector2){
|
public worldToScreenPoint(worldPosition: Vector2){
|
||||||
this.updateMatrixes();
|
this.updateMatrixes();
|
||||||
return Vector2.transform(worldPosition, this._transformMatrix);
|
return Vector2Ext.transformR(worldPosition, this._transformMatrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
public onEntityTransformChanged(comp: ComponentTransform){
|
||||||
|
this._areMatrixesDirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public destory() {
|
public destory() {
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ class SpriteRenderer extends RenderableComponent {
|
|||||||
if (!this.sprite)
|
if (!this.sprite)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this.sprite.x = this.entity.transform.position.x;
|
this.sprite.x = this.entity.transform.position.x - camera.transform.position.x;
|
||||||
this.sprite.y = this.entity.transform.position.y;
|
this.sprite.y = this.entity.transform.position.y - camera.transform.position.y;
|
||||||
this.sprite.rotation = this.entity.transform.rotation;
|
this.sprite.rotation = this.entity.transform.rotation;
|
||||||
this.sprite.anchorOffsetX = this._origin.x;
|
this.sprite.anchorOffsetX = this._origin.x;
|
||||||
this.sprite.anchorOffsetY = this._origin.y;
|
this.sprite.anchorOffsetY = this._origin.y;
|
||||||
|
|||||||
@@ -238,7 +238,6 @@ class Entity {
|
|||||||
|
|
||||||
public update(){
|
public update(){
|
||||||
this.components.update();
|
this.components.update();
|
||||||
this.transform.updateTransform();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public onAddedToScene(){
|
public onAddedToScene(){
|
||||||
|
|||||||
@@ -147,6 +147,9 @@ class Scene extends egret.DisplayObjectContainer {
|
|||||||
|
|
||||||
public render(){
|
public render(){
|
||||||
for (let i = 0; i <this._renderers.length; i ++){
|
for (let i = 0; i <this._renderers.length; i ++){
|
||||||
|
if (this._renderers[i].camera)
|
||||||
|
this._renderers[i].camera.forceMatrixUpdate();
|
||||||
|
this.camera.forceMatrixUpdate();
|
||||||
this._renderers[i].render(this);
|
this._renderers[i].render(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ class Transform {
|
|||||||
private _rotationMatrix: Matrix2D;
|
private _rotationMatrix: Matrix2D;
|
||||||
private _scaleMatrix: Matrix2D;
|
private _scaleMatrix: Matrix2D;
|
||||||
|
|
||||||
private _worldTransform = Matrix2D.identity;
|
private _worldTransform = new Matrix2D();
|
||||||
private _worldToLocalTransform = Matrix2D.identity;
|
private _worldToLocalTransform = new Matrix2D();
|
||||||
private _worldInverseTransform = Matrix2D.identity;
|
private _worldInverseTransform = new Matrix2D();
|
||||||
|
|
||||||
private _rotation: number = 0;
|
private _rotation: number = 0;
|
||||||
private _position: Vector2;
|
private _position: Vector2;
|
||||||
@@ -75,7 +75,7 @@ class Transform {
|
|||||||
public get worldToLocalTransform(){
|
public get worldToLocalTransform(){
|
||||||
if (this._worldToLocalDirty){
|
if (this._worldToLocalDirty){
|
||||||
if (!this.parent){
|
if (!this.parent){
|
||||||
this._worldInverseTransform = Matrix2D.identity;
|
this._worldInverseTransform = new Matrix2D();
|
||||||
} else{
|
} else{
|
||||||
this.parent.updateTransform();
|
this.parent.updateTransform();
|
||||||
this._worldToLocalTransform = Matrix2D.invert(this.parent._worldTransform, this._worldToLocalTransform);
|
this._worldToLocalTransform = Matrix2D.invert(this.parent._worldTransform, this._worldToLocalTransform);
|
||||||
@@ -134,7 +134,7 @@ class Transform {
|
|||||||
this._position = this._localPosition;
|
this._position = this._localPosition;
|
||||||
}else{
|
}else{
|
||||||
this.parent.updateTransform();
|
this.parent.updateTransform();
|
||||||
this._position = Vector2.transform(this._localPosition, this.parent._worldTransform);
|
this._position = Vector2Ext.transformR(this._localPosition, this.parent._worldTransform);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._position;
|
return this._position;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
class DefaultRenderer extends Renderer {
|
class DefaultRenderer extends Renderer {
|
||||||
public render(scene: Scene) {
|
public render(scene: Scene) {
|
||||||
let cam = this.camera ? this.camera : scene.camera;
|
let cam = this.camera ? this.camera : scene.camera;
|
||||||
|
this.beginRender(cam);
|
||||||
|
|
||||||
for (let i = 0; i < scene.renderableComponents.count; i++){
|
for (let i = 0; i < scene.renderableComponents.count; i++){
|
||||||
let renderable = scene.renderableComponents.buffer[i];
|
let renderable = scene.renderableComponents.buffer[i];
|
||||||
|
|||||||
@@ -15,6 +15,15 @@ abstract class Renderer {
|
|||||||
*/
|
*/
|
||||||
public onAddedToScene(scene: Scene){}
|
public onAddedToScene(scene: Scene){}
|
||||||
|
|
||||||
|
protected beginRender(cam: Camera){
|
||||||
|
cam.transform.updateTransform();
|
||||||
|
|
||||||
|
let entities = SceneManager.getActiveScene().entities;
|
||||||
|
for (let i = 0; i < entities.buffer.length; i ++){
|
||||||
|
entities.buffer[i].transform.updateTransform();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param scene
|
* @param scene
|
||||||
|
|||||||
@@ -20,15 +20,15 @@ class Matrix2D {
|
|||||||
return Matrix2D._identity;
|
return Matrix2D._identity;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(m11: number, m12: number, m21: number, m22: number, m31: number, m32: number){
|
constructor(m11?: number, m12?: number, m21?: number, m22?: number, m31?: number, m32?: number){
|
||||||
this.m11 = m11;
|
this.m11 = m11 ? m11 : 1;
|
||||||
this.m12 = m12;
|
this.m12 = m12 ? m12 : 0;
|
||||||
|
|
||||||
this.m21 = m21;
|
this.m21 = m21 ? m21 : 0;
|
||||||
this.m22 = m22;
|
this.m22 = m22 ? m22 : 1;
|
||||||
|
|
||||||
this.m31 = m31;
|
this.m31 = m31 ? m31 : 0;
|
||||||
this.m32 = m32;
|
this.m32 = m32 ? m32 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 存储在这个矩阵中的位置 */
|
/** 存储在这个矩阵中的位置 */
|
||||||
@@ -108,6 +108,8 @@ class Matrix2D {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static multiply(matrix1: Matrix2D, matrix2: Matrix2D){
|
public static multiply(matrix1: Matrix2D, matrix2: Matrix2D){
|
||||||
|
let result = new Matrix2D();
|
||||||
|
|
||||||
let m11 = ( matrix1.m11 * matrix2.m11 ) + ( matrix1.m12 * matrix2.m21 );
|
let m11 = ( matrix1.m11 * matrix2.m11 ) + ( matrix1.m12 * matrix2.m21 );
|
||||||
let m12 = ( matrix1.m11 * matrix2.m12 ) + ( matrix1.m12 * matrix2.m22 );
|
let m12 = ( matrix1.m11 * matrix2.m12 ) + ( matrix1.m12 * matrix2.m22 );
|
||||||
|
|
||||||
@@ -117,15 +119,16 @@ class Matrix2D {
|
|||||||
let m31 = ( matrix1.m31 * matrix2.m11 ) + ( matrix1.m32 * matrix2.m21 ) + matrix2.m31;
|
let m31 = ( matrix1.m31 * matrix2.m11 ) + ( matrix1.m32 * matrix2.m21 ) + matrix2.m31;
|
||||||
let m32 = ( matrix1.m31 * matrix2.m12 ) + ( matrix1.m32 * matrix2.m22 ) + matrix2.m32;
|
let m32 = ( matrix1.m31 * matrix2.m12 ) + ( matrix1.m32 * matrix2.m22 ) + matrix2.m32;
|
||||||
|
|
||||||
matrix1.m11 = m11;
|
result.m11 = m11;
|
||||||
matrix1.m12 = m12;
|
result.m12 = m12;
|
||||||
|
|
||||||
matrix1.m21 = m21;
|
result.m21 = m21;
|
||||||
matrix1.m22 = m22;
|
result.m22 = m22;
|
||||||
|
|
||||||
matrix1.m31 = m31;
|
result.m31 = m31;
|
||||||
matrix1.m32 = m32;
|
result.m32 = m32;
|
||||||
return matrix1;
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static multiplyTranslation(matrix: Matrix2D, x: number, y: number){
|
public static multiplyTranslation(matrix: Matrix2D, x: number, y: number){
|
||||||
@@ -137,7 +140,7 @@ class Matrix2D {
|
|||||||
return this.m11 * this.m22 - this.m12 * this.m21;
|
return this.m11 * this.m22 - this.m12 * this.m21;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static invert(matrix: Matrix2D, result: Matrix2D = Matrix2D.identity){
|
public static invert(matrix: Matrix2D, result: Matrix2D = new Matrix2D()){
|
||||||
let det = 1 / matrix.determinant();
|
let det = 1 / matrix.determinant();
|
||||||
|
|
||||||
result.m11 = matrix.m22 * det;
|
result.m11 = matrix.m22 * det;
|
||||||
@@ -152,7 +155,9 @@ class Matrix2D {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static createTranslation(xPosition: number, yPosition: number, result: Matrix2D = Matrix2D.identity){
|
public static createTranslation(xPosition: number, yPosition: number, result?: Matrix2D){
|
||||||
|
result = result ? result : new Matrix2D();
|
||||||
|
|
||||||
result.m11 = 1;
|
result.m11 = 1;
|
||||||
result.m12 = 0;
|
result.m12 = 0;
|
||||||
|
|
||||||
@@ -166,7 +171,7 @@ class Matrix2D {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static createRotation(radians: number, result?: Matrix2D){
|
public static createRotation(radians: number, result?: Matrix2D){
|
||||||
result = Matrix2D.identity;
|
result = new Matrix2D();
|
||||||
|
|
||||||
let val1 = Math.cos(radians);
|
let val1 = Math.cos(radians);
|
||||||
let val2 = Math.sin(radians);
|
let val2 = Math.sin(radians);
|
||||||
@@ -179,7 +184,7 @@ class Matrix2D {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static createScale(xScale: number, yScale: number, result: Matrix2D = Matrix2D.identity){
|
public static createScale(xScale: number, yScale: number, result: Matrix2D = new Matrix2D()){
|
||||||
result.m11 = xScale;
|
result.m11 = xScale;
|
||||||
result.m12 = 0;
|
result.m12 = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,12 @@ class Vector2Ext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static transformR(position: Vector2, matrix: Matrix2D){
|
||||||
|
let x = (position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31;
|
||||||
|
let y = (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32;
|
||||||
|
return new Vector2(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user