Skip to content

Instantly share code, notes, and snippets.

@dejw
Created December 4, 2012 12:23
Show Gist options
  • Select an option

  • Save dejw/4203259 to your computer and use it in GitHub Desktop.

Select an option

Save dejw/4203259 to your computer and use it in GitHub Desktop.
The Crushinator
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
this.enemyPos = null;
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
robot.ahead(50);
robot.turn(15);
var pos = robot.position;
var enemyPos = this.enemyPos;
if(enemyPos != null){
//enemyPos = {x:pos.x+100, y:pos.y};
var enemyAngle = Math.atan2(enemyPos.y - pos.y, enemyPos.x - pos.x);
enemyAngle = enemyAngle * 180 / 3.14159 + 180;
robot.log(enemyAngle);
robot.log(robot.cannonAbsoluteAngle);
var amount = enemyAngle - robot.cannonAbsoluteAngle;
if(amount > 180) {
amount -= 180;
}
if (amount > 0) {
amount += 10;
} else {
amount -= 10;
}
robot.rotateCannon(amount);
} else {
robot.rotateCannon(90);
}
};
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot;
robot.fire();
this.enemyPos = ev.scannedRobot.position;
};
Robot.prototype.onHitByBullet = function(ev) {
var robot;
robot = ev.robot;
robot.turn(robot.cannonRelativeAngle - ev.bulletBearing);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment