mirror of
https://github.com/photonstorm/phaser
synced 2024-11-10 15:14:47 +00:00
Added Camera.followOffset property and helper methods setLerp and setFollowOffset.
This commit is contained in:
parent
9834e9d30b
commit
7d692bc7dc
2 changed files with 69 additions and 7 deletions
|
@ -14,8 +14,11 @@
|
|||
* 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.
|
||||
* 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.
|
||||
* 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.
|
||||
* 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).
|
||||
* 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.
|
||||
* Camera.setLerp is a chainable method to set the Camera.lerp property.
|
||||
* Camera.followOffset is a new property that allows you to specify an offset from the target position that the camera is following (thanks @hermbit)
|
||||
* Camera.setFollowOffset is a chainable method to set the Camera.followOffset property.
|
||||
* 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.
|
||||
* 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.
|
||||
|
||||
### Updates
|
||||
|
||||
|
|
|
@ -306,6 +306,8 @@ var Camera = new Class({
|
|||
/**
|
||||
* The linear interpolation value to use when following a target.
|
||||
*
|
||||
* Can also be set via `setLerp` or as part of the `startFollow` call.
|
||||
*
|
||||
* The default values of 1 means the camera will instantly snap to the target coordinates.
|
||||
* A lower value, such as 0.1 means the camera will more slowly track the target, giving
|
||||
* a smooth transition. You can set the horizontal and vertical values independently, and also
|
||||
|
@ -320,8 +322,9 @@ var Camera = new Class({
|
|||
this.lerp = new Vector2(1, 1);
|
||||
|
||||
/**
|
||||
* The values stored in this property are added to the follow target position, allowing you to
|
||||
* offset the camera from the actual target x/y coordinates by the followOffset amount.
|
||||
* The values stored in this property are subtracted from the Camera targets position, allowing you to
|
||||
* offset the camera from the actual target x/y coordinates by this amount.
|
||||
* Can also be set via `setFollowOffset` or as part of the `startFollow` call.
|
||||
*
|
||||
* @name Phaser.Cameras.Scene2D.Camera#followOffset
|
||||
* @type {Phaser.Math.Vector2}
|
||||
|
@ -798,6 +801,56 @@ var Camera = new Class({
|
|||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the linear interpolation value to use when following a target.
|
||||
*
|
||||
* The default values of 1 means the camera will instantly snap to the target coordinates.
|
||||
* A lower value, such as 0.1 means the camera will more slowly track the target, giving
|
||||
* a smooth transition. You can set the horizontal and vertical values independently, and also
|
||||
* adjust this value in real-time during your game.
|
||||
*
|
||||
* Be sure to keep the value between 0 and 1. A value of zero will disable tracking on that axis.
|
||||
*
|
||||
* @method Phaser.Cameras.Scene2D.Camera#setLerp
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @param {number} [x=1] - The amount added to the horizontal linear interpolation of the follow target.
|
||||
* @param {number} [y=1] - The amount added to the vertical linear interpolation of the follow target.
|
||||
*
|
||||
* @return {this} This Camera instance.
|
||||
*/
|
||||
setLerp: function (x, y)
|
||||
{
|
||||
if (x === undefined) { x = 1; }
|
||||
if (y === undefined) { y = x; }
|
||||
|
||||
this.lerp.set(x, y);
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the horizontal and vertical offset of the camera from its follow target.
|
||||
* The values are subtracted from the targets position during the Cameras update step.
|
||||
*
|
||||
* @method Phaser.Cameras.Scene2D.Camera#setFollowOffset
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @param {number} [x=0] - The horizontal offset from the camera follow target.x position.
|
||||
* @param {number} [y=0] - The vertical offset from the camera follow target.y position.
|
||||
*
|
||||
* @return {this} This Camera instance.
|
||||
*/
|
||||
setFollowOffset: function (x, y)
|
||||
{
|
||||
if (x === undefined) { x = 0; }
|
||||
if (y === undefined) { y = 0; }
|
||||
|
||||
this.followOffset.set(x, y);
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the background color for this Camera.
|
||||
*
|
||||
|
@ -1076,14 +1129,18 @@ var Camera = new Class({
|
|||
* @param {boolean} [roundPixels=false] - Round the camera position to whole integers to avoid sub-pixel rendering?
|
||||
* @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.
|
||||
* @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.
|
||||
* @param {number} [offsetX=0] - The horizontal offset from the camera follow target.x position.
|
||||
* @param {number} [offsetY=0] - The vertical offset from the camera follow target.y position.
|
||||
*
|
||||
* @return {this} This Camera instance.
|
||||
*/
|
||||
startFollow: function (target, roundPixels, lerpX, lerpY)
|
||||
startFollow: function (target, roundPixels, lerpX, lerpY, offsetX, offsetY)
|
||||
{
|
||||
if (roundPixels === undefined) { roundPixels = false; }
|
||||
if (lerpX === undefined) { lerpX = 1; }
|
||||
if (lerpY === undefined) { lerpY = lerpX; }
|
||||
if (offsetX === undefined) { offsetX = 0; }
|
||||
if (offsetY === undefined) { offsetY = offsetX; }
|
||||
|
||||
this._follow = target;
|
||||
|
||||
|
@ -1094,13 +1151,15 @@ var Camera = new Class({
|
|||
|
||||
this.lerp.set(lerpX, lerpY);
|
||||
|
||||
this.followOffset.set(offsetX, offsetY);
|
||||
|
||||
// Move the camera there immediately, to avoid a large lerp during preUpdate
|
||||
var zoom = this.zoom;
|
||||
var originX = this.width / 2;
|
||||
var originY = this.height / 2;
|
||||
|
||||
this.scrollX = (target.x - originX) / zoom;
|
||||
this.scrollY = (target.y - originY) / zoom;
|
||||
this.scrollX = (target.x - offsetX - originX) / zoom;
|
||||
this.scrollY = (target.y - offsetY - originY) / zoom;
|
||||
|
||||
return this;
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue