Skip to content

Instantly share code, notes, and snippets.

@glauberramos
Forked from marano/robot.js
Created December 6, 2012 13:02
Show Gist options
  • Save glauberramos/4224315 to your computer and use it in GitHub Desktop.
Save glauberramos/4224315 to your computer and use it in GitHub Desktop.
Shrimp Hunter
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 <= 5) {
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;
}
};
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);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment