Skip to content

Commit 7d692bc

Browse files
committed
Added Camera.followOffset property and helper methods setLerp and setFollowOffset.
1 parent 9834e9d commit 7d692bc

2 files changed

Lines changed: 69 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* PluginManager.registerFileType has a new property `addToScene` which allows you to inject the new file type into the LoaderPlugin of the given Scene. You could use this to add the file type into the Scene in which it was loaded.
1515
* PluginManager.install has a new property `mapping`. This allows you to give a Global Plugin a property key, so that it is automatically injected into any Scenes as a Scene level instance. This allows you to have a single global plugin running in the PluginManager, that is injected into every Scene automatically.
1616
* Camera.lerp has been implemented and allows you to specify the linear interpolation value used when following a target, to provide for smoothed camera tracking.
17-
* Camera.startFollow has 2 new arguments: `lerpX` and `lerpY` which allow you to set the interpolation value used when following the target. The default is 1 (no interpolation).
18-
* Camera.startFollow will now immediately set the camera scrollX and Y values to be that of the target to avoid large initial lerps during the first few preUpdates.
17+
* Camera.setLerp is a chainable method to set the Camera.lerp property.
18+
* Camera.followOffset is a new property that allows you to specify an offset from the target position that the camera is following (thanks @hermbit)
19+
* Camera.setFollowOffset is a chainable method to set the Camera.followOffset property.
20+
* Camera.startFollow has 4 new arguments: `lerpX` and `lerpY` which allow you to set the interpolation value used when following the target. The default is 1 (no interpolation) and `offsetX` and `offsetY` which allow you to set the follow offset values.
21+
* Camera.startFollow will now immediately set the camera `scrollX` and `scrollY` values to be that of the target position to avoid a large initial lerps during the first few preUpdates.
1922

2023
### Updates
2124

src/cameras/2d/Camera.js

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ var Camera = new Class({
306306
/**
307307
* The linear interpolation value to use when following a target.
308308
*
309+
* Can also be set via `setLerp` or as part of the `startFollow` call.
310+
*
309311
* The default values of 1 means the camera will instantly snap to the target coordinates.
310312
* A lower value, such as 0.1 means the camera will more slowly track the target, giving
311313
* a smooth transition. You can set the horizontal and vertical values independently, and also
@@ -320,8 +322,9 @@ var Camera = new Class({
320322
this.lerp = new Vector2(1, 1);
321323

322324
/**
323-
* The values stored in this property are added to the follow target position, allowing you to
324-
* offset the camera from the actual target x/y coordinates by the followOffset amount.
325+
* The values stored in this property are subtracted from the Camera targets position, allowing you to
326+
* offset the camera from the actual target x/y coordinates by this amount.
327+
* Can also be set via `setFollowOffset` or as part of the `startFollow` call.
325328
*
326329
* @name Phaser.Cameras.Scene2D.Camera#followOffset
327330
* @type {Phaser.Math.Vector2}
@@ -798,6 +801,56 @@ var Camera = new Class({
798801
return this;
799802
},
800803

804+
/**
805+
* Sets the linear interpolation value to use when following a target.
806+
*
807+
* The default values of 1 means the camera will instantly snap to the target coordinates.
808+
* A lower value, such as 0.1 means the camera will more slowly track the target, giving
809+
* a smooth transition. You can set the horizontal and vertical values independently, and also
810+
* adjust this value in real-time during your game.
811+
*
812+
* Be sure to keep the value between 0 and 1. A value of zero will disable tracking on that axis.
813+
*
814+
* @method Phaser.Cameras.Scene2D.Camera#setLerp
815+
* @since 3.9.0
816+
*
817+
* @param {number} [x=1] - The amount added to the horizontal linear interpolation of the follow target.
818+
* @param {number} [y=1] - The amount added to the vertical linear interpolation of the follow target.
819+
*
820+
* @return {this} This Camera instance.
821+
*/
822+
setLerp: function (x, y)
823+
{
824+
if (x === undefined) { x = 1; }
825+
if (y === undefined) { y = x; }
826+
827+
this.lerp.set(x, y);
828+
829+
return this;
830+
},
831+
832+
/**
833+
* Sets the horizontal and vertical offset of the camera from its follow target.
834+
* The values are subtracted from the targets position during the Cameras update step.
835+
*
836+
* @method Phaser.Cameras.Scene2D.Camera#setFollowOffset
837+
* @since 3.9.0
838+
*
839+
* @param {number} [x=0] - The horizontal offset from the camera follow target.x position.
840+
* @param {number} [y=0] - The vertical offset from the camera follow target.y position.
841+
*
842+
* @return {this} This Camera instance.
843+
*/
844+
setFollowOffset: function (x, y)
845+
{
846+
if (x === undefined) { x = 0; }
847+
if (y === undefined) { y = 0; }
848+
849+
this.followOffset.set(x, y);
850+
851+
return this;
852+
},
853+
801854
/**
802855
* Sets the background color for this Camera.
803856
*
@@ -1076,14 +1129,18 @@ var Camera = new Class({
10761129
* @param {boolean} [roundPixels=false] - Round the camera position to whole integers to avoid sub-pixel rendering?
10771130
* @param {float} [lerpX=1] - A value between 0 and 1. This value specifies the amount of linear interpolation to use when horizontally tracking the target. The closer the value to 1, the faster the camera will track.
10781131
* @param {float} [lerpY=1] - A value between 0 and 1. This value specifies the amount of linear interpolation to use when vertically tracking the target. The closer the value to 1, the faster the camera will track.
1132+
* @param {number} [offsetX=0] - The horizontal offset from the camera follow target.x position.
1133+
* @param {number} [offsetY=0] - The vertical offset from the camera follow target.y position.
10791134
*
10801135
* @return {this} This Camera instance.
10811136
*/
1082-
startFollow: function (target, roundPixels, lerpX, lerpY)
1137+
startFollow: function (target, roundPixels, lerpX, lerpY, offsetX, offsetY)
10831138
{
10841139
if (roundPixels === undefined) { roundPixels = false; }
10851140
if (lerpX === undefined) { lerpX = 1; }
10861141
if (lerpY === undefined) { lerpY = lerpX; }
1142+
if (offsetX === undefined) { offsetX = 0; }
1143+
if (offsetY === undefined) { offsetY = offsetX; }
10871144

10881145
this._follow = target;
10891146

@@ -1094,13 +1151,15 @@ var Camera = new Class({
10941151

10951152
this.lerp.set(lerpX, lerpY);
10961153

1154+
this.followOffset.set(offsetX, offsetY);
1155+
10971156
// Move the camera there immediately, to avoid a large lerp during preUpdate
10981157
var zoom = this.zoom;
10991158
var originX = this.width / 2;
11001159
var originY = this.height / 2;
11011160

1102-
this.scrollX = (target.x - originX) / zoom;
1103-
this.scrollY = (target.y - originY) / zoom;
1161+
this.scrollX = (target.x - offsetX - originX) / zoom;
1162+
this.scrollY = (target.y - offsetY - originY) / zoom;
11041163

11051164
return this;
11061165
},

0 commit comments

Comments
 (0)