Skip to content

Instantly share code, notes, and snippets.

@cfsghost
Created December 5, 2012 02:08
Show Gist options
  • Save cfsghost/4211485 to your computer and use it in GitHub Desktop.
Save cfsghost/4211485 to your computer and use it in GitHub Desktop.
Frextank
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
var self = this;
self.robot = robot;
self.members = {};
robot.clone();
};
Robot.prototype.onIdle = function(ev) {
var self = this;
var robot = ev.robot;
// Initializing all tanks
if (!self.members[robot.id]) {
self.members[robot.id] = {
scanning: true,
direction: (self.robot.id == robot.id) ? 0 : 1,
scanCount: 0
};
}
// Cannot find enemy for a long time, so trying to change direction
if (self.members[robot.id].scanCount == 9) {
self.members[robot.id].scanCount = 0;
self.members[robot.id].direction = !self.members[robot.id].scanCount;
} else {
self.members[robot.id].scanCount++;
}
if (self.members[robot.id].scanning) {
robot.turn(30);
if (self.members[robot.id].direction)
robot.ahead(65);
else
robot.back(65);
robot.rotateCannon(15);
}
};
Robot.prototype.onHitByBullet = function(ev) {
var self = this;
var robot = ev.robot;
// run away right now
robot.stop();
robot.fire();
robot.turn(30);
if (self.members[robot.id].direction)
robot.ahead(200);
else
robot.back(200);
self.members[robot.id].scanning = true;
};
Robot.prototype.onScannedRobot = function(ev) {
var self = this;
var robot = ev.robot;
if (!self.members[ev.scannedRobot.id]) {
robot.stop();
robot.fire();
robot.rotateCannon(-15);
if (self.members[robot.id].direction) {
// Just go stright to Enemy, and kill it
if (robot.cannonRelativeAngle >= 355 &&
robot.cannonRelativeAngle >= 0 &&
robot.cannonRelativeAngle <= 5) {
robot.turn(-30);
}
robot.ahead(50);
} else {
// Just go stright to Enemy, and kill it
if (robot.cannonRelativeAngle >= 175 &&
robot.cannonRelativeAngle <= 85) {
robot.turn(-30);
}
robot.back(50);
}
}
};
Robot.prototype.onRobotCollision = function(ev) {
var self = this;
var robot = ev.robot;
if (self.members[robot.id].direction)
robot.back(200);
else
robot.ahead(200);
self.members[robot.id].scanning = true;
};
Robot.prototype.onWallCollision = function(ev) {
var self = this;
var robot = ev.robot;
if (self.members[robot.id].direction)
robot.back(300);
else
robot.ahead(300);
self.members[robot.id].scanning = true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment