From: dsc Date: Thu, 31 Mar 2011 10:31:08 +0000 (-0700) Subject: Adds Unit base class X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=596c9a0dbed2681efc396ab576fe36de41d7723f;p=tanks.git Adds Unit base class --- diff --git a/src/tanks/game.cjs b/src/tanks/game.cjs index a213ba9..fabbc25 100644 --- a/src/tanks/game.cjs +++ b/src/tanks/game.cjs @@ -201,11 +201,11 @@ evt.subclass('Game', { if ( !this.byId[unit.__id__] ) { this.byId[unit.__id__] = unit; - if (unit.active) + if (unit.isActive) this.active.push(unit); if (unit.isProjectile) this.bullets.push(unit); - if (unit.isCombatant) + if (unit.isUnit) this.units.push(unit); if (unit.tick) this.toTick.push(unit); @@ -250,13 +250,13 @@ evt.subclass('Game', { delete this.byId[unit.__id__]; this.map.removeBlocker(unit); - if (unit.active) + if (unit.isActive) this.active.remove(unit); if (unit.tick) this.toTick.remove(unit); if (unit.isProjectile) this.bullets.remove(unit); - if (unit.isCombatant) + if (unit.isUnit) this.units.remove(unit); return unit; diff --git a/src/tanks/map/pathing/index.cjs b/src/tanks/map/pathing/index.cjs index 4eb559e..ea3502a 100644 --- a/src/tanks/map/pathing/index.cjs +++ b/src/tanks/map/pathing/index.cjs @@ -9,6 +9,6 @@ require('Y').Y .extend(exports, { 'Map' : require('tanks/map/pathing/map').Map, 'Traversal' : require('tanks/map/pathing/traversal').Traversal, - 'LinearTrajectory' : require('tanks/map/pathing/linear-trajectory').LinearTrajectory, + 'LinearTrajectory' : require('tanks/map/pathing/lineartrajectory').LinearTrajectory, 'CircularTrajectory' : require('tanks/map/pathing/circular-trajectory').CircularTrajectory }); diff --git a/src/tanks/map/pathing/linear-trajectory.cjs b/src/tanks/map/pathing/lineartrajectory.cjs similarity index 100% rename from src/tanks/map/pathing/linear-trajectory.cjs rename to src/tanks/map/pathing/lineartrajectory.cjs diff --git a/src/tanks/map/pathing/map-searching.cjs b/src/tanks/map/pathing/map-searching.cjs index 1650944..f3cfa86 100644 --- a/src/tanks/map/pathing/map-searching.cjs +++ b/src/tanks/map/pathing/map-searching.cjs @@ -7,7 +7,7 @@ var Y = require('Y').Y , manhattan = vec.manhattan , Map = require('tanks/map/pathing/map').Map -, LinearTrajectory = require('tanks/map/pathing/linear-trajectory').LinearTrajectory +, LinearTrajectory = require('tanks/map/pathing/lineartrajectory').LinearTrajectory , Bullet = require('tanks/thing/bullet').Bullet , _X = 0, _Y = 1 @@ -86,11 +86,11 @@ function nearBulletFilter(agent){ function nearEnemyFilter(agent){ var me = this; // Runs in the context of the 'me' unit; @see this.findNearLike() - return agent.isCombatant && agent.align !== me.align; + return agent.isUnit && agent.align !== me.align; } function nearEnemyInSightFilter(agent){ var me = this; // Runs in the context of the 'me' unit; @see this.findNearLike() - return ( agent.isCombatant && agent.align !== me.align && + return ( agent.isUnit && agent.align !== me.align && new LinearTrajectory(me, me.loc, agent.loc).canSee(agent) ); } diff --git a/src/tanks/thing/bullet.cjs b/src/tanks/thing/bullet.cjs index 3f1a565..fe0381c 100644 --- a/src/tanks/thing/bullet.cjs +++ b/src/tanks/thing/bullet.cjs @@ -13,8 +13,8 @@ var Y = require('Y').Y , stat = require('tanks/effects/stat') , Explosion = require('tanks/fx/explosion').Explosion , Thing = require('tanks/thing/thing').Thing -, LinearTrajectory = require('tanks/map/pathing/linear-trajectory').LinearTrajectory , Traversal = require('tanks/map/pathing/traversal').Traversal +, LinearTrajectory = require('tanks/map/pathing/lineartrajectory').LinearTrajectory , @@ -35,7 +35,8 @@ Thing.subclass('Bullet', { // Instance blocking : BoundsType.BLOCKING, - isRenderable : true, + + isCombatant : true, isProjectile : true, bounces : 0, @@ -44,6 +45,7 @@ Thing.subclass('Bullet', { width : 6, height : 6, + isRenderable : true, color: '#FFF6AE', stats : { diff --git a/src/tanks/thing/index.cjs b/src/tanks/thing/index.cjs index d4fd8a5..b6bf07d 100644 --- a/src/tanks/thing/index.cjs +++ b/src/tanks/thing/index.cjs @@ -6,7 +6,7 @@ var Y = require('Y').Y , tank = require('tanks/thing/tank') , thing = require('tanks/thing/thing') , tower = require('tanks/thing/tower') -// , unit = require('tanks/thing/unit') +, unit = require('tanks/thing/unit') ; Y.core.extend(exports, { 'Bullet' : bullet.Bullet, @@ -16,5 +16,5 @@ Y.core.extend(exports, { 'Tank' : tank.Tank, 'Thing' : thing.Thing, 'Tower' : tower.Tower, - // 'Unit' : unit.Unit + 'Unit' : unit.Unit }); diff --git a/src/tanks/thing/shield.cjs b/src/tanks/thing/shield.cjs index b2a00b9..fad604b 100644 --- a/src/tanks/thing/shield.cjs +++ b/src/tanks/thing/shield.cjs @@ -20,6 +20,7 @@ Component.subclass('Shield', { // Instance blocking : BoundsType.IRREGULAR, // Need to allow shields etc to passthrough like-aligned units + isCombatant : true, isComponent : true, isShield : true, diff --git a/src/tanks/thing/tank.cjs b/src/tanks/thing/tank.cjs index 1cdca54..bed6e6d 100644 --- a/src/tanks/thing/tank.cjs +++ b/src/tanks/thing/tank.cjs @@ -12,9 +12,9 @@ var Y = require('Y').Y , constants = require('tanks/constants') , BoundsType = constants.BoundsType , DensityType = constants.DensityType -, LinearTrajectory = require('tanks/map/pathing/linear-trajectory').LinearTrajectory +, LinearTrajectory = require('tanks/map/pathing/lineartrajectory').LinearTrajectory , Traversal = require('tanks/map/pathing/traversal').Traversal -, Thing = require('tanks/thing/thing').Thing +, Unit = require('tanks/thing/unit').Unit , Bullet = require('tanks/thing/bullet').Bullet , Lootable = require('tanks/mixins/lootable').Lootable , Barrel = require('tanks/fx/barrel').Barrel @@ -25,7 +25,7 @@ var Y = require('Y').Y Tank = exports['Tank'] = -Thing.subclass('Tank', function(Tank){ +Unit.subclass('Tank', function(Tank){ Y.core.descriptors(this, { isUnit : true, @@ -76,7 +76,7 @@ Thing.subclass('Tank', function(Tank){ this['init'] = function initTank(align){ - Thing.init.call(this, align); + Unit.init.call(this, align); this.projectile = this.defaultProjectile = Bullet.lookup(this.projectile); this.colors = Y.extend({}, this.colors); this.atkGauge = new CooldownGauge(this.cooldowns.attack, this.width+1,this.height+1); @@ -294,27 +294,6 @@ Thing.subclass('Tank', function(Tank){ }; - this['getTurretLoc'] = - function getTurretLoc(){ - var WIGGLE = 2 - , loc = this.loc - , barrel = this.barrelShape - - , theta = barrel.transform.rotate - , sin = Math.sin(theta), cos = Math.cos(theta) - - // sqrt(2)/2 * (P.width + WIGGLE) - // is max diagonal to ensure we don't overlap with the firing unit - , pw = this.projectile.fn.width - , len = barrel.bbox.width + 0.707*(pw+WIGGLE) - - , x = loc.x + len*cos - , y = loc.y + len*sin - ; - return new Vec(x,y); - }; - - /// Rendering Methods /// @@ -355,8 +334,6 @@ Thing.subclass('Tank', function(Tank){ new Barrel(b.width,b.height, b.originX,b.originY) .appendTo( this.shape ) // have to append early to avoid problems with relative positioning .position(b.x, b.y) - // .position(w2-2, h2-bh/2) - // .origin(2, bh/2) .fill( colors.barrel ) ; return this; @@ -374,12 +351,35 @@ Thing.subclass('Tank', function(Tank){ return this; }; + + this['getTurretLoc'] = + function getTurretLoc(){ + var WIGGLE = 2 + , loc = this.loc + , barrel = this.barrelShape + + , theta = barrel.transform.rotate + , sin = Math.sin(theta), cos = Math.cos(theta) + + // sqrt(2)/2 * (P.width + WIGGLE) + // is max diagonal to ensure we don't overlap with the firing unit + , pw = this.projectile.fn.width + , len = barrel.bbox.width + 0.707*(pw+WIGGLE) + + , x = loc.x + len*cos + , y = loc.y + len*sin + ; + return new Vec(x,y); + }; + + this['rotateBarrel'] = function rotateBarrel(x,y){ this.barrelShape.rotate(this.angleTo(x,y)); return this; }; + // FIXME: delegate to Barrel this['rotateBarrelRelPage'] = function rotateBarrelRelPage(pageX, pageY){ var shape = this.shape diff --git a/src/tanks/thing/thing.cjs b/src/tanks/thing/thing.cjs index 9a04e02..4982eea 100644 --- a/src/tanks/thing/thing.cjs +++ b/src/tanks/thing/thing.cjs @@ -64,10 +64,11 @@ new evt.Class('Thing', { isThing : true, isUnit : false, isCombatant : false, - isWall : false, isProjectile : false, + isWall : false, isAccessory : false, isComponent : false, + isShield : false, isActive : true, // Agent takes actions? isRenderable : false, // Agent will present itself for rendering when ready // FIXME: stupid hack diff --git a/src/tanks/thing/tower.cjs b/src/tanks/thing/tower.cjs index 393dc7d..b8ca0cc 100644 --- a/src/tanks/thing/tower.cjs +++ b/src/tanks/thing/tower.cjs @@ -11,9 +11,9 @@ var Y = require('Y').Y , constants = require('tanks/constants') , BoundsType = constants.BoundsType , DensityType = constants.DensityType -, LinearTrajectory = require('tanks/map/pathing/linear-trajectory').LinearTrajectory +, LinearTrajectory = require('tanks/map/pathing/lineartrajectory').LinearTrajectory , Traversal = require('tanks/map/pathing/traversal').Traversal -, Thing = require('tanks/thing/thing').Thing +, Unit = require('tanks/thing/unit').Unit , Bullet = require('tanks/thing/bullet').Bullet , Lootable = require('tanks/mixins/lootable').Lootable , Barrel = require('tanks/fx/barrel').Barrel @@ -24,7 +24,7 @@ var Y = require('Y').Y Tower = exports['Tower'] = -Thing.subclass('Tower', function(Tower){ +Unit.subclass('Tower', function(Tower){ Y.core.descriptors(this, { isCombatant : true, @@ -67,7 +67,7 @@ Thing.subclass('Tower', function(Tower){ this['init'] = function initTower(align){ - Thing.init.call(this, align); + Unit.init.call(this, align); this.projectile = this.defaultProjectile = Bullet.lookup(this.projectile); this.colors = Y.extend({}, this.colors); this.atkGauge = new CooldownGauge(this.cooldowns.attack, this.width+1,this.height+1); @@ -211,9 +211,7 @@ Thing.subclass('Tower', function(Tower){ if (this.shape) this.shape.remove(); var colors = this.colors - , w = this.width, w2 = w/2 - , h = this.height, h2 = h/2 - , r = w / 2 + , r = this.width / 2 ; this.shape = @@ -233,9 +231,8 @@ Thing.subclass('Tower', function(Tower){ new Barrel(b.width,b.height, b.originX,b.originY) .appendTo( this.shape ) // have to append early to avoid problems with relative positioning .position(b.x, b.y) - // .position(w2-2, h2-bh/2) - // .origin(2, bh/2) - .fill( colors.barrel ) ; + .fill( colors.barrel ) + ; return this; }; diff --git a/src/tanks/thing/unit.cjs b/src/tanks/thing/unit.cjs index e69de29..ea00ed2 100644 --- a/src/tanks/thing/unit.cjs +++ b/src/tanks/thing/unit.cjs @@ -0,0 +1,38 @@ +var Y = require('Y').Y +, Thing = require('tanks/thing/thing').Thing +, LinearTrajectory = require('tanks/map/pathing/lineartrajectory').LinearTrajectory +, Traversal = require('tanks/map/pathing/traversal').Traversal +, + +Unit = +exports['Unit'] = +Thing.subclass('Unit', { + isUnit : true, + isCombatant : true, + + nShots : 0, + + + init : function initUnit(align){ + Thing.init.call(this, align); + }, + + ableToShoot : function ableToShoot(){ + return this.nShots < this.stats.shots.val && this.cooldowns.attack.ready; + }, + + move : function move(x,y){ + if (x instanceof Array) { y=x[_Y]; x=x[_X]; } + + var loc = this.loc; + this.trajectory = new LinearTrajectory(this, loc.x,loc.y, x,y, this.movePerMs()); + + var tvsl = new Traversal(this) + , to = tvsl.traverse(this.elapsed, x,y) ; + + this.game.moveThingTo(this, to.x,to.y); + + return this; + } +}) +;