From c92877e83327f583ca557a6fc17dbdd5248cedc0 Mon Sep 17 00:00:00 2001 From: dsc Date: Mon, 13 Dec 2010 05:57:00 -0800 Subject: [PATCH] Fixes speedy player tank bug. --- src/tanks/config.cjs | 2 +- src/tanks/game.cjs | 4 +- src/tanks/thing/bullet.cjs | 5 ---- src/tanks/thing/player.cjs | 44 +++++++++++++++++++--------------------- src/tanks/thing/tank.cjs | 48 +++++++++++++++++++++++++------------------ 5 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/tanks/config.cjs b/src/tanks/config.cjs index 5d10793..2518524 100644 --- a/src/tanks/config.cjs +++ b/src/tanks/config.cjs @@ -7,7 +7,7 @@ exports['defaults'] = { createGridCanvas : 1, createGridTable : 0, showGridCoords : 0, - showCountdown : 1 + showCountdown : 0 }, pathing : { overlayAIPaths : 0, diff --git a/src/tanks/game.cjs b/src/tanks/game.cjs index af6ccf8..6cf740c 100644 --- a/src/tanks/game.cjs +++ b/src/tanks/game.cjs @@ -47,7 +47,7 @@ Y.subclass('Game', { .appendTo(this.root); this.pathmap = this.level.pathmap; - Thing.addEventListener('create', this.addUnit); + Thing.addEventListener('init', this.addUnit); Thing.addEventListener('destroy', this.killUnit); this.addEventListener('tick', this.tick); @@ -56,7 +56,7 @@ Y.subclass('Game', { }, destroy : function destroy(){ - Thing.removeEventListener('create', this.addUnit); + Thing.removeEventListener('init', this.addUnit); Thing.removeEventListener('destroy', this.killUnit); this.stop(); this.resetGlobals(); diff --git a/src/tanks/thing/bullet.cjs b/src/tanks/thing/bullet.cjs index d609c96..e6f3992 100644 --- a/src/tanks/thing/bullet.cjs +++ b/src/tanks/thing/bullet.cjs @@ -147,11 +147,6 @@ Thing.subclass('Bullet', { this.shape.layer.attr('title', ''+loc); return this; - }, - - toString : function toString(){ - // return this.className+'(loc='+this.loc+', traj='+this.trajectory+', owner='+this.owner+')'; - return this.className+'(loc='+this.loc+')'; } }); diff --git a/src/tanks/thing/player.cjs b/src/tanks/thing/player.cjs index 3e143d7..381fe66 100644 --- a/src/tanks/thing/player.cjs +++ b/src/tanks/thing/player.cjs @@ -17,10 +17,8 @@ Tank.subclass('PlayerTank', { // Attributes stats: { hp : 1, // health - - move : 0.75, // move speed (squares/sec) + move : 0.75, // move speed (squares/sec) rotate : HALF_PI, // rotation speed (radians/sec) - power : 1, // attack power speed : 0.5, // attack cool (sec) shots : 5 // max projectiles in the air at once @@ -88,28 +86,28 @@ Tank.subclass('PlayerTank', { if (!dir) return; // @see Tank.move() - this.move(this.loc.moveByDir(dir, REF_SIZE)); + this.move(this.loc.moveByDir(dir, this.stats.move * SQUARETH)); }, - _move : function move(){ - var dir = this.activeKeys - .unique() - .sort() - .join(' '); - - if (!dir) return; - - var toLoc = this.loc.moveByDir(dir, (this.stats.move * SQUARETH)) - - , x = toLoc.x, y = toLoc.y - , bb = this.boundingBox.relocated(x,y) - - , blockers = this.game.pathmap.get(bb.x1,bb.y1, bb.x2,bb.y2).remove(this) - ; - - if ( !blockers.size() ) - this.game.moveUnitTo(this, x,y); - }, + // _move : function _move(){ + // var dir = this.activeKeys + // .unique() + // .sort() + // .join(' '); + // + // if (!dir) return; + // + // var toLoc = this.loc.moveByDir(dir, (this.stats.move * SQUARETH)) + // + // , x = toLoc.x, y = toLoc.y + // , bb = this.boundingBox.relocated(x,y) + // + // , blockers = this.game.pathmap.get(bb.x1,bb.y1, bb.x2,bb.y2).remove(this) + // ; + // + // if ( !blockers.size() ) + // this.game.moveUnitTo(this, x,y); + // }, diff --git a/src/tanks/thing/tank.cjs b/src/tanks/thing/tank.cjs index ec1eeee..2613072 100644 --- a/src/tanks/thing/tank.cjs +++ b/src/tanks/thing/tank.cjs @@ -42,7 +42,7 @@ Thing.subclass('Tank', function(Tank){ // AI "Cooldowns" (max frequency of each action per sec) ai : { - path : 1.5, // calculate a path to enemy + path : 1.0, // calculate a path to enemy dodge : 1.0, // dodge an incoming bullet shootIncoming : 0.5, // shoot down incoming bullet shootEnemy : 0.75 // shoot at enemy tank if in range @@ -227,7 +227,7 @@ Thing.subclass('Tank', function(Tank){ */ this['shoot'] = function shoot(x,y){ - var WIGGLE = 4 // additional space which must be clear in front of the barrel + var WIGGLE = 3 // additional space which must be clear in front of the barrel , xydef = (x !== undefined && y !== undefined) ; @@ -236,26 +236,34 @@ Thing.subclass('Tank', function(Tank){ if ( this.nShots >= this.stats.shots || !this.cooldowns.attack.activate(NOW) ) return null; - var barrel = this.barrel - , mid = this.midpoint - , bb = this.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.game.pathmap.get(x1-sz,y1-sz, x1+sz,y1+sz).remove(this) + var tloc = this.getTurretLoc() + , tx = tloc.x, ty = tloc.y + , x1 = tx - WIGGLE, y1 = ty - WIGGLE + , x2 = tx + WIGGLE, y2 = ty + WIGGLE + , blockers = this.game.pathmap.get(x1,y1, x1,y1).remove(this) + + // var barrel = this.barrel + // , loc = this.loc + // , bb = this.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.game.pathmap.get(x1-sz,y1-sz, x1+sz,y1+sz).remove(this) ; if ( blockers.size() ) return null; // console.log('squelch!', blockers); if (!xydef) { - x = x0 + REF_SIZE*cos; - y = y0 + REF_SIZE*sin; + var theta = this.barrel.transform.rotate + , sin = Math.sin(theta), cos = Math.cos(theta); + x = tx + REF_SIZE*cos; + y = ty + REF_SIZE*sin; } var ProjectileType = this.projectile @@ -353,16 +361,16 @@ Thing.subclass('Tank', function(Tank){ this['getTurretLoc'] = function getTurretLoc(){ - var WIGGLE = 8 - , loc = this.loc, mid = this.midpoint + var WIGGLE = 9 // 1.4 * 6 for max diagonal + , loc = this.loc , barrel = this.barrel , theta = barrel.transform.rotate , sin = Math.sin(theta), cos = Math.cos(theta) , len = barrel.boundingBox.width + WIGGLE - , x = mid.x + len*cos - , y = mid.y + len*sin + , x = loc.x + len*cos + , y = loc.y + len*sin ; // console.log('getTurretLoc()', 'loc:', loc, '(x,y):', [x,y]); return new Vec(x,y); -- 1.7.0.4