Skip to content

Commit 2b9be45

Browse files
committed
Starting Camera.deadzone support
1 parent 8732872 commit 2b9be45

1 file changed

Lines changed: 61 additions & 2 deletions

File tree

src/cameras/2d/Camera.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var CenterOn = require('../../geom/rectangle/CenterOn');
78
var Clamp = require('../../math/Clamp');
89
var Class = require('../../utils/Class');
910
var DegToRad = require('../../math/DegToRad');
@@ -344,6 +345,28 @@ var Camera = new Class({
344345
*/
345346
this.followOffset = new Vector2();
346347

348+
/**
349+
* The mid-point of the Camera in 'world' coordinates.
350+
*
351+
* This value is updated in the preRender method, after the scroll values and follower
352+
* update have been calculated.
353+
*
354+
* @name Phaser.Cameras.Scene2D.Camera#midPoint
355+
* @type {Phaser.Math.Vector2}
356+
* @readOnly
357+
* @since 3.11.0
358+
*/
359+
this.midPoint = new Vector2(width / 2, height / 2);
360+
361+
/**
362+
* Camera dead zone.
363+
*
364+
* @name Phaser.Cameras.Scene2D.Camera#deadzone
365+
* @type {?Phaser.Geom.Rectangle}
366+
* @since 3.11.0
367+
*/
368+
this.deadzone = null;
369+
347370
/**
348371
* Internal follow target reference.
349372
*
@@ -726,11 +749,45 @@ var Camera = new Class({
726749
var originX = width / 2;
727750
var originY = height / 2;
728751
var follow = this._follow;
752+
var deadzone = this.deadzone;
729753

730754
if (follow)
731755
{
732-
this.scrollX = Linear(this.scrollX, (follow.x - this.followOffset.x) - originX, this.lerp.x) / zoom;
733-
this.scrollY = Linear(this.scrollY, (follow.y - this.followOffset.y) - originY, this.lerp.y) / zoom;
756+
var fx = (follow.x - this.followOffset.x);
757+
var fy = (follow.y - this.followOffset.y);
758+
759+
this._fx = fx;
760+
this._fy = fy;
761+
762+
if (deadzone)
763+
{
764+
CenterOn(deadzone, this.midPoint.x, this.midPoint.y);
765+
766+
if (fx <= deadzone.x)
767+
{
768+
this.scrollX = Linear(fx, deadzone.x, this.lerp.x) / zoom;
769+
this.scrollX -= deadzone.x;
770+
console.log(this.scrollX);
771+
// debugger;
772+
}
773+
else if (fx >= deadzone.right)
774+
{
775+
this.scrollX = Linear(fx, deadzone.right, this.lerp.x) / zoom;
776+
this.scrollX -= deadzone.right;
777+
console.log(this.scrollX);
778+
// debugger;
779+
}
780+
781+
// if (fy < deadzone.y || fy > deadzone.bottom)
782+
// {
783+
// this.scrollY = Linear(this.scrollY, fy - originY, this.lerp.y) / zoom;
784+
// }
785+
}
786+
else
787+
{
788+
this.scrollX = Linear(this.scrollX, fx, this.lerp.x) / zoom;
789+
this.scrollY = Linear(this.scrollY, fy, this.lerp.y) / zoom;
790+
}
734791
}
735792

736793
if (this.useBounds)
@@ -765,6 +822,8 @@ var Camera = new Class({
765822
this.scrollY = Math.round(this.scrollY);
766823
}
767824

825+
this.midPoint.set(this.scrollX + originX, this.scrollY + originY);
826+
768827
matrix.loadIdentity();
769828
matrix.scale(resolution, resolution);
770829
matrix.translate(this.x + originX, this.y + originY);

0 commit comments

Comments
 (0)