Skip to content

Instantly share code, notes, and snippets.

@marano
Created December 6, 2012 04:09
Show Gist options
  • Save marano/4221712 to your computer and use it in GitHub Desktop.
Save marano/4221712 to your computer and use it in GitHub Desktop.
Shrimp Hunter 2000
var aVera = true;
var Robot = function(robot) {};
var RobotState = function (rotateStep) {
data = {};
data.id = 0;
data.isLookingAtTarget = false;
data.timesShooting = 0;
data.rotateStep = rotateStep;
return data;
};
var goodRobot = RobotState(1);
var evilRobot = RobotState(-1);
var clonedEvilMe = false;
var currentRobotData = function (robot) {
if (robot.parentId == null) {
return goodRobot;
} else {
return evilRobot;
}
};
var robotOnIdle = function (robot) {
var robotData = currentRobotData(robot);
if (robotData.isLookingAtTarget) {
if (robotData.timesShooting < 3) {
if (robot.gunCoolDownTime == 0) {
robotData.timesShooting = robotData.timesShooting + 1;
robot.fire();
}
} else {
robotData.timesShooting = 0;
robotData.isLookingAtTarget = false;
}
} else {
robot.turn(robotData.rotateStep);
}
};
var robotOnScannedRobot = function (robot, scannedRobot) {
var robotData = currentRobotData(robot);
if (aVera) {
if (scannedRobot.id != goodRobot.id && scannedRobot.parentId != goodRobot.id) {
robotData.isLookingAtTarget = true;
}
} else {
robotData.isLookingAtTarget = true;
}
};
var robotOnHitByBullet = function (robot, bearing) {
robot.turn(bearing - 90);
robot.ahead(50);
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
if (robot.parentId == null) {
if (!clonedEvilMe) {
goodRobot.id = robot.id;
robot.clone();
clonedEvilMe = true;
}
}
robotOnIdle(robot);
};
Robot.prototype.onScannedRobot = function(ev) {
robotOnScannedRobot(ev.robot, ev.scannedRobot);
};
Robot.prototype.onHitByBullet = function(ev) {
robotOnHitByBullet(ev.robot, ev.bearing);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment