Skip to content

Instantly share code, notes, and snippets.

@loktar00
Created December 3, 2012 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loktar00/4196687 to your computer and use it in GitHub Desktop.
Save loktar00/4196687 to your computer and use it in GitHub Desktop.
Grunt
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
this.rotate = 1;
this.dist = 0;
this.lastAngle = 0;
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
robot.turn(this.rotate);
this.lastAngle = this.rotate;
};
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot;
var turnAngle = this.getAngle(robot.position, ev.scannedRobot.position);
if (turnAngle < -180){
turnAngle += 360;
}else if(turnAngle > 180){
turnAngle -= 360;
}
this.rotate = robot.cannonAbsoluteAngle - turnAngle
this.dist = this.getDist(robot.position, ev.scannedRobot.position);
robot.fire();
robot.ahead(this.dist/2);
};
Robot.prototype.onHitByBullet = function(ev) {
var robot = ev.robot;
this.rotate = ev.bearing;
robot.turn(ev.bearing/2);
robot.ahead(15);
};
Robot.prototype.getAngle = function(a,b){
var tx = b.x - a.x,
ty = b.y - a.y,
rad = Math.atan2(ty,tx),
angle = (rad/Math.PI * 180);
return angle;
}
Robot.prototype.getDist = function(a,b){
return Math.sqrt((b.x - a.x) *(b.x - a.x) + (b.y - a.y) * (b.y - a.y));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment