Skip to content

Instantly share code, notes, and snippets.

@khepin
Forked from f6p/robot.js
Created December 7, 2012 03:10
Show Gist options
  • Save khepin/4230452 to your computer and use it in GitHub Desktop.
Save khepin/4230452 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);
alert(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 seek(robot) {
var step = baseStep(robot);
robot.rotateCannon(step);
robot.turn(step);
robot.ahead(step);
};
function shootAndAdjust(robot, adjustment) {
robot.fire();
robot.rotateCannon(adjustment);
};
// 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 = -12 * direction(robot);
shootAndAdjust(robot, -adj);
shootAndAdjust(robot, adj);
shootAndAdjust(robot, adj * 3);
}
};
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