Created
December 3, 2012 17:10
-
-
Save ArtBIT/4196416 to your computer and use it in GitHub Desktop.
DOOM II
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MAX_SPEED = 100; | |
//FightCode can only understand your robot | |
//if its class is called Robot | |
var Robot = function(robot) { | |
this.pivot = {x:robot.arenaWidth/2, y:robot.arenaHeight/2, d:0}; | |
this.dir = 1; | |
this.speed = MAX_SPEED*.8; | |
}; | |
Robot.prototype.onIdle = function(ev) { | |
var robot = ev.robot; | |
robot.rotateCannon(180); | |
this.doMove(ev); | |
/* | |
if (Math.random()>0.5) { | |
robot.ahead(50); | |
} | |
else { | |
robot.back(50); | |
} | |
*/ | |
}; | |
Robot.prototype.strafe = function(ev) { | |
var robot = ev.robot; | |
var dx = this.pivot.x - robot.position.x; | |
var dy = this.pivot.y - robot.position.y; | |
var d = this.pivot.d = Math.sqrt(dx*dx+dy*dy); | |
var ang = (Math.atan2(dy, dx) / Math.PI) * 180; | |
var dang = closestAngle(ang,robot.angle); | |
robot.turn(dang); | |
this.doMove(ev); | |
robot.rotateCannon(-dang); | |
//robot.rotateCannon(closestAngle(ang, robot.cannonAbsoluteAngle)); | |
}; | |
function closestAngle(a, b) { | |
if (Math.abs(b-a) < 180) { | |
return b-a; | |
} | |
if (b>a) { | |
return b-a-360; | |
} | |
return b-a+360; | |
}; | |
Robot.prototype.doMove = function(ev) { | |
var robot = ev.robot; | |
this.speed += (this.speed - (MAX_SPEED/2))/8; | |
var dist = this.speed * this.dir; | |
if (dist > 0) { | |
robot.ahead(dist); | |
} else { | |
robot.back(-dist); | |
} | |
}; | |
Robot.prototype.closeIn = function(ev) { | |
this.pivot.x = ev.scannedRobot.position.x; | |
this.pivot.y = ev.scannedRobot.position.y; | |
}; | |
Robot.prototype.onScannedRobot = function(ev) { | |
var robot = ev.robot; | |
robot.stop(); | |
robot.fire(); | |
this.closeIn(ev); | |
this.strafe(ev); | |
}; | |
Robot.prototype.onHitByBullet = function(ev) { | |
var robot = ev.robot; | |
this.dir *= -1; | |
this.speed = MAX_SPEED; | |
if (robot.gunCoolDownTime) { | |
this.doMove(ev); | |
} | |
if (robot.life < 80) { | |
robot.clone(); | |
} | |
}; | |
Robot.prototype.onWallCollision = function(ev) { | |
this.dir *= -1; | |
this.doMove(ev); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment