-function strip(){
- return this._o.replace(/(^\s+|\s+$)/g, '');
-}
-
-function trim(val){
- var s = this._o+'';
- val = val+'';
- if (s.length < val.length)
- return s;
- else if ( s.indexOf(val) === 0 )
- return s.slice(val.length);
- else
- return s;
-}
-
-function rtrim(val){
- var s = this._o+'';
- val = val+'';
-
- var idx = (s.length - val.length);
- if (idx < 0)
- return s;
- else if ( s.lastIndexOf(val) === idx )
- return s.slice(0, idx);
- else
- return s;
-}
-
-function startsWith(val){
- return this._o.indexOf(val) === 0;
-}
-
-function endsWith(val){
- var s = this._o;
- return s.lastIndexOf(val) === (s.length - val.length);
-}
-
var
YString =
Y.YString =
-YCollection.subclass('YString', {
- init : function init(o){
- if (!o) o = "";
- this._o = o;
- YCollection.init.call(this, o);
- },
- strip: strip,
- trim: trim,
- ltrim: trim,
- rtrim: rtrim,
- startsWith: startsWith,
- endsWith: endsWith,
+YCollection.subclass('YString', function(YString){
+ mixinNames(YString, String, [
+ 'slice', 'split',
+ 'substr', 'substring',
+ 'concat', 'replace',
+ 'toLowerCase', 'toUpperCase'
+ ], true, true);
+ mixinNames(YString, String, [ 'indexOf', 'lastIndexOf', 'charAt', 'charCodeAt' ], true, false);
- compare : function compare(n){
- var m = this._o;
- return (m > n ? 1 :
- (m < n ? -1 : 0 ));
- },
-
- toString: function(){
- // return 'Y"'+this._o+'"';
- return this._o;
+ function trim(val){
+ var s = this._o+'';
+ val = val+'';
+ if (s.length < val.length)
+ return s;
+ else if ( s.indexOf(val) === 0 )
+ return s.slice(val.length);
+ else
+ return s;
}
+
+ Y.op.extend(this, {
+ init : function init(o){
+ if (!o) o = "";
+ this._o = o;
+ YCollection.init.call(this, o);
+ },
+
+ strip: function strip(){
+ return this._o.replace(/(^\s+|\s+$)/g, '');
+ },
+
+ trim: trim,
+ ltrim: trim,
+
+ rtrim: function rtrim(val){
+ var s = this._o+'';
+ val = val+'';
+
+ var idx = (s.length - val.length);
+ if (idx < 0)
+ return s;
+ else if ( s.lastIndexOf(val) === idx )
+ return s.slice(0, idx);
+ else
+ return s;
+ },
+
+ startsWith: function startsWith(val){
+ return this._o.indexOf(val) === 0;
+ },
+ endsWith: function endsWith(val){
+ var s = this._o;
+ return s.lastIndexOf(val) === (s.length - val.length);
+ },
+
+ compare : function compare(n){
+ var m = this._o;
+ return (m > n ? 1 :
+ (m < n ? -1 : 0 ));
+ },
+
+ toString: function(){
+ return this._o;
+ }
+ });
});
jQuery(main);
this.main = main;
+
+// Main method is only executed once, so we'll setup things
+// that don't change between games.
function main(){
- var v = $('#viewport');
+ qkv = Y(window.location.search).fromKV();
- if (window.LBT) LBT.stop();
- v.empty();
+ /// Debug ///
+ if (qkv.debug) $('#debug').toggle();
- LBT = new tanks.Game();
- ctx = LBT.level.ctx;
+ // Show debug
+ $(document).bind('keydown', 'ctrl+c', function(evt){ $('#debug').toggle(); });
+
+ // Don't fire on clicks in the debug menu
+ $('#debug').bind('mousedown', Y.op.K(false));
- P = LBT.addUnit(new PlayerTank(1), 5,9);
- E =
- LBT.addUnit(new Tank(2), 0,1);
- LBT.addUnit(new Tank(2), 1,0);
- LBT.addUnit(new Tank(2), 8,1);
+ // Update config when changed
+ $('#debug input').bind('change', updateConfig);
+
+
+ // Create #pause box
+ $('#welcome').clone()
+ .attr('id', 'pause')
+ .hide()
+ .appendTo( $('body') )
+ .find('.box').html('<h1>Paused!</h1>');
+ // Create Victory/Defeat box
+ $('#welcome').clone()
+ .attr('id', 'gameover')
+ .hide()
+ .appendTo( $('body') )
+ .find('.box')
+ .html('<h1>You Win!</h1><p></p><div class="restart pinkbutton rounded">Play Again</div>');
+
+
+ // Bind to all future restart buttons
+ $('#gameover .restart')
+ .live('click', function(evt){
+ setupGame();
+ setupUI();
+
+ startGame();
+ });
+
+ setupGame();
setupUI();
+}
+
+function gameExists(){ return !!tanks.currentGame; }
+
+function setupGame(){
+ if ( gameExists() ) teardownGame();
- // toggleGame();
- updateInfo();
+ LBT = tanks.currentGame = new tanks.Game();
- pm = LBT.pathmap;
+ LBT.addEventListener('win', gameover('You Win!', 'Play Again', ''));
+ LBT.addEventListener('lose', gameover('You Lose :(', 'Try Again', ''));
+
+ ctx = LBT.level.ctx;
+}
+
+function teardownGame(){
+ LBT.stop();
+ $('#viewport').empty();
}
// Set up UI listeners
function setupUI(){
- LBT.loop.spark = new FpsSparkline(LBT.loop, '.fps-sparkline', 0,0);
+ if ( gameExists() ) teardownUI();
+ $('#welcome').show();
+ LBT.loop.spark = new FpsSparkline(LBT.loop, '.fps-sparkline', 0,0);
initConfig();
- $('#config').bind('mousedown', Y.op.K(false));
- $('#config input').bind('change', updateConfig);
-
LBT.root.draw();
- $(document).bind('keydown', 'ctrl+c', toggleConfig);
-
// Start button (click or return key)
- $(document).bind('keydown', 'return', startGame);
$(document).bind('click', startGame);
-
- $('#welcome').clone()
- .attr('id', 'pause')
- .hide()
- .appendTo( $('body') )
- .find('.box').html('<h1>Paused!</h1>');
+ $(document).bind('keydown', 'return', startGame);
LBT.root.draw();
setInterval(updateInfo, 1000);
+ updateInfo();
+
// Fix grid-size on resize
// $(window).bind('resize', resizeGame);
}
-
-function startGame(evt){
- $(document).unbind('click', startGame);
+function teardownUI(){
+ $(document).unbind('click', startGame);
$(document).unbind('keydown', startGame);
+ $(document).unbind('keydown', pauseGame);
- $('#welcome').hide();
- countdownToStart(3);
+ $('#welcome, #pause, #gameover').hide();
}
-function onGameStart(){
- $('#overlay').hide();
- toggleGame();
- $(document).bind('keydown', 'return', pauseGame);
-}
-function countdownToStart(n){
- var el
- , showFor = 750
- , pauseFor = 500
-
- , body = $('body')
- , sizeRatio = 0.6
- , values = new Y(1,n+1).unshift('GO').end()
- , colors = [ '#E73075', '#2992C5', '#2992C5', '#2992C5' ]
- ;
-
- function tickDown(){
- var bw = body.width()
- , bh = body.height()
- , size = bh*sizeRatio
-
- , el = $('<div/>')
- .width(bh*0.75).height(bh*0.75)
- .text( values.pop() )
- .css({
- 'position' : 'fixed',
- 'top' : ((bh - size)/2)+'px',
- 'left' : (bw/2 - bh*0.375)+'px',
- 'overflow' : 'hidden',
- 'z-index' : 1000,
-
- 'text-align' : 'center',
- 'font-size' : size+'px',
- 'color' : '#000000',
- 'background-color' : colors.pop(),
- })
- ;
-
- ' -moz- -webkit-'
- .split(' ')
- .forEach(function(prefix){
- // el.css(prefix+'border-radius', (size*0.25)+'px');
- el.css(prefix+'border-radius', (bh/2)+'px');
- });
-
- body.append(el);
-
- setTimeout(function(){
- el.remove();
-
- if ( values.length ) {
- setTimeout(tickDown, pauseFor);
- } else {
- onGameStart();
- }
- }, showFor);
- }
+function startGame(evt){
+ $(document).unbind('click', startGame);
+ $(document).unbind('keydown', startGame);
- tickDown();
+ $('#welcome').hide();
+ countdownToStart(3, function(){
+ $('#overlay').hide();
+ $(document).bind('keydown', 'return', pauseGame);
+ toggleGame();
+ });
}
function pauseGame(evt){
- toggleOverlay();
+ $('#overlay').toggle();
$('#pause').toggle();
toggleGame();
}
-
-
-
function toggleGame(evt){
if (LBT.loop.running)
LBT.stop();
updateInfo();
}
-function toggleConfig(evt){ $('#debug').toggle(); }
-function toggleOverlay(evt){ $('#overlay').toggle(); }
-function togglePathingOverlay(evt){ LBT.showOverlay = !(LBT.showOverlay); }
-
function resizeGame(evt){
LBT.resize(evt);
}
}
+function gameover(headline, button, body, evt){
+ $('#overlay').toggle();
+ $('#gameover')
+ .find('h1').text(headline).end()
+ .find('.pinkbutton').text(button).end()
+ .find('p').text(body).end()
+ .show();
+}
+gameover = Y(gameover).curry();
+
function initConfig(){
- var c = tanks.config, p = c.pathing;
+ var c = tanks.config
+ , p = c.pathing
+ ;
$('#config [name=pathmap]').attr('checked', p.overlayPathmap);
$('#config [name=aipaths]').attr('checked', p.overlayAIPaths);
$('#config [name=trajectories]').attr('checked', p.traceTrajectories);
- $('#config [name=gridCoords]').attr('checked', tanks.config.debug.gridShowCoords);
- $('#viewport .layer.grid')[(tanks.config.debug.gridShowCoords ? 'add' : 'remove')+'Class']('gridShowCoords');
-
- // $('#config [name=bullets]').val(c.debug.projectiles);
- // updateBullets( c.debug.projectiles );
+ $('#config [name=gridCoords]').attr('checked', c.debug.gridShowCoords);
+ $('#viewport .layer.grid')[(c.debug.gridShowCoords ? 'add' : 'remove')+'Class']('gridShowCoords');
}
function updateConfig(evt){
- var p = tanks.config.pathing;
+ var c = tanks.config
+ , p = c.pathing
+ ;
+
p.overlayPathmap = $('#config [name=pathmap]').attr('checked');
p.overlayAIPaths = $('#config [name=aipaths]').attr('checked');
p.traceTrajectories = $('#config [name=trajectories]').attr('checked');
- tanks.config.debug.gridShowCoords = $('#config [name=gridCoords]').attr('checked');
- $('#viewport .layer.grid')[(tanks.config.debug.gridShowCoords ? 'add' : 'remove')+'Class']('gridShowCoords');
+ c.debug.gridShowCoords = $('#config [name=gridCoords]').attr('checked');
+ $('#viewport .layer.grid')[(c.debug.gridShowCoords ? 'add' : 'remove')+'Class']('gridShowCoords');
}
-function spawnBullet(x,y, atX,atY){
- if (x === undefined && y === undefined) {
- var loc = randOpenLoc();
- x = loc.x; y = loc.y;
- }
- if (atX === undefined && atY === undefined) do {
- atX = rand(0,COLUMNS*REF_SIZE);
- atY = rand(0,ROWS*REF_SIZE);
- } while ( atX === x && atY === y);
-
- btank.setLocation(x,y);
- return btank.shoot(atX,atY);
-}
-
-function updateBullets(n){
- if ( !Y.isNumber(n) || isNaN(n) || n === bullets.size() ) return;
-
- while (n < bullets.size()) {
- var i = Math.round(rand(0, bullets.size()-1));
- bullets.remove( bullets.attr(i).remove() );
- }
-
- while (n > bullets.size())
- bullets.push(spawnBullet());
-}
-
-function rand(a,b){ return a + Math.random()*(b-a); }
-function randOpenLoc(){
- var BP = Bullet.prototype
- , w = BP.width, h = BP.height
- , offX = BP.offsetX, offY = BP.offsetY
- ;
- do {
- var x = rand(-offX, COLUMNS*REF_SIZE-w)
- , y = rand(-offY, ROWS*REF_SIZE-h);
- } while ( LBT.pathmap.get(x+offX,y+offY, x+w,y+h).size() );
- return new math.Vec(x,y);
-}
-
-
-
// Update performance info periodically
function updateInfo(){
var loop = LBT.loop
return false;
}
+function countdownToStart(n, fn){
+ var el
+ , showFor = 750
+ , pauseFor = 500
+
+ , body = $('body')
+ , sizeRatio = 0.6
+ , values = new Y(1,n+1).unshift('GO').end()
+ , colors = [ '#E73075', '#2992C5', '#2992C5', '#2992C5' ]
+ ;
+
+ function tickDown(){
+ var bw = body.width()
+ , bh = body.height()
+ , size = bh*sizeRatio
+
+ , el = $('<div/>')
+ .width(bh*0.75).height(bh*0.75)
+ .text( values.pop() )
+ .css({
+ 'position' : 'fixed',
+ 'top' : ((bh - size)/2)+'px',
+ 'left' : (bw/2 - bh*0.375)+'px',
+ 'overflow' : 'hidden',
+ 'z-index' : 1000,
+
+ 'text-align' : 'center',
+ 'font-size' : size+'px',
+ 'color' : '#000000',
+ 'background-color' : colors.pop(),
+ })
+ ;
+
+ ' -moz- -webkit-'
+ .split(' ')
+ .forEach(function(prefix){
+ // el.css(prefix+'border-radius', (size*0.25)+'px');
+ el.css(prefix+'border-radius', (bh/2)+'px');
+ });
+
+ body.append(el);
+
+ setTimeout(function(){
+ el.remove();
+
+ if ( values.length ) {
+ setTimeout(tickDown, pauseFor);
+ } else {
+ fn();
+ }
+ }, showFor);
+ }
+
+ tickDown();
+}
-// function initUki(){
-// uki({ view:'Box', rect:'0 0 250 500', anchor:'top width', className:'box', childViews:[
-// { view:'Box', rect:'8 8 250 0', anchor:'top width height', childViews:[
-// { view:'HFlow', rect:'250 25', anchor:'width', childViews:[
-// { view:'Label', rect:'200 25', anchor:'top left', text:'Bullets' },
-// { view:'TextField', rect:'50 25', anchor:'top right', name:'bullets', value:'10', background:'' }
-// ]}
-// ]}
-// ]})
-// .attachTo(
-// $('<div/>').css({
-// 'position':'relative',
-// 'width':'100%',
-// 'top':0, 'left':0
-// }).appendTo('#controls')[0], '0 0');
-// // .attachTo(window, '10 10');
-// }
})();
-function dumpPathmap(){
- var pm = LBT.pathmap;
- console.warn(new Date(), pm);
- pm.collect(pm.x1,pm.y1, pm.x2,pm.y2, function(acc, v, r){
- console.log(r+'');
- });
- console.log(' ');
-}