Starting Camera.deadzone support

This commit is contained in:
Richard Davey 2018-06-19 12:29:04 +01:00
parent 873287241b
commit 2b9be45741

View file

@ -4,6 +4,7 @@
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/ */
var CenterOn = require('../../geom/rectangle/CenterOn');
var Clamp = require('../../math/Clamp'); var Clamp = require('../../math/Clamp');
var Class = require('../../utils/Class'); var Class = require('../../utils/Class');
var DegToRad = require('../../math/DegToRad'); var DegToRad = require('../../math/DegToRad');
@ -344,6 +345,28 @@ var Camera = new Class({
*/ */
this.followOffset = new Vector2(); this.followOffset = new Vector2();
/**
* The mid-point of the Camera in 'world' coordinates.
*
* This value is updated in the preRender method, after the scroll values and follower
* update have been calculated.
*
* @name Phaser.Cameras.Scene2D.Camera#midPoint
* @type {Phaser.Math.Vector2}
* @readOnly
* @since 3.11.0
*/
this.midPoint = new Vector2(width / 2, height / 2);
/**
* Camera dead zone.
*
* @name Phaser.Cameras.Scene2D.Camera#deadzone
* @type {?Phaser.Geom.Rectangle}
* @since 3.11.0
*/
this.deadzone = null;
/** /**
* Internal follow target reference. * Internal follow target reference.
* *
@ -726,11 +749,45 @@ var Camera = new Class({
var originX = width / 2; var originX = width / 2;
var originY = height / 2; var originY = height / 2;
var follow = this._follow; var follow = this._follow;
var deadzone = this.deadzone;
if (follow) if (follow)
{ {
this.scrollX = Linear(this.scrollX, (follow.x - this.followOffset.x) - originX, this.lerp.x) / zoom; var fx = (follow.x - this.followOffset.x);
this.scrollY = Linear(this.scrollY, (follow.y - this.followOffset.y) - originY, this.lerp.y) / zoom; var fy = (follow.y - this.followOffset.y);
this._fx = fx;
this._fy = fy;
if (deadzone)
{
CenterOn(deadzone, this.midPoint.x, this.midPoint.y);
if (fx <= deadzone.x)
{
this.scrollX = Linear(fx, deadzone.x, this.lerp.x) / zoom;
this.scrollX -= deadzone.x;
console.log(this.scrollX);
// debugger;
}
else if (fx >= deadzone.right)
{
this.scrollX = Linear(fx, deadzone.right, this.lerp.x) / zoom;
this.scrollX -= deadzone.right;
console.log(this.scrollX);
// debugger;
}
// if (fy < deadzone.y || fy > deadzone.bottom)
// {
// this.scrollY = Linear(this.scrollY, fy - originY, this.lerp.y) / zoom;
// }
}
else
{
this.scrollX = Linear(this.scrollX, fx, this.lerp.x) / zoom;
this.scrollY = Linear(this.scrollY, fy, this.lerp.y) / zoom;
}
} }
if (this.useBounds) if (this.useBounds)
@ -765,6 +822,8 @@ var Camera = new Class({
this.scrollY = Math.round(this.scrollY); this.scrollY = Math.round(this.scrollY);
} }
this.midPoint.set(this.scrollX + originX, this.scrollY + originY);
matrix.loadIdentity(); matrix.loadIdentity();
matrix.scale(resolution, resolution); matrix.scale(resolution, resolution);
matrix.translate(this.x + originX, this.y + originY); matrix.translate(this.x + originX, this.y + originY);