Skip to content

Instantly share code, notes, and snippets.

@mdonahoo
Created December 3, 2012 23:17
Show Gist options
  • Save mdonahoo/4198995 to your computer and use it in GitHub Desktop.
Save mdonahoo/4198995 to your computer and use it in GitHub Desktop.
Zigzagger
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
this.movementFn = 'ahead';
this.turnMultiplier = 1;
this.cannonTurn = 45;
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
robot[this.movementFn](50);
robot.turn(90 * this.turnMultiplier);
robot.rotateCannon(this.cannonTurn * this.turnMultiplier);
if (this.cannonTurn == 45) {
this.cannonTurn = -90;
} else {
this.cannonTurn = 45;
}
this.turnMultiplier *= -1;
};
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot;
robot.stop();
robot.fire();
};
Robot.prototype.onRobotCollision = function(ev) {
var robot = ev.robot;
robot.turn(90 - ev.bulletBearing);
};
Robot.prototype.onWallCollision = function(ev) {
var robot = ev.robot;
robot.stop();
robot.turn(ev.bearing);
if (this.movementFn == 'ahead') {
this.movementFn = 'back';
} else {
this.movementFn = 'ahead';
}
robot.rotateCannon(360);
};
Robot.prototype.onHitByBullet = function(ev) {
var robot = ev.robot;
robot.turn(90 - ev.bulletBearing);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment