Skip to content

Instantly share code, notes, and snippets.

@jaskolek
Created December 4, 2012 19:08
Show Gist options
  • Save jaskolek/4207600 to your computer and use it in GitHub Desktop.
Save jaskolek/4207600 to your computer and use it in GitHub Desktop.
jaskolek4
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
this.roundCount = 0;
this.asyncFns = new Array();
this.pauses = new Array();
this.toCannonAngle = 270;
this.toAngle = 90;
this.speed = 5;
this.cannonSpeed = 5;
this.turnSpeed = 2;
this.margin = 200;
this.scanV = 15;
this.shots = 0;
this.round = 0;
//robot.clone();
this.addAsyncFn( "goForward", this.goForward);
this.addAsyncFn( "aimCenter", this.aimCenter );
this.addAsyncFn( "scan", this.scan );
this.addAsyncFn( "asyncTurn", this.asyncTurn );
this.addAsyncFn( "asyncCannon", this.asyncCannon );
this.addAsyncFn( "goAround", this.goAround );
this.addAsyncFn( "autoShot", this.autoShot );
};
Robot.prototype.autoShot = function( ev, obj ){
if( obj.shots > 0 && ev.robot.gunCoolDownTime == 0 ){
ev.robot.fire();
obj.shots --;
}
}
Robot.prototype.goForward = function( ev, obj ){
if( obj.speed > 0 ){
ev.robot.ahead( obj.speed );
}
if( obj.speed < 0 ){
ev.robot.back( -obj.speed );
}
}
Robot.prototype.scan = function( ev, obj ){
var addon = obj.round % (obj.scanV * 4 + 2);
if( addon > obj.scanV * 2 ){
addon = obj.scanV * 3 - addon + 1;
}else{
addon = addon - obj.scanV;
}
obj.toCannonAngle += addon;
}
Robot.prototype.aimCenter = function( ev, obj ){
var robot = ev.robot;
var angle = Math.atan( ( ( robot.position.y - robot.arenaHeight / 2 ) / ( Math.abs( robot.position.x - robot.arenaWidth / 2 ) ) ) ) * 180 / Math.PI;
if( robot.position.x < robot.arenaWidth / 2 ){
angle = 180 - angle;
}
obj.toCannonAngle = angle;
}
Robot.prototype.goAround = function( ev, obj ){
var robot = ev.robot;
pause = true;
if( robot.position.x > robot.arenaWidth - obj.margin && obj.toAngle != 180 ){
obj.toAngle = 180;
}else if( robot.position.x < obj.margin && obj.toAngle != 0 ){
obj.toAngle = 0;
}else if( robot.position.y > robot.arenaHeight - obj.margin && obj.toAngle != 270 ){
obj.toAngle = 270;
}else if( robot.position.y < obj.margin && obj.toAngle != 90 ){
obj.toAngle = 90;
}else{
pause = false;
}
if( pause == true ){
obj.pauseAsyncFn( "goAround", Math.round( 90 / obj.turnSpeed ) + 20 );
}
}
Robot.prototype.asyncCannon = function( ev, obj ){
var robot = ev.robot;
var diff = robot.cannonAbsoluteAngle - ( obj.toCannonAngle % 360 );
if( diff < -180 ) diff += 360;
if( diff > 180 ) diff -= 360;
if( Math.abs( diff ) < obj.cannonSpeed ){
robot.rotateCannon( -diff );
}else{
if( diff < 0 ) robot.rotateCannon( obj.cannonSpeed );
if( diff > 0 ) robot.rotateCannon( -obj.cannonSpeed );
}
}
Robot.prototype.asyncTurn = function( ev, obj ){
var robot = ev.robot;
var diff = robot.angle - ( obj.toAngle % 360 );
if( diff < -180 ) diff += 360;
if( diff > 180 ) diff -= 360;
robot.log( diff );
if( Math.abs( diff ) < obj.turnSpeed ){
robot.turn( -diff );
}else{
if( diff < 0 ) robot.turn( obj.turnSpeed );
if( diff > 0 ) robot.turn( -obj.turnSpeed );
}
}
Robot.prototype.pauseAsyncFn = function( name, rounds ){
this.pauses[name] += rounds;
}
Robot.prototype.addAsyncFn = function( name, fn ){
this.asyncFns[name] = fn;
this.pauses[name] = 0;
}
Robot.prototype.removeAsyncFn = function( name ){
}
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
for( i in this.asyncFns ){
if( this.pauses[i] == 0 ){
this.asyncFns[i](ev, this);
}else{
this.pauses[i]--;
}
}
this.round++;
};
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot;
this.shots = 3;
};
Robot.prototype.onWallCollision = function(ev) {
var robot = ev.robot;
robot.turn(ev.bearing + 90); // turn enought to be in a straight
// angle with the wall.
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment