Skip to content

Instantly share code, notes, and snippets.

@xala3pa
Forked from mauriciofaustino/robot.js
Created December 7, 2012 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xala3pa/4235441 to your computer and use it in GitHub Desktop.
Save xala3pa/4235441 to your computer and use it in GitHub Desktop.
mRC
var Robot = function(robot) {
this.direction = 1;
this.found = false;
this.loseNum = 0;
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
if(this.found) {
robot.turnGunRight(15 * this.direction);
andar(robot,15);
robot.turn(15*this.direction);
if(this.loseNum>5) {
this.found=false;
this.direction *= -1;
}
this.loseNum=this.loseNum+1;
} else {
andar(robot,50);
robot.rotateCannon(80*this.direction);
robot.turn(15*this.direction);
}
};
function andar(robot,i) {
if(this.direction>0) {
robot.ahead(i);
} else {
robot.back(i);
}
}
Robot.prototype.onRobotCollision = function(ev) {
var robot = ev.robot, collidedRobot = ev.collidedRobot;
robot.ignore('onRobotCollision')
if (ev.bearing > -90 && ev.bearing < 90) {
robot.back(100);
} else {
robot.ahead(100);
}
if (robot.id != collidedRobot.parentId && robot.parentId != collidedRobot.id) {
robot.turnGunRight(ev.bearing - robot.cannonRelativeAngle);
robot.turnGunLeft(ev.bearing - robot.cannonRelativeAngle);
}
robot.listen('onRobotCollision')
};
Robot.prototype.onWallCollision = function(ev) {
var robot = ev.robot;
robot.turn(45*this.direction);
robot.back(200);
};
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot, scannedRobot = ev.scannedRobot;
this.direction *= -1;
this.found=true;
this.loseNum=0;
if(!this.found) {
robot.rotateCannon(80*this.direction);
}
if (robot.id == scannedRobot.parentId || robot.parentId == scannedRobot.id) {
return;
}
robot.fire();
};
Robot.prototype.onHitByBullet = function(ev) {
var robot = ev.robot;
//robot.turn(90 - ev.bulletBearing);
//robot.ahead(40);
if(robot.parentId!=null){
robot.turn(ev.bearing);
robot.fire();
robot.fire();
robot.fire();
robot.fire();
robot.fire();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment