Skip to content

Instantly share code, notes, and snippets.

@heynemann
Created November 28, 2012 16:35
Show Gist options
  • Save heynemann/4162401 to your computer and use it in GitHub Desktop.
Save heynemann/4162401 to your computer and use it in GitHub Desktop.
Merry Go round
var foundRobotPosition = {x:0, y:0},
foundByParent=false,
foundByClone=false;
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot){
robot.clone();
this.options = {
direction: 1,
aheadWhileTurning: 3,
shootIterations: 5,
distanceToWalkBackOnHit: 10,
hasCloned: false
};
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
if (
(foundByClone && robot.parentId == null)
||
(foundByParent && robot.parentId != null)
){
var deltaX = Math.abs(robot.position.x - foundRobotPosition.x);
var deltaY = Math.abs(robot.position.y - foundRobotPosition.y);
var angle = Math.atan(deltaY / deltaX);
angle = angle * (180/Math.PI);
foundByClone = false;
foundByParent = false;
robot.turn(angle);
for (var i=0; i < this.options.shootIterations; i++) {
robot.fire();
robot.ahead(10);
robot.rotateCannon(-1);
}
}
robot.turn(1);
robot.ahead(1);
};
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot, scanned = ev.scannedRobot;
foundRobotPosition = scanned.position;
if (robot.parentId == null) {
foundByParent = true;
} else {
foundByClone = true;
}
if (scanned.parentId != null ||
robot.parentId == scanned.id ||
robot.id == scanned.parentId){
return;
}
//this.options.direction = (scanned.angle > 180) ? 1 : -1;
for (var i=0; i < this.options.shootIterations; i++) {
robot.fire();
robot.ahead(10);
robot.rotateCannon(-1);
}
};
Robot.prototype.onWallCollision = function(ev) {
ev.robot.turn(180);
ev.robot.back(this.options.distanceToWalkBackOnHit);
};
Robot.prototype.onRobotCollision = function(ev) {
};
Robot.prototype.onHitByBullet = function(ev) {
ev.robot.turn(ev.bearing);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment