Skip to content

Instantly share code, notes, and snippets.

@tmazur
Created December 5, 2012 09:21
Show Gist options
  • Save tmazur/4214193 to your computer and use it in GitHub Desktop.
Save tmazur/4214193 to your computer and use it in GitHub Desktop.
TM
function Robot(robot) {
robot.clone()
}
// well, we need to do something...
// whenever our robot is idle, this method gets called.
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
if(robot.parentId) {
robot.turn(10);
robot.ahead(1);
robot.rotateCannon(10);
}
else {
robot.turn(-10);
robot.rotateCannon(-10);
robot.ahead(-20);
}
};
// this method gets called whenever we hit another robot...
Robot.prototype.onRobotCollision = function(ev) {
var robot = ev.robot;
var bFriendly = ( robot.parentId !=null && robot.parentId==collidedRobot.id);
bFriendly |= ( collidedRobot.parentId !=null && collidedRobot.parentId==robot.id);
if(bFriendly && ev.myFault) {
robot.turn(90-ev.bearing);
} else {
robot.turn(ev.bearing);
robot.rotateCannon(360);
robot.fire(1);
}
};
// this method gets called whenever we hit a wall...
Robot.prototype.onWallCollision = function(ev) {
var robot = ev.robot;
robot.turn(90-ev.bearing); // turn enought to be in a straight
robot.ahead(50);
};
// yay we see another robot! time to wreak some havoc...
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot;
var enemy = ev.scannedRobot;
var bFriendly = ( robot.parentId !=null && robot.parentId==enemy.id);
bFriendly |= ( enemy.parentId !=null && enemy.parentId==robot.id);
if(!bFriendly) {
robot = ev.robot;
robot.fire(1);
robot.stop;
} else {
robot.turn(5);
}
};
// ohhh... we were hit by another robot...
Robot.prototype.onHitByBullet = function(ev) {
var robot = ev.robot;
robot.turn(90-ev.bearing); // turn enought to be in a straight
robot.back(50);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment