2020-07-22 20:07:14 +08:00
|
|
|
|
module es {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
export enum CameraStyle {
|
|
|
|
|
|
lockOn,
|
|
|
|
|
|
cameraWindow,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
export class CameraInset {
|
|
|
|
|
|
public left: number;
|
|
|
|
|
|
public right: number;
|
|
|
|
|
|
public top: number;
|
|
|
|
|
|
public bottom: number;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
export class Camera extends Component {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 对entity.transform.position的快速访问
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get position() {
|
|
|
|
|
|
return this.entity.transform.position;
|
|
|
|
|
|
}
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 对entity.transform.position的快速访问
|
|
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
|
|
|
|
|
public set position(value: Vector2) {
|
|
|
|
|
|
this.entity.transform.position = value;
|
|
|
|
|
|
}
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-22 23:30:31 +08:00
|
|
|
|
* 对entity.transform.rotation的快速访问
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-07-22 23:30:31 +08:00
|
|
|
|
public get rotation(): number {
|
|
|
|
|
|
return this.entity.transform.rotation;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 对entity.transform.rotation的快速访问
|
|
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
|
|
|
|
|
public set rotation(value: number) {
|
|
|
|
|
|
this.entity.transform.rotation = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 缩放值应该在-1和1之间、然后将该值从minimumZoom转换为maximumZoom。
|
|
|
|
|
|
* 允许你设置适当的最小/最大值,然后使用更直观的-1到1的映射来更改缩放
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get zoom() {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
if (this._zoom == 0)
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
|
|
if (this._zoom < 1)
|
|
|
|
|
|
return MathHelper.map(this._zoom, this._minimumZoom, 1, -1, 0);
|
|
|
|
|
|
|
|
|
|
|
|
return MathHelper.map(this._zoom, 1, this._maximumZoom, 0, 1);
|
|
|
|
|
|
}
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 缩放值应该在-1和1之间、然后将该值从minimumZoom转换为maximumZoom。
|
|
|
|
|
|
* 允许你设置适当的最小/最大值,然后使用更直观的-1到1的映射来更改缩放
|
|
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
|
|
|
|
|
public set zoom(value: number) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this.setZoom(value);
|
|
|
|
|
|
}
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 相机变焦可以达到的最小非缩放值(0-number.max)。默认为0.3
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get minimumZoom() {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this._minimumZoom;
|
|
|
|
|
|
}
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 相机变焦可以达到的最小非缩放值(0-number.max)。默认为0.3
|
|
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
|
|
|
|
|
public set minimumZoom(value: number) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this.setMinimumZoom(value);
|
|
|
|
|
|
}
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 相机变焦可以达到的最大非缩放值(0-number.max)。默认为3
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get maximumZoom() {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this._maximumZoom;
|
|
|
|
|
|
}
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 相机变焦可以达到的最大非缩放值(0-number.max)。默认为3
|
|
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
|
|
|
|
|
public set maximumZoom(value: number) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this.setMaximumZoom(value);
|
|
|
|
|
|
}
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
2020-07-23 13:25:10 +08:00
|
|
|
|
* 相机的世界-空间边界
|
2020-07-22 23:30:31 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public get bounds(){
|
2020-07-23 13:25:10 +08:00
|
|
|
|
if (this._areMatrixedDirty)
|
|
|
|
|
|
this.updateMatrixes();
|
|
|
|
|
|
|
|
|
|
|
|
if (this._areBoundsDirty){
|
|
|
|
|
|
// 旋转或非旋转的边界都需要左上角和右下角
|
|
|
|
|
|
let topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top));
|
|
|
|
|
|
let bottomRight = this.screenToWorldPoint(new Vector2(SceneManager.stage.stageWidth - this._inset.right,
|
|
|
|
|
|
SceneManager.stage.stageHeight - this._inset.bottom));
|
|
|
|
|
|
|
|
|
|
|
|
if (this.entity.transform.rotation != 0){
|
|
|
|
|
|
// 特别注意旋转的边界。我们需要找到绝对的最小/最大值并从中创建边界
|
|
|
|
|
|
let topRight = this.screenToWorldPoint(new Vector2(SceneManager.stage.stageWidth - this._inset.right,
|
|
|
|
|
|
this._inset.top));
|
|
|
|
|
|
let bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left,
|
|
|
|
|
|
SceneManager.stage.stageHeight - this._inset.bottom));
|
|
|
|
|
|
|
|
|
|
|
|
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
|
|
|
|
|
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
|
|
|
|
|
let minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
|
|
|
|
|
let 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 = maxX - 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 transformMatrix(): Matrix2D {
|
|
|
|
|
|
if (this._areMatrixedDirty)
|
|
|
|
|
|
this.updateMatrixes();
|
|
|
|
|
|
return this._transformMatrix;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 用于从屏幕坐标到世界的转换
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get inverseTransformMatrix(): Matrix2D {
|
|
|
|
|
|
if (this._areMatrixedDirty)
|
|
|
|
|
|
this.updateMatrixes();
|
|
|
|
|
|
return this._inverseTransformMatrix;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public get origin() {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this._origin;
|
2020-06-09 11:09:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
public set origin(value: Vector2) {
|
|
|
|
|
|
if (this._origin != value) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._origin = value;
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this._areMatrixedDirty = true;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-19 09:16:49 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
public _zoom;
|
|
|
|
|
|
public _minimumZoom = 0.3;
|
|
|
|
|
|
public _maximumZoom = 3;
|
|
|
|
|
|
public _bounds: Rectangle;
|
|
|
|
|
|
public _inset: CameraInset;
|
|
|
|
|
|
public _transformMatrix: Matrix2D = new Matrix2D().identity();
|
|
|
|
|
|
public _inverseTransformMatrix: Matrix2D = new Matrix2D().identity();
|
|
|
|
|
|
public _origin: Vector2 = Vector2.zero;
|
|
|
|
|
|
|
|
|
|
|
|
public _areMatrixedDirty: boolean = true;
|
|
|
|
|
|
public _areBoundsDirty: boolean = true;
|
|
|
|
|
|
public _isProjectionMatrixDirty = true;
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 如果相机模式为cameraWindow 则会进行缓动移动
|
|
|
|
|
|
* 该值为移动速度
|
|
|
|
|
|
*/
|
|
|
|
|
|
public followLerp = 0.1;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 在cameraWindow模式下,宽度/高度被用做边界框,允许在不移动相机的情况下移动
|
|
|
|
|
|
* 在lockOn模式下,只使用deadZone的x/y值 你可以通过直接setCenteredDeadzone重写它来自定义deadZone
|
|
|
|
|
|
*/
|
|
|
|
|
|
public deadzone: Rectangle = new Rectangle();
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 相机聚焦于屏幕中心的偏移
|
|
|
|
|
|
*/
|
|
|
|
|
|
public focusOffset: Vector2 = new Vector2();
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 如果为true 相机位置则不会超出地图矩形(0, 0, mapwidth, mapheight)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public mapLockEnabled: boolean = false;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 當前地圖映射的寬度和高度
|
|
|
|
|
|
*/
|
|
|
|
|
|
public mapSize: Vector2 = new Vector2();
|
2020-06-19 09:16:49 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
public _targetEntity: Entity;
|
|
|
|
|
|
public _targetCollider: Collider;
|
|
|
|
|
|
public _desiredPositionDelta: Vector2 = new Vector2();
|
|
|
|
|
|
public _cameraStyle: CameraStyle;
|
|
|
|
|
|
public _worldSpaceDeadZone: Rectangle = new Rectangle();
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
constructor(targetEntity: Entity = null, cameraStyle: CameraStyle = CameraStyle.lockOn) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
super();
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this._targetEntity = targetEntity;
|
|
|
|
|
|
this._cameraStyle = cameraStyle;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this.setZoom(0);
|
|
|
|
|
|
}
|
2020-06-18 23:22:54 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 当场景渲染目标的大小发生变化时,我们会更新相机的原点并调整它的位置以保持它原来的位置
|
|
|
|
|
|
* @param newWidth
|
|
|
|
|
|
* @param newHeight
|
|
|
|
|
|
*/
|
|
|
|
|
|
public onSceneSizeChanged(newWidth: number, newHeight: number) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
let oldOrigin = this._origin;
|
|
|
|
|
|
this.origin = new Vector2(newWidth / 2, newHeight / 2);
|
2020-06-18 23:22:54 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.entity.transform.position = Vector2.add(this.entity.transform.position, Vector2.subtract(this._origin, oldOrigin));
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
protected updateMatrixes(){
|
|
|
|
|
|
if (!this._areMatrixedDirty)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
let tempMat: Matrix2D;
|
|
|
|
|
|
this._transformMatrix = Matrix2D.create().translate(-this.entity.transform.position.x, -this.entity.transform.position.y);
|
|
|
|
|
|
|
|
|
|
|
|
if (this._zoom != 1){
|
|
|
|
|
|
tempMat = Matrix2D.create().scale(this._zoom, this._zoom);
|
|
|
|
|
|
this._transformMatrix = this._transformMatrix.multiply(tempMat);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.entity.transform.rotation != 0){
|
|
|
|
|
|
tempMat = Matrix2D.create().rotate(this.entity.transform.rotation);
|
|
|
|
|
|
this._transformMatrix = this._transformMatrix.multiply(tempMat);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tempMat = Matrix2D.create().translate(this._origin.x, this._origin.y);
|
|
|
|
|
|
this._transformMatrix =this._transformMatrix.multiply(tempMat);
|
|
|
|
|
|
|
|
|
|
|
|
this._inverseTransformMatrix = this._transformMatrix.invert();
|
|
|
|
|
|
|
|
|
|
|
|
// 无论何时矩阵改变边界都是无效的
|
|
|
|
|
|
this._areBoundsDirty = true;
|
|
|
|
|
|
this._areMatrixedDirty = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置用于从视口边缘插入摄像机边界的量
|
|
|
|
|
|
* @param left
|
|
|
|
|
|
* @param right
|
|
|
|
|
|
* @param top
|
|
|
|
|
|
* @param bottom
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setInset(left: number, right: number, top: number, bottom: number): Camera {
|
|
|
|
|
|
this._inset = new CameraInset();
|
|
|
|
|
|
this._inset.left = left;
|
|
|
|
|
|
this._inset.right = right;
|
|
|
|
|
|
this._inset.top = top;
|
|
|
|
|
|
this._inset.bottom = bottom;
|
|
|
|
|
|
this._areBoundsDirty = true;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 对entity.transform.setPosition快速访问
|
|
|
|
|
|
* @param position
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setPosition(position: Vector2) {
|
|
|
|
|
|
this.entity.transform.setPosition(position.x, position.y);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 对entity.transform.setRotation的快速访问
|
|
|
|
|
|
* @param rotation
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setRotation(rotation: number): Camera {
|
|
|
|
|
|
this.entity.transform.setRotation(rotation);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this;
|
2020-06-09 11:09:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 设置缩放值,缩放值应该在-1到1之间。然后将该值从minimumZoom转换为maximumZoom
|
|
|
|
|
|
* 允许您设置适当的最小/最大值。使用更直观的-1到1的映射来更改缩放
|
|
|
|
|
|
* @param zoom
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setZoom(zoom: number): Camera {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
let newZoom = MathHelper.clamp(zoom, -1, 1);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
if (newZoom == 0) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._zoom = 1;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
} else if (newZoom < 0) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._zoom = MathHelper.map(newZoom, -1, 0, this._minimumZoom, 1);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom);
|
|
|
|
|
|
}
|
2020-07-23 13:25:10 +08:00
|
|
|
|
this._areMatrixedDirty = true;
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 相机变焦可以达到的最小非缩放值(0-number.max) 默认为0.3
|
|
|
|
|
|
* @param minZoom
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setMinimumZoom(minZoom: number): Camera {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
if (minZoom <= 0) {
|
|
|
|
|
|
console.error("minimumZoom must be greater than zero");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
if (this._zoom < minZoom)
|
|
|
|
|
|
this._zoom = this.minimumZoom;
|
|
|
|
|
|
|
|
|
|
|
|
this._minimumZoom = minZoom;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 相机变焦可以达到的最大非缩放值(0-number.max) 默认为3
|
|
|
|
|
|
* @param maxZoom
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setMaximumZoom(maxZoom: number): Camera {
|
|
|
|
|
|
if (maxZoom <= 0) {
|
|
|
|
|
|
console.error("maximumZoom must be greater than zero");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this._zoom > maxZoom)
|
|
|
|
|
|
this._zoom = maxZoom;
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this._maximumZoom = maxZoom;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this;
|
2020-07-01 14:19:40 +08:00
|
|
|
|
}
|
2020-06-19 00:38:37 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
public onEntityTransformChanged(comp: transform.Component) {
|
|
|
|
|
|
this._areMatrixedDirty = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
public zoomIn(deltaZoom: number) {
|
|
|
|
|
|
this.zoom += deltaZoom;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public zoomOut(deltaZoom: number) {
|
|
|
|
|
|
this.zoom -= deltaZoom;
|
|
|
|
|
|
}
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
2020-07-23 13:25:10 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 将一个点从世界坐标转换到屏幕
|
|
|
|
|
|
* @param worldPosition
|
|
|
|
|
|
*/
|
|
|
|
|
|
public worldToScreenPoint(worldPosition: Vector2): Vector2{
|
|
|
|
|
|
this.updateMatrixes();
|
|
|
|
|
|
worldPosition = Vector2.transform(worldPosition, this._transformMatrix);
|
|
|
|
|
|
return worldPosition;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 将点从屏幕坐标转换为世界坐标
|
|
|
|
|
|
* @param screenPosition
|
|
|
|
|
|
*/
|
|
|
|
|
|
public screenToWorldPoint(screenPosition: Vector2): Vector2{
|
|
|
|
|
|
this.updateMatrixes();
|
|
|
|
|
|
screenPosition = Vector2.transform(screenPosition, this._inverseTransformMatrix);
|
|
|
|
|
|
return screenPosition;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 返回鼠标在世界空间中的位置
|
|
|
|
|
|
*/
|
|
|
|
|
|
public mouseToWorldPoint(): Vector2{
|
|
|
|
|
|
return this.screenToWorldPoint(Input.touchPosition);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
public onAddedToEntity() {
|
|
|
|
|
|
this.follow(this._targetEntity, this._cameraStyle);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
public update() {
|
|
|
|
|
|
let halfScreen = Vector2.multiply(new Vector2(this.bounds.width, this.bounds.height), new Vector2(0.5));
|
|
|
|
|
|
this._worldSpaceDeadZone.x = this.position.x - halfScreen.x * SceneManager.scene.scaleX + this.deadzone.x + this.focusOffset.x;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._worldSpaceDeadZone.y = this.position.y - halfScreen.y * SceneManager.scene.scaleY + this.deadzone.y + this.focusOffset.y;
|
|
|
|
|
|
this._worldSpaceDeadZone.width = this.deadzone.width;
|
|
|
|
|
|
this._worldSpaceDeadZone.height = this.deadzone.height;
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
if (this._targetEntity)
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this.updateFollow();
|
2020-06-19 00:38:37 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this.position = Vector2.lerp(this.position, Vector2.add(this.position, this._desiredPositionDelta), this.followLerp);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.entity.transform.roundPosition();
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
if (this.mapLockEnabled) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this.position = this.clampToMapSize(this.position);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.entity.transform.roundPosition();
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
2020-07-01 14:19:40 +08:00
|
|
|
|
}
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 固定相机 永远不会离开地图的可见区域
|
|
|
|
|
|
* @param position
|
|
|
|
|
|
*/
|
|
|
|
|
|
public clampToMapSize(position: Vector2) {
|
|
|
|
|
|
let halfScreen = Vector2.multiply(new Vector2(this.bounds.width, this.bounds.height), new Vector2(0.5));
|
2020-07-22 20:07:14 +08:00
|
|
|
|
let cameraMax = new Vector2(this.mapSize.x - halfScreen.x, this.mapSize.y - halfScreen.y);
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return Vector2.clamp(position, halfScreen, cameraMax);
|
|
|
|
|
|
}
|
2020-06-08 16:23:48 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
public updateFollow() {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
if (this._cameraStyle == CameraStyle.lockOn) {
|
|
|
|
|
|
let targetX = this._targetEntity.transform.position.x;
|
|
|
|
|
|
let targetY = this._targetEntity.transform.position.y;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
|
|
|
|
|
if (this._worldSpaceDeadZone.x > targetX)
|
|
|
|
|
|
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
else if (this._worldSpaceDeadZone.x < targetX)
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
|
|
|
|
|
|
|
|
|
|
|
if (this._worldSpaceDeadZone.y < targetY)
|
|
|
|
|
|
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
else if (this._worldSpaceDeadZone.y > targetY)
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
|
|
|
|
|
} else {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
if (!this._targetCollider) {
|
|
|
|
|
|
this._targetCollider = this._targetEntity.getComponent<Collider>(Collider);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
if (!this._targetCollider)
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
let targetBounds = this._targetEntity.getComponent<Collider>(Collider).bounds;
|
|
|
|
|
|
if (!this._worldSpaceDeadZone.containsRect(targetBounds)) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
if (this._worldSpaceDeadZone.left > targetBounds.left)
|
|
|
|
|
|
this._desiredPositionDelta.x = targetBounds.left - this._worldSpaceDeadZone.left;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
else if (this._worldSpaceDeadZone.right < targetBounds.right)
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._desiredPositionDelta.x = targetBounds.right - this._worldSpaceDeadZone.right;
|
|
|
|
|
|
|
|
|
|
|
|
if (this._worldSpaceDeadZone.bottom < targetBounds.bottom)
|
|
|
|
|
|
this._desiredPositionDelta.y = targetBounds.bottom - this._worldSpaceDeadZone.bottom;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
else if (this._worldSpaceDeadZone.top > targetBounds.top)
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._desiredPositionDelta.y = targetBounds.top - this._worldSpaceDeadZone.top;
|
|
|
|
|
|
}
|
2020-07-01 14:19:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-10 17:41:53 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
public follow(targetEntity: Entity, cameraStyle: CameraStyle = CameraStyle.cameraWindow) {
|
|
|
|
|
|
this._targetEntity = targetEntity;
|
|
|
|
|
|
this._cameraStyle = cameraStyle;
|
|
|
|
|
|
|
|
|
|
|
|
switch (this._cameraStyle) {
|
|
|
|
|
|
case CameraStyle.cameraWindow:
|
|
|
|
|
|
let w = this.bounds.width / 6;
|
|
|
|
|
|
let h = this.bounds.height / 3;
|
|
|
|
|
|
this.deadzone = new Rectangle((this.bounds.width - w) / 2, (this.bounds.height - h) / 2, w, h);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case CameraStyle.lockOn:
|
|
|
|
|
|
this.deadzone = new Rectangle(this.bounds.width / 2, this.bounds.height / 2, 10, 10);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 以给定的尺寸设置当前相机边界中心的死区
|
|
|
|
|
|
* @param width
|
|
|
|
|
|
* @param height
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setCenteredDeadzone(width: number, height: number) {
|
|
|
|
|
|
this.deadzone = new Rectangle((this.bounds.width - width) / 2, (this.bounds.height - height) / 2, width, height);
|
|
|
|
|
|
}
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|