From 0f9815a49de19362437160b2231df4fd9de6fe0a Mon Sep 17 00:00:00 2001 From: dsc Date: Mon, 11 Apr 2011 01:41:08 -0700 Subject: [PATCH] Tower refactor to use Unit code. --- src/tanks/thing/tower.cjs | 170 ++++++++++++++------------------------------- 1 files changed, 51 insertions(+), 119 deletions(-) diff --git a/src/tanks/thing/tower.cjs b/src/tanks/thing/tower.cjs index 4b665dd..15bfa07 100644 --- a/src/tanks/thing/tower.cjs +++ b/src/tanks/thing/tower.cjs @@ -24,61 +24,55 @@ var Y = require('Y').Y Tower = exports['Tower'] = -Unit.subclass('Tower', function(Tower){ +Unit.subclass('Tower', { + isUnit : true, + isCombatant : true, + lootTable : '', + + colors : { + body : '#980011', + barrel : '#244792' + }, + + hasBarrel : true, + barrel: { + width : 'unit.width * 0.75', + height : 'unit.height / 6', + originX : 2, + originY : '50%', + x : '50%', + y : '50%' + }, + + // Bounding box + width : REF_SIZE*0.55, + height : REF_SIZE*0.55, + + // Attributes + stats : {}, + + // AI "Cooldowns" (max frequency of each action per sec) + ai : {}, + + projectile : 'bullet', + defaultProjectile : null, + + /// Instance /// + + align : null, + buffs : null, + + nShots : 0, - Y.core.descriptors(this, { - isUnit : true, - isCombatant : true, - lootTable : '', - - colors : { - body : '#980011', - barrel : '#244792' - }, - - hasBarrel : true, - barrel: { - width : 'unit.width * 0.75', - height : 'unit.height / 6', - originX : 2, - originY : '50%', - x : '50%', - y : '50%' - }, - - // Bounding box - width : REF_SIZE*0.55, - height : REF_SIZE*0.55, - - // Attributes - stats : {}, - - // AI "Cooldowns" (max frequency of each action per sec) - ai : {}, - - projectile : 'bullet', - defaultProjectile : null, - - /// Instance /// - - align : null, - buffs : null, - - nShots : 0 - }); - this['init'] = - function initTower(align){ + init : function initTower(align){ Unit.init.call(this, align); // this.atkGauge = new CooldownGauge(this.cooldowns.attack, this.width+1,this.height+1); - }; + }, - // Lootable.mixInto(Tower); - - this['act'] = - function act(elapsed, now){ + act : function act(elapsed, now){ var ai = this.ai, map = this.game.map; this.elapsed = elapsed; this.now = now; @@ -110,71 +104,12 @@ Unit.subclass('Tower', function(Tower){ this.components.invoke('act', elapsed, now); return this; - }; - - - /** - * Fires this agent's cannon. If a target location is omitted, the shot - * will be fired in the direction of the tank's current barrel rotation. - * - * @param {Number} [x] Target X coordinate. - * @param {Number} [y] Target Y coordinate. - */ - this['shoot'] = - function shoot(x,y){ - if ( this.nShots >= this.stats.shots.val || !this.cooldowns.attack.ready ) - return null; - - if (x instanceof Array) { y = x[_Y]; x = x[_X]; } - var xydef = (x !== undefined && y !== undefined); - if (xydef) - this.rotateBarrel(x,y); - - // Additional space on each side which must be clear around the - // shot to ensure we don't shoot ourself in the foot (literally) - var WIGGLE = 2 - , Projectile = this.projectile - , pw2 = Projectile.fn.width/2, ph2 = Projectile.fn.height/2 - - , tloc = this.getTurretLoc() - , tx = tloc.x, ty = tloc.y - - , x1 = tx - pw2 - WIGGLE, y1 = ty - ph2 - WIGGLE - , x2 = tx + pw2 + WIGGLE, y2 = ty + ph2 + WIGGLE - , blockers = this.game.map.get(x1,y1, x2,y2).filter(filterShoot, this) - ; - - if ( blockers.size() ) - return null; // console.log('squelch!', blockers); - - if (!xydef) { - var theta = this.barrelShape.transform.rotate - , sin = Math.sin(theta), cos = Math.cos(theta); - x = tx + REF_SIZE*cos; - y = ty + REF_SIZE*sin; - } - - this.cooldowns.attack.activate(this.now); - this.nShots++; - - var p = new Projectile(this, tx,ty, x,y); - p.on('destroy', this.onBulletDeath); - return p; - }; - function filterShoot(v){ - return (v !== this) - && (v.density === DensityType.BOUNDARY || (v.isWall && v.density === DensityType.DENSE)); - } + }, - this['ableToShoot'] = - function ableToShoot(){ - return this.nShots < this.stats.shots.val && this.cooldowns.attack.ready; - }; - this['getTurretLoc'] = - function getTurretLoc(){ + getTurretLoc : function getTurretLoc(){ var WIGGLE = 2 , loc = this.loc , barrel = this.barrelShape @@ -191,7 +126,7 @@ Unit.subclass('Tower', function(Tower){ , y = loc.y + len*sin ; return new Vec(x,y); - }; + }, @@ -202,8 +137,7 @@ Unit.subclass('Tower', function(Tower){ * Sets up unit appearance for minimal updates. Called once at start, * or when the world needs to be redrawn from scratch. */ - this['render'] = - function render(parent){ + render : function render(parent){ if (this.shape) this.shape.remove(); var colors = this.colors @@ -231,17 +165,15 @@ Unit.subclass('Tower', function(Tower){ ; return this; - }; + }, - this['rotateBarrel'] = - function rotateBarrel(x,y){ + rotateBarrel : function rotateBarrel(x,y){ this.barrelShape.rotate(this.angleTo(x,y)); return this; - }; + }, - this['rotateBarrelRelPage'] = - function rotateBarrelRelPage(pageX, pageY){ + rotateBarrelRelPage : function rotateBarrelRelPage(pageX, pageY){ var shape = this.shape , off = shape.offset() , w = this.width, h = this.height @@ -251,7 +183,7 @@ Unit.subclass('Tower', function(Tower){ ; this.barrelShape.rotate(theta); return this; - }; + } }); -- 1.7.0.4