@@ -128,6 +128,17 @@ Phaser.Camera = function (game, id, x, y, width, height) {
128128 */
129129 this . _position = new Phaser . Point ( ) ;
130130
131+ this . _shake = {
132+ intensity : 0 ,
133+ duration : 0 ,
134+ horizontal : false ,
135+ vertical : false ,
136+ x : 0 ,
137+ y : 0
138+ } ;
139+
140+ this . shakeOnComplete = new Phaser . Signal ( ) ;
141+
131142} ;
132143
133144/**
@@ -154,6 +165,24 @@ Phaser.Camera.FOLLOW_TOPDOWN = 2;
154165*/
155166Phaser . Camera . FOLLOW_TOPDOWN_TIGHT = 3 ;
156167
168+ /**
169+ * @constant
170+ * @type {number }
171+ */
172+ Phaser . Camera . SHAKE_BOTH = 4 ;
173+
174+ /**
175+ * @constant
176+ * @type {number }
177+ */
178+ Phaser . Camera . SHAKE_HORIZONTAL = 5 ;
179+
180+ /**
181+ * @constant
182+ * @type {number }
183+ */
184+ Phaser . Camera . SHAKE_VERTICAL = 6 ;
185+
157186Phaser . Camera . prototype = {
158187
159188 /**
@@ -267,6 +296,11 @@ Phaser.Camera.prototype = {
267296 this . updateTarget ( ) ;
268297 }
269298
299+ if ( this . _shake . duration > 0 )
300+ {
301+ this . updateShake ( ) ;
302+ }
303+
270304 if ( this . bounds )
271305 {
272306 this . checkBounds ( ) ;
@@ -277,8 +311,57 @@ Phaser.Camera.prototype = {
277311 this . view . floor ( ) ;
278312 }
279313
280- this . displayObject . position . x = - this . view . x ;
281- this . displayObject . position . y = - this . view . y ;
314+ this . displayObject . position . x = - this . view . x + this . _shake . x ;
315+ this . displayObject . position . y = - this . view . y + this . _shake . y ;
316+
317+ } ,
318+
319+ updateShake : function ( ) {
320+
321+ this . _shake . duration -= this . game . time . elapsedMS ;
322+
323+ if ( this . _shake . duration <= 0 )
324+ {
325+ this . shakeOnComplete . dispatch ( ) ;
326+ this . _shake . x = 0 ;
327+ this . _shake . y = 0 ;
328+ }
329+ else
330+ {
331+ if ( this . _shake . horizontal )
332+ {
333+ this . _shake . x = this . game . rnd . frac ( ) * this . _shake . intensity * this . view . width * 2 - this . _shake . intensity * this . view . width ;
334+ }
335+
336+ if ( this . _shake . vertical )
337+ {
338+ this . _shake . y = this . game . rnd . frac ( ) * this . _shake . intensity * this . view . height * 2 - this . _shake . intensity * this . view . height ;
339+ }
340+ }
341+
342+ } ,
343+
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 ) ;
282365
283366 } ,
284367
0 commit comments