Enforces shot-limit.
authordsc <david.schoonover@gmail.com>
Thu, 18 Nov 2010 07:48:51 +0000 (23:48 -0800)
committerdsc <david.schoonover@gmail.com>
Thu, 18 Nov 2010 07:48:51 +0000 (23:48 -0800)
index.php
src/tanks/game/game.js
src/tanks/main.js
src/tanks/thing/bullet.js
src/tanks/thing/tank.js
src/tanks/ui/player.js

index 66277c1..9499a94 100644 (file)
--- a/index.php
+++ b/index.php
@@ -23,7 +23,7 @@
     <li><label for="info_fps"   >fps</label>      <input id="info_fps"     name="fps"     value="" type="text"></li>
     <li><label for="info_frame" >frame</label>    <input id="info_frame"   name="frame"   value="" type="text"></li>
     <li><div class="sep"></div></li>
-    <li><label for="info_objects">objects</label> <input id="info_objects" name="objects" value="" type="text"></li>
+    <li><label for="info_active" >active</label>  <input id="info_active"  name="active"  value="" type="text"></li>
     <li><label for="info_units"  >units</label>   <input id="info_units"   name="units"   value="" type="text"></li>
     <li><label for="info_bullets">bullets</label> <input id="info_bullets" name="bullets" value="" type="text"></li>
 </ul>
index a26567d..5b7acfe 100644 (file)
@@ -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);
index c8b3929..ae192e0 100644 (file)
@@ -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 );
     
index 86e6d8a..5a05fc9 100644 (file)
@@ -20,6 +20,8 @@ Bullet = Thing.subclass('Bullet', {
         
         this.addEventListener('collide', self.onCollide.bind(this));
     },
+    
+    
     blocking    : true,
     bounces     : 0,
     bounceLimit : 1,
index 8650a5c..ea3666a 100644 (file)
@@ -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
index aa1be67..7bb3b03 100644 (file)
@@ -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();