Skip to content

Instantly share code, notes, and snippets.

@f6p
Created December 5, 2012 20:40
Show Gist options
  • Save f6p/4219294 to your computer and use it in GitHub Desktop.
Save f6p/4219294 to your computer and use it in GitHub Desktop.
NecroBadger
// helpers
function areEnemies(robot, sighted) {
var sightedIsChild = (robot.id == sighted.parentId);
var sightedIsParent = (robot.parentId == sighted.id);
return !(sightedIsChild || sightedIsParent);
};
function baseStep(robot) {
return 9 * direction(robot);
};
function charge(robot) {
robot.ahead(180);
};
function direction(robot) {
return isParent(robot) ? 1 : -1;
};
function fallback(robot) {
robot.ignore('onHitByBullet');
robot.back(180);
robot.listen('onHitByBullet');
};
function isParent(robot) {
return robot.parentId ? true : false;
};
function random(max) {
return parseInt(Math.random() * max);
};
function seek(robot) {
var step = baseStep(robot);
robot.rotateCannon(step);
robot.turn(step);
robot.ahead(3 * step);
};
function shootRandomly(robot, max) {
var adj = random(max * 2) - max;
robot.fire();
robot.rotateCannon(adj);
robot.fire();
return adj;
};
// robot
var Robot = function(robot) {
robot.clone();
};
Robot.prototype.onIdle = function(ev) {
seek(ev.robot);
};
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot;
var scanned = ev.scannedRobot;
if (areEnemies(robot, scanned)) {
var adj = -36 * direction(robot);
for (i = 1; i < 3; i++)
shootRandomly(robot, 9 * i);
robot.rotateCannon(adj);
}
};
Robot.prototype.onWallCollision = function(ev) {
fallback(ev.robot);
};
Robot.prototype.onHitByBullet = function(ev) {
charge(ev.robot);
};
Robot.prototype.onRobotCollision = function(ev) {
fallback(ev.robot);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment