From 9e8eb721bcf9c4394a9f2ba4c504fe510469ab5b Mon Sep 17 00:00:00 2001 From: dsc Date: Wed, 17 Nov 2010 23:48:51 -0800 Subject: [PATCH] Enforces shot-limit. --- index.php | 2 +- src/tanks/game/game.js | 7 ++----- src/tanks/main.js | 14 ++++++++------ src/tanks/thing/bullet.js | 2 ++ src/tanks/thing/tank.js | 25 ++++++++++++++++++------- src/tanks/ui/player.js | 3 +-- 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/index.php b/index.php index 66277c1..9499a94 100644 --- a/index.php +++ b/index.php @@ -23,7 +23,7 @@
  • -
  • +
  • diff --git a/src/tanks/game/game.js b/src/tanks/game/game.js index a26567d..5b7acfe 100644 --- a/src/tanks/game/game.js +++ b/src/tanks/game/game.js @@ -1,7 +1,7 @@ tanks.Game = new Y.Class('Game', { init : function init(viewport){ - Y.bindAll(this); // Seal all methods + Y.bindAll(this); this.byId = {}; this.active = new Y.YArray(); @@ -11,9 +11,6 @@ tanks.Game = new Y.Class('Game', { this.loop = new EventLoop(this, FRAME_RATE); - // this.resize = this.resize.bind(this); - // this.tick = this.tick.bind(this); - this.root = this.grid = new Grid(COLUMNS,ROWS, CELL_SIZE) @@ -99,7 +96,7 @@ tanks.Game = new Y.Class('Game', { if (unit instanceof Y.event.YEvent) unit = unit.instance; - console.log('killUnit(', unit, ')'); + // console.log('killUnit(', unit, ')'); delete this.byId[unit.id]; this.active.remove(unit); diff --git a/src/tanks/main.js b/src/tanks/main.js index c8b3929..ae192e0 100644 --- a/src/tanks/main.js +++ b/src/tanks/main.js @@ -28,6 +28,7 @@ function setupUI(){ btank = new Tank(1); btank.act = function(){ return this; }; + btank.stats.shots = Infinity; LBT.addUnit(btank, 0,0); LBT.pathmap.removeBlocker(btank); btank.shape.hide(); @@ -109,7 +110,7 @@ function spawnBullet(x,y, atX,atY){ } while ( atX === x && atY === y); btank.setLocation(x,y); - return btank.fireProjectile(atX,atY); + return btank.shoot(atX,atY); } function updateBullets(n){ @@ -141,17 +142,18 @@ function randOpenLoc(){ // Update performance info periodically function updateInfo(){ - var loop = LBT.loop - , fps = loop.fps() - , n_units = LBT.units.size() - , n_projs = LBT.bullets.size() + var loop = LBT.loop + , fps = loop.fps() + , n_active = LBT.active.size() + , n_units = LBT.units.size() + , n_projs = LBT.bullets.size() ; $('#info [name=fps]').val( fps.toFixed(2) + " / " + loop.framerate ); $('#info [name=frame]').val( loop.frametime().toFixed(3)+" ms" ); $('#info #state').text( loop.running ? 'Running!' : ('Paused (tick '+TICKS+')') ); - $('#info [name=objects]').val( n_units+n_projs ); + $('#info [name=active]').val( n_active ); $('#info [name=units]').val( n_units ); $('#info [name=bullets]').val( n_projs ); diff --git a/src/tanks/thing/bullet.js b/src/tanks/thing/bullet.js index 86e6d8a..5a05fc9 100644 --- a/src/tanks/thing/bullet.js +++ b/src/tanks/thing/bullet.js @@ -20,6 +20,8 @@ Bullet = Thing.subclass('Bullet', { this.addEventListener('collide', self.onCollide.bind(this)); }, + + blocking : true, bounces : 0, bounceLimit : 1, diff --git a/src/tanks/thing/tank.js b/src/tanks/thing/tank.js index 8650a5c..ea3666a 100644 --- a/src/tanks/thing/tank.js +++ b/src/tanks/thing/tank.js @@ -19,14 +19,14 @@ Tank = Thing.subclass('Tank', { shots : 5 // max projectiles in the air at once }, + nShots : 0, + init : function init(align){ Thing.init.call(this, align); - this.bullets = new Y.YArray(); + this.onBulletDeath = this.onBulletDeath.bind(this); }, - filterTarget : function filterTarget(unit){ return unit.align !== this.align; }, - attack : function attack(unit){ var atk_cool = this.cooldowns.attr('attack'); if ( atk_cool.activate(NOW) ) @@ -34,18 +34,29 @@ Tank = Thing.subclass('Tank', { return this; }, - doAttack : function doAttack(target){ - this.fireProjectile(target); + doAttack : function doAttack(x,y){ + this.shoot(x,y); + return this; }, - fireProjectile : function fireProjectile(x,y){ + shoot : function shoot(x,y){ + if (this.nShots >= this.stats.shots) + return null; + var ProjectileType = this.projectile , p = new ProjectileType(this, x,y); - this.bullets.push(p); + + this.nShots++; + p.addEventListener('destroy', this.onBulletDeath); + this.game.addUnit(p).render(this.game.level); return p; }, + onBulletDeath : function onBulletDeath(evt){ + this.nShots--; + }, + getTurretLoc : function getTurretLoc(){ var loc = this.loc , barrel = this.barrel, bb = barrel.boundingBox diff --git a/src/tanks/ui/player.js b/src/tanks/ui/player.js index aa1be67..7bb3b03 100644 --- a/src/tanks/ui/player.js +++ b/src/tanks/ui/player.js @@ -61,8 +61,7 @@ Player = new Y.Class('Player', { var action = this.queue.shift(); if (action && action.type === 'fire') { - // console.log('fire', [action.x, action.y], 'loc', this.tank.loc) - this.tank.fireProjectile(action.x, action.y); + this.tank.shoot(action.x, action.y); } else if ( this.activeKeys.size() ) this.move(); -- 1.7.0.4