@@ -91,6 +91,7 @@ Systems.prototype = {
9191 this . transform = new Component . Transform ( this . state ) ;
9292
9393 this . cameras = [ ] ;
94+ this . cameraPool = [ ] ;
9495 this . mainCamera = new Camera ( 0 , 0 , this . game . config . width , this . game . config . height ) ;
9596 this . cameras . push ( this . mainCamera ) ;
9697 this . inject ( ) ;
@@ -174,20 +175,53 @@ Systems.prototype = {
174175
175176 addCamera : function ( x , y , width , height )
176177 {
177- var camera = new Camera ( x , y , width , height ) ;
178+ var camera = null ;
179+ if ( this . cameraPool . length > 0 )
180+ {
181+ camera = this . cameraPool . pop ( ) ;
182+ camera . setViewport ( x , y , width , height ) ;
183+ }
184+ else
185+ {
186+ camera = new Camera ( x , y , width , height ) ;
187+ }
178188 camera . setState ( this . state ) ;
179189 this . cameras . push ( camera ) ;
180190 return camera ;
181191 } ,
182192
193+ addCameraReference : function ( camera )
194+ {
195+ var index = this . cameras . indexOf ( camera ) ;
196+ var poolIndex = this . cameraPool . indexOf ( camera ) ;
197+
198+ if ( index < 0 && poolIndex >= 0 )
199+ {
200+ this . cameras . push ( camera ) ;
201+ this . cameraPool . slice ( poolIndex , 1 ) ;
202+ return camera ;
203+ }
204+
205+ return null ;
206+ } ,
207+
208+ removeCamera : function ( camera )
209+ {
210+ var cameraIndex = this . cameras . indexOf ( camera ) ;
211+ if ( cameraIndex >= 0 )
212+ {
213+ this . cameraPool . push ( this . cameras [ cameraIndex ] ) ;
214+ this . cameras . splice ( cameraIndex , 1 ) ;
215+ }
216+ } ,
217+
183218 resetCameras : function ( )
184219 {
185- this . cameras . length = 1 ;
186- this . mainCamera = this . cameras [ 0 ] ;
187- this . mainCamera . x = 0 ;
188- this . mainCamera . y = 0 ;
189- this . mainCamera . width = this . game . config . width ;
190- this . mainCamera . height = this . game . config . height ;
220+ while ( this . cameras . length > 0 )
221+ {
222+ this . cameraPool . push ( this . cameras . pop ( ) ) ;
223+ }
224+ this . mainCamera = this . addCamera ( 0 , 0 , this . game . config . width , this . game . config . height ) ;
191225 }
192226
193227} ;
0 commit comments