合并camera
This commit is contained in:
Vendored
+29
-52
@@ -145,8 +145,6 @@ declare abstract class Component extends egret.DisplayObjectContainer {
|
|||||||
updateInterval: number;
|
updateInterval: number;
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
setEnabled(isEnabled: boolean): this;
|
setEnabled(isEnabled: boolean): this;
|
||||||
readonly stage: egret.Stage;
|
|
||||||
readonly scene: Scene;
|
|
||||||
initialize(): void;
|
initialize(): void;
|
||||||
onAddedToEntity(): void;
|
onAddedToEntity(): void;
|
||||||
onRemovedFromEntity(): void;
|
onRemovedFromEntity(): void;
|
||||||
@@ -160,6 +158,7 @@ declare abstract class Component extends egret.DisplayObjectContainer {
|
|||||||
}
|
}
|
||||||
declare class Entity extends egret.DisplayObjectContainer {
|
declare class Entity extends egret.DisplayObjectContainer {
|
||||||
private static _idGenerator;
|
private static _idGenerator;
|
||||||
|
private _position;
|
||||||
name: string;
|
name: string;
|
||||||
readonly id: number;
|
readonly id: number;
|
||||||
scene: Scene;
|
scene: Scene;
|
||||||
@@ -308,62 +307,30 @@ declare class Transform {
|
|||||||
declare class Camera extends Component {
|
declare class Camera extends Component {
|
||||||
private _zoom;
|
private _zoom;
|
||||||
private _origin;
|
private _origin;
|
||||||
private _transformMatrix;
|
|
||||||
private _inverseTransformMatrix;
|
|
||||||
private _minimumZoom;
|
private _minimumZoom;
|
||||||
private _maximumZoom;
|
private _maximumZoom;
|
||||||
private _areMatrixesDirty;
|
|
||||||
private _inset;
|
|
||||||
private _bounds;
|
|
||||||
private _areBoundsDirty;
|
|
||||||
readonly bounds: Rectangle;
|
|
||||||
zoom: number;
|
|
||||||
minimumZoom: number;
|
|
||||||
maximumZoom: number;
|
|
||||||
origin: Vector2;
|
|
||||||
position: Vector2;
|
|
||||||
readonly transformMatrix: Matrix2D;
|
|
||||||
readonly inverseTransformMatrix: Matrix2D;
|
|
||||||
constructor();
|
|
||||||
onSceneSizeChanged(newWidth: number, newHeight: number): void;
|
|
||||||
setMinimumZoom(minZoom: number): Camera;
|
|
||||||
setMaximumZoom(maxZoom: number): Camera;
|
|
||||||
setZoom(zoom: number): this;
|
|
||||||
setPosition(position: Vector2): this;
|
|
||||||
forceMatrixUpdate(): void;
|
|
||||||
protected updateMatrixes(): void;
|
|
||||||
screenToWorldPoint(screenPosition: Vector2): Vector2;
|
|
||||||
worldToScreenPoint(worldPosition: Vector2): Vector2;
|
|
||||||
onEntityTransformChanged(comp: ComponentTransform): void;
|
|
||||||
destory(): void;
|
|
||||||
}
|
|
||||||
declare class CameraInset {
|
|
||||||
left: number;
|
|
||||||
right: number;
|
|
||||||
top: number;
|
|
||||||
bottom: number;
|
|
||||||
}
|
|
||||||
declare class ComponentPool<T extends PooledComponent> {
|
|
||||||
private _cache;
|
|
||||||
private _type;
|
|
||||||
constructor(typeClass: any);
|
|
||||||
obtain(): T;
|
|
||||||
free(component: T): void;
|
|
||||||
}
|
|
||||||
declare class FollowCamera extends Component {
|
|
||||||
camera: Camera;
|
|
||||||
followLerp: number;
|
followLerp: number;
|
||||||
deadzone: Rectangle;
|
deadzone: Rectangle;
|
||||||
focusOffset: Vector2;
|
focusOffset: Vector2;
|
||||||
mapLockEnabled: boolean;
|
mapLockEnabled: boolean;
|
||||||
mapSize: Vector2;
|
mapSize: Vector2;
|
||||||
private _targetEntity;
|
targetEntity: Entity;
|
||||||
private _cameraStyle;
|
|
||||||
private _worldSpaceDeadZone;
|
private _worldSpaceDeadZone;
|
||||||
private _desiredPositionDelta;
|
private _desiredPositionDelta;
|
||||||
private _targetCollider;
|
private _targetCollider;
|
||||||
constructor(targetEntity: Entity, cameraStyle?: CameraStyle);
|
cameraStyle: CameraStyle;
|
||||||
onAddedToEntity(): void;
|
zoom: number;
|
||||||
|
minimumZoom: number;
|
||||||
|
maximumZoom: number;
|
||||||
|
origin: Vector2;
|
||||||
|
position: Vector2;
|
||||||
|
constructor();
|
||||||
|
onSceneSizeChanged(newWidth: number, newHeight: number): void;
|
||||||
|
setMinimumZoom(minZoom: number): Camera;
|
||||||
|
setMaximumZoom(maxZoom: number): Camera;
|
||||||
|
setZoom(zoom: number): Camera;
|
||||||
|
setRotation(rotation: number): Camera;
|
||||||
|
setPosition(position: Vector2): this;
|
||||||
follow(targetEntity: Entity, cameraStyle?: CameraStyle): void;
|
follow(targetEntity: Entity, cameraStyle?: CameraStyle): void;
|
||||||
update(): void;
|
update(): void;
|
||||||
private clampToMapSize;
|
private clampToMapSize;
|
||||||
@@ -373,6 +340,13 @@ declare enum CameraStyle {
|
|||||||
lockOn = 0,
|
lockOn = 0,
|
||||||
cameraWindow = 1
|
cameraWindow = 1
|
||||||
}
|
}
|
||||||
|
declare class ComponentPool<T extends PooledComponent> {
|
||||||
|
private _cache;
|
||||||
|
private _type;
|
||||||
|
constructor(typeClass: any);
|
||||||
|
obtain(): T;
|
||||||
|
free(component: T): void;
|
||||||
|
}
|
||||||
declare class Mesh extends Component {
|
declare class Mesh extends Component {
|
||||||
private _verts;
|
private _verts;
|
||||||
private _primitiveCount;
|
private _primitiveCount;
|
||||||
@@ -396,7 +370,7 @@ declare class PolygonMesh extends Mesh {
|
|||||||
declare abstract class PooledComponent extends Component {
|
declare abstract class PooledComponent extends Component {
|
||||||
abstract reset(): any;
|
abstract reset(): any;
|
||||||
}
|
}
|
||||||
declare abstract class RenderableComponent extends Component implements IRenderable {
|
declare abstract class RenderableComponent extends PooledComponent implements IRenderable {
|
||||||
private _isVisible;
|
private _isVisible;
|
||||||
protected _areBoundsDirty: boolean;
|
protected _areBoundsDirty: boolean;
|
||||||
protected _bounds: Rectangle;
|
protected _bounds: Rectangle;
|
||||||
@@ -427,13 +401,16 @@ declare class Sprite {
|
|||||||
}
|
}
|
||||||
declare class SpriteRenderer extends RenderableComponent {
|
declare class SpriteRenderer extends RenderableComponent {
|
||||||
private _origin;
|
private _origin;
|
||||||
|
private _bitmap;
|
||||||
|
private _sprite;
|
||||||
origin: Vector2;
|
origin: Vector2;
|
||||||
setOrigin(origin: Vector2): this;
|
setOrigin(origin: Vector2): this;
|
||||||
setSprite(sprite: Sprite): void;
|
setSprite(sprite: Sprite): SpriteRenderer;
|
||||||
setColor(color: number): void;
|
setColor(color: number): SpriteRenderer;
|
||||||
isVisibleFromCamera(camera: Camera): boolean;
|
isVisibleFromCamera(camera: Camera): boolean;
|
||||||
render(camera: Camera): void;
|
render(camera: Camera): void;
|
||||||
onRemovedFromEntity(): void;
|
onRemovedFromEntity(): void;
|
||||||
|
reset(): void;
|
||||||
}
|
}
|
||||||
interface ITriggerListener {
|
interface ITriggerListener {
|
||||||
onTriggerEnter(other: Collider, local: Collider): any;
|
onTriggerEnter(other: Collider, local: Collider): any;
|
||||||
@@ -703,12 +680,12 @@ declare class PolyLight extends RenderableComponent {
|
|||||||
protected _radius: number;
|
protected _radius: number;
|
||||||
private _lightEffect;
|
private _lightEffect;
|
||||||
private _indices;
|
private _indices;
|
||||||
readonly bounds: Rectangle;
|
|
||||||
radius: number;
|
radius: number;
|
||||||
constructor(radius: number, color: number, power: number);
|
constructor(radius: number, color: number, power: number);
|
||||||
private computeTriangleIndices;
|
private computeTriangleIndices;
|
||||||
setRadius(radius: number): void;
|
setRadius(radius: number): void;
|
||||||
render(camera: Camera): void;
|
render(camera: Camera): void;
|
||||||
|
reset(): void;
|
||||||
}
|
}
|
||||||
declare abstract class SceneTransition {
|
declare abstract class SceneTransition {
|
||||||
private _hasPreviousSceneRender;
|
private _hasPreviousSceneRender;
|
||||||
|
|||||||
+109
-236
@@ -802,24 +802,6 @@ var Component = (function (_super) {
|
|||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Object.defineProperty(Component.prototype, "stage", {
|
|
||||||
get: function () {
|
|
||||||
if (!this.entity)
|
|
||||||
return null;
|
|
||||||
return this.entity.stage;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Object.defineProperty(Component.prototype, "scene", {
|
|
||||||
get: function () {
|
|
||||||
if (!this.entity)
|
|
||||||
return null;
|
|
||||||
return this.entity.scene;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Component.prototype.initialize = function () {
|
Component.prototype.initialize = function () {
|
||||||
};
|
};
|
||||||
Component.prototype.onAddedToEntity = function () {
|
Component.prototype.onAddedToEntity = function () {
|
||||||
@@ -850,6 +832,7 @@ var Entity = (function (_super) {
|
|||||||
__extends(Entity, _super);
|
__extends(Entity, _super);
|
||||||
function Entity(name) {
|
function Entity(name) {
|
||||||
var _this = _super.call(this) || this;
|
var _this = _super.call(this) || this;
|
||||||
|
_this._position = Vector2.zero;
|
||||||
_this._updateOrder = 0;
|
_this._updateOrder = 0;
|
||||||
_this._enabled = true;
|
_this._enabled = true;
|
||||||
_this._tag = 0;
|
_this._tag = 0;
|
||||||
@@ -868,11 +851,10 @@ var Entity = (function (_super) {
|
|||||||
});
|
});
|
||||||
Object.defineProperty(Entity.prototype, "position", {
|
Object.defineProperty(Entity.prototype, "position", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return new Vector2(this.x, this.y);
|
return this._position;
|
||||||
},
|
},
|
||||||
set: function (value) {
|
set: function (value) {
|
||||||
this.x = value.x;
|
this._position = value;
|
||||||
this.y = value.y;
|
|
||||||
},
|
},
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: true
|
configurable: true
|
||||||
@@ -1043,6 +1025,8 @@ var Scene = (function (_super) {
|
|||||||
_this.renderableComponents = new RenderableComponentList();
|
_this.renderableComponents = new RenderableComponentList();
|
||||||
_this.entities = new EntityList(_this);
|
_this.entities = new EntityList(_this);
|
||||||
_this.content = new ContentManager();
|
_this.content = new ContentManager();
|
||||||
|
_this.width = SceneManager.stage.stageWidth;
|
||||||
|
_this.height = SceneManager.stage.stageHeight;
|
||||||
_this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this);
|
_this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this);
|
||||||
_this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this);
|
_this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this);
|
||||||
return _this;
|
return _this;
|
||||||
@@ -1128,7 +1112,6 @@ var Scene = (function (_super) {
|
|||||||
this.entities.removeAllEntities();
|
this.entities.removeAllEntities();
|
||||||
this.removeChildren();
|
this.removeChildren();
|
||||||
Physics.clear();
|
Physics.clear();
|
||||||
this.camera.destory();
|
|
||||||
this.camera = null;
|
this.camera = null;
|
||||||
this.content.dispose();
|
this.content.dispose();
|
||||||
if (this.entityProcessors)
|
if (this.entityProcessors)
|
||||||
@@ -1170,9 +1153,6 @@ 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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1584,48 +1564,21 @@ 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 = new Matrix2D();
|
|
||||||
_this._inverseTransformMatrix = new Matrix2D();
|
|
||||||
_this._minimumZoom = 0.3;
|
_this._minimumZoom = 0.3;
|
||||||
_this._maximumZoom = 3;
|
_this._maximumZoom = 3;
|
||||||
_this._areMatrixesDirty = true;
|
_this.followLerp = 0.1;
|
||||||
_this._inset = new CameraInset();
|
_this.deadzone = new Rectangle();
|
||||||
_this._bounds = new Rectangle();
|
_this.focusOffset = new Vector2();
|
||||||
_this._areBoundsDirty = true;
|
_this.mapLockEnabled = false;
|
||||||
|
_this.mapSize = new Vector2();
|
||||||
|
_this._worldSpaceDeadZone = new Rectangle();
|
||||||
|
_this._desiredPositionDelta = new Vector2();
|
||||||
|
_this.cameraStyle = CameraStyle.lockOn;
|
||||||
|
_this.width = SceneManager.stage.stageWidth;
|
||||||
|
_this.height = SceneManager.stage.stageHeight;
|
||||||
_this.setZoom(0);
|
_this.setZoom(0);
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
Object.defineProperty(Camera.prototype, "bounds", {
|
|
||||||
get: function () {
|
|
||||||
if (this._areMatrixesDirty)
|
|
||||||
this.updateMatrixes();
|
|
||||||
if (this._areBoundsDirty) {
|
|
||||||
var stage = this.stage;
|
|
||||||
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));
|
|
||||||
if (this.entity.rotation != 0) {
|
|
||||||
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 minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
|
||||||
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
|
||||||
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
|
||||||
var maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
|
||||||
this._bounds.location = new Vector2(minX, minY);
|
|
||||||
this._bounds.width = maxX - minX;
|
|
||||||
this._bounds.height = maxY - minY;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this._bounds.location = topLeft;
|
|
||||||
this._bounds.width = bottomRight.x - topLeft.x;
|
|
||||||
this._bounds.height = bottomRight.y - topLeft.y;
|
|
||||||
}
|
|
||||||
this._areBoundsDirty = false;
|
|
||||||
}
|
|
||||||
return this._bounds;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Object.defineProperty(Camera.prototype, "zoom", {
|
Object.defineProperty(Camera.prototype, "zoom", {
|
||||||
get: function () {
|
get: function () {
|
||||||
if (this._zoom == 0)
|
if (this._zoom == 0)
|
||||||
@@ -1667,7 +1620,6 @@ var Camera = (function (_super) {
|
|||||||
set: function (value) {
|
set: function (value) {
|
||||||
if (this._origin != value) {
|
if (this._origin != value) {
|
||||||
this._origin = value;
|
this._origin = value;
|
||||||
this._areMatrixesDirty = true;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
@@ -1683,24 +1635,6 @@ var Camera = (function (_super) {
|
|||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: true
|
configurable: true
|
||||||
});
|
});
|
||||||
Object.defineProperty(Camera.prototype, "transformMatrix", {
|
|
||||||
get: function () {
|
|
||||||
if (this._areBoundsDirty)
|
|
||||||
this.updateMatrixes();
|
|
||||||
return this._transformMatrix;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Object.defineProperty(Camera.prototype, "inverseTransformMatrix", {
|
|
||||||
get: function () {
|
|
||||||
if (this._areBoundsDirty)
|
|
||||||
this.updateMatrixes();
|
|
||||||
return this._inverseTransformMatrix;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Camera.prototype.onSceneSizeChanged = function (newWidth, newHeight) {
|
Camera.prototype.onSceneSizeChanged = function (newWidth, newHeight) {
|
||||||
var oldOrigin = this._origin;
|
var oldOrigin = this._origin;
|
||||||
this.origin = new Vector2(newWidth / 2, newHeight / 2);
|
this.origin = new Vector2(newWidth / 2, newHeight / 2);
|
||||||
@@ -1729,59 +1663,96 @@ var Camera = (function (_super) {
|
|||||||
else {
|
else {
|
||||||
this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom);
|
this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom);
|
||||||
}
|
}
|
||||||
this._areMatrixesDirty = true;
|
SceneManager.scene.scaleX = this._zoom;
|
||||||
|
SceneManager.scene.scaleY = this._zoom;
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
Camera.prototype.setRotation = function (rotation) {
|
||||||
|
SceneManager.scene.rotation = rotation;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Camera.prototype.setPosition = function (position) {
|
Camera.prototype.setPosition = function (position) {
|
||||||
this.entity.position = position;
|
this.entity.position = position;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Camera.prototype.forceMatrixUpdate = function () {
|
Camera.prototype.follow = function (targetEntity, cameraStyle) {
|
||||||
this._areMatrixesDirty = true;
|
if (cameraStyle === void 0) { cameraStyle = CameraStyle.cameraWindow; }
|
||||||
};
|
this.targetEntity = targetEntity;
|
||||||
Camera.prototype.updateMatrixes = function () {
|
this.cameraStyle = cameraStyle;
|
||||||
if (!this._areMatrixesDirty)
|
var cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||||
return;
|
switch (this.cameraStyle) {
|
||||||
var tempMat;
|
case CameraStyle.cameraWindow:
|
||||||
this._transformMatrix = Matrix2D.createTranslation(-this.entity.position.x, -this.entity.position.y);
|
var w = cameraBounds.width / 6;
|
||||||
if (this._zoom != 1) {
|
var h = cameraBounds.height / 3;
|
||||||
tempMat = Matrix2D.createScale(this._zoom, this._zoom);
|
this.deadzone = new Rectangle((cameraBounds.width - w) / 2, (cameraBounds.height - h) / 2, w, h);
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
break;
|
||||||
|
case CameraStyle.lockOn:
|
||||||
|
this.deadzone = new Rectangle(cameraBounds.width / 2, cameraBounds.height / 2, 10, 10);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (this.entity.rotation != 0) {
|
};
|
||||||
tempMat = Matrix2D.createRotation(this.entity.rotation);
|
Camera.prototype.update = function () {
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
var cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||||
|
var halfScreen = Vector2.multiply(new Vector2(cameraBounds.width, cameraBounds.height), new Vector2(0.5));
|
||||||
|
this._worldSpaceDeadZone.x = this.position.x - halfScreen.x + this.deadzone.x + this.focusOffset.x;
|
||||||
|
this._worldSpaceDeadZone.y = this.position.y - halfScreen.y + this.deadzone.y + this.focusOffset.y;
|
||||||
|
this._worldSpaceDeadZone.width = this.deadzone.width;
|
||||||
|
this._worldSpaceDeadZone.height = this.deadzone.height;
|
||||||
|
if (this.targetEntity)
|
||||||
|
this.updateFollow();
|
||||||
|
this.position = Vector2.lerp(this.position, Vector2.add(this.position, this._desiredPositionDelta), this.followLerp);
|
||||||
|
this.entity.roundPosition();
|
||||||
|
if (this.mapLockEnabled) {
|
||||||
|
this.position = this.clampToMapSize(this.position);
|
||||||
|
this.entity.roundPosition();
|
||||||
}
|
}
|
||||||
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
|
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
|
||||||
this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix);
|
|
||||||
this._areBoundsDirty = true;
|
|
||||||
this._areMatrixesDirty = false;
|
|
||||||
};
|
};
|
||||||
Camera.prototype.screenToWorldPoint = function (screenPosition) {
|
Camera.prototype.clampToMapSize = function (position) {
|
||||||
this.updateMatrixes();
|
var cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||||
return Vector2Ext.transformR(screenPosition, this._inverseTransformMatrix);
|
var halfScreen = Vector2.multiply(new Vector2(cameraBounds.width, cameraBounds.height), new Vector2(0.5));
|
||||||
|
var cameraMax = new Vector2(this.mapSize.x - halfScreen.x, this.mapSize.y - halfScreen.y);
|
||||||
|
return Vector2.clamp(position, halfScreen, cameraMax);
|
||||||
};
|
};
|
||||||
Camera.prototype.worldToScreenPoint = function (worldPosition) {
|
Camera.prototype.updateFollow = function () {
|
||||||
this.updateMatrixes();
|
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
|
||||||
return Vector2Ext.transformR(worldPosition, this._transformMatrix);
|
if (this.cameraStyle == CameraStyle.lockOn) {
|
||||||
};
|
var targetX = this.targetEntity.position.x;
|
||||||
Camera.prototype.onEntityTransformChanged = function (comp) {
|
var targetY = this.targetEntity.position.y;
|
||||||
this._areMatrixesDirty = true;
|
if (this._worldSpaceDeadZone.x > targetX)
|
||||||
};
|
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
||||||
Camera.prototype.destory = function () {
|
else if (this._worldSpaceDeadZone.x < targetX)
|
||||||
|
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
||||||
|
if (this._worldSpaceDeadZone.y < targetY)
|
||||||
|
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
||||||
|
else if (this._worldSpaceDeadZone.y > targetY)
|
||||||
|
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (!this._targetCollider) {
|
||||||
|
this._targetCollider = this.targetEntity.getComponent(Collider);
|
||||||
|
if (!this._targetCollider)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var targetBounds = this.targetEntity.getComponent(Collider).bounds;
|
||||||
|
if (!this._worldSpaceDeadZone.containsRect(targetBounds)) {
|
||||||
|
if (this._worldSpaceDeadZone.left > targetBounds.left)
|
||||||
|
this._desiredPositionDelta.x = targetBounds.left - this._worldSpaceDeadZone.left;
|
||||||
|
else if (this._worldSpaceDeadZone.right < targetBounds.right)
|
||||||
|
this._desiredPositionDelta.x = targetBounds.right - this._worldSpaceDeadZone.right;
|
||||||
|
if (this._worldSpaceDeadZone.bottom < targetBounds.bottom)
|
||||||
|
this._desiredPositionDelta.y = targetBounds.bottom - this._worldSpaceDeadZone.bottom;
|
||||||
|
else if (this._worldSpaceDeadZone.top > targetBounds.top)
|
||||||
|
this._desiredPositionDelta.y = targetBounds.top - this._worldSpaceDeadZone.top;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return Camera;
|
return Camera;
|
||||||
}(Component));
|
}(Component));
|
||||||
var CameraInset = (function () {
|
var CameraStyle;
|
||||||
function CameraInset() {
|
(function (CameraStyle) {
|
||||||
this.left = 0;
|
CameraStyle[CameraStyle["lockOn"] = 0] = "lockOn";
|
||||||
this.right = 0;
|
CameraStyle[CameraStyle["cameraWindow"] = 1] = "cameraWindow";
|
||||||
this.top = 0;
|
})(CameraStyle || (CameraStyle = {}));
|
||||||
this.bottom = 0;
|
|
||||||
}
|
|
||||||
return CameraInset;
|
|
||||||
}());
|
|
||||||
var ComponentPool = (function () {
|
var ComponentPool = (function () {
|
||||||
function ComponentPool(typeClass) {
|
function ComponentPool(typeClass) {
|
||||||
this._type = typeClass;
|
this._type = typeClass;
|
||||||
@@ -1801,103 +1772,6 @@ var ComponentPool = (function () {
|
|||||||
};
|
};
|
||||||
return ComponentPool;
|
return ComponentPool;
|
||||||
}());
|
}());
|
||||||
var FollowCamera = (function (_super) {
|
|
||||||
__extends(FollowCamera, _super);
|
|
||||||
function FollowCamera(targetEntity, cameraStyle) {
|
|
||||||
if (cameraStyle === void 0) { cameraStyle = CameraStyle.lockOn; }
|
|
||||||
var _this = _super.call(this) || this;
|
|
||||||
_this.followLerp = 0.1;
|
|
||||||
_this.deadzone = new Rectangle();
|
|
||||||
_this.focusOffset = new Vector2();
|
|
||||||
_this.mapSize = new Vector2();
|
|
||||||
_this._worldSpaceDeadZone = new Rectangle();
|
|
||||||
_this._desiredPositionDelta = new Vector2();
|
|
||||||
_this._targetEntity = targetEntity;
|
|
||||||
_this._cameraStyle = cameraStyle;
|
|
||||||
_this.camera = null;
|
|
||||||
return _this;
|
|
||||||
}
|
|
||||||
FollowCamera.prototype.onAddedToEntity = function () {
|
|
||||||
if (!this.camera)
|
|
||||||
this.camera = this.entity.scene.camera;
|
|
||||||
this.follow(this._targetEntity, this._cameraStyle);
|
|
||||||
};
|
|
||||||
FollowCamera.prototype.follow = function (targetEntity, cameraStyle) {
|
|
||||||
if (cameraStyle === void 0) { cameraStyle = CameraStyle.cameraWindow; }
|
|
||||||
this._targetEntity = targetEntity;
|
|
||||||
this._cameraStyle = cameraStyle;
|
|
||||||
var cameraBounds = this.camera.bounds;
|
|
||||||
switch (this._cameraStyle) {
|
|
||||||
case CameraStyle.cameraWindow:
|
|
||||||
var w = cameraBounds.width / 6;
|
|
||||||
var h = cameraBounds.height / 3;
|
|
||||||
this.deadzone = new Rectangle((cameraBounds.width - w) / 2, (cameraBounds.height - h) / 2, w, h);
|
|
||||||
break;
|
|
||||||
case CameraStyle.lockOn:
|
|
||||||
this.deadzone = new Rectangle(cameraBounds.width / 2, cameraBounds.height / 2, 10, 10);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
FollowCamera.prototype.update = function () {
|
|
||||||
var halfScreen = Vector2.multiply(this.camera.bounds.size, new Vector2(0.5));
|
|
||||||
this._worldSpaceDeadZone.x = this.camera.position.x - halfScreen.x + this.deadzone.x + this.focusOffset.x;
|
|
||||||
this._worldSpaceDeadZone.y = this.camera.position.y - halfScreen.y + this.deadzone.y + this.focusOffset.y;
|
|
||||||
this._worldSpaceDeadZone.width = this.deadzone.width;
|
|
||||||
this._worldSpaceDeadZone.height = this.deadzone.height;
|
|
||||||
if (this._targetEntity)
|
|
||||||
this.updateFollow();
|
|
||||||
this.camera.position = Vector2.lerp(this.camera.position, Vector2.add(this.camera.position, this._desiredPositionDelta), this.followLerp);
|
|
||||||
this.camera.entity.roundPosition();
|
|
||||||
if (this.mapLockEnabled) {
|
|
||||||
this.camera.position = this.clampToMapSize(this.camera.position);
|
|
||||||
this.camera.entity.roundPosition();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
FollowCamera.prototype.clampToMapSize = function (position) {
|
|
||||||
var halfScreen = Vector2.multiply(new Vector2(this.camera.bounds.width, this.camera.bounds.height), new Vector2(0.5));
|
|
||||||
var cameraMax = new Vector2(this.mapSize.x - halfScreen.x, this.mapSize.y - halfScreen.y);
|
|
||||||
return Vector2.clamp(position, halfScreen, cameraMax);
|
|
||||||
};
|
|
||||||
FollowCamera.prototype.updateFollow = function () {
|
|
||||||
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
|
|
||||||
if (this._cameraStyle == CameraStyle.lockOn) {
|
|
||||||
var targetX = this._targetEntity.position.x;
|
|
||||||
var targetY = this._targetEntity.position.y;
|
|
||||||
if (this._worldSpaceDeadZone.x > targetX)
|
|
||||||
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
|
||||||
else if (this._worldSpaceDeadZone.x < targetX)
|
|
||||||
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
|
||||||
if (this._worldSpaceDeadZone.y < targetY)
|
|
||||||
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
|
||||||
else if (this._worldSpaceDeadZone.y > targetY)
|
|
||||||
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (!this._targetCollider) {
|
|
||||||
this._targetCollider = this._targetEntity.getComponent(Collider);
|
|
||||||
if (!this._targetCollider)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var targetBounds = this._targetEntity.getComponent(Collider).bounds;
|
|
||||||
if (!this._worldSpaceDeadZone.containsRect(targetBounds)) {
|
|
||||||
if (this._worldSpaceDeadZone.left > targetBounds.left)
|
|
||||||
this._desiredPositionDelta.x = targetBounds.left - this._worldSpaceDeadZone.left;
|
|
||||||
else if (this._worldSpaceDeadZone.right < targetBounds.right)
|
|
||||||
this._desiredPositionDelta.x = targetBounds.right - this._worldSpaceDeadZone.right;
|
|
||||||
if (this._worldSpaceDeadZone.bottom < targetBounds.bottom)
|
|
||||||
this._desiredPositionDelta.y = targetBounds.bottom - this._worldSpaceDeadZone.bottom;
|
|
||||||
else if (this._worldSpaceDeadZone.top > targetBounds.top)
|
|
||||||
this._desiredPositionDelta.y = targetBounds.top - this._worldSpaceDeadZone.top;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return FollowCamera;
|
|
||||||
}(Component));
|
|
||||||
var CameraStyle;
|
|
||||||
(function (CameraStyle) {
|
|
||||||
CameraStyle[CameraStyle["lockOn"] = 0] = "lockOn";
|
|
||||||
CameraStyle[CameraStyle["cameraWindow"] = 1] = "cameraWindow";
|
|
||||||
})(CameraStyle || (CameraStyle = {}));
|
|
||||||
var Mesh = (function (_super) {
|
var Mesh = (function (_super) {
|
||||||
__extends(Mesh, _super);
|
__extends(Mesh, _super);
|
||||||
function Mesh() {
|
function Mesh() {
|
||||||
@@ -2017,14 +1891,14 @@ var RenderableComponent = (function (_super) {
|
|||||||
RenderableComponent.prototype.onBecameVisible = function () { };
|
RenderableComponent.prototype.onBecameVisible = function () { };
|
||||||
RenderableComponent.prototype.onBecameInvisible = function () { };
|
RenderableComponent.prototype.onBecameInvisible = function () { };
|
||||||
RenderableComponent.prototype.isVisibleFromCamera = function (camera) {
|
RenderableComponent.prototype.isVisibleFromCamera = function (camera) {
|
||||||
this.isVisible = camera.bounds.intersects(this.bounds);
|
this.isVisible = camera.getBounds().intersects(this.getBounds());
|
||||||
return this.isVisible;
|
return this.isVisible;
|
||||||
};
|
};
|
||||||
RenderableComponent.prototype.onEntityTransformChanged = function (comp) {
|
RenderableComponent.prototype.onEntityTransformChanged = function (comp) {
|
||||||
this._areBoundsDirty = true;
|
this._areBoundsDirty = true;
|
||||||
};
|
};
|
||||||
return RenderableComponent;
|
return RenderableComponent;
|
||||||
}(Component));
|
}(PooledComponent));
|
||||||
var ScreenSpaceCamera = (function (_super) {
|
var ScreenSpaceCamera = (function (_super) {
|
||||||
__extends(ScreenSpaceCamera, _super);
|
__extends(ScreenSpaceCamera, _super);
|
||||||
function ScreenSpaceCamera() {
|
function ScreenSpaceCamera() {
|
||||||
@@ -2036,7 +1910,7 @@ var ScreenSpaceCamera = (function (_super) {
|
|||||||
}(Camera));
|
}(Camera));
|
||||||
var Sprite = (function () {
|
var Sprite = (function () {
|
||||||
function Sprite(texture, sourceRect, origin) {
|
function Sprite(texture, sourceRect, origin) {
|
||||||
if (sourceRect === void 0) { sourceRect = new Rectangle(texture.textureWidth, texture.textureHeight); }
|
if (sourceRect === void 0) { sourceRect = new Rectangle(0, 0, texture.textureWidth, texture.textureHeight); }
|
||||||
if (origin === void 0) { origin = sourceRect.getHalfSize(); }
|
if (origin === void 0) { origin = sourceRect.getHalfSize(); }
|
||||||
this.uvs = new Rectangle();
|
this.uvs = new Rectangle();
|
||||||
this.texture2D = texture;
|
this.texture2D = texture;
|
||||||
@@ -2070,13 +1944,17 @@ var SpriteRenderer = (function (_super) {
|
|||||||
SpriteRenderer.prototype.setOrigin = function (origin) {
|
SpriteRenderer.prototype.setOrigin = function (origin) {
|
||||||
if (this._origin != origin) {
|
if (this._origin != origin) {
|
||||||
this._origin = origin;
|
this._origin = origin;
|
||||||
this._areBoundsDirty = true;
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
SpriteRenderer.prototype.setSprite = function (sprite) {
|
SpriteRenderer.prototype.setSprite = function (sprite) {
|
||||||
this.removeChildren();
|
this.removeChildren();
|
||||||
this.addChild(new egret.Bitmap(sprite.texture2D));
|
this._sprite = sprite;
|
||||||
|
if (this._sprite)
|
||||||
|
this._origin = this._sprite.origin;
|
||||||
|
this._bitmap = new egret.Bitmap(sprite.texture2D);
|
||||||
|
this.addChild(this._bitmap);
|
||||||
|
return this;
|
||||||
};
|
};
|
||||||
SpriteRenderer.prototype.setColor = function (color) {
|
SpriteRenderer.prototype.setColor = function (color) {
|
||||||
var colorMatrix = [
|
var colorMatrix = [
|
||||||
@@ -2090,19 +1968,23 @@ var SpriteRenderer = (function (_super) {
|
|||||||
colorMatrix[12] = color % 256 / 255;
|
colorMatrix[12] = color % 256 / 255;
|
||||||
var colorFilter = new egret.ColorMatrixFilter(colorMatrix);
|
var colorFilter = new egret.ColorMatrixFilter(colorMatrix);
|
||||||
this.filters = [colorFilter];
|
this.filters = [colorFilter];
|
||||||
|
return this;
|
||||||
};
|
};
|
||||||
SpriteRenderer.prototype.isVisibleFromCamera = function (camera) {
|
SpriteRenderer.prototype.isVisibleFromCamera = function (camera) {
|
||||||
var topLeft = camera.screenToWorldPoint(new Vector2(0, 0));
|
this.isVisible = new Rectangle(0, 0, 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.visible = this.isVisible;
|
this.visible = this.isVisible;
|
||||||
return this.isVisible;
|
return this.isVisible;
|
||||||
};
|
};
|
||||||
SpriteRenderer.prototype.render = function (camera) {
|
SpriteRenderer.prototype.render = function (camera) {
|
||||||
|
this.x = this.entity.position.x - this.origin.x - camera.position.x + camera.origin.x;
|
||||||
|
this.y = this.entity.position.y - this.origin.y - camera.position.y + camera.origin.y;
|
||||||
};
|
};
|
||||||
SpriteRenderer.prototype.onRemovedFromEntity = function () {
|
SpriteRenderer.prototype.onRemovedFromEntity = function () {
|
||||||
if (this.parent)
|
if (this.parent)
|
||||||
this.parent.removeChild(this);
|
this.parent.removeChild(this);
|
||||||
};
|
};
|
||||||
|
SpriteRenderer.prototype.reset = function () {
|
||||||
|
};
|
||||||
return SpriteRenderer;
|
return SpriteRenderer;
|
||||||
}(RenderableComponent));
|
}(RenderableComponent));
|
||||||
var Mover = (function (_super) {
|
var Mover = (function (_super) {
|
||||||
@@ -3202,17 +3084,6 @@ var PolyLight = (function (_super) {
|
|||||||
_this.computeTriangleIndices();
|
_this.computeTriangleIndices();
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
Object.defineProperty(PolyLight.prototype, "bounds", {
|
|
||||||
get: function () {
|
|
||||||
if (this._areBoundsDirty) {
|
|
||||||
this._bounds.calculateBounds(this.entity.position, this._localOffset, new Vector2(this._radius), Vector2.one, 0, this._radius * 2, this._radius * 2);
|
|
||||||
this._areBoundsDirty = false;
|
|
||||||
}
|
|
||||||
return this._bounds;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Object.defineProperty(PolyLight.prototype, "radius", {
|
Object.defineProperty(PolyLight.prototype, "radius", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._radius;
|
return this._radius;
|
||||||
@@ -3240,6 +3111,8 @@ var PolyLight = (function (_super) {
|
|||||||
};
|
};
|
||||||
PolyLight.prototype.render = function (camera) {
|
PolyLight.prototype.render = function (camera) {
|
||||||
};
|
};
|
||||||
|
PolyLight.prototype.reset = function () {
|
||||||
|
};
|
||||||
return PolyLight;
|
return PolyLight;
|
||||||
}(RenderableComponent));
|
}(RenderableComponent));
|
||||||
var SceneTransition = (function () {
|
var SceneTransition = (function () {
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -9,13 +9,15 @@ class MainScene extends Scene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async onStart() {
|
public async onStart() {
|
||||||
let data = await this.content.loadRes("http://www.hyuan.org/123.jpeg", false);
|
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
|
||||||
let bgSprite = new Sprite(data);
|
|
||||||
let bg = this.createEntity("bg");
|
let bg = this.createEntity("bg");
|
||||||
bg.position = new Vector2(0, 0);
|
bg.addComponent(new SpriteRenderer()).setSprite(sprite).setColor(0xff0000);
|
||||||
bg.addComponent(new SpriteRenderer()).setSprite(bgSprite);
|
bg.addComponent(new PlayerController());
|
||||||
|
bg.addComponent(new Mover());
|
||||||
|
// bg.addComponent(new BoxCollider());
|
||||||
|
bg.position = new Vector2(Math.random() * 200, Math.random() * 200);
|
||||||
|
|
||||||
for (let i = 0; i < 20; i++) {
|
for (let i = 0; i < 100; i++) {
|
||||||
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
|
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
|
||||||
let player2 = this.createEntity("player2");
|
let player2 = this.createEntity("player2");
|
||||||
player2.addComponent(new SpriteRenderer()).setSprite(sprite);
|
player2.addComponent(new SpriteRenderer()).setSprite(sprite);
|
||||||
@@ -23,6 +25,8 @@ class MainScene extends Scene {
|
|||||||
player2.addComponent(new BoxCollider());
|
player2.addComponent(new BoxCollider());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.camera.follow(bg, CameraStyle.lockOn);
|
||||||
|
|
||||||
let pool = new ComponentPool<SimplePooled>(SimplePooled);
|
let pool = new ComponentPool<SimplePooled>(SimplePooled);
|
||||||
let c1 = pool.obtain();
|
let c1 = pool.obtain();
|
||||||
let c2 = pool.obtain();
|
let c2 = pool.obtain();
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ class PlayerController extends Component {
|
|||||||
|
|
||||||
if (this.down){
|
if (this.down){
|
||||||
let camera = SceneManager.scene.camera;
|
let camera = SceneManager.scene.camera;
|
||||||
let worldVec = camera.screenToWorldPoint(this.touchPoint);
|
|
||||||
this.mover.move(Input.touchPositionDelta);
|
this.mover.move(Input.touchPositionDelta);
|
||||||
console.log(Input.touchPositionDelta);
|
console.log(Input.touchPositionDelta);
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+29
-52
@@ -145,8 +145,6 @@ declare abstract class Component extends egret.DisplayObjectContainer {
|
|||||||
updateInterval: number;
|
updateInterval: number;
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
setEnabled(isEnabled: boolean): this;
|
setEnabled(isEnabled: boolean): this;
|
||||||
readonly stage: egret.Stage;
|
|
||||||
readonly scene: Scene;
|
|
||||||
initialize(): void;
|
initialize(): void;
|
||||||
onAddedToEntity(): void;
|
onAddedToEntity(): void;
|
||||||
onRemovedFromEntity(): void;
|
onRemovedFromEntity(): void;
|
||||||
@@ -160,6 +158,7 @@ declare abstract class Component extends egret.DisplayObjectContainer {
|
|||||||
}
|
}
|
||||||
declare class Entity extends egret.DisplayObjectContainer {
|
declare class Entity extends egret.DisplayObjectContainer {
|
||||||
private static _idGenerator;
|
private static _idGenerator;
|
||||||
|
private _position;
|
||||||
name: string;
|
name: string;
|
||||||
readonly id: number;
|
readonly id: number;
|
||||||
scene: Scene;
|
scene: Scene;
|
||||||
@@ -308,62 +307,30 @@ declare class Transform {
|
|||||||
declare class Camera extends Component {
|
declare class Camera extends Component {
|
||||||
private _zoom;
|
private _zoom;
|
||||||
private _origin;
|
private _origin;
|
||||||
private _transformMatrix;
|
|
||||||
private _inverseTransformMatrix;
|
|
||||||
private _minimumZoom;
|
private _minimumZoom;
|
||||||
private _maximumZoom;
|
private _maximumZoom;
|
||||||
private _areMatrixesDirty;
|
|
||||||
private _inset;
|
|
||||||
private _bounds;
|
|
||||||
private _areBoundsDirty;
|
|
||||||
readonly bounds: Rectangle;
|
|
||||||
zoom: number;
|
|
||||||
minimumZoom: number;
|
|
||||||
maximumZoom: number;
|
|
||||||
origin: Vector2;
|
|
||||||
position: Vector2;
|
|
||||||
readonly transformMatrix: Matrix2D;
|
|
||||||
readonly inverseTransformMatrix: Matrix2D;
|
|
||||||
constructor();
|
|
||||||
onSceneSizeChanged(newWidth: number, newHeight: number): void;
|
|
||||||
setMinimumZoom(minZoom: number): Camera;
|
|
||||||
setMaximumZoom(maxZoom: number): Camera;
|
|
||||||
setZoom(zoom: number): this;
|
|
||||||
setPosition(position: Vector2): this;
|
|
||||||
forceMatrixUpdate(): void;
|
|
||||||
protected updateMatrixes(): void;
|
|
||||||
screenToWorldPoint(screenPosition: Vector2): Vector2;
|
|
||||||
worldToScreenPoint(worldPosition: Vector2): Vector2;
|
|
||||||
onEntityTransformChanged(comp: ComponentTransform): void;
|
|
||||||
destory(): void;
|
|
||||||
}
|
|
||||||
declare class CameraInset {
|
|
||||||
left: number;
|
|
||||||
right: number;
|
|
||||||
top: number;
|
|
||||||
bottom: number;
|
|
||||||
}
|
|
||||||
declare class ComponentPool<T extends PooledComponent> {
|
|
||||||
private _cache;
|
|
||||||
private _type;
|
|
||||||
constructor(typeClass: any);
|
|
||||||
obtain(): T;
|
|
||||||
free(component: T): void;
|
|
||||||
}
|
|
||||||
declare class FollowCamera extends Component {
|
|
||||||
camera: Camera;
|
|
||||||
followLerp: number;
|
followLerp: number;
|
||||||
deadzone: Rectangle;
|
deadzone: Rectangle;
|
||||||
focusOffset: Vector2;
|
focusOffset: Vector2;
|
||||||
mapLockEnabled: boolean;
|
mapLockEnabled: boolean;
|
||||||
mapSize: Vector2;
|
mapSize: Vector2;
|
||||||
private _targetEntity;
|
targetEntity: Entity;
|
||||||
private _cameraStyle;
|
|
||||||
private _worldSpaceDeadZone;
|
private _worldSpaceDeadZone;
|
||||||
private _desiredPositionDelta;
|
private _desiredPositionDelta;
|
||||||
private _targetCollider;
|
private _targetCollider;
|
||||||
constructor(targetEntity: Entity, cameraStyle?: CameraStyle);
|
cameraStyle: CameraStyle;
|
||||||
onAddedToEntity(): void;
|
zoom: number;
|
||||||
|
minimumZoom: number;
|
||||||
|
maximumZoom: number;
|
||||||
|
origin: Vector2;
|
||||||
|
position: Vector2;
|
||||||
|
constructor();
|
||||||
|
onSceneSizeChanged(newWidth: number, newHeight: number): void;
|
||||||
|
setMinimumZoom(minZoom: number): Camera;
|
||||||
|
setMaximumZoom(maxZoom: number): Camera;
|
||||||
|
setZoom(zoom: number): Camera;
|
||||||
|
setRotation(rotation: number): Camera;
|
||||||
|
setPosition(position: Vector2): this;
|
||||||
follow(targetEntity: Entity, cameraStyle?: CameraStyle): void;
|
follow(targetEntity: Entity, cameraStyle?: CameraStyle): void;
|
||||||
update(): void;
|
update(): void;
|
||||||
private clampToMapSize;
|
private clampToMapSize;
|
||||||
@@ -373,6 +340,13 @@ declare enum CameraStyle {
|
|||||||
lockOn = 0,
|
lockOn = 0,
|
||||||
cameraWindow = 1
|
cameraWindow = 1
|
||||||
}
|
}
|
||||||
|
declare class ComponentPool<T extends PooledComponent> {
|
||||||
|
private _cache;
|
||||||
|
private _type;
|
||||||
|
constructor(typeClass: any);
|
||||||
|
obtain(): T;
|
||||||
|
free(component: T): void;
|
||||||
|
}
|
||||||
declare class Mesh extends Component {
|
declare class Mesh extends Component {
|
||||||
private _verts;
|
private _verts;
|
||||||
private _primitiveCount;
|
private _primitiveCount;
|
||||||
@@ -396,7 +370,7 @@ declare class PolygonMesh extends Mesh {
|
|||||||
declare abstract class PooledComponent extends Component {
|
declare abstract class PooledComponent extends Component {
|
||||||
abstract reset(): any;
|
abstract reset(): any;
|
||||||
}
|
}
|
||||||
declare abstract class RenderableComponent extends Component implements IRenderable {
|
declare abstract class RenderableComponent extends PooledComponent implements IRenderable {
|
||||||
private _isVisible;
|
private _isVisible;
|
||||||
protected _areBoundsDirty: boolean;
|
protected _areBoundsDirty: boolean;
|
||||||
protected _bounds: Rectangle;
|
protected _bounds: Rectangle;
|
||||||
@@ -427,13 +401,16 @@ declare class Sprite {
|
|||||||
}
|
}
|
||||||
declare class SpriteRenderer extends RenderableComponent {
|
declare class SpriteRenderer extends RenderableComponent {
|
||||||
private _origin;
|
private _origin;
|
||||||
|
private _bitmap;
|
||||||
|
private _sprite;
|
||||||
origin: Vector2;
|
origin: Vector2;
|
||||||
setOrigin(origin: Vector2): this;
|
setOrigin(origin: Vector2): this;
|
||||||
setSprite(sprite: Sprite): void;
|
setSprite(sprite: Sprite): SpriteRenderer;
|
||||||
setColor(color: number): void;
|
setColor(color: number): SpriteRenderer;
|
||||||
isVisibleFromCamera(camera: Camera): boolean;
|
isVisibleFromCamera(camera: Camera): boolean;
|
||||||
render(camera: Camera): void;
|
render(camera: Camera): void;
|
||||||
onRemovedFromEntity(): void;
|
onRemovedFromEntity(): void;
|
||||||
|
reset(): void;
|
||||||
}
|
}
|
||||||
interface ITriggerListener {
|
interface ITriggerListener {
|
||||||
onTriggerEnter(other: Collider, local: Collider): any;
|
onTriggerEnter(other: Collider, local: Collider): any;
|
||||||
@@ -703,12 +680,12 @@ declare class PolyLight extends RenderableComponent {
|
|||||||
protected _radius: number;
|
protected _radius: number;
|
||||||
private _lightEffect;
|
private _lightEffect;
|
||||||
private _indices;
|
private _indices;
|
||||||
readonly bounds: Rectangle;
|
|
||||||
radius: number;
|
radius: number;
|
||||||
constructor(radius: number, color: number, power: number);
|
constructor(radius: number, color: number, power: number);
|
||||||
private computeTriangleIndices;
|
private computeTriangleIndices;
|
||||||
setRadius(radius: number): void;
|
setRadius(radius: number): void;
|
||||||
render(camera: Camera): void;
|
render(camera: Camera): void;
|
||||||
|
reset(): void;
|
||||||
}
|
}
|
||||||
declare abstract class SceneTransition {
|
declare abstract class SceneTransition {
|
||||||
private _hasPreviousSceneRender;
|
private _hasPreviousSceneRender;
|
||||||
|
|||||||
+109
-236
@@ -802,24 +802,6 @@ var Component = (function (_super) {
|
|||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Object.defineProperty(Component.prototype, "stage", {
|
|
||||||
get: function () {
|
|
||||||
if (!this.entity)
|
|
||||||
return null;
|
|
||||||
return this.entity.stage;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Object.defineProperty(Component.prototype, "scene", {
|
|
||||||
get: function () {
|
|
||||||
if (!this.entity)
|
|
||||||
return null;
|
|
||||||
return this.entity.scene;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Component.prototype.initialize = function () {
|
Component.prototype.initialize = function () {
|
||||||
};
|
};
|
||||||
Component.prototype.onAddedToEntity = function () {
|
Component.prototype.onAddedToEntity = function () {
|
||||||
@@ -850,6 +832,7 @@ var Entity = (function (_super) {
|
|||||||
__extends(Entity, _super);
|
__extends(Entity, _super);
|
||||||
function Entity(name) {
|
function Entity(name) {
|
||||||
var _this = _super.call(this) || this;
|
var _this = _super.call(this) || this;
|
||||||
|
_this._position = Vector2.zero;
|
||||||
_this._updateOrder = 0;
|
_this._updateOrder = 0;
|
||||||
_this._enabled = true;
|
_this._enabled = true;
|
||||||
_this._tag = 0;
|
_this._tag = 0;
|
||||||
@@ -868,11 +851,10 @@ var Entity = (function (_super) {
|
|||||||
});
|
});
|
||||||
Object.defineProperty(Entity.prototype, "position", {
|
Object.defineProperty(Entity.prototype, "position", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return new Vector2(this.x, this.y);
|
return this._position;
|
||||||
},
|
},
|
||||||
set: function (value) {
|
set: function (value) {
|
||||||
this.x = value.x;
|
this._position = value;
|
||||||
this.y = value.y;
|
|
||||||
},
|
},
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: true
|
configurable: true
|
||||||
@@ -1043,6 +1025,8 @@ var Scene = (function (_super) {
|
|||||||
_this.renderableComponents = new RenderableComponentList();
|
_this.renderableComponents = new RenderableComponentList();
|
||||||
_this.entities = new EntityList(_this);
|
_this.entities = new EntityList(_this);
|
||||||
_this.content = new ContentManager();
|
_this.content = new ContentManager();
|
||||||
|
_this.width = SceneManager.stage.stageWidth;
|
||||||
|
_this.height = SceneManager.stage.stageHeight;
|
||||||
_this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this);
|
_this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this);
|
||||||
_this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this);
|
_this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this);
|
||||||
return _this;
|
return _this;
|
||||||
@@ -1128,7 +1112,6 @@ var Scene = (function (_super) {
|
|||||||
this.entities.removeAllEntities();
|
this.entities.removeAllEntities();
|
||||||
this.removeChildren();
|
this.removeChildren();
|
||||||
Physics.clear();
|
Physics.clear();
|
||||||
this.camera.destory();
|
|
||||||
this.camera = null;
|
this.camera = null;
|
||||||
this.content.dispose();
|
this.content.dispose();
|
||||||
if (this.entityProcessors)
|
if (this.entityProcessors)
|
||||||
@@ -1170,9 +1153,6 @@ 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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1584,48 +1564,21 @@ 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 = new Matrix2D();
|
|
||||||
_this._inverseTransformMatrix = new Matrix2D();
|
|
||||||
_this._minimumZoom = 0.3;
|
_this._minimumZoom = 0.3;
|
||||||
_this._maximumZoom = 3;
|
_this._maximumZoom = 3;
|
||||||
_this._areMatrixesDirty = true;
|
_this.followLerp = 0.1;
|
||||||
_this._inset = new CameraInset();
|
_this.deadzone = new Rectangle();
|
||||||
_this._bounds = new Rectangle();
|
_this.focusOffset = new Vector2();
|
||||||
_this._areBoundsDirty = true;
|
_this.mapLockEnabled = false;
|
||||||
|
_this.mapSize = new Vector2();
|
||||||
|
_this._worldSpaceDeadZone = new Rectangle();
|
||||||
|
_this._desiredPositionDelta = new Vector2();
|
||||||
|
_this.cameraStyle = CameraStyle.lockOn;
|
||||||
|
_this.width = SceneManager.stage.stageWidth;
|
||||||
|
_this.height = SceneManager.stage.stageHeight;
|
||||||
_this.setZoom(0);
|
_this.setZoom(0);
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
Object.defineProperty(Camera.prototype, "bounds", {
|
|
||||||
get: function () {
|
|
||||||
if (this._areMatrixesDirty)
|
|
||||||
this.updateMatrixes();
|
|
||||||
if (this._areBoundsDirty) {
|
|
||||||
var stage = this.stage;
|
|
||||||
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));
|
|
||||||
if (this.entity.rotation != 0) {
|
|
||||||
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 minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
|
||||||
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
|
||||||
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
|
||||||
var maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
|
||||||
this._bounds.location = new Vector2(minX, minY);
|
|
||||||
this._bounds.width = maxX - minX;
|
|
||||||
this._bounds.height = maxY - minY;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this._bounds.location = topLeft;
|
|
||||||
this._bounds.width = bottomRight.x - topLeft.x;
|
|
||||||
this._bounds.height = bottomRight.y - topLeft.y;
|
|
||||||
}
|
|
||||||
this._areBoundsDirty = false;
|
|
||||||
}
|
|
||||||
return this._bounds;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Object.defineProperty(Camera.prototype, "zoom", {
|
Object.defineProperty(Camera.prototype, "zoom", {
|
||||||
get: function () {
|
get: function () {
|
||||||
if (this._zoom == 0)
|
if (this._zoom == 0)
|
||||||
@@ -1667,7 +1620,6 @@ var Camera = (function (_super) {
|
|||||||
set: function (value) {
|
set: function (value) {
|
||||||
if (this._origin != value) {
|
if (this._origin != value) {
|
||||||
this._origin = value;
|
this._origin = value;
|
||||||
this._areMatrixesDirty = true;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
@@ -1683,24 +1635,6 @@ var Camera = (function (_super) {
|
|||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: true
|
configurable: true
|
||||||
});
|
});
|
||||||
Object.defineProperty(Camera.prototype, "transformMatrix", {
|
|
||||||
get: function () {
|
|
||||||
if (this._areBoundsDirty)
|
|
||||||
this.updateMatrixes();
|
|
||||||
return this._transformMatrix;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Object.defineProperty(Camera.prototype, "inverseTransformMatrix", {
|
|
||||||
get: function () {
|
|
||||||
if (this._areBoundsDirty)
|
|
||||||
this.updateMatrixes();
|
|
||||||
return this._inverseTransformMatrix;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Camera.prototype.onSceneSizeChanged = function (newWidth, newHeight) {
|
Camera.prototype.onSceneSizeChanged = function (newWidth, newHeight) {
|
||||||
var oldOrigin = this._origin;
|
var oldOrigin = this._origin;
|
||||||
this.origin = new Vector2(newWidth / 2, newHeight / 2);
|
this.origin = new Vector2(newWidth / 2, newHeight / 2);
|
||||||
@@ -1729,59 +1663,96 @@ var Camera = (function (_super) {
|
|||||||
else {
|
else {
|
||||||
this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom);
|
this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom);
|
||||||
}
|
}
|
||||||
this._areMatrixesDirty = true;
|
SceneManager.scene.scaleX = this._zoom;
|
||||||
|
SceneManager.scene.scaleY = this._zoom;
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
Camera.prototype.setRotation = function (rotation) {
|
||||||
|
SceneManager.scene.rotation = rotation;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Camera.prototype.setPosition = function (position) {
|
Camera.prototype.setPosition = function (position) {
|
||||||
this.entity.position = position;
|
this.entity.position = position;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Camera.prototype.forceMatrixUpdate = function () {
|
Camera.prototype.follow = function (targetEntity, cameraStyle) {
|
||||||
this._areMatrixesDirty = true;
|
if (cameraStyle === void 0) { cameraStyle = CameraStyle.cameraWindow; }
|
||||||
};
|
this.targetEntity = targetEntity;
|
||||||
Camera.prototype.updateMatrixes = function () {
|
this.cameraStyle = cameraStyle;
|
||||||
if (!this._areMatrixesDirty)
|
var cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||||
return;
|
switch (this.cameraStyle) {
|
||||||
var tempMat;
|
case CameraStyle.cameraWindow:
|
||||||
this._transformMatrix = Matrix2D.createTranslation(-this.entity.position.x, -this.entity.position.y);
|
var w = cameraBounds.width / 6;
|
||||||
if (this._zoom != 1) {
|
var h = cameraBounds.height / 3;
|
||||||
tempMat = Matrix2D.createScale(this._zoom, this._zoom);
|
this.deadzone = new Rectangle((cameraBounds.width - w) / 2, (cameraBounds.height - h) / 2, w, h);
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
break;
|
||||||
|
case CameraStyle.lockOn:
|
||||||
|
this.deadzone = new Rectangle(cameraBounds.width / 2, cameraBounds.height / 2, 10, 10);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (this.entity.rotation != 0) {
|
};
|
||||||
tempMat = Matrix2D.createRotation(this.entity.rotation);
|
Camera.prototype.update = function () {
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
var cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||||
|
var halfScreen = Vector2.multiply(new Vector2(cameraBounds.width, cameraBounds.height), new Vector2(0.5));
|
||||||
|
this._worldSpaceDeadZone.x = this.position.x - halfScreen.x + this.deadzone.x + this.focusOffset.x;
|
||||||
|
this._worldSpaceDeadZone.y = this.position.y - halfScreen.y + this.deadzone.y + this.focusOffset.y;
|
||||||
|
this._worldSpaceDeadZone.width = this.deadzone.width;
|
||||||
|
this._worldSpaceDeadZone.height = this.deadzone.height;
|
||||||
|
if (this.targetEntity)
|
||||||
|
this.updateFollow();
|
||||||
|
this.position = Vector2.lerp(this.position, Vector2.add(this.position, this._desiredPositionDelta), this.followLerp);
|
||||||
|
this.entity.roundPosition();
|
||||||
|
if (this.mapLockEnabled) {
|
||||||
|
this.position = this.clampToMapSize(this.position);
|
||||||
|
this.entity.roundPosition();
|
||||||
}
|
}
|
||||||
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
|
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
|
||||||
this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix);
|
|
||||||
this._areBoundsDirty = true;
|
|
||||||
this._areMatrixesDirty = false;
|
|
||||||
};
|
};
|
||||||
Camera.prototype.screenToWorldPoint = function (screenPosition) {
|
Camera.prototype.clampToMapSize = function (position) {
|
||||||
this.updateMatrixes();
|
var cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||||
return Vector2Ext.transformR(screenPosition, this._inverseTransformMatrix);
|
var halfScreen = Vector2.multiply(new Vector2(cameraBounds.width, cameraBounds.height), new Vector2(0.5));
|
||||||
|
var cameraMax = new Vector2(this.mapSize.x - halfScreen.x, this.mapSize.y - halfScreen.y);
|
||||||
|
return Vector2.clamp(position, halfScreen, cameraMax);
|
||||||
};
|
};
|
||||||
Camera.prototype.worldToScreenPoint = function (worldPosition) {
|
Camera.prototype.updateFollow = function () {
|
||||||
this.updateMatrixes();
|
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
|
||||||
return Vector2Ext.transformR(worldPosition, this._transformMatrix);
|
if (this.cameraStyle == CameraStyle.lockOn) {
|
||||||
};
|
var targetX = this.targetEntity.position.x;
|
||||||
Camera.prototype.onEntityTransformChanged = function (comp) {
|
var targetY = this.targetEntity.position.y;
|
||||||
this._areMatrixesDirty = true;
|
if (this._worldSpaceDeadZone.x > targetX)
|
||||||
};
|
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
||||||
Camera.prototype.destory = function () {
|
else if (this._worldSpaceDeadZone.x < targetX)
|
||||||
|
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
||||||
|
if (this._worldSpaceDeadZone.y < targetY)
|
||||||
|
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
||||||
|
else if (this._worldSpaceDeadZone.y > targetY)
|
||||||
|
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (!this._targetCollider) {
|
||||||
|
this._targetCollider = this.targetEntity.getComponent(Collider);
|
||||||
|
if (!this._targetCollider)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var targetBounds = this.targetEntity.getComponent(Collider).bounds;
|
||||||
|
if (!this._worldSpaceDeadZone.containsRect(targetBounds)) {
|
||||||
|
if (this._worldSpaceDeadZone.left > targetBounds.left)
|
||||||
|
this._desiredPositionDelta.x = targetBounds.left - this._worldSpaceDeadZone.left;
|
||||||
|
else if (this._worldSpaceDeadZone.right < targetBounds.right)
|
||||||
|
this._desiredPositionDelta.x = targetBounds.right - this._worldSpaceDeadZone.right;
|
||||||
|
if (this._worldSpaceDeadZone.bottom < targetBounds.bottom)
|
||||||
|
this._desiredPositionDelta.y = targetBounds.bottom - this._worldSpaceDeadZone.bottom;
|
||||||
|
else if (this._worldSpaceDeadZone.top > targetBounds.top)
|
||||||
|
this._desiredPositionDelta.y = targetBounds.top - this._worldSpaceDeadZone.top;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return Camera;
|
return Camera;
|
||||||
}(Component));
|
}(Component));
|
||||||
var CameraInset = (function () {
|
var CameraStyle;
|
||||||
function CameraInset() {
|
(function (CameraStyle) {
|
||||||
this.left = 0;
|
CameraStyle[CameraStyle["lockOn"] = 0] = "lockOn";
|
||||||
this.right = 0;
|
CameraStyle[CameraStyle["cameraWindow"] = 1] = "cameraWindow";
|
||||||
this.top = 0;
|
})(CameraStyle || (CameraStyle = {}));
|
||||||
this.bottom = 0;
|
|
||||||
}
|
|
||||||
return CameraInset;
|
|
||||||
}());
|
|
||||||
var ComponentPool = (function () {
|
var ComponentPool = (function () {
|
||||||
function ComponentPool(typeClass) {
|
function ComponentPool(typeClass) {
|
||||||
this._type = typeClass;
|
this._type = typeClass;
|
||||||
@@ -1801,103 +1772,6 @@ var ComponentPool = (function () {
|
|||||||
};
|
};
|
||||||
return ComponentPool;
|
return ComponentPool;
|
||||||
}());
|
}());
|
||||||
var FollowCamera = (function (_super) {
|
|
||||||
__extends(FollowCamera, _super);
|
|
||||||
function FollowCamera(targetEntity, cameraStyle) {
|
|
||||||
if (cameraStyle === void 0) { cameraStyle = CameraStyle.lockOn; }
|
|
||||||
var _this = _super.call(this) || this;
|
|
||||||
_this.followLerp = 0.1;
|
|
||||||
_this.deadzone = new Rectangle();
|
|
||||||
_this.focusOffset = new Vector2();
|
|
||||||
_this.mapSize = new Vector2();
|
|
||||||
_this._worldSpaceDeadZone = new Rectangle();
|
|
||||||
_this._desiredPositionDelta = new Vector2();
|
|
||||||
_this._targetEntity = targetEntity;
|
|
||||||
_this._cameraStyle = cameraStyle;
|
|
||||||
_this.camera = null;
|
|
||||||
return _this;
|
|
||||||
}
|
|
||||||
FollowCamera.prototype.onAddedToEntity = function () {
|
|
||||||
if (!this.camera)
|
|
||||||
this.camera = this.entity.scene.camera;
|
|
||||||
this.follow(this._targetEntity, this._cameraStyle);
|
|
||||||
};
|
|
||||||
FollowCamera.prototype.follow = function (targetEntity, cameraStyle) {
|
|
||||||
if (cameraStyle === void 0) { cameraStyle = CameraStyle.cameraWindow; }
|
|
||||||
this._targetEntity = targetEntity;
|
|
||||||
this._cameraStyle = cameraStyle;
|
|
||||||
var cameraBounds = this.camera.bounds;
|
|
||||||
switch (this._cameraStyle) {
|
|
||||||
case CameraStyle.cameraWindow:
|
|
||||||
var w = cameraBounds.width / 6;
|
|
||||||
var h = cameraBounds.height / 3;
|
|
||||||
this.deadzone = new Rectangle((cameraBounds.width - w) / 2, (cameraBounds.height - h) / 2, w, h);
|
|
||||||
break;
|
|
||||||
case CameraStyle.lockOn:
|
|
||||||
this.deadzone = new Rectangle(cameraBounds.width / 2, cameraBounds.height / 2, 10, 10);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
FollowCamera.prototype.update = function () {
|
|
||||||
var halfScreen = Vector2.multiply(this.camera.bounds.size, new Vector2(0.5));
|
|
||||||
this._worldSpaceDeadZone.x = this.camera.position.x - halfScreen.x + this.deadzone.x + this.focusOffset.x;
|
|
||||||
this._worldSpaceDeadZone.y = this.camera.position.y - halfScreen.y + this.deadzone.y + this.focusOffset.y;
|
|
||||||
this._worldSpaceDeadZone.width = this.deadzone.width;
|
|
||||||
this._worldSpaceDeadZone.height = this.deadzone.height;
|
|
||||||
if (this._targetEntity)
|
|
||||||
this.updateFollow();
|
|
||||||
this.camera.position = Vector2.lerp(this.camera.position, Vector2.add(this.camera.position, this._desiredPositionDelta), this.followLerp);
|
|
||||||
this.camera.entity.roundPosition();
|
|
||||||
if (this.mapLockEnabled) {
|
|
||||||
this.camera.position = this.clampToMapSize(this.camera.position);
|
|
||||||
this.camera.entity.roundPosition();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
FollowCamera.prototype.clampToMapSize = function (position) {
|
|
||||||
var halfScreen = Vector2.multiply(new Vector2(this.camera.bounds.width, this.camera.bounds.height), new Vector2(0.5));
|
|
||||||
var cameraMax = new Vector2(this.mapSize.x - halfScreen.x, this.mapSize.y - halfScreen.y);
|
|
||||||
return Vector2.clamp(position, halfScreen, cameraMax);
|
|
||||||
};
|
|
||||||
FollowCamera.prototype.updateFollow = function () {
|
|
||||||
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
|
|
||||||
if (this._cameraStyle == CameraStyle.lockOn) {
|
|
||||||
var targetX = this._targetEntity.position.x;
|
|
||||||
var targetY = this._targetEntity.position.y;
|
|
||||||
if (this._worldSpaceDeadZone.x > targetX)
|
|
||||||
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
|
||||||
else if (this._worldSpaceDeadZone.x < targetX)
|
|
||||||
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
|
||||||
if (this._worldSpaceDeadZone.y < targetY)
|
|
||||||
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
|
||||||
else if (this._worldSpaceDeadZone.y > targetY)
|
|
||||||
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (!this._targetCollider) {
|
|
||||||
this._targetCollider = this._targetEntity.getComponent(Collider);
|
|
||||||
if (!this._targetCollider)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var targetBounds = this._targetEntity.getComponent(Collider).bounds;
|
|
||||||
if (!this._worldSpaceDeadZone.containsRect(targetBounds)) {
|
|
||||||
if (this._worldSpaceDeadZone.left > targetBounds.left)
|
|
||||||
this._desiredPositionDelta.x = targetBounds.left - this._worldSpaceDeadZone.left;
|
|
||||||
else if (this._worldSpaceDeadZone.right < targetBounds.right)
|
|
||||||
this._desiredPositionDelta.x = targetBounds.right - this._worldSpaceDeadZone.right;
|
|
||||||
if (this._worldSpaceDeadZone.bottom < targetBounds.bottom)
|
|
||||||
this._desiredPositionDelta.y = targetBounds.bottom - this._worldSpaceDeadZone.bottom;
|
|
||||||
else if (this._worldSpaceDeadZone.top > targetBounds.top)
|
|
||||||
this._desiredPositionDelta.y = targetBounds.top - this._worldSpaceDeadZone.top;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return FollowCamera;
|
|
||||||
}(Component));
|
|
||||||
var CameraStyle;
|
|
||||||
(function (CameraStyle) {
|
|
||||||
CameraStyle[CameraStyle["lockOn"] = 0] = "lockOn";
|
|
||||||
CameraStyle[CameraStyle["cameraWindow"] = 1] = "cameraWindow";
|
|
||||||
})(CameraStyle || (CameraStyle = {}));
|
|
||||||
var Mesh = (function (_super) {
|
var Mesh = (function (_super) {
|
||||||
__extends(Mesh, _super);
|
__extends(Mesh, _super);
|
||||||
function Mesh() {
|
function Mesh() {
|
||||||
@@ -2017,14 +1891,14 @@ var RenderableComponent = (function (_super) {
|
|||||||
RenderableComponent.prototype.onBecameVisible = function () { };
|
RenderableComponent.prototype.onBecameVisible = function () { };
|
||||||
RenderableComponent.prototype.onBecameInvisible = function () { };
|
RenderableComponent.prototype.onBecameInvisible = function () { };
|
||||||
RenderableComponent.prototype.isVisibleFromCamera = function (camera) {
|
RenderableComponent.prototype.isVisibleFromCamera = function (camera) {
|
||||||
this.isVisible = camera.bounds.intersects(this.bounds);
|
this.isVisible = camera.getBounds().intersects(this.getBounds());
|
||||||
return this.isVisible;
|
return this.isVisible;
|
||||||
};
|
};
|
||||||
RenderableComponent.prototype.onEntityTransformChanged = function (comp) {
|
RenderableComponent.prototype.onEntityTransformChanged = function (comp) {
|
||||||
this._areBoundsDirty = true;
|
this._areBoundsDirty = true;
|
||||||
};
|
};
|
||||||
return RenderableComponent;
|
return RenderableComponent;
|
||||||
}(Component));
|
}(PooledComponent));
|
||||||
var ScreenSpaceCamera = (function (_super) {
|
var ScreenSpaceCamera = (function (_super) {
|
||||||
__extends(ScreenSpaceCamera, _super);
|
__extends(ScreenSpaceCamera, _super);
|
||||||
function ScreenSpaceCamera() {
|
function ScreenSpaceCamera() {
|
||||||
@@ -2036,7 +1910,7 @@ var ScreenSpaceCamera = (function (_super) {
|
|||||||
}(Camera));
|
}(Camera));
|
||||||
var Sprite = (function () {
|
var Sprite = (function () {
|
||||||
function Sprite(texture, sourceRect, origin) {
|
function Sprite(texture, sourceRect, origin) {
|
||||||
if (sourceRect === void 0) { sourceRect = new Rectangle(texture.textureWidth, texture.textureHeight); }
|
if (sourceRect === void 0) { sourceRect = new Rectangle(0, 0, texture.textureWidth, texture.textureHeight); }
|
||||||
if (origin === void 0) { origin = sourceRect.getHalfSize(); }
|
if (origin === void 0) { origin = sourceRect.getHalfSize(); }
|
||||||
this.uvs = new Rectangle();
|
this.uvs = new Rectangle();
|
||||||
this.texture2D = texture;
|
this.texture2D = texture;
|
||||||
@@ -2070,13 +1944,17 @@ var SpriteRenderer = (function (_super) {
|
|||||||
SpriteRenderer.prototype.setOrigin = function (origin) {
|
SpriteRenderer.prototype.setOrigin = function (origin) {
|
||||||
if (this._origin != origin) {
|
if (this._origin != origin) {
|
||||||
this._origin = origin;
|
this._origin = origin;
|
||||||
this._areBoundsDirty = true;
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
SpriteRenderer.prototype.setSprite = function (sprite) {
|
SpriteRenderer.prototype.setSprite = function (sprite) {
|
||||||
this.removeChildren();
|
this.removeChildren();
|
||||||
this.addChild(new egret.Bitmap(sprite.texture2D));
|
this._sprite = sprite;
|
||||||
|
if (this._sprite)
|
||||||
|
this._origin = this._sprite.origin;
|
||||||
|
this._bitmap = new egret.Bitmap(sprite.texture2D);
|
||||||
|
this.addChild(this._bitmap);
|
||||||
|
return this;
|
||||||
};
|
};
|
||||||
SpriteRenderer.prototype.setColor = function (color) {
|
SpriteRenderer.prototype.setColor = function (color) {
|
||||||
var colorMatrix = [
|
var colorMatrix = [
|
||||||
@@ -2090,19 +1968,23 @@ var SpriteRenderer = (function (_super) {
|
|||||||
colorMatrix[12] = color % 256 / 255;
|
colorMatrix[12] = color % 256 / 255;
|
||||||
var colorFilter = new egret.ColorMatrixFilter(colorMatrix);
|
var colorFilter = new egret.ColorMatrixFilter(colorMatrix);
|
||||||
this.filters = [colorFilter];
|
this.filters = [colorFilter];
|
||||||
|
return this;
|
||||||
};
|
};
|
||||||
SpriteRenderer.prototype.isVisibleFromCamera = function (camera) {
|
SpriteRenderer.prototype.isVisibleFromCamera = function (camera) {
|
||||||
var topLeft = camera.screenToWorldPoint(new Vector2(0, 0));
|
this.isVisible = new Rectangle(0, 0, 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.visible = this.isVisible;
|
this.visible = this.isVisible;
|
||||||
return this.isVisible;
|
return this.isVisible;
|
||||||
};
|
};
|
||||||
SpriteRenderer.prototype.render = function (camera) {
|
SpriteRenderer.prototype.render = function (camera) {
|
||||||
|
this.x = this.entity.position.x - this.origin.x - camera.position.x + camera.origin.x;
|
||||||
|
this.y = this.entity.position.y - this.origin.y - camera.position.y + camera.origin.y;
|
||||||
};
|
};
|
||||||
SpriteRenderer.prototype.onRemovedFromEntity = function () {
|
SpriteRenderer.prototype.onRemovedFromEntity = function () {
|
||||||
if (this.parent)
|
if (this.parent)
|
||||||
this.parent.removeChild(this);
|
this.parent.removeChild(this);
|
||||||
};
|
};
|
||||||
|
SpriteRenderer.prototype.reset = function () {
|
||||||
|
};
|
||||||
return SpriteRenderer;
|
return SpriteRenderer;
|
||||||
}(RenderableComponent));
|
}(RenderableComponent));
|
||||||
var Mover = (function (_super) {
|
var Mover = (function (_super) {
|
||||||
@@ -3202,17 +3084,6 @@ var PolyLight = (function (_super) {
|
|||||||
_this.computeTriangleIndices();
|
_this.computeTriangleIndices();
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
Object.defineProperty(PolyLight.prototype, "bounds", {
|
|
||||||
get: function () {
|
|
||||||
if (this._areBoundsDirty) {
|
|
||||||
this._bounds.calculateBounds(this.entity.position, this._localOffset, new Vector2(this._radius), Vector2.one, 0, this._radius * 2, this._radius * 2);
|
|
||||||
this._areBoundsDirty = false;
|
|
||||||
}
|
|
||||||
return this._bounds;
|
|
||||||
},
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
Object.defineProperty(PolyLight.prototype, "radius", {
|
Object.defineProperty(PolyLight.prototype, "radius", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._radius;
|
return this._radius;
|
||||||
@@ -3240,6 +3111,8 @@ var PolyLight = (function (_super) {
|
|||||||
};
|
};
|
||||||
PolyLight.prototype.render = function (camera) {
|
PolyLight.prototype.render = function (camera) {
|
||||||
};
|
};
|
||||||
|
PolyLight.prototype.reset = function () {
|
||||||
|
};
|
||||||
return PolyLight;
|
return PolyLight;
|
||||||
}(RenderableComponent));
|
}(RenderableComponent));
|
||||||
var SceneTransition = (function () {
|
var SceneTransition = (function () {
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -25,20 +25,6 @@ abstract class Component extends egret.DisplayObjectContainer {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get stage(){
|
|
||||||
if (!this.entity)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return this.entity.stage;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get scene(){
|
|
||||||
if (!this.entity)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return this.entity.scene;
|
|
||||||
}
|
|
||||||
|
|
||||||
public initialize(){
|
public initialize(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,48 +2,29 @@
|
|||||||
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 = new Matrix2D();
|
|
||||||
private _inverseTransformMatrix = new Matrix2D();
|
|
||||||
|
|
||||||
private _minimumZoom = 0.3;
|
private _minimumZoom = 0.3;
|
||||||
private _maximumZoom = 3;
|
private _maximumZoom = 3;
|
||||||
private _areMatrixesDirty = true;
|
|
||||||
private _inset: CameraInset = new CameraInset();
|
|
||||||
private _bounds: Rectangle = new Rectangle();
|
|
||||||
private _areBoundsDirty = true;
|
|
||||||
|
|
||||||
public get bounds(){
|
/**
|
||||||
if (this._areMatrixesDirty)
|
* 如果相机模式为cameraWindow 则会进行缓动移动
|
||||||
this.updateMatrixes();
|
* 该值为移动速度
|
||||||
|
*/
|
||||||
if (this._areBoundsDirty){
|
public followLerp = 0.1;
|
||||||
let stage = this.stage;
|
public deadzone: Rectangle = new Rectangle();
|
||||||
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));
|
public focusOffset: Vector2 = new Vector2();
|
||||||
|
/** 是否地图锁定 如果锁定则需要设置mapSize属性 */
|
||||||
if (this.entity.rotation != 0){
|
public mapLockEnabled: boolean = false;
|
||||||
let topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
|
/** 设置地图大小 默认从0 0左上角开始 只需要输入地图宽高 */
|
||||||
let bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom));
|
public mapSize: Vector2 = new Vector2();
|
||||||
|
/** 跟随的实体 设置后镜头将锁定目标为中心 */
|
||||||
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
public targetEntity: Entity;
|
||||||
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
private _worldSpaceDeadZone: Rectangle = new Rectangle();
|
||||||
let minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
private _desiredPositionDelta: Vector2 = new Vector2();
|
||||||
let maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
private _targetCollider: Collider;
|
||||||
|
/** 相机模式 */
|
||||||
this._bounds.location = new Vector2(minX, minY);
|
public cameraStyle: CameraStyle = CameraStyle.lockOn;
|
||||||
this._bounds.width = maxX - minX;
|
|
||||||
this._bounds.height = maxY - minY;
|
|
||||||
}else{
|
|
||||||
this._bounds.location = topLeft;
|
|
||||||
this._bounds.width = bottomRight.x - topLeft.x;
|
|
||||||
this._bounds.height = bottomRight.y - topLeft.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._areBoundsDirty = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this._bounds;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get zoom(){
|
public get zoom(){
|
||||||
if (this._zoom == 0)
|
if (this._zoom == 0)
|
||||||
@@ -82,7 +63,6 @@ class Camera extends Component {
|
|||||||
public set origin(value: Vector2){
|
public set origin(value: Vector2){
|
||||||
if (this._origin != value){
|
if (this._origin != value){
|
||||||
this._origin = value;
|
this._origin = value;
|
||||||
this._areMatrixesDirty = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,21 +74,11 @@ class Camera extends Component {
|
|||||||
this.entity.position = value;
|
this.entity.position = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get transformMatrix(){
|
|
||||||
if (this._areBoundsDirty)
|
|
||||||
this.updateMatrixes();
|
|
||||||
return this._transformMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get inverseTransformMatrix(){
|
|
||||||
if (this._areBoundsDirty)
|
|
||||||
this.updateMatrixes();
|
|
||||||
return this._inverseTransformMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
this.width = SceneManager.stage.stageWidth;
|
||||||
|
this.height = SceneManager.stage.stageHeight;
|
||||||
this.setZoom(0);
|
this.setZoom(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +105,7 @@ class Camera extends Component {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public setZoom(zoom: number){
|
public setZoom(zoom: number): Camera{
|
||||||
let newZoom = MathHelper.clamp(zoom, -1, 1);
|
let newZoom = MathHelper.clamp(zoom, -1, 1);
|
||||||
if (newZoom == 0){
|
if (newZoom == 0){
|
||||||
this._zoom = 1;
|
this._zoom = 1;
|
||||||
@@ -145,8 +115,13 @@ class Camera extends Component {
|
|||||||
this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom);
|
this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._areMatrixesDirty = true;
|
SceneManager.scene.scaleX = this._zoom;
|
||||||
|
SceneManager.scene.scaleY = this._zoom;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public setRotation(rotation: number): Camera {
|
||||||
|
SceneManager.scene.rotation = rotation;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,57 +131,91 @@ class Camera extends Component {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public forceMatrixUpdate(){
|
public follow(targetEntity: Entity, cameraStyle: CameraStyle = CameraStyle.cameraWindow){
|
||||||
this._areMatrixesDirty = true;
|
this.targetEntity = targetEntity;
|
||||||
}
|
this.cameraStyle = cameraStyle;
|
||||||
|
let cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||||
|
|
||||||
protected updateMatrixes(){
|
switch (this.cameraStyle){
|
||||||
if (!this._areMatrixesDirty)
|
case CameraStyle.cameraWindow:
|
||||||
return;
|
let w = cameraBounds.width / 6;
|
||||||
|
let h = cameraBounds.height / 3;
|
||||||
let tempMat: Matrix2D;
|
this.deadzone = new Rectangle((cameraBounds.width - w) / 2, (cameraBounds.height - h) / 2, w, h);
|
||||||
this._transformMatrix = Matrix2D.createTranslation(-this.entity.position.x, -this.entity.position.y);
|
break;
|
||||||
if (this._zoom != 1){
|
case CameraStyle.lockOn:
|
||||||
tempMat = Matrix2D.createScale(this._zoom, this._zoom);
|
this.deadzone = new Rectangle(cameraBounds.width / 2, cameraBounds.height / 2, 10, 10);
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (this.entity.rotation != 0){
|
public update(){
|
||||||
tempMat = Matrix2D.createRotation(this.entity.rotation);
|
let cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
let halfScreen = Vector2.multiply(new Vector2(cameraBounds.width, cameraBounds.height), new Vector2(0.5));
|
||||||
|
this._worldSpaceDeadZone.x = this.position.x - halfScreen.x + this.deadzone.x + this.focusOffset.x;
|
||||||
|
this._worldSpaceDeadZone.y = this.position.y - halfScreen.y + this.deadzone.y + this.focusOffset.y;
|
||||||
|
this._worldSpaceDeadZone.width = this.deadzone.width;
|
||||||
|
this._worldSpaceDeadZone.height = this.deadzone.height;
|
||||||
|
|
||||||
|
if (this.targetEntity)
|
||||||
|
this.updateFollow();
|
||||||
|
|
||||||
|
this.position = Vector2.lerp(this.position, Vector2.add(this.position, this._desiredPositionDelta), this.followLerp);
|
||||||
|
this.entity.roundPosition();
|
||||||
|
|
||||||
|
if (this.mapLockEnabled){
|
||||||
|
this.position = this.clampToMapSize(this.position);
|
||||||
|
this.entity.roundPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
|
|
||||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
|
||||||
|
|
||||||
this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix);
|
|
||||||
|
|
||||||
this._areBoundsDirty = true;
|
|
||||||
this._areMatrixesDirty = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public screenToWorldPoint(screenPosition: Vector2){
|
private clampToMapSize(position: Vector2){
|
||||||
this.updateMatrixes();
|
let cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||||
return Vector2Ext.transformR(screenPosition, this._inverseTransformMatrix);
|
let halfScreen = Vector2.multiply(new Vector2(cameraBounds.width, cameraBounds.height), new Vector2(0.5));
|
||||||
|
let cameraMax = new Vector2(this.mapSize.x - halfScreen.x, this.mapSize.y - halfScreen.y);
|
||||||
|
|
||||||
|
return Vector2.clamp(position, halfScreen, cameraMax);
|
||||||
}
|
}
|
||||||
|
|
||||||
public worldToScreenPoint(worldPosition: Vector2){
|
private updateFollow(){
|
||||||
this.updateMatrixes();
|
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
|
||||||
return Vector2Ext.transformR(worldPosition, this._transformMatrix);
|
|
||||||
}
|
|
||||||
|
|
||||||
public onEntityTransformChanged(comp: ComponentTransform){
|
if (this.cameraStyle == CameraStyle.lockOn){
|
||||||
this._areMatrixesDirty = true;
|
let targetX = this.targetEntity.position.x;
|
||||||
}
|
let targetY = this.targetEntity.position.y;
|
||||||
|
|
||||||
public destory() {
|
if (this._worldSpaceDeadZone.x > targetX)
|
||||||
|
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
||||||
|
else if(this._worldSpaceDeadZone.x < targetX)
|
||||||
|
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
||||||
|
|
||||||
|
if (this._worldSpaceDeadZone.y < targetY)
|
||||||
|
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
||||||
|
else if(this._worldSpaceDeadZone.y > targetY)
|
||||||
|
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
||||||
|
} else {
|
||||||
|
if (!this._targetCollider){
|
||||||
|
this._targetCollider = this.targetEntity.getComponent<Collider>(Collider);
|
||||||
|
if (!this._targetCollider)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let targetBounds = this.targetEntity.getComponent<Collider>(Collider).bounds;
|
||||||
|
if (!this._worldSpaceDeadZone.containsRect(targetBounds)){
|
||||||
|
if (this._worldSpaceDeadZone.left > targetBounds.left)
|
||||||
|
this._desiredPositionDelta.x = targetBounds.left - this._worldSpaceDeadZone.left;
|
||||||
|
else if(this._worldSpaceDeadZone.right < targetBounds.right)
|
||||||
|
this._desiredPositionDelta.x = targetBounds.right - this._worldSpaceDeadZone.right;
|
||||||
|
|
||||||
|
if (this._worldSpaceDeadZone.bottom < targetBounds.bottom)
|
||||||
|
this._desiredPositionDelta.y = targetBounds.bottom - this._worldSpaceDeadZone.bottom;
|
||||||
|
else if(this._worldSpaceDeadZone.top > targetBounds.top)
|
||||||
|
this._desiredPositionDelta.y = targetBounds.top - this._worldSpaceDeadZone.top;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CameraInset {
|
enum CameraStyle {
|
||||||
public left = 0;
|
lockOn,
|
||||||
public right = 0;
|
cameraWindow,
|
||||||
public top = 0;
|
|
||||||
public bottom = 0;
|
|
||||||
}
|
}
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
class FollowCamera extends Component {
|
|
||||||
public camera: Camera;
|
|
||||||
public followLerp = 0.1;
|
|
||||||
public deadzone: Rectangle = new Rectangle();
|
|
||||||
public focusOffset: Vector2 = new Vector2();
|
|
||||||
public mapLockEnabled: boolean;
|
|
||||||
public mapSize: Vector2 = new Vector2();
|
|
||||||
|
|
||||||
private _targetEntity: Entity;
|
|
||||||
private _cameraStyle: CameraStyle;
|
|
||||||
private _worldSpaceDeadZone: Rectangle = new Rectangle();
|
|
||||||
private _desiredPositionDelta: Vector2 = new Vector2();
|
|
||||||
private _targetCollider: Collider;
|
|
||||||
|
|
||||||
constructor(targetEntity: Entity, cameraStyle: CameraStyle = CameraStyle.lockOn){
|
|
||||||
super();
|
|
||||||
|
|
||||||
this._targetEntity = targetEntity;
|
|
||||||
this._cameraStyle = cameraStyle;
|
|
||||||
this.camera = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public onAddedToEntity(){
|
|
||||||
if (!this.camera)
|
|
||||||
this.camera = this.entity.scene.camera;
|
|
||||||
|
|
||||||
this.follow(this._targetEntity, this._cameraStyle);
|
|
||||||
}
|
|
||||||
|
|
||||||
public follow(targetEntity: Entity, cameraStyle: CameraStyle = CameraStyle.cameraWindow){
|
|
||||||
this._targetEntity = targetEntity;
|
|
||||||
this._cameraStyle = cameraStyle;
|
|
||||||
let cameraBounds = this.camera.bounds;
|
|
||||||
|
|
||||||
switch (this._cameraStyle){
|
|
||||||
case CameraStyle.cameraWindow:
|
|
||||||
let w = cameraBounds.width / 6;
|
|
||||||
let h = cameraBounds.height / 3;
|
|
||||||
this.deadzone = new Rectangle((cameraBounds.width - w) / 2, (cameraBounds.height - h) / 2, w, h);
|
|
||||||
break;
|
|
||||||
case CameraStyle.lockOn:
|
|
||||||
this.deadzone = new Rectangle(cameraBounds.width / 2, cameraBounds.height / 2, 10, 10);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public update(){
|
|
||||||
let halfScreen = Vector2.multiply(this.camera.bounds.size, new Vector2(0.5));
|
|
||||||
this._worldSpaceDeadZone.x = this.camera.position.x - halfScreen.x + this.deadzone.x + this.focusOffset.x;
|
|
||||||
this._worldSpaceDeadZone.y = this.camera.position.y - halfScreen.y + this.deadzone.y + this.focusOffset.y;
|
|
||||||
this._worldSpaceDeadZone.width = this.deadzone.width;
|
|
||||||
this._worldSpaceDeadZone.height = this.deadzone.height;
|
|
||||||
|
|
||||||
if (this._targetEntity)
|
|
||||||
this.updateFollow();
|
|
||||||
|
|
||||||
this.camera.position = Vector2.lerp(this.camera.position, Vector2.add(this.camera.position, this._desiredPositionDelta), this.followLerp);
|
|
||||||
this.camera.entity.roundPosition();
|
|
||||||
|
|
||||||
if (this.mapLockEnabled){
|
|
||||||
this.camera.position = this.clampToMapSize(this.camera.position);
|
|
||||||
this.camera.entity.roundPosition();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private clampToMapSize(position: Vector2){
|
|
||||||
let halfScreen = Vector2.multiply(new Vector2(this.camera.bounds.width, this.camera.bounds.height), new Vector2(0.5));
|
|
||||||
let cameraMax = new Vector2(this.mapSize.x - halfScreen.x, this.mapSize.y - halfScreen.y);
|
|
||||||
|
|
||||||
return Vector2.clamp(position, halfScreen, cameraMax);
|
|
||||||
}
|
|
||||||
|
|
||||||
private updateFollow(){
|
|
||||||
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
|
|
||||||
|
|
||||||
if (this._cameraStyle == CameraStyle.lockOn){
|
|
||||||
let targetX = this._targetEntity.position.x;
|
|
||||||
let targetY = this._targetEntity.position.y;
|
|
||||||
|
|
||||||
if (this._worldSpaceDeadZone.x > targetX)
|
|
||||||
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
|
||||||
else if(this._worldSpaceDeadZone.x < targetX)
|
|
||||||
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
|
||||||
|
|
||||||
if (this._worldSpaceDeadZone.y < targetY)
|
|
||||||
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
|
||||||
else if(this._worldSpaceDeadZone.y > targetY)
|
|
||||||
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
|
||||||
} else {
|
|
||||||
if (!this._targetCollider){
|
|
||||||
this._targetCollider = this._targetEntity.getComponent<Collider>(Collider);
|
|
||||||
if (!this._targetCollider)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let targetBounds = this._targetEntity.getComponent<Collider>(Collider).bounds;
|
|
||||||
if (!this._worldSpaceDeadZone.containsRect(targetBounds)){
|
|
||||||
if (this._worldSpaceDeadZone.left > targetBounds.left)
|
|
||||||
this._desiredPositionDelta.x = targetBounds.left - this._worldSpaceDeadZone.left;
|
|
||||||
else if(this._worldSpaceDeadZone.right < targetBounds.right)
|
|
||||||
this._desiredPositionDelta.x = targetBounds.right - this._worldSpaceDeadZone.right;
|
|
||||||
|
|
||||||
if (this._worldSpaceDeadZone.bottom < targetBounds.bottom)
|
|
||||||
this._desiredPositionDelta.y = targetBounds.bottom - this._worldSpaceDeadZone.bottom;
|
|
||||||
else if(this._worldSpaceDeadZone.top > targetBounds.top)
|
|
||||||
this._desiredPositionDelta.y = targetBounds.top - this._worldSpaceDeadZone.top;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum CameraStyle {
|
|
||||||
lockOn,
|
|
||||||
cameraWindow,
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* 所有可渲染组件的基类
|
* 所有可渲染组件的基类
|
||||||
*/
|
*/
|
||||||
abstract class RenderableComponent extends Component implements IRenderable {
|
abstract class RenderableComponent extends PooledComponent implements IRenderable {
|
||||||
private _isVisible: boolean;
|
private _isVisible: boolean;
|
||||||
protected _areBoundsDirty = true;
|
protected _areBoundsDirty = true;
|
||||||
protected _bounds: Rectangle = new Rectangle();
|
protected _bounds: Rectangle = new Rectangle();
|
||||||
@@ -49,7 +49,7 @@ abstract class RenderableComponent extends Component implements IRenderable {
|
|||||||
public abstract render(camera: Camera);
|
public abstract render(camera: Camera);
|
||||||
|
|
||||||
public isVisibleFromCamera(camera: Camera): boolean{
|
public isVisibleFromCamera(camera: Camera): boolean{
|
||||||
this.isVisible = camera.bounds.intersects(this.bounds);
|
this.isVisible = camera.getBounds().intersects(this.getBounds());
|
||||||
return this.isVisible;
|
return this.isVisible;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ class Sprite {
|
|||||||
public readonly center: Vector2;
|
public readonly center: Vector2;
|
||||||
public origin: Vector2;
|
public origin: Vector2;
|
||||||
public readonly uvs: Rectangle = new Rectangle();
|
public readonly uvs: Rectangle = new Rectangle();
|
||||||
|
|
||||||
constructor(texture: egret.Texture,
|
constructor(texture: egret.Texture,
|
||||||
sourceRect: Rectangle = new Rectangle(texture.textureWidth, texture.textureHeight),
|
sourceRect: Rectangle = new Rectangle(0, 0, texture.textureWidth, texture.textureHeight),
|
||||||
origin: Vector2 = sourceRect.getHalfSize()){
|
origin: Vector2 = sourceRect.getHalfSize()) {
|
||||||
this.texture2D = texture;
|
this.texture2D = texture;
|
||||||
this.sourceRect = sourceRect;
|
this.sourceRect = sourceRect;
|
||||||
this.center = new Vector2(sourceRect.width * 0.5, sourceRect.height * 0.5);
|
this.center = new Vector2(sourceRect.width * 0.5, sourceRect.height * 0.5);
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
class SpriteRenderer extends RenderableComponent {
|
class SpriteRenderer extends RenderableComponent {
|
||||||
private _origin: Vector2;
|
private _origin: Vector2;
|
||||||
|
private _bitmap: egret.Bitmap;
|
||||||
|
private _sprite: Sprite;
|
||||||
|
|
||||||
public get origin(){
|
public get origin(){
|
||||||
return this._origin;
|
return this._origin;
|
||||||
@@ -10,17 +12,21 @@ class SpriteRenderer extends RenderableComponent {
|
|||||||
public setOrigin(origin: Vector2){
|
public setOrigin(origin: Vector2){
|
||||||
if (this._origin != origin){
|
if (this._origin != origin){
|
||||||
this._origin = origin;
|
this._origin = origin;
|
||||||
this._areBoundsDirty = true;
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public setSprite(sprite: Sprite){
|
public setSprite(sprite: Sprite): SpriteRenderer{
|
||||||
this.removeChildren();
|
this.removeChildren();
|
||||||
this.addChild(new egret.Bitmap(sprite.texture2D));
|
this._sprite = sprite;
|
||||||
|
if (this._sprite) this._origin = this._sprite.origin;
|
||||||
|
this._bitmap = new egret.Bitmap(sprite.texture2D);
|
||||||
|
this.addChild(this._bitmap);
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public setColor(color: number){
|
public setColor(color: number): SpriteRenderer{
|
||||||
let colorMatrix = [
|
let colorMatrix = [
|
||||||
1, 0, 0, 0, 0,
|
1, 0, 0, 0, 0,
|
||||||
0, 1, 0, 0, 0,
|
0, 1, 0, 0, 0,
|
||||||
@@ -32,21 +38,27 @@ class SpriteRenderer extends RenderableComponent {
|
|||||||
colorMatrix[12] = color % 256 / 255;
|
colorMatrix[12] = color % 256 / 255;
|
||||||
let colorFilter = new egret.ColorMatrixFilter(colorMatrix);
|
let colorFilter = new egret.ColorMatrixFilter(colorMatrix);
|
||||||
this.filters = [colorFilter];
|
this.filters = [colorFilter];
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public isVisibleFromCamera(camera: Camera): boolean{
|
public isVisibleFromCamera(camera: Camera): boolean{
|
||||||
let topLeft = camera.screenToWorldPoint(new Vector2(0, 0));
|
this.isVisible = new Rectangle(0, 0, 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.visible = this.isVisible;
|
this.visible = this.isVisible;
|
||||||
return this.isVisible;
|
return this.isVisible;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 渲染处理 在每个模块中处理各自的渲染逻辑 */
|
||||||
public render(camera: Camera){
|
public render(camera: Camera){
|
||||||
|
this.x = this.entity.position.x - this.origin.x - camera.position.x + camera.origin.x;
|
||||||
|
this.y = this.entity.position.y - this.origin.y - camera.position.y + camera.origin.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public onRemovedFromEntity(){
|
public onRemovedFromEntity(){
|
||||||
if (this.parent)
|
if (this.parent)
|
||||||
this.parent.removeChild(this);
|
this.parent.removeChild(this);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public reset(){
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
class Entity extends egret.DisplayObjectContainer {
|
class Entity extends egret.DisplayObjectContainer {
|
||||||
private static _idGenerator: number;
|
private static _idGenerator: number;
|
||||||
|
|
||||||
|
private _position: Vector2 = Vector2.zero;
|
||||||
public name: string;
|
public name: string;
|
||||||
public readonly id: number;
|
public readonly id: number;
|
||||||
/** 当前实体所属的场景 */
|
/** 当前实体所属的场景 */
|
||||||
@@ -19,12 +20,11 @@ class Entity extends egret.DisplayObjectContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public get position(){
|
public get position(){
|
||||||
return new Vector2(this.x, this.y);
|
return this._position;
|
||||||
}
|
}
|
||||||
|
|
||||||
public set position(value: Vector2){
|
public set position(value: Vector2){
|
||||||
this.x = value.x;
|
this._position = value;
|
||||||
this.y = value.y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public get scale(){
|
public get scale(){
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ class Scene extends egret.DisplayObjectContainer {
|
|||||||
this.renderableComponents = new RenderableComponentList();
|
this.renderableComponents = new RenderableComponentList();
|
||||||
this.entities = new EntityList(this);
|
this.entities = new EntityList(this);
|
||||||
this.content = new ContentManager();
|
this.content = new ContentManager();
|
||||||
|
this.width = SceneManager.stage.stageWidth;
|
||||||
|
this.height = SceneManager.stage.stageHeight;
|
||||||
|
|
||||||
this.addEventListener(egret.Event.ACTIVATE, this.onActive, this);
|
this.addEventListener(egret.Event.ACTIVATE, this.onActive, this);
|
||||||
this.addEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
|
this.addEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
|
||||||
@@ -135,7 +137,6 @@ class Scene extends egret.DisplayObjectContainer {
|
|||||||
|
|
||||||
Physics.clear();
|
Physics.clear();
|
||||||
|
|
||||||
this.camera.destory();
|
|
||||||
this.camera = null;
|
this.camera = null;
|
||||||
this.content.dispose();
|
this.content.dispose();
|
||||||
|
|
||||||
@@ -191,9 +192,6 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,16 +4,6 @@ class PolyLight extends RenderableComponent {
|
|||||||
private _lightEffect;
|
private _lightEffect;
|
||||||
private _indices: number[] = [];
|
private _indices: number[] = [];
|
||||||
|
|
||||||
public get bounds(){
|
|
||||||
if (this._areBoundsDirty){
|
|
||||||
this._bounds.calculateBounds(this.entity.position, this._localOffset, new Vector2(this._radius),
|
|
||||||
Vector2.one, 0, this._radius * 2, this._radius * 2);
|
|
||||||
this._areBoundsDirty = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this._bounds;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get radius(){
|
public get radius(){
|
||||||
return this._radius;
|
return this._radius;
|
||||||
}
|
}
|
||||||
@@ -49,4 +39,8 @@ class PolyLight extends RenderableComponent {
|
|||||||
|
|
||||||
public render(camera: Camera) {
|
public render(camera: Camera) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public reset(){
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user