this.on('tick', this.tick);
this.level.setup(tanks.data);
- this.viewport.centerOn();
+ this.viewport.centerOn( this.player.loc );
this.emit('ready', this);
},
this.animations = this.animations.filter(this.tickAnimations, this);
this.draw();
+ this.viewport.centerOn( this.player.loc );
if ( this.gameover )
return;
, Vec = require('ezl/math/vec').Vec
, HtmlLayer = require('ezl/layer/html').HtmlLayer
-, PAD_X = 1024, PAD_Y = 690
+, clamp = require('ezl/math').clamp
, min = Y(Math.min).limit(2)
, max = Y(Math.max).limit(2)
+
+, PAD_X = 1024, PAD_Y = 690
,
Viewport =
_layerId : 'viewport',
spacer : null, // inner layer to induce scrolling so we can arbitrarily center the elements
-
+ maxBleedX : 10, // Maximum amount of empty space to show to the left/right of content. (Tip: set to Infinity to disable)
+ maxBleedY : 10, // Maximum amount of empty space to show to the top/bottom of content. (Tip: set to Infinity to disable)
init : function initViewport(props, attrs, html){
* @return {Vec} Current scroll position (left, top).
*/
scroll : function scroll(x,y){
- var el = this.layer;
+ var el = this.layer
+ , bx = this.maxBleedX, by = this.maxBleedY
+ , cw = this.contentWidth, ch = this.contentHeight
+ , w = el.width(), h = el.height()
+ , minX = -bx, minY = -by
+ , maxX = w+bx, maxY = h+by
+ ;
if (arguments.length === 0)
return new Vec(el.scrollLeft()-PAD_X, el.scrollTop()-PAD_Y);
if (x instanceof Array) { y=x[1]; x=x[0]; }
- if (x !== undefined) el.scrollLeft( x + PAD_X );
- if (y !== undefined) el.scrollTop( y + PAD_Y );
+ if (x !== undefined) {
+ var v = (cw < w ? (cw-w)/2 : clamp(x, minX, maxX));
+ el.scrollLeft( PAD_X + v );
+ }
+ if (y !== undefined) {
+ var v = (ch < h ? (ch-h)/2 : clamp(y, minY, maxY));
+ el.scrollTop( PAD_Y + v );
+ }
return this;
},
* @return {this}
*/
centerOn : function centerOn(x,y){
+ if (x instanceof Array) { y=x[1]; x=x[0]; }
if (x === undefined) x = this.contentWidth/2;
if (y === undefined) y = this.contentHeight/2;
var el = this.layer