Skip to content

Instantly share code, notes, and snippets.

@folkien
Forked from AndreMeira/robot.js
Created December 4, 2012 21:18
Show Gist options
  • Save folkien/4208782 to your computer and use it in GitHub Desktop.
Save folkien/4208782 to your computer and use it in GitHub Desktop.
Fight me if u can?
var direction;
var start;
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
direction = 1;
start = 0;
robot.clone();
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
if (!start) {
if(robot.parentId){
robot.ahead(50);
robot.turnLeft(45);
}
else {
robot.turnRight(45);
}
start = 1;
}
robot.ahead(10);
robot.rotateCannon(20 * direction);
};
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot;
var i = 0;
if ((ev.scannedRobot.parentId == robot.id)||(ev.scannedRobot.id == robot.parentId)) {
robot.turn(15);
} else {
//Gdy szefunio
direction = (ev.bearing>0) ? 1 : -1;
for(i=0;i<5;i++) {
robot.fire();
robot.ahead(1);
robot.fire();
}
robot.rotateCannon(-20*direction);
}
};
Robot.prototype.onWallCollision = function(ev) {
var robot = ev.robot;
robot.turn(90-(ev.bearing)); // turn enought to be in a straight
// angle with the wall.
};
Robot.prototype.onRobotCollision = function(ev) {
var robot = ev.robot;
var wrog = ev.collidedRobot;
if((wrog.parentId = robot.id)||(wrog.id==robot.parentId)) {
robot.turn(20*direction);
robot.ahead(100); // trying to run away
} else {
robot.turn(ev.bearing);
robot.fire();
}
};
Robot.prototype.onHitByBullet = function(ev) {
var robot = ev.robot;
robot.ahead(15);
robot.turn(ev.bearing); // Turn to wherever the bullet was fired
// so we can see who shot it
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment