From: dsc Date: Thu, 18 Nov 2010 01:47:06 +0000 (-0800) Subject: Adds support for firing with spacebar. X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=0eb9aef50e29677094c058eb4997ca0cf9b18c4f;p=tanks.git Adds support for firing with spacebar. --- diff --git a/notes.md b/notes.md index 26c0f5a..de1c16c 100644 --- a/notes.md +++ b/notes.md @@ -1,4 +1,5 @@ # Bugs +- Turret can fire through walls # TODOs - Move game objects into namespace `tanks` @@ -12,3 +13,31 @@ - Should use unit vectors for trajectories? --> matrix math for reflections? + +# The Tank AI Challenge + +Your goal is to design an AI tank which does its best to: 1) stay alive; 2) kill all enemy tanks. + +Rules: +- All tanks have 1hp. +- Tanks shoot bullets. Bullets explode on contact with tanks and other bullets, and bounce once on contact with a wall. +- Each tank can have up to 5 bullets in the air at a time. + +Each tick, you get a bunch of inputs: +- A pathmap of the level (which you can ask whether an area is passable) +- A list of friendly and enemy units (including bullets), each with its attributes (trajectory, position, hp, speed, power, shots, etc) +- Your attributes (position, hp, speed, power, etc) + +Ticks occur 30 times per second. When called, you'll return one action, which can be: +- Move toward (x,y) +- Fire a projectile toward (x,y) +- Do nothing +That is all. + +An AI script looks like a list of ordered (test, action) pairs. The game will consider each test in turn, and the first matching will be the action taken. Here's an example: + +- If there is a bullet that will collide with me in less than 15 ticks, shoot a bullet at its location. +- If there is a bullet that will collide with me in less than 30 ticks, move perpendicular to its trajectory. +- If there is an enemy within 100 pixels, shoot a bullet at it. +- Finally, move toward the closest enemy tank. + diff --git a/src/portal/math/vec.js b/src/portal/math/vec.js index dbab10f..7debadf 100644 --- a/src/portal/math/vec.js +++ b/src/portal/math/vec.js @@ -58,6 +58,13 @@ math.Vec = new Y.Class('Vec', [], { return this.x*b.x + this.y*b.y; }, + rotate : function rotate(theta){ + var sin = Math.sin(theta) + , cos = Math.cos(theta) + , x = this.x, y = this.y ; + return this.setXY(x*cos - y*sin, x*sin + y*cos); + }, + toString : function toString(){ var p = 2, x = this.x, y = this.y; x = ((x % 1 !== 0) ? x.toFixed(p) : x); diff --git a/src/tanks/config.js b/src/tanks/config.js index 1b268a0..0588ebb 100644 --- a/src/tanks/config.js +++ b/src/tanks/config.js @@ -5,6 +5,6 @@ tanks.config = { traceTrajectories : false }, debug : { - projectiles : 10 + projectiles : 0 } }; \ No newline at end of file diff --git a/src/tanks/thing/tank.js b/src/tanks/thing/tank.js index c65ab5c..2d6314b 100644 --- a/src/tanks/thing/tank.js +++ b/src/tanks/thing/tank.js @@ -49,8 +49,8 @@ Tank = new Y.Class('Tank', Thing, { , barrel = this.barrel, bb = barrel.boundingBox , theta = barrel.transform.rotate, sin = Math.sin(theta), cos = Math.cos(theta) , x0 = bb.x2-bb.x1, y0 = (bb.y2-bb.y1)/2 - , x = loc.x + bb.x1 + x0*cos - y0*sin - , y = loc.y + bb.y1 + x0*sin + y0*cos + , x = loc.x + bb.x1 + x0*cos - y0*sin + 3 + , y = loc.y + bb.y1 + x0*sin + y0*cos + 3 ; // console.log('getTurretLoc()', 'loc:', loc, 'bb.(x2,y2):', [bb.x2,bb.y2], '(x,y):', [x,y]); return new math.Vec(x,y); diff --git a/src/tanks/ui/player.js b/src/tanks/ui/player.js index 6db2f20..841d7f1 100644 --- a/src/tanks/ui/player.js +++ b/src/tanks/ui/player.js @@ -60,6 +60,21 @@ Player = new Y.Class('Player', { }, keydown : function keydown(evt){ + 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 + }); + return; + } + var dir = Key.getDir(evt.which) , inv = Key.getInverseDir(evt.which); @@ -67,7 +82,6 @@ Player = new Y.Class('Player', { this.activeKeys.remove(inv); this.activeKeys.push(dir); } - this.updateMeta(evt); }, keyup : function keyup(evt){ @@ -122,9 +136,10 @@ Player = new Y.Class('Player', { var action = this.queue.shift(); - if (action && action.type === 'fire') + 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() ) + } else if ( this.activeKeys.size() ) this.move(); return this.tank;