// -*- mode: JavaScript; tab-width: 4; indent-tabs-mode: nil; -*-
-Bullet = new Y.Class('Bullet', Thing, {
+Bullet = Thing.subclass('Bullet', {
traceTrajectories : true,
range : 0.1 // attack range (squares)
},
+ bounces : 0,
+
fillStats : function(){
this.stats = Y({},
Thing.fillStats(this.owner.stats),
move : function move(){
var to = this.trajectory.step( ELAPSED );
- this.game.moveAgentTo(this, to.x, to.y);
+ this.bounces = this.trajectory.bounces;
+ if (this.bounces > 1)
+ this.destroy();
+ else
+ this.game.moveAgentTo(this, to.x, to.y);
return this;
},
;
},
+
+ act : function act(){
+ if (this.tank.dead)
+ return this.tank;
+
+ 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);
+ } else if ( this.activeKeys.size() )
+ this.move();
+
+ return this.tank;
+ },
+
+ fire : function fire(){
+ var WIGGLE = 4
+
+ , tank = this.tank
+ , barrel = tank.barrel
+ , bb = tank.boundingBox
+ , w2 = bb.width/2, h2 = bb.height/2
+ , x0 = bb.x1+w2, y0 = bb.y1+h2
+
+ , theta = barrel.transform.rotate
+ , sin = Math.sin(theta), cos = Math.cos(theta)
+
+ , x1 = x0 + w2*cos, y1 = y0 + h2*sin
+ , sz = (barrel.boundingBox.width - w2)/2 + WIGGLE
+ , blockers = this.pathmap.get(x1-sz,y1-sz, x1+sz,y1+sz).remove(tank)
+ ;
+
+ if ( blockers.size() ) return; // console.log('squelch!', blockers);
+
+ this.queue.push({
+ type : 'fire',
+ x : x0 + REF_SIZE*cos,
+ y : y0 + REF_SIZE*sin
+ });
+ },
+
+ move : function move(){
+ var dir = this.activeKeys
+ .unique()
+ .sort()
+ .join(' ');
+
+ if (!dir) return;
+
+ var tank = this.tank
+ , toLoc = tank.loc.moveByDir(dir, (tank.stats.move * SQUARETH))
+
+ , x = toLoc.x, y = toLoc.y
+ , bb = tank.boundingBox.add(x,y)
+
+ , blockers = this.pathmap.get(bb.x1,bb.y1, bb.x2,bb.y2).remove(tank)
+ ;
+
+ if ( !blockers.size() )
+ this.game.moveAgentTo(tank, x,y);
+ },
+
+
+
+
updateMeta : function updateMeta(evt){
this.shift = evt.shiftKey;
this.alt = evt.altKey;
this.updateMeta(evt);
if (evt.which === Key.fire) {
- var theta = this.tank.barrel.transform.rotate
- , sin = Math.sin(theta), cos = Math.cos(theta)
- , bb = this.tank.boundingBox
- ;
- this.queue.push({
- type : 'fire',
- x : bb.x1+bb.width/2 + REF_SIZE*cos,
- y : bb.y1+bb.height/2 + REF_SIZE*sin
- });
+ this.fire();
return;
}
, theta = Math.atan2(-y,-x);
barrel.rotate(theta);
- },
-
-
-
- act : function act(){
- if (this.tank.dead)
- return this.tank;
-
- 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);
- } else if ( this.activeKeys.size() )
- this.move();
-
- return this.tank;
- },
-
- move : function move(){
- var dir = this.activeKeys
- .unique()
- .sort()
- .join(' ');
-
- if (!dir) return;
-
- var tank = this.tank
- , toLoc = tank.loc.moveByDir(dir, (tank.stats.move * SQUARETH))
-
- , x = toLoc.x, y = toLoc.y
- , bb = tank.boundingBox.add(x,y)
-
- , blockers = this.pathmap.get(bb.x1,bb.y1, bb.x2,bb.y2).remove(tank)
- ;
-
- if ( !blockers.size() )
- this.game.moveAgentTo(tank, x,y);
}
});
\ No newline at end of file