Skip to content

Commit 0b0a5d4

Browse files
committed
Camera shake working and documented.
1 parent a916812 commit 0b0a5d4

1 file changed

Lines changed: 79 additions & 31 deletions

File tree

src/core/Camera.js

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Phaser.Camera = function (game, id, x, y, width, height) {
109109
this.lerp = new Phaser.Point(1, 1);
110110

111111
/**
112-
* @property {Phaser.Point} _targetPosition - Internal point used to calculate target position
112+
* @property {Phaser.Point} _targetPosition - Internal point used to calculate target position.
113113
* @private
114114
*/
115115
this._targetPosition = new Phaser.Point();
@@ -133,6 +133,7 @@ Phaser.Camera = function (game, id, x, y, width, height) {
133133
duration: 0,
134134
horizontal: false,
135135
vertical: false,
136+
shakeBounds: true,
136137
x: 0,
137138
y: 0
138139
};
@@ -285,6 +286,46 @@ Phaser.Camera.prototype = {
285286

286287
},
287288

289+
/**
290+
* This creates a camera shake effect. It works by applying a random amount of additional
291+
* spacing on the x and y axis each frame. You can control the intensity and duration
292+
* of the effect, and if it should effect both axis or just one.
293+
*
294+
* When the shake effect ends the signal Camera.shakeOnComplete is dispatched.
295+
*
296+
* @method Phaser.Camera#shake
297+
* @param {float} [intensity=0.05] - The intensity of the camera shake. Given as a percentage of the camera size representing the maximum distance that the camera can move while shaking.
298+
* @param {number} [duration=500] - The duration of the shake effect in milliseconds.
299+
* @param {boolean} [force=true] - If a camera shake effect is already running and force is true it will replace the previous effect, resetting the duration.
300+
* @param {number} [direction=Phaser.Camera.SHAKE_BOTH] - The directions in which the camera can shake. Either Phaser.Camera.SHAKE_BOTH, Phaser.Camera.SHAKE_HORIZONTAL or Phaser.Camera.SHAKE_VERTICAL.
301+
* @param {boolean} [shakeBounds=true] - Is the effect allowed to shake the camera beyond its bounds (if set?).
302+
*/
303+
shake: function (intensity, duration, force, direction, shakeBounds) {
304+
305+
if (intensity === undefined) { intensity = 0.05; }
306+
if (duration === undefined) { duration = 500; }
307+
if (force === undefined) { force = true; }
308+
if (direction === undefined) { direction = Phaser.Camera.SHAKE_BOTH; }
309+
if (shakeBounds === undefined) { shakeBounds = true; }
310+
311+
if (!force && this._shake.duration > 0)
312+
{
313+
// Can't reset an already running shake
314+
return;
315+
}
316+
317+
this._shake.intensity = intensity;
318+
this._shake.duration = duration;
319+
this._shake.shakeBounds = shakeBounds;
320+
321+
this._shake.x = 0;
322+
this._shake.y = 0;
323+
324+
this._shake.horizontal = (direction === Phaser.Camera.SHAKE_BOTH || direction === Phaser.Camera.SHAKE_HORIZONTAL);
325+
this._shake.vertical = (direction === Phaser.Camera.SHAKE_BOTH || direction === Phaser.Camera.SHAKE_VERTICAL);
326+
327+
},
328+
288329
/**
289330
* Update focusing and scrolling.
290331
* @method Phaser.Camera#update
@@ -309,10 +350,12 @@ Phaser.Camera.prototype = {
309350
if (this.roundPx)
310351
{
311352
this.view.floor();
353+
this._shake.x = Math.floor(this._shake.x);
354+
this._shake.y = Math.floor(this._shake.y);
312355
}
313356

314-
this.displayObject.position.x = -this.view.x + this._shake.x;
315-
this.displayObject.position.y = -this.view.y + this._shake.y;
357+
this.displayObject.position.x = -(this.view.x + this._shake.x);
358+
this.displayObject.position.y = -(this.view.y + this._shake.y);
316359

317360
},
318361

@@ -341,30 +384,6 @@ Phaser.Camera.prototype = {
341384

342385
},
343386

344-
shake: function (intensity, duration, force, direction) {
345-
346-
if (intensity === undefined) { intensity = 0.05; }
347-
if (duration === undefined) { duration = 500; }
348-
if (force === undefined) { force = true; }
349-
if (direction === undefined) { direction = Phaser.Camera.SHAKE_BOTH; }
350-
351-
if (!force && this._shake.duration > 0)
352-
{
353-
// Can't reset an already running shake
354-
return;
355-
}
356-
357-
this._shake.intensity = intensity;
358-
this._shake.duration = duration;
359-
360-
this._shake.x = 0;
361-
this._shake.y = 0;
362-
363-
this._shake.horizontal = (direction === Phaser.Camera.SHAKE_BOTH || direction === Phaser.Camera.SHAKE_HORIZONTAL);
364-
this._shake.vertical = (direction === Phaser.Camera.SHAKE_BOTH || direction === Phaser.Camera.SHAKE_VERTICAL);
365-
366-
},
367-
368387
/**
369388
* Internal method
370389
* @method Phaser.Camera#updateTarget
@@ -428,29 +447,58 @@ Phaser.Camera.prototype = {
428447
this.atLimit.x = false;
429448
this.atLimit.y = false;
430449

450+
var vx = this.view.x + this._shake.x;
451+
var vw = this.view.right + this._shake.x;
452+
var vy = this.view.y + this._shake.y;
453+
var vh = this.view.bottom + this._shake.y;
454+
431455
// Make sure we didn't go outside the cameras bounds
432-
if (this.view.x <= this.bounds.x * this.scale.x)
456+
if (vx <= this.bounds.x * this.scale.x)
433457
{
434458
this.atLimit.x = true;
435459
this.view.x = this.bounds.x * this.scale.x;
460+
461+
if (!this._shake.shakeBounds)
462+
{
463+
// The camera is up against the bounds, so reset the shake
464+
this._shake.x = 0;
465+
}
436466
}
437467

438-
if (this.view.right >= this.bounds.right * this.scale.x)
468+
if (vw >= this.bounds.right * this.scale.x)
439469
{
440470
this.atLimit.x = true;
441471
this.view.x = (this.bounds.right * this.scale.x) - this.width;
472+
473+
if (!this._shake.shakeBounds)
474+
{
475+
// The camera is up against the bounds, so reset the shake
476+
this._shake.x = 0;
477+
}
442478
}
443479

444-
if (this.view.y <= this.bounds.top * this.scale.y)
480+
if (vy <= this.bounds.top * this.scale.y)
445481
{
446482
this.atLimit.y = true;
447483
this.view.y = this.bounds.top * this.scale.y;
484+
485+
if (!this._shake.shakeBounds)
486+
{
487+
// The camera is up against the bounds, so reset the shake
488+
this._shake.y = 0;
489+
}
448490
}
449491

450-
if (this.view.bottom >= this.bounds.bottom * this.scale.y)
492+
if (vh >= this.bounds.bottom * this.scale.y)
451493
{
452494
this.atLimit.y = true;
453495
this.view.y = (this.bounds.bottom * this.scale.y) - this.height;
496+
497+
if (!this._shake.shakeBounds)
498+
{
499+
// The camera is up against the bounds, so reset the shake
500+
this._shake.y = 0;
501+
}
454502
}
455503

456504
},

0 commit comments

Comments
 (0)